holylovenia commited on
Commit
dee7838
·
verified ·
1 Parent(s): a693aee

Upload memolon.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. memolon.py +145 -0
memolon.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ from pathlib import Path
17
+ from typing import Dict, List, Tuple
18
+
19
+ import datasets
20
+
21
+ from seacrowd.utils.configs import SEACrowdConfig
22
+ from seacrowd.utils.constants import Licenses, Tasks
23
+
24
+ _CITATION = """\
25
+ @inproceedings{buechel-etal-2020-learning-evaluating,
26
+ title = "Learning and Evaluating Emotion Lexicons for 91 Languages",
27
+ author = {Buechel, Sven and
28
+ R{\"u}cker, Susanna and
29
+ Hahn, Udo},
30
+ editor = "Jurafsky, Dan and
31
+ Chai, Joyce and
32
+ Schluter, Natalie and
33
+ Tetreault, Joel",
34
+ booktitle = "Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics",
35
+ month = jul,
36
+ year = "2020",
37
+ address = "Online",
38
+ publisher = "Association for Computational Linguistics",
39
+ url = "https://aclanthology.org/2020.acl-main.112",
40
+ doi = "10.18653/v1/2020.acl-main.112",
41
+ pages = "1202--1217",
42
+ }
43
+ """
44
+
45
+ _DATASETNAME = "memolon"
46
+
47
+ _DESCRIPTION = """\
48
+ MEmoLon is an emotion lexicons for 91 languages, each one covers eight emotional variables and comprises over 100k word entries. There are several versions of the lexicons, the difference being the choice of the expansion model.
49
+ """
50
+
51
+ _HOMEPAGE = "https://zenodo.org/record/3756607/files/MTL_grouped.zip?download=1"
52
+
53
+ _LICENSE = Licenses.MIT.value
54
+
55
+ _URLS = {
56
+ _DATASETNAME: "https://zenodo.org/record/3756607/files/MTL_grouped.zip?download=1",
57
+ }
58
+
59
+ _SOURCE_VERSION = "1.0.0"
60
+ _SEACROWD_VERSION = "2024.06.20"
61
+
62
+ _LANGUAGES = ["ceb", "tgl", "ind", "sun", "jav", "zsm", "vie", "tha", "mya"]
63
+
64
+ _LANGUAGE_MAP = {"ceb": "Cebuano", "tgl": "Tagalog", "ind": "Indonesian", "sun": "Sundanese", "jav": "Javanese", "zsm": "Malay", "vie": "Vietnamese", "tha": "Thai", "mya": "Burmese"}
65
+
66
+ _SUPPORTED_TASKS = [Tasks.EMOTION_CLASSIFICATION]
67
+
68
+ _LOCAL = False
69
+
70
+
71
+ def seacrowd_config_constructor(lang: str, schema: str, version: str) -> SEACrowdConfig:
72
+ if lang not in _LANGUAGE_MAP:
73
+ raise ValueError(f"Invalid lang {lang}")
74
+
75
+ if schema != "source" and schema != "seacrowd_text_multi":
76
+ raise ValueError(f"Invalid schema: {schema}")
77
+
78
+ return SEACrowdConfig(
79
+ name="memolon_{lang}_{schema}".format(lang=lang, schema=schema),
80
+ version=datasets.Version(version),
81
+ description="MEmoLon {schema} schema for {lang} language".format(lang=_LANGUAGE_MAP[lang], schema=schema),
82
+ schema=schema,
83
+ subset_id="memolon",
84
+ )
85
+
86
+
87
+ class Memolon(datasets.GeneratorBasedBuilder):
88
+ """MEmoLon is an emotion lexicons for 91 languages, each one covers eight emotional variables and comprises over 100k word entries."""
89
+
90
+ BUILDER_CONFIGS = [SEACrowdConfig(name=f"{_DATASETNAME}_{lang}_source", version=datasets.Version(_SOURCE_VERSION), description=f"MEmoLon source schema for {lang} language", schema="source", subset_id="memolon") for lang in _LANGUAGE_MAP]
91
+
92
+ DEFAULT_CONFIG_NAME = None
93
+
94
+ def _info(self) -> datasets.DatasetInfo:
95
+ if self.config.schema == "source":
96
+ features = datasets.Features(
97
+ {
98
+ "word": datasets.Value("string"),
99
+ "valence": datasets.Value("float32"),
100
+ "arousal": datasets.Value("float32"),
101
+ "dominance": datasets.Value("float32"),
102
+ "joy": datasets.Value("float32"),
103
+ "anger": datasets.Value("float32"),
104
+ "sadness": datasets.Value("float32"),
105
+ "fear": datasets.Value("float32"),
106
+ "disgust": datasets.Value("float32"),
107
+ }
108
+ )
109
+
110
+ return datasets.DatasetInfo(
111
+ description=_DESCRIPTION,
112
+ features=features,
113
+ homepage=_HOMEPAGE,
114
+ license=_LICENSE,
115
+ citation=_CITATION,
116
+ )
117
+
118
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
119
+ """Returns SplitGenerators."""
120
+ urls = _URLS[_DATASETNAME]
121
+ base_path = Path(dl_manager.download_and_extract(urls))
122
+ lang = self.config.name.split("_")[1]
123
+ train_data_path = base_path / f"{lang}.tsv"
124
+
125
+ return [
126
+ datasets.SplitGenerator(
127
+ name=datasets.Split.TRAIN,
128
+ gen_kwargs={
129
+ "filepath": train_data_path,
130
+ "split": "train",
131
+ },
132
+ )
133
+ ]
134
+
135
+ def _generate_examples(self, filepath: Path, split: str) -> Tuple[int, Dict]:
136
+ """Yields examples as (key, example) tuples."""
137
+ rows = []
138
+ with open(filepath, encoding='utf-8') as file:
139
+ for line in file:
140
+ rows.append(line.split("\t"))
141
+
142
+ if self.config.schema == "source":
143
+ for key, row in enumerate(rows[1:]):
144
+ example = {"word": row[0], "valence": row[1], "arousal": row[2], "dominance": row[3], "joy": row[4], "anger": row[5], "sadness": row[6], "fear": row[7], "disgust": row[8]}
145
+ yield key, example