adding load script
Browse files- wmt22_african.py +105 -0
wmt22_african.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
"""WMT21 Metrics Shared Task: Segment-Level Data"""
|
16 |
+
|
17 |
+
import datasets
|
18 |
+
import csv
|
19 |
+
|
20 |
+
_CITATION = "" # TODO
|
21 |
+
|
22 |
+
|
23 |
+
_DESCRIPTION = "" # TODO
|
24 |
+
|
25 |
+
_HOMEPAGE = "" # TODO
|
26 |
+
|
27 |
+
_LICENSE = "Unknown" # TODO
|
28 |
+
|
29 |
+
|
30 |
+
_LANGUAGE_PAIRS = [('afr', 'eng'), ('afr', 'som'), ('amh', 'eng'), ('amh', 'fra'), ('amh', 'nya'), ('amh', 'orm'), ('amh', 'sna'), ('amh', 'som'), ('amh', 'ssw'), ('amh', 'swh'), ('amh', 'tsn'), ('amh', 'tso'), ('amh', 'umb'), ('amh', 'xho'), ('amh', 'yor'), ('amh', 'zul'), ('eng', 'fuv'), ('eng', 'hau'), ('eng', 'ibo'), ('eng', 'kam'), ('eng', 'kin'), ('eng', 'lin'), ('eng', 'lug'), ('eng', 'luo'), ('eng', 'nso'), ('eng', 'nya'), ('eng', 'orm'), ('eng', 'sna'), ('eng', 'som'), ('eng', 'ssw'), ('eng', 'swh'), ('eng', 'tsn'), ('eng', 'tso'), ('eng', 'umb'), ('eng', 'wol'), ('eng', 'xho'), ('eng', 'yor'), ('eng', 'zul'), ('fra', 'hau'), ('fra', 'ibo'), ('fra', 'kam'), ('fra', 'kin'), ('fra', 'lin'), ('fra', 'lug'), ('fra', 'luo'), ('fra', 'nso'), ('fra', 'nya'), ('fra', 'orm'), ('fra', 'som'), ('fra', 'ssw'), ('fra', 'swh'), ('fra', 'tsn'), ('fra', 'tso'), ('fra', 'umb'), ('fra', 'wol'), ('fra', 'xho'), ('fra', 'zul'), ('fuv', 'hau'), ('fuv', 'ibo'), ('fuv', 'kam'), ('fuv', 'kin'), ('fuv', 'lug'), ('fuv', 'luo'), ('fuv', 'nso'), ('fuv', 'nya'), ('fuv', 'orm'), ('fuv', 'sna'), ('fuv', 'som'), ('fuv', 'ssw'), ('fuv', 'swh'), ('fuv', 'tsn'), ('fuv', 'tso'), ('fuv', 'umb'), ('fuv', 'xho'), ('fuv', 'yor'), ('fuv', 'zul'), ('hau', 'ibo'), ('hau', 'kam'), ('hau', 'kin'), ('hau', 'lug'), ('hau', 'luo'), ('hau', 'nso'), ('hau', 'nya'), ('hau', 'orm'), ('hau', 'sna'), ('hau', 'som'), ('hau', 'ssw'), ('hau', 'swh'), ('hau', 'tsn'), ('hau', 'tso'), ('hau', 'umb'), ('hau', 'xho'), ('hau', 'yor'), ('hau', 'zul'), ('ibo', 'kam'), ('ibo', 'kin'), ('ibo', 'lug'), ('ibo', 'luo'), ('ibo', 'nso'), ('ibo', 'nya'), ('ibo', 'orm'), ('ibo', 'sna'), ('ibo', 'som'), ('ibo', 'ssw'), ('ibo', 'swh'), ('ibo', 'tsn'), ('ibo', 'tso'), ('ibo', 'umb'), ('ibo', 'xho'), ('ibo', 'yor'), ('ibo', 'zul'), ('kam', 'kin'), ('kam', 'lug'), ('kam', 'luo'), ('kam', 'nso'), ('kam', 'nya'), ('kam', 'orm'), ('kam', 'sna'), ('kam', 'som'), ('kam', 'ssw'), ('kam', 'swh'), ('kam', 'tsn'), ('kam', 'tso'), ('kam', 'umb'), ('kam', 'xho'), ('kam', 'yor'), ('kam', 'zul'), ('kin', 'lug'), ('kin', 'luo'), ('kin', 'nso'), ('kin', 'nya'), ('kin', 'orm'), ('kin', 'sna'), ('kin', 'som'), ('kin', 'ssw'), ('kin', 'swh'), ('kin', 'tsn'), ('kin', 'tso'), ('kin', 'umb'), ('kin', 'xho'), ('kin', 'yor'), ('kin', 'zul'), ('lug', 'luo'), ('lug', 'nso'), ('lug', 'nya'), ('lug', 'orm'), ('lug', 'sna'), ('lug', 'som'), ('lug', 'ssw'), ('lug', 'swh'), ('lug', 'tsn'), ('lug', 'tso'), ('lug', 'umb'), ('lug', 'xho'), ('lug', 'yor'), ('lug', 'zul'), ('luo', 'nso'), ('luo', 'nya'), ('luo', 'orm'), ('luo', 'sna'), ('luo', 'som'), ('luo', 'ssw'), ('luo', 'swh'), ('luo', 'tsn'), ('luo', 'tso'), ('luo', 'umb'), ('luo', 'xho'), ('luo', 'yor'), ('luo', 'zul'), ('nso', 'nya'), ('nso', 'orm'), ('nso', 'sna'), ('nso', 'som'), ('nso', 'ssw'), ('nso', 'swh'), ('nso', 'tsn'), ('nso', 'tso'), ('nso', 'umb'), ('nso', 'xho'), ('nso', 'yor'), ('nso', 'zul'), ('nya', 'orm'), ('nya', 'sna'), ('nya', 'som'), ('nya', 'ssw'), ('nya', 'swh'), ('nya', 'tsn'), ('nya', 'tso'), ('nya', 'umb'), ('nya', 'xho'), ('nya', 'yor'), ('nya', 'zul'), ('orm', 'sna'), ('orm', 'som'), ('orm', 'ssw'), ('orm', 'swh'), ('orm', 'tsn'), ('orm', 'tso'), ('orm', 'umb'), ('orm', 'xho'), ('orm', 'yor'), ('orm', 'zul'), ('sna', 'som'), ('sna', 'ssw'), ('sna', 'swh'), ('sna', 'tsn'), ('sna', 'tso'), ('sna', 'umb'), ('sna', 'xho'), ('sna', 'yor'), ('sna', 'zul'), ('som', 'ssw'), ('som', 'swh'), ('som', 'tsn'), ('som', 'tso'), ('som', 'umb'), ('som', 'wol'), ('som', 'xho'), ('som', 'yor'), ('som', 'zul'), ('ssw', 'swh'), ('ssw', 'tsn'), ('ssw', 'tso'), ('ssw', 'umb'), ('ssw', 'xho'), ('ssw', 'yor'), ('ssw', 'zul'), ('swh', 'tsn'), ('swh', 'tso'), ('swh', 'umb'), ('swh', 'xho'), ('swh', 'yor'), ('swh', 'zul'), ('tsn', 'tso'), ('tsn', 'umb'), ('tsn', 'xho'), ('tsn', 'yor'), ('tsn', 'zul'), ('tso', 'umb'), ('tso', 'xho'), ('tso', 'yor'), ('tso', 'zul'), ('umb', 'xho'), ('umb', 'yor'), ('umb', 'zul'), ('xho', 'yor'), ('xho', 'zul'), ('yor', 'zul')]
|
31 |
+
|
32 |
+
_URL_BASE = "https://huggingface.co/datasets/allenai/wmt22_african/tree/main/"
|
33 |
+
_URLs = {f"{src_lg}-{trg_lg}": f"{_URL_BASE}wmt22_african_{src_lg}-{trg_lg}.gz" for src_lg, trg_lg in _LANGUAGE_PAIRS}
|
34 |
+
|
35 |
+
|
36 |
+
class Wmt22AfricanTaskConfig(datasets.BuilderConfig):
|
37 |
+
"""BuilderConfig for WMT22 African Shared Task."""
|
38 |
+
def __init__(self, src_lg, tgt_lg, **kwargs):
|
39 |
+
super(Wmt22AfricanTaskConfig, self).__init__(**kwargs)
|
40 |
+
self.src_lg = src_lg
|
41 |
+
self.tgt_lg = tgt_lg
|
42 |
+
|
43 |
+
|
44 |
+
class Wmt21MetricsTask(datasets.GeneratorBasedBuilder):
|
45 |
+
"""WMT Metrics Shared Task."""
|
46 |
+
|
47 |
+
BUILDER_CONFIGS = [
|
48 |
+
Wmt22AfricanTaskConfig(
|
49 |
+
name=f"{src_lg}-{tgt_lg}",
|
50 |
+
version=datasets.Version("1.1.0"),
|
51 |
+
description=f"WMT 2022 African: {src_lg} - {tgt_lg}",
|
52 |
+
src_lg=src_lg,
|
53 |
+
tgt_lg=tgt_lg,
|
54 |
+
)
|
55 |
+
for (src_lg, tgt_lg) in _LANGUAGE_PAIRS
|
56 |
+
]
|
57 |
+
BUILDER_CONFIG_CLASS = Wmt22AfricanTaskConfig
|
58 |
+
|
59 |
+
def _info(self):
|
60 |
+
# define feature types
|
61 |
+
features = datasets.Features(
|
62 |
+
{
|
63 |
+
'translation': datasets.Translation(languages=(self.config.src_lg, self.config.tgt_lg)),
|
64 |
+
'laser_score':datasets.Value("float32"),
|
65 |
+
'source_sentence_lid':datasets.Value("float32"),
|
66 |
+
'target_sentence_lid':datasets.Value("float32"),
|
67 |
+
}
|
68 |
+
)
|
69 |
+
|
70 |
+
return datasets.DatasetInfo(
|
71 |
+
description=_DESCRIPTION,
|
72 |
+
features=features,
|
73 |
+
supervised_keys=None,
|
74 |
+
homepage=_HOMEPAGE,
|
75 |
+
license=_LICENSE,
|
76 |
+
citation=_CITATION,
|
77 |
+
)
|
78 |
+
|
79 |
+
def _split_generators(self, dl_manager):
|
80 |
+
"""Returns SplitGenerators."""
|
81 |
+
pair = f"{self.config.src_lg}-{self.config.tgt_lg}" # string identifier for language pair
|
82 |
+
url = _URLs[pair] # url for download of pair-specific file
|
83 |
+
data_file = dl_manager.download_and_extract(url) # extract downloaded data and store path in data_file
|
84 |
+
|
85 |
+
return [
|
86 |
+
datasets.SplitGenerator(
|
87 |
+
name=datasets.Split.TRAIN,
|
88 |
+
gen_kwargs={
|
89 |
+
"filepath": data_file,
|
90 |
+
"source_lg": self.config.src_lg,
|
91 |
+
"target_lg": self.config.tgt_lg,
|
92 |
+
}
|
93 |
+
)
|
94 |
+
]
|
95 |
+
|
96 |
+
def _generate_examples(self, filepath, source_lg, target_lg):
|
97 |
+
with open(filepath, encoding="utf-8") as f:
|
98 |
+
reader = csv.DictReader(f, delimiter=";") # read each line into dict
|
99 |
+
for id_, row in enumerate(reader):
|
100 |
+
row["translation"] = {source_lg : row["source"], target_lg: row["system_output"]} # create translation json
|
101 |
+
row = {k: None if not v else v for k, v in row.items()} # replace empty values
|
102 |
+
yield id_, row
|
103 |
+
|
104 |
+
# to test the script, go to the root folder of the repo (wmt22_african) and run:
|
105 |
+
# datasets-cli test wmt22_african --save_infos --all_configs
|