holylovenia
commited on
Commit
•
31bb229
1
Parent(s):
50e04d5
Upload xed.py with huggingface_hub
Browse files
xed.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pathlib import Path
|
2 |
+
from typing import Dict, List, Tuple
|
3 |
+
|
4 |
+
import datasets
|
5 |
+
import pandas as pd
|
6 |
+
|
7 |
+
from seacrowd.utils import schemas
|
8 |
+
from seacrowd.utils.configs import SEACrowdConfig
|
9 |
+
from seacrowd.utils.constants import Licenses, Tasks
|
10 |
+
|
11 |
+
_CITATION = """
|
12 |
+
@inproceedings{ohman2020xed,
|
13 |
+
title={{XED}: A Multilingual Dataset for Sentiment Analysis and Emotion Detection},
|
14 |
+
author={{\"O}hman, Emily and P{`a}mies, Marc and Kajava, Kaisla and Tiedemann, J{\"o}rg},
|
15 |
+
booktitle={The 28th International Conference on Computational Linguistics (COLING 2020)},
|
16 |
+
year={2020}
|
17 |
+
}
|
18 |
+
"""
|
19 |
+
_DATASETNAME = "xed"
|
20 |
+
|
21 |
+
_DESCRIPTION = """\
|
22 |
+
This is the XED dataset. The dataset consists of emotion annotated movie subtitles
|
23 |
+
from OPUS. We use Plutchik's 8 core emotions to annotate. The data is multilabel.
|
24 |
+
The original annotations have been sourced for mainly English and Finnish, with the
|
25 |
+
rest created using annotation projection to aligned subtitles in 41 additional languages,
|
26 |
+
with 31 languages included in the final dataset (more than 950 lines of annotated subtitle
|
27 |
+
lines). The dataset is an ongoing project with forthcoming additions such as machine translated datasets.
|
28 |
+
"""
|
29 |
+
|
30 |
+
_HOMEPAGE = "https://github.com/Helsinki-NLP/XED"
|
31 |
+
|
32 |
+
_LANGUAGES = ["ind", "vie"]
|
33 |
+
|
34 |
+
# This License is from the bottom of homepage's README not Unknown (as from Issues)
|
35 |
+
_LICENSE = Licenses.CC_BY_4_0.value
|
36 |
+
|
37 |
+
_LOCAL = False
|
38 |
+
|
39 |
+
_URLS = {"ind": "https://raw.githubusercontent.com/Helsinki-NLP/XED/master/Projections/id-projections.tsv", "vie": "https://raw.githubusercontent.com/Helsinki-NLP/XED/master/Projections/vi-projections.tsv"}
|
40 |
+
|
41 |
+
# Because of the multi-label attribute, I choose ASPECT_BASED_SENTIMENT_ANALYSIS than SENTIMENT_ANALYSIS
|
42 |
+
_SUPPORTED_TASKS = [Tasks.ASPECT_BASED_SENTIMENT_ANALYSIS]
|
43 |
+
|
44 |
+
_SOURCE_VERSION = "1.0.0"
|
45 |
+
|
46 |
+
_SEACROWD_VERSION = "2024.06.20"
|
47 |
+
|
48 |
+
|
49 |
+
class XEDDataset(datasets.GeneratorBasedBuilder):
|
50 |
+
"""
|
51 |
+
This is the XED dataset. The dataset consists of emotion annotated movie subtitles
|
52 |
+
from OPUS. We use Plutchik's 8 core emotions to annotate. The data is multilabel.
|
53 |
+
The original annotations have been sourced for mainly English and Finnish, with the
|
54 |
+
rest created using annotation projection to aligned subtitles in 41 additional languages,
|
55 |
+
with 31 languages included in the final dataset (more than 950 lines of annotated subtitle
|
56 |
+
lines). The dataset is an ongoing project with forthcoming additions such as machine translated datasets.
|
57 |
+
"""
|
58 |
+
|
59 |
+
BUILDER_CONFIGS = [
|
60 |
+
SEACrowdConfig(
|
61 |
+
name=f"{_DATASETNAME}_{LANG}_source",
|
62 |
+
version=datasets.Version(_SOURCE_VERSION),
|
63 |
+
description=f"{_DATASETNAME} {LANG} source schema",
|
64 |
+
schema="source",
|
65 |
+
subset_id=f"{_DATASETNAME}_{LANG}",
|
66 |
+
)
|
67 |
+
for LANG in _LANGUAGES
|
68 |
+
] + [
|
69 |
+
SEACrowdConfig(
|
70 |
+
name=f"{_DATASETNAME}_{LANG}_seacrowd_text_multi",
|
71 |
+
version=datasets.Version(_SEACROWD_VERSION),
|
72 |
+
description=f"{_DATASETNAME} {LANG} SEACrowd schema",
|
73 |
+
schema="seacrowd_text_multi",
|
74 |
+
subset_id=f"{_DATASETNAME}_{LANG}",
|
75 |
+
)
|
76 |
+
for LANG in _LANGUAGES
|
77 |
+
]
|
78 |
+
|
79 |
+
DEFAULT_CONFIG_NAME = f"{_DATASETNAME}_ind_source"
|
80 |
+
_LABELS = ["Anger", "Anticipation", "Disgust", "Fear", "Joy", "Sadness", "Surprise", "Trust"]
|
81 |
+
|
82 |
+
def _info(self) -> datasets.DatasetInfo:
|
83 |
+
|
84 |
+
if self.config.schema == "source":
|
85 |
+
features = datasets.Features({"Sentence": datasets.Value("string"), "Emotions": datasets.Sequence(feature=datasets.ClassLabel(names=self._LABELS))})
|
86 |
+
|
87 |
+
elif self.config.schema == "seacrowd_text_multi":
|
88 |
+
features = schemas.text_multi_features(self._LABELS)
|
89 |
+
|
90 |
+
return datasets.DatasetInfo(
|
91 |
+
description=_DESCRIPTION,
|
92 |
+
features=features,
|
93 |
+
homepage=_HOMEPAGE,
|
94 |
+
license=_LICENSE,
|
95 |
+
citation=_CITATION,
|
96 |
+
)
|
97 |
+
|
98 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
99 |
+
"""Returns SplitGenerators."""
|
100 |
+
|
101 |
+
language = self.config.name.split("_")[1]
|
102 |
+
|
103 |
+
if language in _LANGUAGES:
|
104 |
+
data_path = Path(dl_manager.download_and_extract(_URLS[language]))
|
105 |
+
else:
|
106 |
+
data_path = [Path(dl_manager.download_and_extract(_URLS[language])) for language in _LANGUAGES]
|
107 |
+
|
108 |
+
return [
|
109 |
+
datasets.SplitGenerator(
|
110 |
+
name=datasets.Split.TRAIN,
|
111 |
+
gen_kwargs={
|
112 |
+
"filepath": data_path,
|
113 |
+
"split": "train",
|
114 |
+
},
|
115 |
+
)
|
116 |
+
]
|
117 |
+
|
118 |
+
def _generate_examples(self, filepath: Path, split: str) -> Tuple[int, Dict]:
|
119 |
+
"""Yields examples as (key, example) tuples."""
|
120 |
+
|
121 |
+
emotions_mapping = {1: "Anger", 2: "Anticipation", 3: "Disgust", 4: "Fear", 5: "Joy", 6: "Sadness", 7: "Surprise", 8: "Trust"}
|
122 |
+
|
123 |
+
df = pd.read_csv(filepath, sep="\t", names=["Sentence", "Emotions"], index_col=None)
|
124 |
+
df["Emotions"] = df["Emotions"].apply(lambda x: list(map(int, x.split(", "))))
|
125 |
+
df["Emotions"] = df["Emotions"].apply(lambda x: [emotions_mapping[emotion] for emotion in x])
|
126 |
+
|
127 |
+
for index, row in df.iterrows():
|
128 |
+
|
129 |
+
if self.config.schema == "source":
|
130 |
+
example = row.to_dict()
|
131 |
+
|
132 |
+
elif self.config.schema == "seacrowd_text_multi":
|
133 |
+
|
134 |
+
example = {
|
135 |
+
"id": str(index),
|
136 |
+
"text": str(row["Sentence"]),
|
137 |
+
"labels": row["Emotions"],
|
138 |
+
}
|
139 |
+
|
140 |
+
yield index, example
|