Datasets:
cjvt
/

Modalities:
Text
Languages:
Slovenian
Libraries:
Datasets
License:
File size: 5,087 Bytes
a9293ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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[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