ma2za commited on
Commit
a4ee508
·
1 Parent(s): 62be3ab

Upload emotions_dataset.py

Browse files
Files changed (1) hide show
  1. emotions_dataset.py +45 -16
emotions_dataset.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from typing import List
2
 
3
  import datasets
@@ -11,7 +12,12 @@ DATASETS_URLS = [{
11
  "https://storage.googleapis.com/gresearch/goemotions/data/full_dataset/goemotions_2.csv",
12
  "https://storage.googleapis.com/gresearch/goemotions/data/full_dataset/goemotions_3.csv",
13
  ],
14
- "license": "apache license 2.0"}
 
 
 
 
 
15
  ]
16
 
17
  _CLASS_NAMES = [
@@ -84,21 +90,44 @@ class EmotionsDataset(datasets.GeneratorBasedBuilder):
84
  splits = []
85
  for d in DATASETS_URLS:
86
  downloaded_files = dl_manager.download_and_extract(d.get("urls"))
87
- splits.append(datasets.SplitGenerator(name=d.get("name"), gen_kwargs={"filepaths": downloaded_files,
88
- "dataset": d.get("name"),
89
- "license": d.get("license")}))
 
90
  return splits
91
 
92
  def _generate_examples(self, filepaths, dataset, license):
93
- for i, filepath in enumerate(filepaths):
94
- df = pd.read_csv(filepath)
95
- current_classes = list(set(df.columns).intersection(set(_CLASS_NAMES)))
96
- df = df[["text"] + current_classes]
97
- df = df[df[current_classes].sum(axis=1) == 1].reset_index(drop=True)
98
- for row_idx, row in df.iterrows():
99
- uid = f"{i}_{row_idx}"
100
- yield uid, {"text": row["text"],
101
- "id": uid,
102
- "dataset": dataset,
103
- "license": license,
104
- "label": row[current_classes][row == 1].index.item()}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import zipfile
2
  from typing import List
3
 
4
  import datasets
 
12
  "https://storage.googleapis.com/gresearch/goemotions/data/full_dataset/goemotions_2.csv",
13
  "https://storage.googleapis.com/gresearch/goemotions/data/full_dataset/goemotions_3.csv",
14
  ],
15
+ "license": "apache license 2.0"},
16
+ {
17
+ "name": "daily_dialog",
18
+ "urls": ["http://yanran.li/files/ijcnlp_dailydialog.zip"],
19
+ "license": "CC BY-NC-SA 4.0"
20
+ }
21
  ]
22
 
23
  _CLASS_NAMES = [
 
90
  splits = []
91
  for d in DATASETS_URLS:
92
  downloaded_files = dl_manager.download_and_extract(d.get("urls"))
93
+ splits.append(datasets.SplitGenerator(name=d.get("name"),
94
+ gen_kwargs={"filepaths": downloaded_files,
95
+ "dataset": d.get("name"),
96
+ "license": d.get("license")}))
97
  return splits
98
 
99
  def _generate_examples(self, filepaths, dataset, license):
100
+ if dataset == "go_emotions":
101
+ for i, filepath in enumerate(filepaths):
102
+ df = pd.read_csv(filepath)
103
+ current_classes = list(set(df.columns).intersection(set(_CLASS_NAMES)))
104
+ df = df[["text"] + current_classes]
105
+ df = df[df[current_classes].sum(axis=1) == 1].reset_index(drop=True)
106
+ for row_idx, row in df.iterrows():
107
+ uid = f"go_emotions_{i}_{row_idx}"
108
+ yield uid, {"text": row["text"],
109
+ "id": uid,
110
+ "dataset": dataset,
111
+ "license": license,
112
+ "label": row[current_classes][row == 1].index.item()}
113
+ elif dataset == "daily_dialog":
114
+ emo_mapping = {0: "no emotion", 1: "anger", 2: "disgust",
115
+ 3: "fear", 4: "happiness", 5: "sadness", 6: "surprise"}
116
+ for i, filepath in enumerate(filepaths):
117
+ with zipfile.ZipFile(filepath, 'r') as archive:
118
+ emotions = archive.open("ijcnlp_dailydialog/dialogues_emotion.txt", "r").read().decode().split("\n")
119
+ text = archive.open("ijcnlp_dailydialog/dialogues_text.txt", "r").read().decode().split("\n")
120
+ for idx_out, (e, t) in enumerate(zip(emotions, text)):
121
+ if len(t.strip()) > 0:
122
+ cast_emotions = [int(j) for j in e.strip().split(" ")]
123
+ cast_dialog = [d.strip() for d in t.split("__eou__") if len(d)]
124
+ for idx_in, (ce, ct) in enumerate(zip(cast_emotions, cast_dialog)):
125
+ uid = f"daily_dialog_{i}_{idx_out}_{idx_in}"
126
+ yield uid, {"text": ct,
127
+ "id": uid,
128
+ "dataset": dataset,
129
+ "license": license,
130
+ "label": emo_mapping[ce]}
131
+
132
+
133
+ print()