|
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} |