File size: 8,899 Bytes
430304e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
beadddb
430304e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247ecfc
 
430304e
 
 
 
 
 
 
 
 
247ecfc
 
 
430304e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247ecfc
 
 
430304e
 
 
 
 
 
 
 
247ecfc
 
 
 
 
 
 
 
3ba34cb
247ecfc
 
 
 
 
 
 
 
430304e
247ecfc
 
 
 
 
 
 
 
3ba34cb
247ecfc
 
 
 
 
 
 
 
430304e
247ecfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
430304e
 
 
 
 
 
 
 
 
 
 
 
 
247ecfc
 
 
430304e
 
247ecfc
 
 
430304e
 
 
33b70ff
430304e
 
beadddb
430304e
beadddb
430304e
beadddb
d7049d6
 
beadddb
 
87b1c21
d7049d6
bb3a1b6
87b1c21
beadddb
87b1c21
 
 
 
 
 
d7049d6
 
 
beadddb
 
d7049d6
beadddb
430304e
bb3a1b6
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# coding=utf-8
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""NaijaSenti: A Nigerian Twitter Sentiment Corpus for Multilingual Sentiment Analysis"""



_HOMEPAGE = "https://github.com/hausanlp/NaijaSenti"

_DESCRIPTION = """\
Naija-Stopwords is a part of the Naija-Senti project. It is a list of collected stopwords from the four most widely spoken languages in Nigeria — Hausa, Igbo, Nigerian-Pidgin, and Yorùbá.
"""


_CITATION = """\
@inproceedings{muhammad-etal-2022-naijasenti,
    title = "{N}aija{S}enti: A {N}igerian {T}witter Sentiment Corpus for Multilingual Sentiment Analysis",
    author = "Muhammad, Shamsuddeen Hassan  and
      Adelani, David Ifeoluwa  and
      Ruder, Sebastian  and
      Ahmad, Ibrahim Sa{'}id  and
      Abdulmumin, Idris  and
      Bello, Bello Shehu  and
      Choudhury, Monojit  and
      Emezue, Chris Chinenye  and
      Abdullahi, Saheed Salahudeen  and
      Aremu, Anuoluwapo  and
      Jorge, Al{\"\i}pio  and
      Brazdil, Pavel",
    booktitle = "Proceedings of the Thirteenth Language Resources and Evaluation Conference",
    month = jun,
    year = "2022",
    address = "Marseille, France",
    publisher = "European Language Resources Association",
    url = "https://aclanthology.org/2022.lrec-1.63",
    pages = "590--602",
}
"""


import textwrap
import pandas as pd

import datasets

#LANGUAGES = ["hausa", "igbo", "yoruba"]
TYPES = ['manual-sentiment', 'translated-sentiment', 'translated-emotion']

class NaijaLexiconsConfig(datasets.BuilderConfig):
    """BuilderConfig for NaijaLexicons"""

    def __init__(
        self,
        text_features,
        label_column,
        label_classes,
        hau_url,
        ibo_url,
        yor_url,
        citation,
        **kwargs,
    ):
        """BuilderConfig for NaijaLexicons.

        Args:
          text_features: `dict[string]`, map from the name of the feature
            dict for each text field to the name of the column in the txt/csv/tsv file
          label_column: `string`, name of the column in the txt/csv/tsv file corresponding
            to the label
          label_classes: `list[string]`, the list of classes if the label is categorical
          train_url: `string`, url to train file from
          valid_url: `string`, url to valid file from
          test_url: `string`, url to test file from
          citation: `string`, citation for the data set
          **kwargs: keyword arguments forwarded to super.
        """
        super(NaijaLexiconsConfig, self).__init__(version=datasets.Version("1.0.0", ""), **kwargs)
        self.text_features = text_features
        self.label_column = label_column
        self.label_classes = label_classes
        self.hau_url = hau_url
        self.ibo_url = ibo_url
        self.yor_url = yor_url
        self.citation = citation


class NaijaLexicons(datasets.GeneratorBasedBuilder):
    """NaijaLexicons benchmark"""

    BUILDER_CONFIGS = []

    for t in TYPES:
        if t == 'translated-emotion':
            BUILDER_CONFIGS.append(
                NaijaLexiconsConfig(
                    name=t,
                    description=textwrap.dedent(
                        f"""{_DESCRIPTION}"""
                    ),
                    text_features={"word": "word", "machine translation": "machine translation", "human translation": "human translation", "emotion_intensity_score": "emotion_intensity_score"},
                    label_classes=["surprise", "fear", "anticipation", "anger", "joy", "trust", "disgust", "sadness"],
                    label_column="label",
                    hau_url=f"https://raw.githubusercontent.com/hausanlp/NaijaSenti/main/data/lexicons/{t}/huggingface/hausa.csv",
                    ibo_url=f"https://raw.githubusercontent.com/hausanlp/NaijaSenti/main/data/lexicons/{t}/huggingface/igbo.csv",
                    yor_url=f"https://raw.githubusercontent.com/hausanlp/NaijaSenti/main/data/lexicons/{t}/huggingface/yoruba.csv",
                    citation=textwrap.dedent(
                        f"""{_CITATION}"""
                    ),
                ),
            )
        elif t == 'translated-sentiment':
            BUILDER_CONFIGS.append(
                NaijaLexiconsConfig(
                    name=t,
                    description=textwrap.dedent(
                        f"""{_DESCRIPTION}"""
                    ),
                    text_features={"word": "word", "machine translation": "machine translation", "human translation": "human translation"},
                    label_classes=["positive", "negative"],
                    label_column="label",
                    hau_url=f"https://raw.githubusercontent.com/hausanlp/NaijaSenti/main/data/lexicons/{t}/huggingface/hausa.csv",
                    ibo_url=f"https://raw.githubusercontent.com/hausanlp/NaijaSenti/main/data/lexicons/{t}/huggingface/igbo.csv",
                    yor_url=f"https://raw.githubusercontent.com/hausanlp/NaijaSenti/main/data/lexicons/{t}/huggingface/yoruba.csv",
                    citation=textwrap.dedent(
                        f"""{_CITATION}"""
                    ),
                ),
            )
        else:
            BUILDER_CONFIGS.append(
                NaijaLexiconsConfig(
                    name=t,
                    description=textwrap.dedent(
                        f"""{_DESCRIPTION}"""
                    ),
                    text_features={"word": "word"},
                    label_classes=["positive", "negative"],
                    label_column="label",
                    hau_url=f"https://raw.githubusercontent.com/hausanlp/NaijaSenti/main/data/lexicons/{t}/huggingface/hausa.csv",
                    ibo_url=f"https://raw.githubusercontent.com/hausanlp/NaijaSenti/main/data/lexicons/{t}/huggingface/igbo.csv",
                    yor_url=f"https://raw.githubusercontent.com/hausanlp/NaijaSenti/main/data/lexicons/{t}/huggingface/yoruba.csv",
                    citation=textwrap.dedent(
                        f"""{_CITATION}"""
                    ),
                ),
            )

    def _info(self):
        features = {text_feature: datasets.Value("string") for text_feature in self.config.text_features}
        features["label"] = datasets.features.ClassLabel(names=self.config.label_classes)

        return datasets.DatasetInfo(
            description=self.config.description,
            features=datasets.Features(features),
            citation=self.config.citation,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        hau_path = dl_manager.download_and_extract(self.config.hau_url)
        ibo_path = dl_manager.download_and_extract(self.config.ibo_url)
        yor_path = dl_manager.download_and_extract(self.config.yor_url)
        
        return [
            datasets.SplitGenerator(name="hausa", gen_kwargs={"filepath": hau_path}),
            datasets.SplitGenerator(name="igbo", gen_kwargs={"filepath": ibo_path}),
            datasets.SplitGenerator(name="yoruba", gen_kwargs={"filepath": yor_path})
        ]

    def _generate_examples(self, filepath):
        # load the dataset
        df = pd.read_csv(filepath)

        print("-"*100)
        print(df.head())
        print("-"*100)

        if self.config.name == "translated-sentiment":
            for id_, row in df.iterrows():
                word = row["word"]
                machine = row["machine"]
                human = row["human"]
                label = row["label"]

                yield id_, {"word": word, "machine translation": machine, "human translation": human, "label": label}

        elif self.config.name == "manual-sentiment":
            for id_, row in df.iterrows():
                word = row["word"]
                label = row["label"]

                yield id_, {"word": word, "label": label}
        
        else:
            for id_, row in df.iterrows():
                word = row["word"]
                machine = row["machine"]
                human = row["human"]
                label = row["label"]
                emotion_intensity_score = row["emotion_intensity_score"]

                yield id_, {"word": word, "machine translation": machine, "human translation": human, "label": label, "emotion_intensity_score": emotion_intensity_score}