Genius1237 commited on
Commit
9adcec4
·
1 Parent(s): 622f3dd

rename script to match dataset name

Browse files
Files changed (1) hide show
  1. TyDiP.py +118 -0
TyDiP.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
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
+ # Lint as: python3
17
+ """TyDiP: A Multilingual Politeness Dataset"""
18
+
19
+
20
+ import csv
21
+ from dataclasses import dataclass
22
+ import datasets
23
+ from datasets.tasks import TextClassification
24
+
25
+
26
+ _CITATION = """\
27
+ @inproceedings{srinivasan-choi-2022-tydip,
28
+ title = "{T}y{D}i{P}: A Dataset for Politeness Classification in Nine Typologically Diverse Languages",
29
+ author = "Srinivasan, Anirudh and
30
+ Choi, Eunsol",
31
+ booktitle = "Findings of the Association for Computational Linguistics: EMNLP 2022",
32
+ month = dec,
33
+ year = "2022",
34
+ address = "Abu Dhabi, United Arab Emirates",
35
+ publisher = "Association for Computational Linguistics",
36
+ url = "https://aclanthology.org/2022.findings-emnlp.420",
37
+ pages = "5723--5738",
38
+ }"""
39
+
40
+ _DESCRIPTION = """\
41
+ The TyDiP dataset is a dataset of requests in conversations between wikipedia editors
42
+ that have been annotated for politeness. The splits available below consists of only
43
+ requests from the top 25 percentile (polite) and bottom 25 percentile (impolite) of
44
+ politeness scores. The English train set and English test set that are
45
+ adapted from the Stanford Politeness Corpus, and test data in 9 more languages
46
+ (Hindi, Korean, Spanish, Tamil, French, Vietnamese, Russian, Afrikaans, Hungarian)
47
+ was annotated by us.
48
+ """
49
+
50
+ _LANGUAGES = ("en", "hi", "ko", "es", "ta", "fr", "vi", "ru", "af", "hu")
51
+
52
+
53
+ # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
54
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
55
+ # _URL = "https://huggingface.co/datasets/Genius1237/TyDiP/resolve/main/data/binary/"
56
+ _URL = "https://huggingface.co/datasets/Genius1237/TyDiP/raw/main/data/binary/"
57
+ _URLS = {
58
+ 'en': {
59
+ 'train': _URL + 'en_train_binary.csv',
60
+ 'test': _URL + 'en_test_binary.csv'
61
+ },
62
+ } | {lang: {'test': _URL + '{}_test_binary.csv'.format(lang)} for lang in _LANGUAGES[1:]}
63
+
64
+
65
+ @dataclass
66
+ class TyDiPConfig(datasets.BuilderConfig):
67
+ """BuilderConfig for TyDiP."""
68
+ lang: str = None
69
+
70
+
71
+ class MultilingualLibrispeech(datasets.GeneratorBasedBuilder):
72
+ """TyDiP dataset."""
73
+
74
+ BUILDER_CONFIGS = [
75
+ TyDiPConfig(name=lang, lang=lang) for lang in _LANGUAGES
76
+ ]
77
+
78
+ def _info(self):
79
+ return datasets.DatasetInfo(
80
+ description=_DESCRIPTION,
81
+ features=datasets.Features(
82
+ {
83
+ "text": datasets.Value("string"),
84
+ "labels": datasets.ClassLabel(num_classes=2, names=[0, 1]),
85
+ }
86
+ ),
87
+ supervised_keys=("text", "labels"),
88
+ homepage=_URL,
89
+ citation=_CITATION,
90
+ task_templates=[TextClassification(text_column="text", label_column="labels")],
91
+ )
92
+
93
+ def _split_generators(self, dl_manager):
94
+ splits = []
95
+ if self.config.lang == 'en':
96
+ file_path = dl_manager.download_and_extract(_URLS['en']['train'])
97
+ splits.append(
98
+ datasets.SplitGenerator(
99
+ name=datasets.Split.TRAIN, gen_kwargs={"data_file": file_path}
100
+ ))
101
+ file_path = dl_manager.download_and_extract(_URLS[self.config.lang]['test'])
102
+ splits.append(
103
+ datasets.SplitGenerator(
104
+ name=datasets.Split.TEST, gen_kwargs={"data_file": file_path}
105
+ )
106
+ )
107
+ return splits
108
+
109
+ def _generate_examples(self, data_file):
110
+ """Generate examples from a TyDiP data file"""
111
+ with open(data_file) as f:
112
+ csv_reader = csv.reader(f)
113
+ for i, row in enumerate(csv_reader):
114
+ if i != 0:
115
+ yield i - 1, {
116
+ "text": row[0],
117
+ "labels": int(float(row[1]) > 0),
118
+ }