Upload emotions_dataset.py
Browse files- emotions_dataset.py +53 -0
emotions_dataset.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List
|
2 |
+
|
3 |
+
import datasets
|
4 |
+
import pandas as pd
|
5 |
+
from datasets import ClassLabel, Value
|
6 |
+
|
7 |
+
_URLS = [
|
8 |
+
"https://storage.googleapis.com/gresearch/goemotions/data/full_dataset/goemotions_1.csv",
|
9 |
+
"https://storage.googleapis.com/gresearch/goemotions/data/full_dataset/goemotions_2.csv",
|
10 |
+
"https://storage.googleapis.com/gresearch/goemotions/data/full_dataset/goemotions_3.csv",
|
11 |
+
]
|
12 |
+
|
13 |
+
|
14 |
+
class EmotionsDatasetConfig(datasets.BuilderConfig):
|
15 |
+
|
16 |
+
def __init__(self, features, label_classes, **kwargs):
|
17 |
+
super().__init__(**kwargs)
|
18 |
+
self.features = features
|
19 |
+
self.label_classes = label_classes
|
20 |
+
|
21 |
+
|
22 |
+
class EmotionsDataset(datasets.GeneratorBasedBuilder):
|
23 |
+
BUILDER_CONFIGS = [
|
24 |
+
EmotionsDatasetConfig(
|
25 |
+
name="all",
|
26 |
+
label_classes=["anger", "joy", "sadness"],
|
27 |
+
features=["text", "label", "dataset"]
|
28 |
+
)
|
29 |
+
]
|
30 |
+
|
31 |
+
DEFAULT_CONFIG_NAME = "all"
|
32 |
+
|
33 |
+
def _info(self):
|
34 |
+
return datasets.DatasetInfo(
|
35 |
+
features=datasets.Features(
|
36 |
+
{
|
37 |
+
"id": datasets.Value("string"),
|
38 |
+
'label': ClassLabel(names=["joy", "anger", "sadness"], id=None),
|
39 |
+
'text': Value(dtype='string', id=None),
|
40 |
+
'dataset': Value(dtype='string', id=None)
|
41 |
+
}
|
42 |
+
)
|
43 |
+
)
|
44 |
+
|
45 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
46 |
+
downloaded_files = dl_manager.download_and_extract(_URLS)
|
47 |
+
return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepaths": downloaded_files})]
|
48 |
+
|
49 |
+
def _generate_examples(self, filepaths):
|
50 |
+
for i, filepath in enumerate(filepaths):
|
51 |
+
df = pd.read_csv(filepath)
|
52 |
+
for row_idx, row in enumerate(df.iterrows()):
|
53 |
+
yield f"{i}_{row_idx}", row
|