atenglens commited on
Commit
0dca074
1 Parent(s): 1838718

Create taiwanese_english_translation.py

Browse files
Files changed (1) hide show
  1. taiwanese_english_translation.py +182 -0
taiwanese_english_translation.py ADDED
@@ -0,0 +1,182 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ # TODO: Address all TODOs and remove all explanatory comments
15
+ """TODO: Add a description here."""
16
+
17
+
18
+ import csv
19
+ import json
20
+ import os
21
+
22
+ import datasets
23
+
24
+
25
+ # TODO: Add BibTeX citation
26
+ # Find for instance the citation on arxiv or on the dataset repo/website
27
+ _CITATION = """\
28
+ @InProceedings{huggingface:dataset,
29
+ title = {A great new dataset},
30
+ author={huggingface, Inc.
31
+ },
32
+ year={2020}
33
+ }
34
+ """
35
+
36
+ # TODO: Add description of the dataset here
37
+ # You can copy an official description
38
+ _DESCRIPTION = """\
39
+ This new dataset is designed to solve this great NLP task and is crafted with a lot of care.
40
+ """
41
+
42
+ # TODO: Add a link to an official homepage for the dataset here
43
+ _HOMEPAGE = "https://github.com/atenglens/taiwanese_english_translation"
44
+
45
+ # TODO: Add the licence for the dataset here if you can find it
46
+ _LICENSE = ""
47
+
48
+ # TODO: Add link to the official dataset URLs here
49
+ # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
50
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
51
+ _URLS = "https://huggingface.co/datasets/atenglens/taiwanese-english-translation"
52
+
53
+ class TaiwaneseEnglishTranslationConfig(datasets.BuilderConfig):
54
+ """BuilderConfig for FLoRes."""
55
+
56
+ def __init__(self, language_pair=(None, None), **kwargs):
57
+ """BuilderConfig for TaiwaneseEnglishTranslation.
58
+ Args:
59
+ for the `datasets.features.text.TextEncoder` used for the features feature.
60
+ language_pair: pair of languages that will be used for translation. Should
61
+ contain 2-letter coded strings. First will be used at source and second
62
+ as target in supervised mode. For example: ("se", "en").
63
+ **kwargs: keyword arguments forwarded to super.
64
+ """
65
+ name = "%s%s" % (language_pair[0], language_pair[1])
66
+
67
+ description = ("Translation dataset from %s to %s") % (language_pair[0], language_pair[1])
68
+ super(TaiwaneseEnglishTranslationConfig, self).__init__(
69
+ name=name,
70
+ description=description,
71
+ version=datasets.Version("1.1.0", ""),
72
+ **kwargs,
73
+ )
74
+
75
+ self.language_pair = language_pair
76
+
77
+ # TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
78
+ class TaiwaneseEnglishTranslation(datasets.GeneratorBasedBuilder):
79
+ """TODO: Short description of my dataset."""
80
+
81
+ VERSION = datasets.Version("1.1.0")
82
+
83
+ # This is an example of a dataset with multiple configurations.
84
+ # If you don't want/need to define several sub-sets in your dataset,
85
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
86
+
87
+ # If you need to make complex sub-parts in the datasets with configurable options
88
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
89
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
90
+
91
+ # You will be able to load one or the other configurations in the following list with
92
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
93
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
94
+ BUILDER_CONFIGS = [
95
+ TaiwaneseEnglishTranslationConfig(
96
+ language_pair=("tw", "en"),
97
+ ),
98
+ ]
99
+
100
+ # DEFAULT_CONFIG_NAME = "first_domain" # It's not mandatory to have a default configuration. Just use one if it make sense.
101
+
102
+ def _info(self):
103
+ # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
104
+ source, target = self.config.language_pair
105
+ features = datasets.Features(
106
+ {"translation": datasets.features.Translation(languages=self.config.language_pair)}
107
+ )
108
+ return datasets.DatasetInfo(
109
+ # This is the description that will appear on the datasets page.
110
+ description=_DESCRIPTION,
111
+ # This defines the different columns of the dataset and their types
112
+ features=features, # Here we define them above because they are different between the two configurations
113
+ # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
114
+ # specify them. They'll be used if as_supervised=True in builder.as_dataset.
115
+ supervised_keys=(source, target),
116
+ # Homepage of the dataset for documentation
117
+ homepage=_HOMEPAGE,
118
+ # License for the dataset if available
119
+ license=_LICENSE,
120
+ # Citation for the dataset
121
+ citation=_CITATION,
122
+ )
123
+
124
+ def _split_generators(self, dl_manager):
125
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
126
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
127
+
128
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
129
+ # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
130
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
131
+ urls = _URLS
132
+ data_dir = dl_manager.download_and_extract(urls)
133
+ return [
134
+ datasets.SplitGenerator(
135
+ name=datasets.Split.TRAIN,
136
+ # These kwargs will be passed to _generate_examples
137
+ gen_kwargs={
138
+ "filepath": os.path.join(data_dir, "train.csv"),
139
+ "split": "train",
140
+ },
141
+ ),
142
+ datasets.SplitGenerator(
143
+ name=datasets.Split.TEST,
144
+ # These kwargs will be passed to _generate_examples
145
+ gen_kwargs={
146
+ "filepath": os.path.join(data_dir, "test.csv"),
147
+ "split": "test"
148
+ },
149
+ ),
150
+ datasets.SplitGenerator(
151
+ name=datasets.Split.VALIDATION,
152
+ # These kwargs will be passed to _generate_examples
153
+ gen_kwargs={
154
+ "filepath": os.path.join(data_dir, "valid.csv"),
155
+ "split": "valid",
156
+ },
157
+ ),
158
+ ]
159
+
160
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
161
+ def _generate_examples(self, source_file, target_file):
162
+ # TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
163
+ # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
164
+ """This function returns the examples in the raw (text) form."""
165
+ with open(source_file, encoding="utf-8") as f:
166
+ source_sentences = f.read().split("\n")
167
+ with open(target_file, encoding="utf-8") as f:
168
+ target_sentences = f.read().split("\n")
169
+
170
+ assert len(target_sentences) == len(source_sentences), "Sizes do not match: %d vs %d for %s vs %s." % (
171
+ len(source_sentences),
172
+ len(target_sentences),
173
+ source_file,
174
+ target_file,
175
+ )
176
+
177
+ source, target = self.config.language_pair
178
+ for idx, (l1, l2) in enumerate(zip(source_sentences, target_sentences)):
179
+ result = {"translation": {source: l1, target: l2}}
180
+ # Make sure that both translations are non-empty.
181
+ if all(result.values()):
182
+ yield idx, result