holylovenia commited on
Commit
c823e63
1 Parent(s): a7f45e8

Upload id_wiki_parallel.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. id_wiki_parallel.py +147 -0
id_wiki_parallel.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2022 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
+
16
+ from pathlib import Path
17
+ from typing import Dict, List, Tuple
18
+
19
+ import datasets
20
+ import pandas as pd
21
+
22
+ from nusacrowd.utils import schemas
23
+ from nusacrowd.utils.configs import NusantaraConfig
24
+ from nusacrowd.utils.constants import DEFAULT_NUSANTARA_VIEW_NAME, DEFAULT_SOURCE_VIEW_NAME, Tasks
25
+
26
+ _CITATION = """\
27
+ @INPROCEEDINGS{
28
+ 7065828,
29
+ author={Trisedya, Bayu Distiawan and Inastra, Dyah},
30
+ booktitle={2014 International Conference on Advanced Computer Science and Information System},
31
+ title={Creating Indonesian-Javanese parallel corpora using wikipedia articles},
32
+ year={2014},
33
+ volume={},
34
+ number={},
35
+ pages={239-245},
36
+ doi={10.1109/ICACSIS.2014.7065828}}
37
+ """
38
+
39
+ _DATASETNAME = "id_wiki_parallel"
40
+ _SOURCE_VIEW_NAME = DEFAULT_SOURCE_VIEW_NAME
41
+ _UNIFIED_VIEW_NAME = DEFAULT_NUSANTARA_VIEW_NAME
42
+
43
+ _LANGUAGES = ["ind", "jav", "min", "sun"]
44
+ _LOCAL = False
45
+
46
+ _DESCRIPTION = """\
47
+ This dataset is designed for machine translation task, specifically jav->ind, min->ind, sun->ind, and vice versa. The data are taken
48
+ from sentences in Wikipedia.
49
+
50
+ (from the publication abstract)
51
+ Parallel corpora are necessary for multilingual researches especially in information retrieval (IR) and natural language processing (NLP). However, such corpora are hard to find, specifically for low-resources languages like ethnic
52
+ languages. Parallel corpora of ethnic languages were usually collected manually. On the other hand, Wikipedia as a free online encyclopedia is supporting more and more languages each year, including ethnic languages in Indonesia. It has
53
+ become one of the largest multilingual sites in World Wide Web that provides free distributed articles. In this paper, we explore a few sentence alignment methods which have been used before for another domain. We want to check whether
54
+ Wikipedia can be used as one of the resources for collecting parallel corpora of Indonesian and Javanese, an ethnic language in Indonesia. We used two approaches of sentence alignment by treating Wikipedia as both parallel corpora and
55
+ comparable corpora. In parallel corpora case, we used sentence length based and word correspondence methods. Meanwhile,
56
+ we used the characteristics of hypertext links from Wikipedia in comparable corpora case. After the experiments, we can
57
+ see that Wikipedia is useful enough for our purpose because both approaches gave positive results.
58
+ """
59
+
60
+ _HOMEPAGE = "https://github.com/dindainastra/indowikiparalelcorpora"
61
+
62
+ _LICENSE = "Unknown"
63
+
64
+ _URLS = {
65
+ _DATASETNAME: {
66
+ "jav": "https://raw.githubusercontent.com/dindainastra/indowikiparalelcorpora/main/manualsets/indojv-parallel.csv",
67
+ "min": "https://raw.githubusercontent.com/dindainastra/indowikiparalelcorpora/main/manualsets/indomin-parallel.csv",
68
+ "sun": "https://raw.githubusercontent.com/dindainastra/indowikiparalelcorpora/main/manualsets/indosun-parallel.csv",
69
+ }
70
+ }
71
+
72
+ _SUPPORTED_TASKS = [Tasks.MACHINE_TRANSLATION]
73
+
74
+ _SOURCE_VERSION = "1.0.0"
75
+ _NUSANTARA_VERSION = "1.0.0"
76
+
77
+
78
+ class IdWikiParallel(datasets.GeneratorBasedBuilder):
79
+ """
80
+ This dataset is designed for machine translation task, specifically jav->ind, min->ind, sun->ind, and vice versa. The data are
81
+ taken from sentences in Wikipedia."""
82
+
83
+ ETHNIC_LANGUAGES = [lang for lang in _LANGUAGES if lang != "ind"]
84
+ BUILDER_CONFIGS = [
85
+ NusantaraConfig(
86
+ name="{dataset_name}_{src}_ind_source".format(dataset_name=_DATASETNAME, src=src),
87
+ version=datasets.Version(_SOURCE_VERSION),
88
+ description="ID Wiki Parallel source schema for {src} to ind and ind to {src}".format(src=src),
89
+ schema="source",
90
+ subset_id="{dataset_name}_{src}_ind".format(dataset_name=_DATASETNAME, src=src),
91
+ )
92
+ for src in ETHNIC_LANGUAGES
93
+ ] + [
94
+ NusantaraConfig(
95
+ name="{dataset_name}_{src}_ind_nusantara_t2t".format(dataset_name=_DATASETNAME, src=src),
96
+ version=datasets.Version(_NUSANTARA_VERSION),
97
+ description="ID Wiki Parallel Nusantara schema for {src} to ind and ind to {src}".format(src=src),
98
+ schema="nusantara_t2t",
99
+ subset_id="{dataset_name}_{src}_ind".format(dataset_name=_DATASETNAME, src=src),
100
+ )
101
+ for src in ETHNIC_LANGUAGES
102
+ ]
103
+
104
+ DEFAULT_CONFIG_NAME = "id_wiki_parallel_jav_ind_source"
105
+
106
+ def _info(self) -> datasets.DatasetInfo:
107
+ if self.config.schema == "source":
108
+ features = datasets.Features({"id": datasets.Value("string"), "text_1": datasets.Value("string"), "text_2": datasets.Value("string")})
109
+ elif self.config.schema == "nusantara_t2t":
110
+ features = schemas.text2text_features
111
+
112
+ return datasets.DatasetInfo(
113
+ description=_DESCRIPTION,
114
+ features=features,
115
+ homepage=_HOMEPAGE,
116
+ license=_LICENSE,
117
+ citation=_CITATION,
118
+ )
119
+
120
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
121
+ split_config_name = self.config.name.split("_")
122
+ src = split_config_name[3]
123
+ data_file = Path(dl_manager.download_and_extract(_URLS[_DATASETNAME][src]))
124
+
125
+ return [
126
+ datasets.SplitGenerator(
127
+ name=datasets.Split.TRAIN,
128
+ gen_kwargs={
129
+ "filepath": data_file,
130
+ "split": "train",
131
+ },
132
+ )
133
+ ]
134
+
135
+ def _generate_examples(self, filepath: Path, split: str) -> Tuple[int, Dict]:
136
+ """Yields examples as (key, example) tuples."""
137
+ split_config_name = self.config.name.split("_")
138
+ src = split_config_name[3]
139
+ df = pd.read_csv(filepath, encoding="utf8").reset_index()
140
+
141
+ for id, row in df.iterrows():
142
+ src_txt = row[1]
143
+ tgt_txt = row[2]
144
+ if self.config.schema == "source":
145
+ yield id, {"id": str(id), "text_1": src_txt, "text_2": tgt_txt}
146
+ elif self.config.schema == "nusantara_t2t":
147
+ yield id, {"id": str(id), "text_1": src_txt, "text_2": tgt_txt, "text_1_name": src, "text_2_name": "ind"}