Create kftt.py
Browse files
kftt.py
ADDED
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# KFTT Dataset
|
3 |
+
|
4 |
+
# Lint as: python3
|
5 |
+
"""The Kyoto Free Translation Task (KFTT) Dataset for Japanese-English machine translation."""
|
6 |
+
|
7 |
+
|
8 |
+
import collections
|
9 |
+
|
10 |
+
import datasets
|
11 |
+
|
12 |
+
|
13 |
+
_DESCRIPTION = """\
|
14 |
+
The Kyoto Free Translation Task is a task for Japanese-English translation that focuses
|
15 |
+
on Wikipedia articles related to Kyoto. The data used was originally prepared by the
|
16 |
+
National Institute for Information and Communication Technology (NICT) and released as
|
17 |
+
the Japanese-English Bilingual Corpus of Wikipedia's Kyoto Articles (we are simply using
|
18 |
+
the data, NICT does not specifically endorse or sponsor this task).
|
19 |
+
"""
|
20 |
+
|
21 |
+
_CITATION = """\
|
22 |
+
@misc{neubig11kftt,
|
23 |
+
author = {Graham Neubig},
|
24 |
+
title = {The {Kyoto} Free Translation Task},
|
25 |
+
howpublished = {http://www.phontron.com/kftt},
|
26 |
+
year = {2011}
|
27 |
+
}
|
28 |
+
"""
|
29 |
+
|
30 |
+
_HOMEPAGE = "http://www.phontron.com/kftt/"
|
31 |
+
|
32 |
+
_LICENSE = "Creative Commons Attribution-Share-Alike License 3.0 (CC BY-SA 3.0)"
|
33 |
+
|
34 |
+
_DATA_URL = "http://www.phontron.com/kftt/download/kftt-data-1.0.tar.gz"
|
35 |
+
|
36 |
+
# Tuple that describes a single pair of files with matching translations.
|
37 |
+
# language_to_file is the map from language (2 letter string: example 'en')
|
38 |
+
# to the file path in the extracted directory.
|
39 |
+
TranslateData = collections.namedtuple("TranslateData", ["url", "language_to_file"])
|
40 |
+
|
41 |
+
|
42 |
+
class KFTTConfig(datasets.BuilderConfig):
|
43 |
+
"""BuilderConfig for KFTT."""
|
44 |
+
|
45 |
+
def __init__(self, language_pair=(None, None), **kwargs):
|
46 |
+
"""BuilderConfig for KFTT.
|
47 |
+
|
48 |
+
Args:
|
49 |
+
for the `datasets.features.text.TextEncoder` used for the features feature.
|
50 |
+
language_pair: pair of languages that will be used for translation. Should
|
51 |
+
contain 2-letter coded strings. First will be used at source and second
|
52 |
+
as target in supervised mode. For example: ("ja", "en").
|
53 |
+
**kwargs: keyword arguments forwarded to super.
|
54 |
+
"""
|
55 |
+
super(KFTTConfig, self).__init__(
|
56 |
+
name="%s-%s" % (language_pair[0], language_pair[1]),
|
57 |
+
description="English-Japanese translation dataset.",
|
58 |
+
version=datasets.Version("1.0.0", ""),
|
59 |
+
**kwargs,
|
60 |
+
)
|
61 |
+
|
62 |
+
# Validate language pair.
|
63 |
+
assert "en" in language_pair
|
64 |
+
assert "ja" in language_pair
|
65 |
+
|
66 |
+
self.language_pair = language_pair
|
67 |
+
|
68 |
+
|
69 |
+
class KFTT(datasets.GeneratorBasedBuilder):
|
70 |
+
"""KFTT machine translation dataset."""
|
71 |
+
|
72 |
+
BUILDER_CONFIGS = [
|
73 |
+
KFTTConfig(
|
74 |
+
language_pair=("en", "ja"),
|
75 |
+
),
|
76 |
+
]
|
77 |
+
|
78 |
+
def _info(self):
|
79 |
+
source, target = self.config.language_pair
|
80 |
+
return datasets.DatasetInfo(
|
81 |
+
description=_DESCRIPTION,
|
82 |
+
features=datasets.Features(
|
83 |
+
{"translation": datasets.features.Translation(languages=self.config.language_pair)}
|
84 |
+
),
|
85 |
+
supervised_keys=(source, target),
|
86 |
+
homepage=_HOMEPAGE,
|
87 |
+
citation=_CITATION,
|
88 |
+
license=_LICENSE,
|
89 |
+
)
|
90 |
+
|
91 |
+
def _split_generators(self, dl_manager):
|
92 |
+
archive = dl_manager.download(_DATA_URL)
|
93 |
+
|
94 |
+
source, target = self.config.language_pair
|
95 |
+
path_tmpl = "kftt-data-1.0/data/orig/kyoto-{split}.{lang}"
|
96 |
+
|
97 |
+
files = {}
|
98 |
+
for split in ("train", "dev", "test", "tune"):
|
99 |
+
files[split] = {
|
100 |
+
"source_file": path_tmpl.format(split=split, lang=source),
|
101 |
+
"target_file": path_tmpl.format(split=split, lang=target),
|
102 |
+
"files": dl_manager.iter_archive(archive),
|
103 |
+
}
|
104 |
+
|
105 |
+
return [
|
106 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs=files["train"]),
|
107 |
+
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs=files["dev"]),
|
108 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs=files["test"]),
|
109 |
+
datasets.SplitGenerator(name=datasets.Split("tune"), gen_kwargs=files["tune"]),
|
110 |
+
]
|
111 |
+
|
112 |
+
def _generate_examples(self, files, source_file, target_file):
|
113 |
+
"""This function returns the examples in the raw (text) form."""
|
114 |
+
source_sentences, target_sentences = None, None
|
115 |
+
for path, f in files:
|
116 |
+
if path == source_file:
|
117 |
+
source_sentences = f.read().decode("utf-8").split("\n")
|
118 |
+
elif path == target_file:
|
119 |
+
target_sentences = f.read().decode("utf-8").split("\n")
|
120 |
+
if source_sentences is not None and target_sentences is not None:
|
121 |
+
break
|
122 |
+
|
123 |
+
assert len(target_sentences) == len(source_sentences), "Sizes do not match: %d vs %d for %s vs %s." % (
|
124 |
+
len(source_sentences),
|
125 |
+
len(target_sentences),
|
126 |
+
source_file,
|
127 |
+
target_file,
|
128 |
+
)
|
129 |
+
|
130 |
+
source, target = self.config.language_pair
|
131 |
+
for idx, (l1, l2) in enumerate(zip(source_sentences, target_sentences)):
|
132 |
+
result = {"translation": {source: l1, target: l2}}
|
133 |
+
# Make sure that both translations are non-empty.
|
134 |
+
if all(result.values()):
|
135 |
+
yield idx, result
|