Sebastian Gehrmann
commited on
Commit
•
2b5d807
1
Parent(s):
f549294
change feature names
Browse files- opusparcus.py +36 -14
opusparcus.py
CHANGED
@@ -19,6 +19,7 @@
|
|
19 |
import csv
|
20 |
import json
|
21 |
import os
|
|
|
22 |
import datasets
|
23 |
import bz2
|
24 |
|
@@ -70,6 +71,24 @@ _URLs = {
|
|
70 |
|
71 |
_VERSION = datasets.Version("1.0.0", "")
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
class OpusparcusConfig(datasets.BuilderConfig):
|
74 |
"""BuilderConfig for Opusparcus."""
|
75 |
|
@@ -109,7 +128,7 @@ LANGS = [ "de", "en", "fi", "fr", "ru", "sv" ]
|
|
109 |
# been annotated manually, and each example has an annotation score
|
110 |
# attached to it.)
|
111 |
QUALITIES = [ 100, 95, 90, 85, 80, 75, 70, 65, 60 ]
|
112 |
-
|
113 |
class Opusparcus(datasets.GeneratorBasedBuilder):
|
114 |
|
115 |
"""Opusparcus is a paraphrase corpus for six European languages:
|
@@ -146,12 +165,12 @@ class Opusparcus(datasets.GeneratorBasedBuilder):
|
|
146 |
# The above commands can alternatively be expressed as:
|
147 |
# data = datasets.load_dataset('GEM/opusparcus', 'de.100')
|
148 |
# data = datasets.load_dataset('GEM/opusparcus', 'fr.75')
|
149 |
-
|
150 |
BUILDER_CONFIGS = [
|
151 |
OpusparcusConfig(lang=lang, quality=quality, version=_VERSION) \
|
152 |
for lang in LANGS for quality in QUALITIES
|
153 |
]
|
154 |
-
|
155 |
# There is no default configuration. User always needs to specify one:
|
156 |
# DEFAULT_CONFIG_NAME = None
|
157 |
|
@@ -161,10 +180,11 @@ class Opusparcus(datasets.GeneratorBasedBuilder):
|
|
161 |
features = datasets.Features(
|
162 |
{
|
163 |
"lang": datasets.Value("string"),
|
164 |
-
"
|
165 |
-
"
|
166 |
"annot_score": datasets.Value("float"),
|
167 |
"gem_id": datasets.Value("string"),
|
|
|
168 |
}
|
169 |
)
|
170 |
|
@@ -200,7 +220,7 @@ class Opusparcus(datasets.GeneratorBasedBuilder):
|
|
200 |
# This is an error: nothing to do here if no language
|
201 |
# has been defined:
|
202 |
return []
|
203 |
-
|
204 |
# Select which file of the training data contains the matching data:
|
205 |
if self.config.quality < 70:
|
206 |
# We need to retrieve the largest training set file
|
@@ -216,7 +236,7 @@ class Opusparcus(datasets.GeneratorBasedBuilder):
|
|
216 |
# download any training data, because there is no matching data.
|
217 |
# The validation and test sets are so small that we do not perform
|
218 |
# any filtering or optimization at this stage.
|
219 |
-
|
220 |
# dl_manager is a datasets.download.DownloadManager, which
|
221 |
# downloads and extracts the URLs
|
222 |
# (It can accept any type or nested list/dict and will give
|
@@ -267,12 +287,12 @@ class Opusparcus(datasets.GeneratorBasedBuilder):
|
|
267 |
"split": "validation.full",
|
268 |
},
|
269 |
),
|
270 |
-
]
|
271 |
|
272 |
# If the desired quality value is 100, no subset of the
|
273 |
# training set is good enough, and we only produce validation
|
274 |
# and test sets, in order to save space and time:
|
275 |
-
|
276 |
if self.config.quality <= 95:
|
277 |
# In this case there is matching training data, so we produce
|
278 |
# a train split.
|
@@ -306,7 +326,7 @@ class Opusparcus(datasets.GeneratorBasedBuilder):
|
|
306 |
# They contain a field "quality" missing from the validation and test sets.
|
307 |
# We also know that this file only contains the desired language,
|
308 |
# because for the training sets the languages are in separate
|
309 |
-
# files, and only the desired language has been downloaded.
|
310 |
with bz2.open(filepath, "rt", encoding="utf-8") as f:
|
311 |
for id_, row in enumerate(f):
|
312 |
data = json.loads(row)
|
@@ -316,10 +336,11 @@ class Opusparcus(datasets.GeneratorBasedBuilder):
|
|
316 |
break
|
317 |
yield id_, {
|
318 |
"lang": data["lang"],
|
319 |
-
"
|
320 |
-
"
|
321 |
"annot_score": 0.0, # means there is no annotation
|
322 |
"gem_id": data["gem_id"],
|
|
|
323 |
}
|
324 |
else:
|
325 |
# The validation and test sets are in jsonl files.
|
@@ -340,9 +361,10 @@ class Opusparcus(datasets.GeneratorBasedBuilder):
|
|
340 |
# "good or mostly good example of paraphrases")
|
341 |
yield id_, {
|
342 |
"lang": data["lang"],
|
343 |
-
"
|
344 |
-
"
|
345 |
"annot_score": data["annot_score"],
|
346 |
"gem_id": data["gem_id"],
|
|
|
347 |
}
|
348 |
|
|
|
19 |
import csv
|
20 |
import json
|
21 |
import os
|
22 |
+
import re
|
23 |
import datasets
|
24 |
import bz2
|
25 |
|
|
|
71 |
|
72 |
_VERSION = datasets.Version("1.0.0", "")
|
73 |
|
74 |
+
|
75 |
+
def detokenize(text):
|
76 |
+
"""
|
77 |
+
Untokenizing a text undoes the tokenizing operation, restoring
|
78 |
+
punctuation and spaces to the places that people expect them to be.
|
79 |
+
Ideally, `untokenize(tokenize(text))` should be identical to `text`,
|
80 |
+
except for line breaks.
|
81 |
+
"""
|
82 |
+
step1 = text.replace("`` ", '"').replace(" ''", '"').replace('. . .', '...')
|
83 |
+
step2 = step1.replace(" ( ", " (").replace(" ) ", ") ")
|
84 |
+
step3 = re.sub(r' ([.,:;?!%]+)([ \'"`])', r"\1\2", step2)
|
85 |
+
step4 = re.sub(r' ([.,:;?!%]+)$', r"\1", step3)
|
86 |
+
step5 = step4.replace(" '", "'").replace(" n't", "n't").replace(
|
87 |
+
"can not", "cannot").replace(" 've", "'ve")
|
88 |
+
step6 = step5.replace(" ` ", " '")
|
89 |
+
return step6.strip()
|
90 |
+
|
91 |
+
|
92 |
class OpusparcusConfig(datasets.BuilderConfig):
|
93 |
"""BuilderConfig for Opusparcus."""
|
94 |
|
|
|
128 |
# been annotated manually, and each example has an annotation score
|
129 |
# attached to it.)
|
130 |
QUALITIES = [ 100, 95, 90, 85, 80, 75, 70, 65, 60 ]
|
131 |
+
|
132 |
class Opusparcus(datasets.GeneratorBasedBuilder):
|
133 |
|
134 |
"""Opusparcus is a paraphrase corpus for six European languages:
|
|
|
165 |
# The above commands can alternatively be expressed as:
|
166 |
# data = datasets.load_dataset('GEM/opusparcus', 'de.100')
|
167 |
# data = datasets.load_dataset('GEM/opusparcus', 'fr.75')
|
168 |
+
|
169 |
BUILDER_CONFIGS = [
|
170 |
OpusparcusConfig(lang=lang, quality=quality, version=_VERSION) \
|
171 |
for lang in LANGS for quality in QUALITIES
|
172 |
]
|
173 |
+
|
174 |
# There is no default configuration. User always needs to specify one:
|
175 |
# DEFAULT_CONFIG_NAME = None
|
176 |
|
|
|
180 |
features = datasets.Features(
|
181 |
{
|
182 |
"lang": datasets.Value("string"),
|
183 |
+
"input": datasets.Value("string"),
|
184 |
+
"target": datasets.Value("string"),
|
185 |
"annot_score": datasets.Value("float"),
|
186 |
"gem_id": datasets.Value("string"),
|
187 |
+
"references": [datasets.Value("string")]
|
188 |
}
|
189 |
)
|
190 |
|
|
|
220 |
# This is an error: nothing to do here if no language
|
221 |
# has been defined:
|
222 |
return []
|
223 |
+
|
224 |
# Select which file of the training data contains the matching data:
|
225 |
if self.config.quality < 70:
|
226 |
# We need to retrieve the largest training set file
|
|
|
236 |
# download any training data, because there is no matching data.
|
237 |
# The validation and test sets are so small that we do not perform
|
238 |
# any filtering or optimization at this stage.
|
239 |
+
|
240 |
# dl_manager is a datasets.download.DownloadManager, which
|
241 |
# downloads and extracts the URLs
|
242 |
# (It can accept any type or nested list/dict and will give
|
|
|
287 |
"split": "validation.full",
|
288 |
},
|
289 |
),
|
290 |
+
]
|
291 |
|
292 |
# If the desired quality value is 100, no subset of the
|
293 |
# training set is good enough, and we only produce validation
|
294 |
# and test sets, in order to save space and time:
|
295 |
+
|
296 |
if self.config.quality <= 95:
|
297 |
# In this case there is matching training data, so we produce
|
298 |
# a train split.
|
|
|
326 |
# They contain a field "quality" missing from the validation and test sets.
|
327 |
# We also know that this file only contains the desired language,
|
328 |
# because for the training sets the languages are in separate
|
329 |
+
# files, and only the desired language has been downloaded.
|
330 |
with bz2.open(filepath, "rt", encoding="utf-8") as f:
|
331 |
for id_, row in enumerate(f):
|
332 |
data = json.loads(row)
|
|
|
336 |
break
|
337 |
yield id_, {
|
338 |
"lang": data["lang"],
|
339 |
+
"input": detokenize(data["sent1"]),
|
340 |
+
"target": detokenize(data["sent2"]),
|
341 |
"annot_score": 0.0, # means there is no annotation
|
342 |
"gem_id": data["gem_id"],
|
343 |
+
"references": [detokenize(data["sent2"])]
|
344 |
}
|
345 |
else:
|
346 |
# The validation and test sets are in jsonl files.
|
|
|
361 |
# "good or mostly good example of paraphrases")
|
362 |
yield id_, {
|
363 |
"lang": data["lang"],
|
364 |
+
"input": detokenize(data["sent1"]),
|
365 |
+
"target": detokenize(data["sent2"]),
|
366 |
"annot_score": data["annot_score"],
|
367 |
"gem_id": data["gem_id"],
|
368 |
+
"references": [detokenize(data["sent2"])]
|
369 |
}
|
370 |
|