File size: 1,534 Bytes
930cbdd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import csv
import pandas as pd
import datasets
from sklearn.model_selection import train_test_split
from datasets.tasks import TextClassification
_DATASET_LABELS = ['NEGATIVE', 'POSITIVE', 'NEUTRAL']
class Custom(datasets.GeneratorBasedBuilder):
def _info(self):
return datasets.DatasetInfo(
description='',
features=datasets.Features(
{
'text': datasets.Value('string'),
'label': datasets.features.ClassLabel(
names=_DATASET_LABELS
),
}
),
homepage='',
citation='',
task_templates=[
TextClassification(text_column='text', label_column='label')
],
)
def _split_generators(self, dl_manager):
data_path = dl_manager.download_and_extract('data.csv')
records = pd.read_csv(data_path)
train_df, val_df = train_test_split(records, test_size=0.2, random_state=42)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN, gen_kwargs={'df': train_df}
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION, gen_kwargs={'df': val_df}
),
]
def _generate_examples(self, df):
for id_, row in df.iterrows():
text, label = row['text'], row['label'],
label = _DATASET_LABELS.index(label)
yield id_, {'text': text, 'label': label} |