File size: 4,892 Bytes
a4ee508 6344f8a 62be3ab a4ee508 6344f8a 17c8294 6344f8a 17c8294 62be3ab 6344f8a 62be3ab 6344f8a 62be3ab a4ee508 62be3ab 6344f8a 62be3ab a4ee508 |
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
import zipfile
from typing import List
import datasets
import pandas as pd
from datasets import ClassLabel, Value
DATASETS_URLS = [{
"name": "go_emotions",
"urls": [
"https://storage.googleapis.com/gresearch/goemotions/data/full_dataset/goemotions_1.csv",
"https://storage.googleapis.com/gresearch/goemotions/data/full_dataset/goemotions_2.csv",
"https://storage.googleapis.com/gresearch/goemotions/data/full_dataset/goemotions_3.csv",
],
"license": "apache license 2.0"},
{
"name": "daily_dialog",
"urls": ["http://yanran.li/files/ijcnlp_dailydialog.zip"],
"license": "CC BY-NC-SA 4.0"
}
]
_CLASS_NAMES = [
"no emotion",
"happiness",
"admiration",
"amusement",
"anger",
"annoyance",
"approval",
"caring",
"confusion",
"curiosity",
"desire",
"disappointment",
"disapproval",
"disgust",
"embarrassment",
"excitement",
"fear",
"gratitude",
"grief",
"joy",
"love",
"nervousness",
"optimism",
"pride",
"realization",
"relief",
"remorse",
"sadness",
"surprise",
"neutral",
]
class EmotionsDatasetConfig(datasets.BuilderConfig):
def __init__(self, features, label_classes, **kwargs):
super().__init__(**kwargs)
self.features = features
self.label_classes = label_classes
class EmotionsDataset(datasets.GeneratorBasedBuilder):
BUILDER_CONFIGS = [
EmotionsDatasetConfig(
name="all",
label_classes=_CLASS_NAMES,
features=["text", "label", "dataset", "license"]
)
]
DEFAULT_CONFIG_NAME = "all"
def _info(self):
return datasets.DatasetInfo(
features=datasets.Features(
{
"id": datasets.Value("string"),
'text': Value(dtype='string', id=None),
'label': ClassLabel(names=_CLASS_NAMES, id=None),
'dataset': Value(dtype='string', id=None),
'license': Value(dtype='string', id=None)
}
)
)
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
splits = []
for d in DATASETS_URLS:
downloaded_files = dl_manager.download_and_extract(d.get("urls"))
splits.append(datasets.SplitGenerator(name=d.get("name"),
gen_kwargs={"filepaths": downloaded_files,
"dataset": d.get("name"),
"license": d.get("license")}))
return splits
def _generate_examples(self, filepaths, dataset, license):
if dataset == "go_emotions":
for i, filepath in enumerate(filepaths):
df = pd.read_csv(filepath)
current_classes = list(set(df.columns).intersection(set(_CLASS_NAMES)))
df = df[["text"] + current_classes]
df = df[df[current_classes].sum(axis=1) == 1].reset_index(drop=True)
for row_idx, row in df.iterrows():
uid = f"go_emotions_{i}_{row_idx}"
yield uid, {"text": row["text"],
"id": uid,
"dataset": dataset,
"license": license,
"label": row[current_classes][row == 1].index.item()}
elif dataset == "daily_dialog":
emo_mapping = {0: "no emotion", 1: "anger", 2: "disgust",
3: "fear", 4: "happiness", 5: "sadness", 6: "surprise"}
for i, filepath in enumerate(filepaths):
with zipfile.ZipFile(filepath, 'r') as archive:
emotions = archive.open("ijcnlp_dailydialog/dialogues_emotion.txt", "r").read().decode().split("\n")
text = archive.open("ijcnlp_dailydialog/dialogues_text.txt", "r").read().decode().split("\n")
for idx_out, (e, t) in enumerate(zip(emotions, text)):
if len(t.strip()) > 0:
cast_emotions = [int(j) for j in e.strip().split(" ")]
cast_dialog = [d.strip() for d in t.split("__eou__") if len(d)]
for idx_in, (ce, ct) in enumerate(zip(cast_emotions, cast_dialog)):
uid = f"daily_dialog_{i}_{idx_out}_{idx_in}"
yield uid, {"text": ct,
"id": uid,
"dataset": dataset,
"license": license,
"label": emo_mapping[ce]}
print()
|