alayaran commited on
Commit
fc4656f
·
1 Parent(s): a0d902d

Create bodo_english_parallel.py

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