Saripudin commited on
Commit
930cbdd
·
1 Parent(s): 3620237

Upload text-dataset-sample.py with huggingface_hub

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