janes_preklop / janes_preklop.py
Matej Klemen
Fix issue with some default language tags being incorrectly formatted using IOB2
0389c7f
import os
import re
import xml.etree.ElementTree as ET
import datasets
XML_NAMESPACE = "{http://www.w3.org/XML/1998/namespace}"
DEFAULT_LANG = "default"
_CITATION = """\
@misc{janes_preklop,
title = {Tweet code-switching corpus Janes-Preklop 1.0},
author = {Reher, {\v S}pela and Erjavec, Toma{\v z} and Fi{\v s}er, Darja},
url = {http://hdl.handle.net/11356/1154},
note = {Slovenian language resource repository {CLARIN}.{SI}},
copyright = {Creative Commons - Attribution-{ShareAlike} 4.0 International ({CC} {BY}-{SA} 4.0)},
issn = {2820-4042},
year = {2017}
}
"""
_DESCRIPTION = """\
Janes-Preklop is a corpus of Slovene tweets that is manually annotated for code-switching (the use of words from two
or more languages within one sentence or utterance), according to the supplied typology.
"""
_HOMEPAGE = "http://nl.ijs.si/janes/"
_LICENSE = "Creative Commons - Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)"
_URLS = {
"janes_preklop_tei": "https://www.clarin.si/repository/xmlui/bitstream/handle/11356/1154/Janes-Preklop.TEI.zip"
}
def namespace(element):
# https://stackoverflow.com/a/12946675
m = re.match(r'\{.*\}', element.tag)
return m.group(0) if m else ''
def word_info(wordlike_tag, _namespace):
if wordlike_tag.tag == f"{_namespace}c":
return None, None
elif wordlike_tag.tag in {f"{_namespace}w", f"{_namespace}pc"}:
_word = wordlike_tag.text.strip()
return [_word], None
words, lang = [], []
if wordlike_tag.tag == f"{_namespace}choice":
# Ignore <orig>, keep <reg>ularized text
reg_tag = wordlike_tag.find(f"{_namespace}reg")
for _child in reg_tag:
_child_words, _child_langs = word_info(_child, _namespace)
if _child_words is None:
continue
words.extend(_child_words)
if _child_langs is None:
_child_langs = [DEFAULT_LANG for _ in range(len(_child_words))]
lang.extend(_child_langs)
return words, lang
elif wordlike_tag.tag == f"{_namespace}seg":
codeswitch_lang = wordlike_tag.attrib.get(f"{XML_NAMESPACE}lang", DEFAULT_LANG)
_seg_words, _seg_langs = [], []
for _child in wordlike_tag:
_child_words, _child_langs = word_info(_child, _namespace)
if _child_words is None:
continue
_seg_words.extend(_child_words)
if _child_langs is None:
_child_langs = [codeswitch_lang for _ in range(len(_child_words))]
_seg_langs.extend(_child_langs)
if codeswitch_lang != DEFAULT_LANG:
_seg_langs = [codeswitch_lang for _ in _seg_langs]
if _seg_langs[0] != DEFAULT_LANG:
_seg_langs[0] = f"B-{_seg_langs[0]}"
for _i in range(1, len(_seg_words)):
_seg_langs[_i] = f"I-{_seg_langs[_i]}"
words.extend(_seg_words)
lang.extend(_seg_langs)
return words, lang
class JanesPreklop(datasets.GeneratorBasedBuilder):
"""Code-switched Slovene tweet dataset. """
VERSION = datasets.Version("1.0.0")
def _info(self):
features = datasets.Features(
{
"id": datasets.Value("string"),
"words": datasets.Sequence(datasets.Value("string")),
"language": datasets.Sequence(datasets.Value("string"))
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
urls = _URLS["janes_preklop_tei"]
data_dir = dl_manager.download_and_extract(urls)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={"file_path": os.path.join(data_dir, "Janes-Preklop.TEI", "janes.preklop.body.xml")},
)
]
def _generate_examples(self, file_path):
curr_doc = ET.parse(file_path)
root = curr_doc.getroot()
NAMESPACE = namespace(root)
idx_ex = 0
for curr_ex in root.iterfind(f"{NAMESPACE}ab"):
ex_words, ex_langs = [], []
for child_tag in curr_ex:
if child_tag.tag not in {f"{NAMESPACE}s", f"{NAMESPACE}c"}:
continue
if child_tag.tag == f"{NAMESPACE}c":
continue
# Iterate over elements of a <s>entence
for word_or_seg_tag in child_tag:
_words, _langs = word_info(word_or_seg_tag, NAMESPACE)
if _words is None:
continue
if _langs is None:
_langs = [DEFAULT_LANG for _ in range(len(_words))]
ex_words.extend(_words)
ex_langs.extend(_langs)
yield idx_ex, {
"id": curr_ex.attrib[f"{XML_NAMESPACE}id"],
"words": ex_words,
"language": ex_langs
}
idx_ex += 1