Update bodo_english_parallel_test.py
Browse files- bodo_english_parallel_test.py +111 -0
bodo_english_parallel_test.py
CHANGED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2020 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 |
+
import os
|
| 18 |
+
|
| 19 |
+
import datasets
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
_DESCRIPTION = """\
|
| 23 |
+
Bodo and English Parallel Sentences
|
| 24 |
+
2 languages, 3 bitexts
|
| 25 |
+
|
| 26 |
+
;) @alayaran
|
| 27 |
+
|
| 28 |
+
"""
|
| 29 |
+
_HOMEPAGE_URL = "http://get.alayaran.com"
|
| 30 |
+
_CITATION = """\
|
| 31 |
+
In progress
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
_VERSION = "1.0.0"
|
| 35 |
+
_BASE_NAME = "{}-parallel/{}.{}"
|
| 36 |
+
_BASE_URL = "http://get.alayaran.com/hf/{}-{}-parallel.zip"
|
| 37 |
+
|
| 38 |
+
_LANGUAGE_PAIRS = [
|
| 39 |
+
("brx", "eng"),
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
class BodoDatasetConfig(datasets.BuilderConfig):
|
| 44 |
+
def __init__(self, *args, lang1=None, lang2=None, **kwargs):
|
| 45 |
+
super().__init__(
|
| 46 |
+
*args,
|
| 47 |
+
name=f"{lang1}-{lang2}",
|
| 48 |
+
**kwargs,
|
| 49 |
+
)
|
| 50 |
+
self.lang1 = lang1
|
| 51 |
+
self.lang2 = lang2
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
class BodoDataset(datasets.GeneratorBasedBuilder):
|
| 55 |
+
BUILDER_CONFIGS = [
|
| 56 |
+
BodoDatasetConfig(
|
| 57 |
+
lang1=lang1,
|
| 58 |
+
lang2=lang2,
|
| 59 |
+
description=f"Translating {lang1} to {lang2} or vice versa",
|
| 60 |
+
version=datasets.Version(_VERSION),
|
| 61 |
+
)
|
| 62 |
+
for lang1, lang2 in _LANGUAGE_PAIRS
|
| 63 |
+
]
|
| 64 |
+
BUILDER_CONFIG_CLASS = BodoDatasetConfig
|
| 65 |
+
|
| 66 |
+
def _info(self):
|
| 67 |
+
return datasets.DatasetInfo(
|
| 68 |
+
description=_DESCRIPTION,
|
| 69 |
+
features=datasets.Features(
|
| 70 |
+
{
|
| 71 |
+
"id": datasets.Value("string"),
|
| 72 |
+
"translation": datasets.Translation(languages=(self.config.lang1, self.config.lang2)),
|
| 73 |
+
},
|
| 74 |
+
),
|
| 75 |
+
supervised_keys=None,
|
| 76 |
+
homepage=_HOMEPAGE_URL,
|
| 77 |
+
citation=_CITATION,
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
def _split_generators(self, dl_manager):
|
| 81 |
+
def _base_url(lang1, lang2):
|
| 82 |
+
return _BASE_URL.format(lang1, lang2)
|
| 83 |
+
|
| 84 |
+
download_url = _base_url(self.config.lang1, self.config.lang2)
|
| 85 |
+
path = dl_manager.download_and_extract(download_url)
|
| 86 |
+
return [
|
| 87 |
+
datasets.SplitGenerator(
|
| 88 |
+
name=datasets.Split.TEST,
|
| 89 |
+
gen_kwargs={"datapath": path},
|
| 90 |
+
)
|
| 91 |
+
]
|
| 92 |
+
|
| 93 |
+
def _generate_examples(self, datapath):
|
| 94 |
+
l1, l2 = self.config.lang1, self.config.lang2
|
| 95 |
+
folder = l1 + "-" + l2
|
| 96 |
+
l1_file = _BASE_NAME.format(folder, 'test', l1)
|
| 97 |
+
l2_file = _BASE_NAME.format(folder, 'test', l2)
|
| 98 |
+
l1_path = os.path.join(datapath, l1_file)
|
| 99 |
+
l2_path = os.path.join(datapath, l2_file)
|
| 100 |
+
with open(l1_path, encoding="utf-8") as f1, open(l2_path, encoding="utf-8") as f2:
|
| 101 |
+
for sentence_counter, (x, y) in enumerate(zip(f1, f2)):
|
| 102 |
+
x = x.strip()
|
| 103 |
+
y = y.strip()
|
| 104 |
+
result = (
|
| 105 |
+
sentence_counter,
|
| 106 |
+
{
|
| 107 |
+
"id": str(sentence_counter),
|
| 108 |
+
"translation": {l1: x, l2: y},
|
| 109 |
+
},
|
| 110 |
+
)
|
| 111 |
+
yield result
|