holylovenia commited on
Commit
e7c6c7e
1 Parent(s): d2d5391

Upload lio_and_central_flores.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. lio_and_central_flores.py +193 -0
lio_and_central_flores.py ADDED
@@ -0,0 +1,193 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ from typing import List, Tuple
3
+
4
+ import datasets
5
+
6
+ from seacrowd.sea_datasets.lio_and_central_flores import processing
7
+ from seacrowd.sea_datasets.lio_and_central_flores.path_url import _URLS_DICT
8
+ from seacrowd.utils import schemas
9
+ from seacrowd.utils.configs import SEACrowdConfig
10
+ from seacrowd.utils.constants import Licenses, Tasks
11
+
12
+ _CITATION = """\
13
+ @misc{alexthesis2018,
14
+ author = {Alexander Elias},
15
+ title = {Lio and the Central Flores languages},
16
+ year = {2018},
17
+ month = {November},
18
+ address = {Rapenburg 70, 2311 EZ Leiden},
19
+ school = {Universiteit Leiden},
20
+ url = {https://studenttheses.universiteitleiden.nl/handle/1887/69452},
21
+ note = {Research Master's thesis},
22
+ }
23
+ """
24
+
25
+ _DATASETNAME = "lio_and_central_flores"
26
+ _DESCRIPTION = """This dataset is a collection of language resources of Li'o, Ende, Nage, and
27
+ So'a which are collected in Ende, Flores, Eastern Nusa Tenggara. This dataset
28
+ is the dataset from the research MA thesis by Alexander Elias. Title: Lio and the Central Flores languages
29
+ """
30
+ _HOMEPAGE = "https://archive.mpi.nl/tla/islandora/search/alexander%20elias?type=dismax&islandora_solr_search_navigation=0&f%5B0%5D=cmd.Contributor%3A%22Alexander%5C%20Elias%22"
31
+ _LICENSE = Licenses.UNKNOWN.value
32
+ _LANGUAGES = ["end", "ljl", "nxe", "eng"]
33
+ LANGUAGES_TO_FILENAME_MAP = {
34
+ "end": "ENDE",
35
+ "nxe": "NAGE",
36
+ "ljl": "LIO",
37
+ }
38
+ _LOCAL = False
39
+
40
+ _URLS = _URLS_DICT
41
+
42
+ _SUPPORTED_TASKS = [Tasks.MACHINE_TRANSLATION]
43
+
44
+ _SOURCE_VERSION = "1.0.0"
45
+ _SEACROWD_VERSION = "2024.06.20"
46
+
47
+
48
+ class LioAndCentralFloresDataset(datasets.GeneratorBasedBuilder):
49
+ """This dataset is a collection of language resources of Li'o, Ende, Nage, and So'a"""
50
+
51
+ SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
52
+ SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
53
+ SEACROWD_SCHEMA_NAME = "t2t"
54
+
55
+ BUILDER_CONFIGS = [
56
+ # We only use source schema here for nage ("nxe") and eng because nage dataset only contain wordlist
57
+ # For "nxe" , include a separate configuration to handle word lists. It will be return nage only word list
58
+ SEACrowdConfig(name=f"{_DATASETNAME}_nxe_wordlist_source", version=SOURCE_VERSION, description=f"{_DATASETNAME} source schema", schema="source", subset_id=f"{_DATASETNAME}_nxe"),
59
+ # Additionally, include a configuration for English word lists in "nxe" datasets. It will be return eng only word corresponding to nage wordlist
60
+ SEACrowdConfig(name=f"{_DATASETNAME}_eng_wordlist_source", version=SOURCE_VERSION, description=f"{_DATASETNAME} source schema", schema="source", subset_id=f"{_DATASETNAME}_eng"),
61
+ ]
62
+
63
+ # For other languages, except "nxe", use a standard source & seacrowd schema configuration
64
+ subset_names = sorted([f"{_DATASETNAME}_{lang}_eng" for lang in _LANGUAGES[:-2]]) + sorted([f"{_DATASETNAME}_eng_{lang}" for lang in _LANGUAGES[:-2]])
65
+
66
+ for name in subset_names:
67
+ # source schema
68
+ source_config = SEACrowdConfig(name=f"{name}_source", version=SOURCE_VERSION, description=f"{_DATASETNAME} source schema", schema="source", subset_id=name)
69
+ BUILDER_CONFIGS.append(source_config)
70
+
71
+ # seacrowd_t2t schema
72
+ seacrowd_config = SEACrowdConfig(name=f"{name}_seacrowd_{SEACROWD_SCHEMA_NAME}", version=SEACROWD_VERSION, description=f"{_DATASETNAME} SEACrowd schema", schema=f"seacrowd_{SEACROWD_SCHEMA_NAME}", subset_id=name)
73
+ BUILDER_CONFIGS.append(seacrowd_config)
74
+
75
+
76
+ def _info(self) -> datasets.DatasetInfo:
77
+ if self.config.schema == "source":
78
+ if "wordlist" in self.config.name:
79
+ features = datasets.Features({"id": datasets.Value("string"), "word": datasets.Value("string")})
80
+ else:
81
+ features = datasets.Features({"source_sentence": datasets.Value("string"), "target_sentence": datasets.Value("string"), "source_lang": datasets.Value("string"), "target_lang": datasets.Value("string")})
82
+ elif self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA_NAME}":
83
+ if "nxe" not in self.config.name:
84
+ features = schemas.text2text_features
85
+ else:
86
+ raise ValueError("Invalid config schema")
87
+
88
+ return datasets.DatasetInfo(
89
+ description=_DESCRIPTION,
90
+ features=features,
91
+ homepage=_HOMEPAGE,
92
+ license=_LICENSE,
93
+ citation=_CITATION,
94
+ )
95
+
96
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
97
+ """Returns SplitGenerators."""
98
+
99
+ dset_lang = None
100
+ for lang in _LANGUAGES[:-1]: # except eng because it exists in all subset names
101
+ if lang in self.config.name:
102
+ dset_lang = lang
103
+ break
104
+ if dset_lang is None:
105
+ raise ValueError("Invalid language name")
106
+
107
+ filepath = {k: v["text_path"] for k, v in _URLS[LANGUAGES_TO_FILENAME_MAP[dset_lang]].items()}
108
+ paths = dl_manager.download(filepath)
109
+
110
+ return [
111
+ datasets.SplitGenerator(
112
+ name=datasets.Split.TRAIN,
113
+ gen_kwargs={
114
+ "filepath": paths,
115
+ "lang_1": self.config.name.split("_")[4],
116
+ "lang_2": self.config.name.split("_")[5]}
117
+ )
118
+ ]
119
+
120
+ def _generate_examples(self, filepath: Path, lang_1: str, lang_2: str):
121
+ """Yields examples as (key, example) tuples."""
122
+
123
+ if "wordlist" in self.config.name:
124
+ if "nxe" in self.config.name: # only nxe
125
+ _, words = self._get_word_(filepath)
126
+ else: # only eng
127
+ words, _ = self._get_word_(filepath)
128
+
129
+ for item in words:
130
+ for idx, word in enumerate(item):
131
+ row = {"id": str(idx), "word": word}
132
+ yield idx, row
133
+ else:
134
+ source_data, target_data = self._get_sentence_(filepath)
135
+ for idx, (eng_text, other_text) in enumerate(zip(source_data, target_data)):
136
+ if self.config.schema == "source":
137
+ if lang_1 == "eng":
138
+ example = {
139
+ "source_sentence": eng_text,
140
+ "target_sentence": other_text,
141
+ "source_lang": lang_1,
142
+ "target_lang": lang_2,
143
+ }
144
+ else:
145
+ example = {
146
+ "source_sentence": other_text,
147
+ "target_sentence": eng_text,
148
+ "source_lang": lang_1,
149
+ "target_lang": lang_2,
150
+ }
151
+
152
+ elif self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA_NAME}":
153
+ if lang_1 == "eng":
154
+ example = {
155
+ "id": str(idx),
156
+ "text_1": eng_text,
157
+ "text_2": other_text,
158
+ "text_1_name": lang_1,
159
+ "text_2_name": lang_2,
160
+ }
161
+ else:
162
+ example = {
163
+ "id": str(idx),
164
+ "text_1": other_text,
165
+ "text_2": eng_text,
166
+ "text_1_name": lang_1,
167
+ "text_2_name": lang_2,
168
+ }
169
+ yield idx, example
170
+
171
+ def _get_sentence_(self, path_dict) -> Tuple[List, List]:
172
+ source_data = []
173
+ target_data = []
174
+ for _, v in path_dict.items():
175
+ with open(v, "r", encoding="utf-8") as f:
176
+ data = f.readlines()
177
+ src, trg = processing.parse_text(data)
178
+ source_data.extend(src)
179
+ target_data.extend(trg)
180
+
181
+ return source_data, target_data
182
+
183
+ def _get_word_(self, path_dict) -> Tuple[List, List]:
184
+ eng_data, ind_data, nage_data = [], [], []
185
+ for _, v in path_dict.items():
186
+ with open(v, "r", encoding="utf-8") as f:
187
+ data = f.readlines()
188
+ eng_word, ind_word, nage_word = processing.parse_wordlist(data)
189
+ eng_data.append(eng_word)
190
+ ind_data.append(ind_word)
191
+ nage_data.append(nage_word)
192
+
193
+ return eng_data, nage_data