Datasets:

ArXiv:
License:
jaygala24 commited on
Commit
482abc1
1 Parent(s): 70d95e3

Upload IN22-Conv.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. IN22-Conv.py +204 -0
IN22-Conv.py ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ """The IN-22 Conv Evaluation Benchmark for evaluation of Machine Translation for Indic Languages."""
3
+
4
+ import os
5
+ import sys
6
+ import datasets
7
+
8
+ from typing import Union, List, Optional
9
+
10
+
11
+ _CITATION = """
12
+ @article{ai4bharat2023indictrans2,
13
+ title = {IndicTrans2: Towards High-Quality and Accessible Machine Translation Models for all 22 Scheduled Indian Languages},
14
+ author = {AI4Bharat and Jay Gala and Pranjal A. Chitale and Raghavan AK and Sumanth Doddapaneni and Varun Gumma and Aswanth Kumar and Janki Nawale and Anupama Sujatha and Ratish Puduppully and Vivek Raghavan and Pratyush Kumar and Mitesh M. Khapra and Raj Dabre and Anoop Kunchukuttan},
15
+ year = {2023},
16
+ journal = {arXiv preprint arXiv: 2305.16307}
17
+ }
18
+ """
19
+
20
+ _DESCRIPTION = """\
21
+ IN-22 is a newly created comprehensive benchmark for evaluating machine translation performance in multi-domain, n-way parallel contexts across 22 Indic languages.
22
+ IN22-Conv is the conversation domain subset of IN22. It is designed to assess translation quality in typical day-to-day conversational-style applications.
23
+ Currently, we use it for sentence-level evaluation of MT systems but can be repurposed for document translation evaluation as well.
24
+ """
25
+
26
+ _HOMEPAGE = "https://github.com/AI4Bharat/IndicTrans2"
27
+
28
+ _LICENSE = "CC-BY-4.0"
29
+
30
+ _LANGUAGES = [
31
+ "asm_Beng", "ben_Beng", "brx_Deva",
32
+ "doi_Deva", "eng_Latn", "gom_Deva",
33
+ "guj_Gujr", "hin_Deva", "kan_Knda",
34
+ "kas_Arab", "mai_Deva", "mal_Mlym",
35
+ "mar_Deva", "mni_Mtei", "npi_Deva",
36
+ "ory_Orya", "pan_Guru", "san_Deva",
37
+ "sat_Olck", "snd_Deva", "tam_Taml",
38
+ "tel_Telu", "urd_Arab"
39
+ ]
40
+
41
+ _URL = "https://indictrans2-public.objectstore.e2enetworks.net/IN22_benchmark.tar.gz"
42
+
43
+ _SPLITS = ["conv"]
44
+
45
+ _SENTENCES_PATHS = {
46
+ lang: {
47
+ split: os.path.join("IN22_benchmark", split, f"test.{lang}")
48
+ for split in _SPLITS
49
+ } for lang in _LANGUAGES
50
+ }
51
+
52
+ _METADATA_PATHS = {
53
+ split: os.path.join("IN22_benchmark", f"metadata_{split}.tsv")
54
+ for split in _SPLITS
55
+ }
56
+
57
+ from itertools import permutations
58
+
59
+ def _pairings(iterable, r=2):
60
+ previous = tuple()
61
+ for p in permutations(sorted(iterable), r):
62
+ if p > previous:
63
+ previous = p
64
+ yield p
65
+
66
+
67
+ class IN22ConvConfig(datasets.BuilderConfig):
68
+ """BuilderConfig for the IN-22 Conv evaluation subset."""
69
+ def __init__(self, lang: str, lang2: str = None, **kwargs):
70
+ """
71
+ Args:
72
+ **kwargs: keyword arguments forwarded to super.
73
+ """
74
+ super().__init__(version=datasets.Version("1.0.0"), **kwargs)
75
+ self.lang = lang
76
+ self.lang2 = lang2
77
+
78
+
79
+ class IN22Conv(datasets.GeneratorBasedBuilder):
80
+ """IN-22 Conv evaluation subset."""
81
+
82
+ BUILDER_CONFIGS = [
83
+ IN22ConvConfig(
84
+ name=lang,
85
+ description=f"IN-22: {lang} subset.",
86
+ lang=lang
87
+ )
88
+ for lang in _LANGUAGES
89
+ ] + [
90
+ IN22ConvConfig(
91
+ name="all",
92
+ description=f"IN-22: all language pairs",
93
+ lang=None
94
+ )
95
+ ] + [
96
+ IN22ConvConfig(
97
+ name=f"{l1}-{l2}",
98
+ description=f"IN-22: {l1}-{l2} aligned subset.",
99
+ lang=l1,
100
+ lang2=l2
101
+ ) for (l1,l2) in _pairings(_LANGUAGES)
102
+ ]
103
+
104
+ def _info(self):
105
+ features = {
106
+ "id": datasets.Value("int32"),
107
+ "doc_id": datasets.Value("int32"),
108
+ "sent_id": datasets.Value("int32"),
109
+ "topic": datasets.Value("string"),
110
+ "domain": datasets.Value("string"),
111
+ "prompt": datasets.Value("string"),
112
+ "scenario": datasets.Value("string"),
113
+ "speaker": datasets.Value("int32"),
114
+ "turn": datasets.Value("int32")
115
+ }
116
+ if self.config.name != "all" and "-" not in self.config.name:
117
+ features["sentence"] = datasets.Value("string")
118
+ elif "-" in self.config.name:
119
+ for lang in [self.config.lang, self.config.lang2]:
120
+ features[f"sentence_{lang}"] = datasets.Value("string")
121
+ else:
122
+ for lang in _LANGUAGES:
123
+ features[f"sentence_{lang}"] = datasets.Value("string")
124
+ return datasets.DatasetInfo(
125
+ description=_DESCRIPTION,
126
+ features=datasets.Features(features),
127
+ homepage=_HOMEPAGE,
128
+ license=_LICENSE,
129
+ citation=_CITATION,
130
+ )
131
+
132
+ def _split_generators(self, dl_manager):
133
+ dl_dir = dl_manager.download_and_extract(_URL)
134
+
135
+ def _get_sentence_paths(split):
136
+ if isinstance(self.config.lang, str) and isinstance(self.config.lang2, str):
137
+ sentence_paths = [os.path.join(dl_dir, _SENTENCES_PATHS[lang][split]) for lang in (self.config.lang, self.config.lang2)]
138
+ elif isinstance(self.config.lang, str):
139
+ sentence_paths = os.path.join(dl_dir, _SENTENCES_PATHS[self.config.lang][split])
140
+ else:
141
+ sentence_paths = [os.path.join(dl_dir, _SENTENCES_PATHS[lang][split]) for lang in _LANGUAGES]
142
+ return sentence_paths
143
+ return [
144
+ datasets.SplitGenerator(
145
+ name=split,
146
+ gen_kwargs={
147
+ "sentence_paths": _get_sentence_paths(split),
148
+ "metadata_path": os.path.join(dl_dir, _METADATA_PATHS[split]),
149
+ }
150
+ ) for split in _SPLITS
151
+ ]
152
+
153
+ def _generate_examples(self, sentence_paths: Union[str, List[str]], metadata_path: str, langs: Optional[List[str]] = None):
154
+ """Yields examples as (key, example) tuples."""
155
+ if isinstance(sentence_paths, str):
156
+ with open(sentence_paths, "r") as sentences_file:
157
+ with open(metadata_path, "r") as metadata_file:
158
+ metadata_lines = [l.strip() for l in metadata_file.readlines()[1:]]
159
+ for id_, (sentence, metadata) in enumerate(
160
+ zip(sentences_file, metadata_lines)
161
+ ):
162
+ sentence = sentence.strip()
163
+ metadata = metadata.split("\t")
164
+ yield id_, {
165
+ "id": id_ + 1,
166
+ "sentence": sentence,
167
+ "doc_id": metadata[0],
168
+ "sent_id": metadata[1],
169
+ "topic": metadata[2],
170
+ "domain": metadata[3],
171
+ "prompt": metadata[4],
172
+ "scenario": metadata[5],
173
+ "speaker": metadata[6],
174
+ "turn": metadata[7]
175
+ }
176
+ else:
177
+ sentences = {}
178
+ if len(sentence_paths) == len(_LANGUAGES):
179
+ langs = _LANGUAGES
180
+ else:
181
+ langs = [self.config.lang, self.config.lang2]
182
+ for path, lang in zip(sentence_paths, langs):
183
+ with open(path, "r") as sent_file:
184
+ sentences[lang] = [l.strip() for l in sent_file.readlines()]
185
+ with open(metadata_path, "r") as metadata_file:
186
+ metadata_lines = [l.strip() for l in metadata_file.readlines()[1:]]
187
+ for id_, metadata in enumerate(metadata_lines):
188
+ metadata = metadata.split("\t")
189
+ yield id_, {
190
+ **{
191
+ "id": id_ + 1,
192
+ "doc_id": metadata[0],
193
+ "sent_id": metadata[1],
194
+ "topic": metadata[2],
195
+ "domain": metadata[3],
196
+ "prompt": metadata[4],
197
+ "scenario": metadata[5],
198
+ "speaker": metadata[6],
199
+ "turn": metadata[7]
200
+ }, **{
201
+ f"sentence_{lang}": sentences[lang][id_]
202
+ for lang in langs
203
+ }
204
+ }