Datasets:
shmuhammad
commited on
Commit
•
4900ca0
1
Parent(s):
6508101
Upload AfriSenti.py
Browse files- AfriSenti.py +122 -0
AfriSenti.py
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2020 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 |
+
|
17 |
+
import csv
|
18 |
+
import textwrap
|
19 |
+
import pandas as pd
|
20 |
+
|
21 |
+
import datasets
|
22 |
+
|
23 |
+
LANGUAGES = ['amh', 'hau', 'ibo', 'arq', 'ary', 'yor', 'por', 'twi', 'tso', 'tir', 'orm', 'pcm', 'kin', 'swa']
|
24 |
+
|
25 |
+
class AfriSentiConfig(datasets.BuilderConfig):
|
26 |
+
"""BuilderConfig for AfriSenti"""
|
27 |
+
|
28 |
+
def __init__(
|
29 |
+
self,
|
30 |
+
text_features,
|
31 |
+
label_column,
|
32 |
+
label_classes,
|
33 |
+
train_url,
|
34 |
+
valid_url,
|
35 |
+
test_url,
|
36 |
+
citation,
|
37 |
+
**kwargs,
|
38 |
+
):
|
39 |
+
"""BuilderConfig for AfriSenti.
|
40 |
+
|
41 |
+
Args:
|
42 |
+
text_features: `dict[string]`, map from the name of the feature
|
43 |
+
dict for each text field to the name of the column in the txt/csv/tsv file
|
44 |
+
label_column: `string`, name of the column in the txt/csv/tsv file corresponding
|
45 |
+
to the label
|
46 |
+
label_classes: `list[string]`, the list of classes if the label is categorical
|
47 |
+
train_url: `string`, url to train file from
|
48 |
+
valid_url: `string`, url to valid file from
|
49 |
+
test_url: `string`, url to test file from
|
50 |
+
citation: `string`, citation for the data set
|
51 |
+
**kwargs: keyword arguments forwarded to super.
|
52 |
+
"""
|
53 |
+
super(AfriSentiConfig, self).__init__(version=datasets.Version("1.0.0", ""), **kwargs)
|
54 |
+
self.text_features = text_features
|
55 |
+
self.label_column = label_column
|
56 |
+
self.label_classes = label_classes
|
57 |
+
self.train_url = train_url
|
58 |
+
self.valid_url = valid_url
|
59 |
+
self.test_url = test_url
|
60 |
+
self.citation = citation
|
61 |
+
|
62 |
+
|
63 |
+
class AfriSenti(datasets.GeneratorBasedBuilder):
|
64 |
+
"""AfriSenti benchmark"""
|
65 |
+
|
66 |
+
BUILDER_CONFIGS = []
|
67 |
+
|
68 |
+
for lang in LANGUAGES:
|
69 |
+
BUILDER_CONFIGS.append(
|
70 |
+
AfriSentiConfig(
|
71 |
+
name=lang,
|
72 |
+
description=textwrap.dedent(
|
73 |
+
f"""\
|
74 |
+
{lang} dataset."""
|
75 |
+
),
|
76 |
+
text_features={"tweet": "tweet"},
|
77 |
+
label_classes=["positive", "neutral", "negative"],
|
78 |
+
label_column="label",
|
79 |
+
train_url=f"https://raw.githubusercontent.com/afrisenti-semeval/afrisent-semeval-2023/main/data/{lang}/train.tsv",
|
80 |
+
valid_url=f"https://raw.githubusercontent.com/afrisenti-semeval/afrisent-semeval-2023/main/data/{lang}/dev.tsv",
|
81 |
+
test_url=f"https://raw.githubusercontent.com/afrisenti-semeval/afrisent-semeval-2023/main/data/{lang}/test.tsv",
|
82 |
+
citation=textwrap.dedent(
|
83 |
+
f"""\
|
84 |
+
{lang} citation"""
|
85 |
+
),
|
86 |
+
),
|
87 |
+
)
|
88 |
+
|
89 |
+
def _info(self):
|
90 |
+
features = {text_feature: datasets.Value("string") for text_feature in self.config.text_features}
|
91 |
+
features["label"] = datasets.features.ClassLabel(names=self.config.label_classes)
|
92 |
+
|
93 |
+
return datasets.DatasetInfo(
|
94 |
+
description=self.config.description,
|
95 |
+
features=datasets.Features(features),
|
96 |
+
citation=self.config.citation,
|
97 |
+
)
|
98 |
+
|
99 |
+
def _split_generators(self, dl_manager):
|
100 |
+
"""Returns SplitGenerators."""
|
101 |
+
train_path = dl_manager.download_and_extract(self.config.train_url)
|
102 |
+
valid_path = dl_manager.download_and_extract(self.config.valid_url)
|
103 |
+
test_path = dl_manager.download_and_extract(self.config.test_url)
|
104 |
+
|
105 |
+
return [
|
106 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": train_path, "split": "train"}),
|
107 |
+
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": valid_path, "split": "dev"}),
|
108 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": test_path, "split": "test"}),
|
109 |
+
]
|
110 |
+
|
111 |
+
def _generate_examples(self, filepath):
|
112 |
+
df = pd.read_csv(filepath, sep='\t')
|
113 |
+
|
114 |
+
print('-'*100)
|
115 |
+
print(df.head())
|
116 |
+
print('-'*100)
|
117 |
+
|
118 |
+
for id_, row in df.iterrows():
|
119 |
+
tweet = row["tweet"]
|
120 |
+
label = row["label"]
|
121 |
+
|
122 |
+
yield id_, {"tweet": tweet, "label": label}
|