rsdo4_en_sl / rsdo4_en_sl.py
Matej Klemen
Add first version of RSDO4 en-sl corpus
8adda2c
raw
history blame contribute delete
No virus
3.08 kB
""" rsdo4_en_sl is a parallel corpus of English-Slovene and Slovene-English translation pairs."""
import xml.etree.ElementTree as ET
import os
import datasets
_CITATION = """\
@misc{rsdo4_en_sl,
title = {Parallel corpus {EN}-{SL} {RSDO4} 1.0},
author = {Repar, Andra{\v z} and Lebar Bajec, Iztok},
url = {http://hdl.handle.net/11356/1457},
year = {2021}
}
"""
_DESCRIPTION = """\
The RSDO4 parallel corpus of English-Slovene and Slovene-English translation pairs was collected as part of work
package 4 of the Slovene in the Digital Environment project. It contains texts collected from public institutions
and texts submitted by individual donors through the text collection portal created within the project. The corpus
consists of 964433 translation pairs (extracted from standard translation formats (TMX, XLIFF) or manually aligned)
in randomized order which can be used for machine translation training.
"""
_HOMEPAGE = "http://hdl.handle.net/11356/1457"
_LICENSE = "Creative Commons - Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)"
_URLS = {
"rsdo4_en_sl": "https://www.clarin.si/repository/xmlui/bitstream/handle/11356/1457/rsdo4-parallel-corpus-1-0.tmx.zip"
}
XML_NAMESPACE = "{http://www.w3.org/XML/1998/namespace}"
class RSDO4EnSl(datasets.GeneratorBasedBuilder):
"""rsdo4_en_sl is a parallel corpus of English-Slovene and Slovene-English translation pairs."""
VERSION = datasets.Version("1.0.0")
def _info(self):
features = datasets.Features(
{
"en_seq": datasets.Value("string"),
"sl_seq": datasets.Value("string"),
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
urls = _URLS["rsdo4_en_sl"]
data_dir = dl_manager.download_and_extract(urls)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"file_path": os.path.join(data_dir, "rsdo4-parallel-corpus-1-0.tmx")
}
)
]
def _generate_examples(self, file_path):
doc = ET.parse(file_path)
root = doc.getroot()
for i, curr_unit in enumerate(root.iterfind(".//tu")):
translation_pair = curr_unit.findall("tuv")
formatted_pair = {}
for curr_seq in translation_pair:
# Not sure whether the languages are erroneously swapped or whether xml:lang represents the "language direction"
seq_lang = "sl" if curr_seq.attrib[f"{XML_NAMESPACE}lang"] == "en" else "en"
curr_text = curr_seq.find("seg").text
curr_text = "" if curr_text is None else curr_text.strip()
formatted_pair[f"{seq_lang}_seq"] = curr_text
yield i, formatted_pair