File size: 7,901 Bytes
990acb6
5dc576d
 
 
 
 
 
990acb6
5dc576d
 
 
 
990acb6
5dc576d
 
990acb6
5dc576d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
990acb6
5dc576d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
990acb6
5dc576d
 
 
 
990acb6
5dc576d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a67f43
5dc576d
 
 
 
 
0a67f43
5dc576d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffcfb5b
5dc576d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
from typing import Dict, TypeVar, Callable, List, Hashable, Union, Optional, Tuple, Collection, Iterable
from xml.etree import ElementTree
import unicodedata
from pathlib import Path
import re

Paths = Tuple[str, ...]
Language = str # Literal['de', 'en']
Translation = Dict[
    Language, str
]
Sample = Dict[
    str, Translation # Literal['translation'], Translation
]
SampleBatch = Dict[
    str, List[Translation] # Literal['translation'], List[Translation]
]

_H1 = TypeVar('_H1', bound=Hashable)
_H2 = TypeVar('_H2', bound=Hashable)
_T1 = TypeVar('_T1')
_T2 = TypeVar('_T2')


def dict_filter_keys(
    input: Dict[_H1, _T1],
    keys: List[_H1]
) -> Dict[_H1, _T1]:
    return dict(
        (key, input[key])
        for key in keys
    )


def dict_flatten(
    input: Dict[_H1, Dict[_H2, _T2]]
) -> Dict[_H2, _T2]:
    return dict(
        items for values in input.values()
        for items in values.items()
    )


def dict_map(
    input: Dict[_H1, _T1],
    map_fn: Callable[[_H1, _T1], Tuple[_H2, _T2]]
) -> Dict[_H2, _T2]:
    return dict(
        map_fn(key, value)
        for key, value in input.items()
    )


def list_keyby(
    input: List[_T1],
    key_fn: Callable[[_T1], _H1]
) -> Dict[_H1, _T1]:
    return dict(
        (key_fn(value), value)
        for value in input
    )


def expand_path(
    path: Path
):
    if path.is_file():
        return [path]

    return list(
        path.iterdir()
    )


def lenif(
    input: Collection[_T1],
    predicate_fn: Callable[[_T1], bool]
):
    return sum(
        predicate_fn(value)
        for value in input
    )


def len_alpha(
    string: str
):
    return lenif(
        input=string,
        predicate_fn=lambda character: character.isalpha()
    )


unicode_control_characters = (
    r'\x00-\x1F\x7F-\x9F\xAD\u0600-\u0605\u061C\u06DD\u070F\u0890-\u0891'
    r'\u08E2\u180E\u200B-\u200F\u202A-\u202E\u2060-\u2064\u2066-\u206F\uFEFF\uFFF9-\uFFFB'
    r'\U000110BD\U000110CD\U00013430-\U0001343F\U0001BCA0-\U0001BCA3\U0001D173-\U0001D17A'
    r'\U000E0001\U000E0020-\U000E007F\uE000\uF8FF\U000F0000\U000FFFFD\U00100000\U0010FFFD'
)


def normalize(
    strip_whitespaces: bool,
    clean_control_characters: bool,
    enforce_unicode_form: Optional[str] = None # Optional[Literal['NFC', 'NFKC', 'NFD', 'NFKD']] = None
):
    regex_pattern = re.compile(
        pattern='[' + unicode_control_characters + ']+'
    )

    def normalize_fn(
        sample: Sample
    ):
        translation = sample['translation']

        if strip_whitespaces:
            translation = dict_map(
                input=translation,
                map_fn=lambda key, value: (
                    key, value.strip()
                )
            )

        if clean_control_characters:
            translation = dict_map(
                input=translation, map_fn=lambda key, value: (
                    key, regex_pattern.sub(
                        repl='', string=value
                    )
                )
            )

        if enforce_unicode_form is not None:
            translation = dict_map(
                input=translation, map_fn=lambda key, value: (
                    key, unicodedata.normalize(
                        enforce_unicode_form, value # pyright: ignore
                    )
                )
            )

        sample['translation'] = translation # pyright: ignore
        return sample

    return normalize_fn


def cleanup(
    length_min: int,
    length_max: int,
    length_ratio_max: Union[int, float],
    alpha_ratio_min: Union[int, float]
):
    def cleanup_fn(
        sample: Sample
    ):
        translation = sample['translation']

        lenghts = list(
            len(value) for value in translation.values()
        )
        alpha_lengths = list(
            len_alpha(value) for value in translation.values()
        )

        return all(
            length_min < length < length_max and alpha_ratio_min < alpha_length / length
            for length, alpha_length in zip(lenghts, alpha_lengths)
        ) and 1 / length_ratio_max < lenghts[0] / lenghts[1] < length_ratio_max

    return cleanup_fn


class NoResultFound(Exception):
    pass


class MultipleResultsFound(Exception):
    pass


def one(
    iterable: Iterable[_T1]
) -> _T1:
    iterator = iter(iterable)

    try:
        value = next(iterator)
    except StopIteration as e:
        raise NoResultFound from e

    try:
        next(iterator)
    except StopIteration:
        pass
    else:
        raise MultipleResultsFound

    return value


def match_one(
    pattern: Union[str, re.Pattern], # pyright: ignore # Union[str, re.Pattern[str]],
    string: str,
    flags: int = 0
):
    return one(
        iterable=re.finditer(
            pattern=pattern, # pyright: ignore
            string=string,
            flags=flags
        )
    )


def parse_sgm(
    filepaths: Paths,
    files: Dict[Language, int],
    encoding: str = 'utf-8'
):
    assert len(filepaths) == 2

    def read_lines_regex(
        filepath: str,
        pattern: re.Pattern # pyright: ignore # re.Pattern[str]
    ):
        with open(
            file=filepath,
            encoding=encoding,
            mode='r'
        ) as file:
            for string in file:
                try:
                    match = match_one(
                        pattern=pattern,
                        string=string
                    )
                    groups = match.groups(
                        default=''
                    )
                    yield groups[0]
                except:
                    yield ''

    regex = re.compile(
        pattern=r'<seg id="\d+">(.*)</seg>'
    )

    for lines in zip(
        read_lines_regex(
            filepath=filepaths[0],
            pattern=regex
        ),
        read_lines_regex(
            filepath=filepaths[1],
            pattern=regex
        )
    ):
        translation: Translation
        translation = dict(
            (language, lines[index])
            for language, index in files.items()
        )

        sample: Sample = dict()
        sample['translation'] = translation
        yield sample


def parse_tsv(
    filepaths: Paths,
    columns: Dict[Language, int],
    encoding: str = 'utf-8'
):
    assert len(filepaths) == 1

    len_columns = len(columns)

    with open(
        file=filepaths[0],
        encoding=encoding,
        mode='r'
    ) as file:
        for line in file:
            parts = line.split('\t')

            if len(parts) < len_columns:
                continue

            translation: Translation = dict()
            for language, index in columns.items():
                translation[language] = parts[index]

            sample: Sample = dict()
            sample['translation'] = translation
            yield sample


def parse_tmx(
    filepaths: Paths,
    attributes: Dict[Language, str],
    encoding: str = 'utf-8',
):
    assert len(filepaths) == 1

    element: ElementTree.Element
    namespaces = {
        'xml': 'http://www.w3.org/XML/1998/namespace'
    }

    with open(
        file=filepaths[0],
        encoding=encoding,
        mode='r'
    ) as file:
        for _, element in ElementTree.iterparse(file):
            if not element.tag == 'tu':
                continue

            translation: Translation = dict()
            for language, selector in attributes.items():
                path = 'tuv[@' + selector + ']'

                segs = element.findall(
                    path=path + '/seg', namespaces=namespaces
                )
                if not len(segs) == 1:
                    continue

                translation[language] = segs[0].text or ''

            element.clear()

            if not len(translation) == 2:
                continue

            sample: Sample = dict()
            sample['translation'] = translation
            yield sample