holylovenia commited on
Commit
3705e99
1 Parent(s): ea6dffc

Upload malindo_parallel.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. malindo_parallel.py +191 -0
malindo_parallel.py ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """
17
+ This template serves as a starting point for contributing a dataset to the SEACrowd Datahub repo.
18
+
19
+
20
+ Full documentation on writing dataset loading scripts can be found here:
21
+ https://huggingface.co/docs/datasets/add_dataset.html
22
+
23
+ To create a dataset loading script you will create a class and implement 3 methods:
24
+ * `_info`: Establishes the schema for the dataset, and returns a datasets.DatasetInfo object.
25
+ * `_split_generators`: Downloads and extracts data for each split (e.g. train/val/test) or associate local data with each split.
26
+ * `_generate_examples`: Creates examples from data on disk that conform to each schema defined in `_info`.
27
+
28
+ """
29
+ import json
30
+ import os
31
+ from pathlib import Path
32
+ from typing import Dict, List, Tuple
33
+
34
+ import datasets
35
+
36
+ from seacrowd.utils import schemas
37
+ from seacrowd.utils.configs import SEACrowdConfig
38
+ from seacrowd.utils.constants import (DEFAULT_SEACROWD_VIEW_NAME,
39
+ DEFAULT_SOURCE_VIEW_NAME, Tasks)
40
+
41
+ _CITATION = """\
42
+ @misc{MALINDO-parallel,
43
+ title = "MALINDO-parallel",
44
+ howpublished = "https://github.com/matbahasa/MALINDO_Parallel/blob/master/README.md",
45
+ note = "Accessed: 2023-01-27",
46
+ }
47
+ """
48
+
49
+ _DATASETNAME = "malindo_parallel"
50
+
51
+
52
+ _DESCRIPTION = """\
53
+ Teks ini adalah skrip video untuk Kampus Terbuka Universiti Bahasa Asing Tokyo pada tahun 2020. Tersedia parallel sentences dalam Bahasa Melayu/Indonesia dan Bahasa Jepang
54
+ """
55
+
56
+
57
+ _HOMEPAGE = "https://github.com/matbahasa/MALINDO_Parallel/tree/master/OpenCampusTUFS"
58
+
59
+
60
+ _LANGUAGES = ["zlm", "jpn"] # We follow ISO639-3 language code (https://iso639-3.sil.org/code_tables/639/data)
61
+
62
+
63
+ _LICENSE = "Creative Commons Attribution 4.0 (cc-by-4.0)"
64
+
65
+
66
+ _LOCAL = False
67
+
68
+
69
+ _URLS = {
70
+ _DATASETNAME: "https://raw.githubusercontent.com/matbahasa/MALINDO_Parallel/master/OpenCampusTUFS/OCTUFS2020.txt",
71
+ }
72
+
73
+
74
+ _SUPPORTED_TASKS = [Tasks.MACHINE_TRANSLATION] # example: [Tasks.TRANSLATION, Tasks.NAMED_ENTITY_RECOGNITION, Tasks.RELATION_EXTRACTION]
75
+
76
+
77
+ _SOURCE_VERSION = "1.0.0"
78
+
79
+ _SEACROWD_VERSION = "2024.06.20"
80
+
81
+
82
+
83
+ class MalindoParallelDataset(datasets.GeneratorBasedBuilder):
84
+ """Data terjemahan bahasa Melayu/Indonesia"""
85
+
86
+ SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
87
+ SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
88
+
89
+
90
+ BUILDER_CONFIGS = [
91
+ SEACrowdConfig(
92
+ name="malindo_parallel_source",
93
+ version=SOURCE_VERSION,
94
+ description="malindo_parallel source schema",
95
+ schema="source",
96
+ subset_id="malindo_parallel",
97
+ ),
98
+ SEACrowdConfig(
99
+ name="malindo_parallel_seacrowd_t2t",
100
+ version=SEACROWD_VERSION,
101
+ description="malindo_parallel SEACrowd schema",
102
+ schema="seacrowd_t2t",
103
+ subset_id="malindo_parallel",
104
+ ),
105
+ ]
106
+
107
+ DEFAULT_CONFIG_NAME = "malindo_parallel_source"
108
+
109
+ def _info(self) -> datasets.DatasetInfo:
110
+
111
+ if self.config.schema == "source":
112
+ features = datasets.Features({"id": datasets.Value("string"), "text": datasets.Value("string")})
113
+
114
+ elif self.config.schema == "seacrowd_t2t":
115
+ features = schemas.text2text_features
116
+
117
+ return datasets.DatasetInfo(
118
+ description=_DESCRIPTION,
119
+ features=features,
120
+ homepage=_HOMEPAGE,
121
+ license=_LICENSE,
122
+ citation=_CITATION,
123
+ )
124
+
125
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
126
+ """Returns SplitGenerators."""
127
+
128
+ urls = _URLS[_DATASETNAME]
129
+ data_dir = dl_manager.download_and_extract(urls)
130
+
131
+ return [
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TRAIN,
134
+
135
+ gen_kwargs={
136
+ "filepath": data_dir,
137
+ "split": "train",
138
+ },
139
+ ),
140
+ ]
141
+
142
+ def _generate_examples(self, filepath: Path, split: str) -> Tuple[int, Dict]:
143
+
144
+ rows = []
145
+ temp_cols = None
146
+ with open(filepath) as file:
147
+ while line := file.readline():
148
+ if temp_cols is None:
149
+ cols = []
150
+ for col in line.split('\t'):
151
+ if len(col.strip('\n'))>0:
152
+ cols.append(col)
153
+ if len(cols) > 2:
154
+ correct_line = line.rstrip()
155
+ rows.append(correct_line)
156
+ else:
157
+ temp_cols = cols
158
+ else:
159
+ temp_cols.append(line)
160
+ correct_line = "\t".join(temp_cols).rstrip()
161
+ temp_cols = None
162
+ rows.append(correct_line)
163
+
164
+ if self.config.schema == "source":
165
+
166
+ for i, row in enumerate(rows):
167
+ t1idx = row.find("\t") + 1
168
+ t2idx = row[t1idx:].find("\t")
169
+ row_id = row[:t1idx]
170
+ row_melayu = row[t1idx : t1idx + t2idx]
171
+ row_japanese = row[t1idx + t2idx + 1 : -1]
172
+ ex = {"id": row_id.rstrip(),
173
+ "text": row_melayu + "\t" + row_japanese}
174
+ yield i, ex
175
+
176
+ elif self.config.schema == "seacrowd_t2t":
177
+
178
+ for i, row in enumerate(rows):
179
+ t1idx = row.find("\t") + 1
180
+ t2idx = row[t1idx:].find("\t")
181
+ row_id = row[:t1idx]
182
+ row_melayu = row[t1idx : t1idx + t2idx]
183
+ row_japanese = row[t1idx + t2idx + 1 : -1]
184
+ ex = {
185
+ "id": row_id.rstrip(),
186
+ "text_1": row_melayu,
187
+ "text_2": row_japanese,
188
+ "text_1_name": "zlm",
189
+ "text_2_name": "jpn",
190
+ }
191
+ yield i, ex