Skip to content

ccat

Classes and functions to convert giellatekno xml formatted files to text.

XMLPrinter

Convert giellatekno xml formatted files to plain text.

Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
class XMLPrinter:
    """Convert giellatekno xml formatted files to plain text."""

    def __init__(
        self,
        lang=None,
        all_paragraphs=False,
        title=False,
        listitem=False,
        table=False,
        correction=False,
        error=False,
        errorort=False,
        errorortreal=False,
        errormorphsyn=False,
        errorsyn=False,
        errorlex=False,
        errorlang=False,
        foreign=False,
        errorformat=False,
        noforeign=False,
        withforeign=False,
        typos=False,
        print_filename=False,
        one_word_per_line=False,
        disambiguation=False,
        dependency=False,
        hyph_replacement="",
    ):
        """Setup all the options.

        The handling of error* elements are governed by the error*,
        noforeign, correction, typos and one_word_per_line arguments.

        If one_word_per_line and typos are False and correction is True, the
        content of the correct attribute should be printed instead of the
        .text part of the error element.

        If one_word_per_line or typos are True, the .text part, the correct
        attribute and the other attributes of the error* element should be
        printed out on one line.

        If typos is True and some of the error* options are True, only the
        elements that are True should be output

        If one_word_per_line is True and some of the error* options are True,
        only the elements that are True should get the error treatment, the
        other ones get treated as plain elements.

        If noforeign is True, neither the errorlang.text part nor the correct
        attribute should be printed.
        """
        self.paragraph = True
        self.all_paragraphs = all_paragraphs

        if title or listitem or table:
            self.paragraph = False

        self.title = title
        self.listitem = listitem
        self.table = table

        self.correction = correction
        self.error = error
        self.errorort = errorort
        self.errorortreal = errorortreal
        self.errormorphsyn = errormorphsyn
        self.errorsyn = errorsyn
        self.errorlex = errorlex
        self.errorlang = errorlang
        self.noforeign = noforeign
        self.foreign = foreign
        self.errorformat = errorformat

        self.error_filtering = (
            error
            or errorort
            or errorortreal
            or errormorphsyn
            or errorsyn
            or errorlex
            or errorlang
            or errorformat
        )

        if withforeign:
            self.correction = False
            self.error = True
            self.errorort = True
            self.errorortreal = True
            self.errormorphsyn = True
            self.errorsyn = True
            self.errorlex = True
            self.errorformat = True
            self.errorlang = False
            self.noforeign = False
            self.error_filtering = True

        self.typos = typos
        self.print_filename = print_filename
        if self.typos:
            self.one_word_per_line = True
        else:
            self.one_word_per_line = one_word_per_line

        if lang and lang.startswith("!"):
            self.lang = lang[1:]
            self.invert_lang = True
        else:
            self.lang = lang
            self.invert_lang = False

        self.disambiguation = disambiguation
        self.dependency = dependency

        if hyph_replacement == "xml":
            self.hyph_replacement = "<hyph/>"
        else:
            self.hyph_replacement = hyph_replacement

    def get_lang(self):
        """Get the lang of the file."""
        return self.etree.getroot().attrib["{http://www.w3.org/XML/1998/namespace}lang"]

    @staticmethod
    def get_element_language(element, parentlang):
        """Get the language of element.

        Elements inherit the parents language if not explicitely set
        """
        if element.get("{http://www.w3.org/XML/1998/namespace}lang") is None:
            return parentlang
        else:
            return element.get("{http://www.w3.org/XML/1998/namespace}lang")

    def collect_not_inline_errors(self, element, textlist):
        """Add the formatted errors as strings to the textlist list."""
        error_string = self.error_not_inline(element)
        if error_string != "":
            textlist.append(error_string)

        for child in element:
            if self.visit_error_not_inline(child):
                self.collect_not_inline_errors(child, textlist)

        if not self.typos:
            if element.tail is not None and element.tail.strip() != "":
                if not self.one_word_per_line:
                    textlist.append(element.tail)
                else:
                    textlist.extend(element.tail.strip().split())

    @staticmethod
    def corrected_texts(error_element):
        """Yield corrected versions of the error element."""
        for correct in error_element.xpath("./correct"):
            correct_text = "" if correct.text is None else correct.text
            tail_text = "" if error_element.tail is None else error_element.tail
            yield f"{correct_text}{tail_text}"

    def error_not_inline(self, element):
        """Collect and format parts of the element.

        Also scan the children if there is no error filtering or
        if the element is filtered
        """
        text = []
        if element.text is not None and element.text.strip() != "":
            text.append(element.text)

        if not self.error_filtering or self.include_this_error(element):
            for child in element:
                if child.tag != "correct":
                    text.extend(corrected for corrected in self.corrected_texts(child))

        text.extend(
            self.get_error_attributes(correct) for correct in element.xpath("./correct")
        )
        return "".join(text)

    @staticmethod
    def combine(text, text_list):
        """Combine a text with a parto f the text_list."""
        return [f"{text}{part}" for part in text_list]

    def get_error_attributes(self, correct_element):
        """Collect and format the attributes + the filename."""
        text = ["\t"]
        text.append("" if correct_element.text is None else correct_element.text)

        attributes = correct_element.attrib
        attr = [key + "=" + str(attributes[key]) for key in sorted(attributes)]

        if attr:
            text.append("\t#")
            text.append(",".join(attr))

            if self.print_filename:
                text.append(f", file: {os.path.basename(self.filename)}")

        elif self.print_filename:
            text.append(f"\t#file: {os.path.basename(self.filename)}")

        return "".join(text)

    def collect_inline_errors(self, element, textlist, parentlang):
        """Add the "correct" element to the list textlist."""
        correct = element.find("./correct")
        if correct is not None and not self.noforeign:
            textlist.append("" if correct.text is None else correct.text)

        self.get_contents(element.tail, textlist, parentlang)

    def collect_text(self, element, parentlang, buffer):
        """Collect text from element, and write the contents to buffer."""
        textlist = []

        self.visit_nonerror_element(element, textlist, parentlang)

        if textlist:
            if not self.one_word_per_line:
                textlist[-1] = textlist[-1].rstrip()
                buffer.write("".join(textlist))
                buffer.write(" ¶\n")
            else:
                buffer.write("\n".join(textlist))
                buffer.write("\n")

    def is_correct_lang(self, elt_lang):
        """Check if elt_lang is a wanted language.

        Args:
            elt_lang (str): a three character language.

        Returns:
            (bool): boolean
        """
        return (
            self.lang is None
            or (not self.invert_lang and elt_lang == self.lang)
            or (self.invert_lang and elt_lang != self.lang)
        )

    def get_contents(self, elt_contents, textlist, elt_lang):
        """Get the contents of a xml document.

        Args:
            elt_contents (str): the text of an etree element.
            textlist (list of str): text will be added this list.
            elt_lang (str): language of the element.
        """
        if elt_contents is not None:
            text = elt_contents
            if self.is_correct_lang(elt_lang):
                if not self.one_word_per_line:
                    textlist.append(text)
                else:
                    textlist.extend(text.split())

    def visit_children(self, element, textlist, parentlang):
        """Visit the children of element, adding their content to textlist."""
        for child in element:
            if child.tag != "correct":
                if child.tag == "errorlang" and self.noforeign and self.typos:
                    pass
                elif child.tag == "errorlang" and self.noforeign:
                    self.get_contents(child.tail, textlist, parentlang)
                elif self.visit_error_inline(child):
                    self.collect_inline_errors(
                        child, textlist, self.get_element_language(child, parentlang)
                    )
                elif self.visit_error_not_inline(child):
                    self.collect_not_inline_errors(child, textlist)
                else:
                    self.visit_nonerror_element(
                        child, textlist, self.get_element_language(element, parentlang)
                    )

    def visit_nonerror_element(self, element, textlist, parentlang):
        """Visit and extract text from non error element."""
        if not self.typos:
            self.get_contents(
                element.text, textlist, self.get_element_language(element, parentlang)
            )
        self.visit_children(element, textlist, parentlang)
        if not self.typos:
            self.get_contents(element.tail, textlist, parentlang)

    def visit_this_node(self, element):
        """Return True if the element should be visited."""
        return (
            self.all_paragraphs
            or (
                self.paragraph is True
                and (element.get("type") is None or element.get("type") == "text")
            )
            or (self.title is True and element.get("type") == "title")
            or (self.listitem is True and element.get("type") == "listitem")
            or (self.table is True and element.get("type") == "tablecell")
        )

    def visit_error_not_inline(self, element):
        """Determine whether element should be visited."""
        return (
            element.tag.startswith("error")
            and self.one_word_per_line
            and not self.error_filtering
            or self.include_this_error(element)
        )

    def visit_error_inline(self, element):
        """Determine whether element should be visited."""
        return (
            element.tag.startswith("error")
            and not self.one_word_per_line
            and (self.correction or self.include_this_error(element))
        )

    def include_this_error(self, element):
        """Determine whether element should be visited."""
        return self.error_filtering and (
            (element.tag == "error" and self.error)
            or (element.tag == "errorort" and self.errorort)
            or (element.tag == "errorortreal" and self.errorortreal)
            or (element.tag == "errormorphsyn" and self.errormorphsyn)
            or (element.tag == "errorsyn" and self.errorsyn)
            or (element.tag == "errorlex" and self.errorlex)
            or (element.tag == "errorformat" and self.errorformat)
            or (element.tag == "errorlang" and self.errorlang)
            or (element.tag == "errorlang" and self.noforeign)
        )

    def parse_file(self, filename):
        """Parse the xml document.

        Args:
            filename (str): path to the filename.
        """
        self.filename = filename
        p = etree.XMLParser(huge_tree=True)
        self.etree = etree.parse(filename, p)

    def process_file(self):
        """Process the given file, adding the text into buffer.

        Returns the buffer
        """
        buffer = StringIO()

        self.handle_hyph()
        if self.dependency:
            self.print_element(self.etree.find(".//dependency"), buffer)
        elif self.disambiguation:
            self.print_element(self.etree.find(".//disambiguation"), buffer)
        else:
            for paragraph in self.etree.findall(".//p"):
                if self.is_correct_lang(
                    self.get_element_language(paragraph, self.get_lang())
                ) and self.visit_this_node(paragraph):
                    self.collect_text(paragraph, self.get_lang(), buffer)

        return buffer

    def handle_hyph(self):
        """Replace hyph tags."""
        hyph_tails = []
        for hyph in self.etree.findall(".//hyph"):
            if hyph.tail is not None:
                hyph_tails.append(hyph.tail)

            if hyph.getnext() is None:
                if hyph.getparent().text is not None:
                    hyph_tails.insert(0, hyph.getparent().text)
                hyph.getparent().text = self.hyph_replacement.join(hyph_tails)
                hyph_tails[:] = []

            hyph.getparent().remove(hyph)

    def print_element(self, element, buffer):
        """Write the text of the element to the buffer.

        Args:
            element (etree._Element):
            buffer ():
        """
        if element is not None and element.text is not None:
            buffer.write(element.text)

    def print_file(self, file_):
        """Print a xml file to stdout."""
        if file_.endswith(".xml"):
            self.parse_file(file_)
            try:
                sys.stdout.write(self.process_file().getvalue())
            except BrokenPipeError:
                pass

__init__(lang=None, all_paragraphs=False, title=False, listitem=False, table=False, correction=False, error=False, errorort=False, errorortreal=False, errormorphsyn=False, errorsyn=False, errorlex=False, errorlang=False, foreign=False, errorformat=False, noforeign=False, withforeign=False, typos=False, print_filename=False, one_word_per_line=False, disambiguation=False, dependency=False, hyph_replacement='')

Setup all the options.

The handling of error elements are governed by the error, noforeign, correction, typos and one_word_per_line arguments.

If one_word_per_line and typos are False and correction is True, the content of the correct attribute should be printed instead of the .text part of the error element.

If one_word_per_line or typos are True, the .text part, the correct attribute and the other attributes of the error* element should be printed out on one line.

If typos is True and some of the error* options are True, only the elements that are True should be output

If one_word_per_line is True and some of the error* options are True, only the elements that are True should get the error treatment, the other ones get treated as plain elements.

If noforeign is True, neither the errorlang.text part nor the correct attribute should be printed.

Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
 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
def __init__(
    self,
    lang=None,
    all_paragraphs=False,
    title=False,
    listitem=False,
    table=False,
    correction=False,
    error=False,
    errorort=False,
    errorortreal=False,
    errormorphsyn=False,
    errorsyn=False,
    errorlex=False,
    errorlang=False,
    foreign=False,
    errorformat=False,
    noforeign=False,
    withforeign=False,
    typos=False,
    print_filename=False,
    one_word_per_line=False,
    disambiguation=False,
    dependency=False,
    hyph_replacement="",
):
    """Setup all the options.

    The handling of error* elements are governed by the error*,
    noforeign, correction, typos and one_word_per_line arguments.

    If one_word_per_line and typos are False and correction is True, the
    content of the correct attribute should be printed instead of the
    .text part of the error element.

    If one_word_per_line or typos are True, the .text part, the correct
    attribute and the other attributes of the error* element should be
    printed out on one line.

    If typos is True and some of the error* options are True, only the
    elements that are True should be output

    If one_word_per_line is True and some of the error* options are True,
    only the elements that are True should get the error treatment, the
    other ones get treated as plain elements.

    If noforeign is True, neither the errorlang.text part nor the correct
    attribute should be printed.
    """
    self.paragraph = True
    self.all_paragraphs = all_paragraphs

    if title or listitem or table:
        self.paragraph = False

    self.title = title
    self.listitem = listitem
    self.table = table

    self.correction = correction
    self.error = error
    self.errorort = errorort
    self.errorortreal = errorortreal
    self.errormorphsyn = errormorphsyn
    self.errorsyn = errorsyn
    self.errorlex = errorlex
    self.errorlang = errorlang
    self.noforeign = noforeign
    self.foreign = foreign
    self.errorformat = errorformat

    self.error_filtering = (
        error
        or errorort
        or errorortreal
        or errormorphsyn
        or errorsyn
        or errorlex
        or errorlang
        or errorformat
    )

    if withforeign:
        self.correction = False
        self.error = True
        self.errorort = True
        self.errorortreal = True
        self.errormorphsyn = True
        self.errorsyn = True
        self.errorlex = True
        self.errorformat = True
        self.errorlang = False
        self.noforeign = False
        self.error_filtering = True

    self.typos = typos
    self.print_filename = print_filename
    if self.typos:
        self.one_word_per_line = True
    else:
        self.one_word_per_line = one_word_per_line

    if lang and lang.startswith("!"):
        self.lang = lang[1:]
        self.invert_lang = True
    else:
        self.lang = lang
        self.invert_lang = False

    self.disambiguation = disambiguation
    self.dependency = dependency

    if hyph_replacement == "xml":
        self.hyph_replacement = "<hyph/>"
    else:
        self.hyph_replacement = hyph_replacement

collect_inline_errors(element, textlist, parentlang)

Add the "correct" element to the list textlist.

Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
272
273
274
275
276
277
278
def collect_inline_errors(self, element, textlist, parentlang):
    """Add the "correct" element to the list textlist."""
    correct = element.find("./correct")
    if correct is not None and not self.noforeign:
        textlist.append("" if correct.text is None else correct.text)

    self.get_contents(element.tail, textlist, parentlang)

collect_not_inline_errors(element, textlist)

Add the formatted errors as strings to the textlist list.

Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
def collect_not_inline_errors(self, element, textlist):
    """Add the formatted errors as strings to the textlist list."""
    error_string = self.error_not_inline(element)
    if error_string != "":
        textlist.append(error_string)

    for child in element:
        if self.visit_error_not_inline(child):
            self.collect_not_inline_errors(child, textlist)

    if not self.typos:
        if element.tail is not None and element.tail.strip() != "":
            if not self.one_word_per_line:
                textlist.append(element.tail)
            else:
                textlist.extend(element.tail.strip().split())

collect_text(element, parentlang, buffer)

Collect text from element, and write the contents to buffer.

Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
280
281
282
283
284
285
286
287
288
289
290
291
292
293
def collect_text(self, element, parentlang, buffer):
    """Collect text from element, and write the contents to buffer."""
    textlist = []

    self.visit_nonerror_element(element, textlist, parentlang)

    if textlist:
        if not self.one_word_per_line:
            textlist[-1] = textlist[-1].rstrip()
            buffer.write("".join(textlist))
            buffer.write(" ¶\n")
        else:
            buffer.write("\n".join(textlist))
            buffer.write("\n")

combine(text, text_list) staticmethod

Combine a text with a parto f the text_list.

Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
247
248
249
250
@staticmethod
def combine(text, text_list):
    """Combine a text with a parto f the text_list."""
    return [f"{text}{part}" for part in text_list]

corrected_texts(error_element) staticmethod

Yield corrected versions of the error element.

Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
219
220
221
222
223
224
225
@staticmethod
def corrected_texts(error_element):
    """Yield corrected versions of the error element."""
    for correct in error_element.xpath("./correct"):
        correct_text = "" if correct.text is None else correct.text
        tail_text = "" if error_element.tail is None else error_element.tail
        yield f"{correct_text}{tail_text}"

error_not_inline(element)

Collect and format parts of the element.

Also scan the children if there is no error filtering or if the element is filtered

Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
def error_not_inline(self, element):
    """Collect and format parts of the element.

    Also scan the children if there is no error filtering or
    if the element is filtered
    """
    text = []
    if element.text is not None and element.text.strip() != "":
        text.append(element.text)

    if not self.error_filtering or self.include_this_error(element):
        for child in element:
            if child.tag != "correct":
                text.extend(corrected for corrected in self.corrected_texts(child))

    text.extend(
        self.get_error_attributes(correct) for correct in element.xpath("./correct")
    )
    return "".join(text)

get_contents(elt_contents, textlist, elt_lang)

Get the contents of a xml document.

Parameters:

Name Type Description Default
elt_contents str

the text of an etree element.

required
textlist list of str

text will be added this list.

required
elt_lang str

language of the element.

required
Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
def get_contents(self, elt_contents, textlist, elt_lang):
    """Get the contents of a xml document.

    Args:
        elt_contents (str): the text of an etree element.
        textlist (list of str): text will be added this list.
        elt_lang (str): language of the element.
    """
    if elt_contents is not None:
        text = elt_contents
        if self.is_correct_lang(elt_lang):
            if not self.one_word_per_line:
                textlist.append(text)
            else:
                textlist.extend(text.split())

get_element_language(element, parentlang) staticmethod

Get the language of element.

Elements inherit the parents language if not explicitely set

Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
191
192
193
194
195
196
197
198
199
200
@staticmethod
def get_element_language(element, parentlang):
    """Get the language of element.

    Elements inherit the parents language if not explicitely set
    """
    if element.get("{http://www.w3.org/XML/1998/namespace}lang") is None:
        return parentlang
    else:
        return element.get("{http://www.w3.org/XML/1998/namespace}lang")

get_error_attributes(correct_element)

Collect and format the attributes + the filename.

Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
def get_error_attributes(self, correct_element):
    """Collect and format the attributes + the filename."""
    text = ["\t"]
    text.append("" if correct_element.text is None else correct_element.text)

    attributes = correct_element.attrib
    attr = [key + "=" + str(attributes[key]) for key in sorted(attributes)]

    if attr:
        text.append("\t#")
        text.append(",".join(attr))

        if self.print_filename:
            text.append(f", file: {os.path.basename(self.filename)}")

    elif self.print_filename:
        text.append(f"\t#file: {os.path.basename(self.filename)}")

    return "".join(text)

get_lang()

Get the lang of the file.

Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
187
188
189
def get_lang(self):
    """Get the lang of the file."""
    return self.etree.getroot().attrib["{http://www.w3.org/XML/1998/namespace}lang"]

handle_hyph()

Replace hyph tags.

Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
430
431
432
433
434
435
436
437
438
439
440
441
442
443
def handle_hyph(self):
    """Replace hyph tags."""
    hyph_tails = []
    for hyph in self.etree.findall(".//hyph"):
        if hyph.tail is not None:
            hyph_tails.append(hyph.tail)

        if hyph.getnext() is None:
            if hyph.getparent().text is not None:
                hyph_tails.insert(0, hyph.getparent().text)
            hyph.getparent().text = self.hyph_replacement.join(hyph_tails)
            hyph_tails[:] = []

        hyph.getparent().remove(hyph)

include_this_error(element)

Determine whether element should be visited.

Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
385
386
387
388
389
390
391
392
393
394
395
396
397
def include_this_error(self, element):
    """Determine whether element should be visited."""
    return self.error_filtering and (
        (element.tag == "error" and self.error)
        or (element.tag == "errorort" and self.errorort)
        or (element.tag == "errorortreal" and self.errorortreal)
        or (element.tag == "errormorphsyn" and self.errormorphsyn)
        or (element.tag == "errorsyn" and self.errorsyn)
        or (element.tag == "errorlex" and self.errorlex)
        or (element.tag == "errorformat" and self.errorformat)
        or (element.tag == "errorlang" and self.errorlang)
        or (element.tag == "errorlang" and self.noforeign)
    )

is_correct_lang(elt_lang)

Check if elt_lang is a wanted language.

Parameters:

Name Type Description Default
elt_lang str

a three character language.

required

Returns:

Type Description
bool

boolean

Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
295
296
297
298
299
300
301
302
303
304
305
306
307
308
def is_correct_lang(self, elt_lang):
    """Check if elt_lang is a wanted language.

    Args:
        elt_lang (str): a three character language.

    Returns:
        (bool): boolean
    """
    return (
        self.lang is None
        or (not self.invert_lang and elt_lang == self.lang)
        or (self.invert_lang and elt_lang != self.lang)
    )

parse_file(filename)

Parse the xml document.

Parameters:

Name Type Description Default
filename str

path to the filename.

required
Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
399
400
401
402
403
404
405
406
407
def parse_file(self, filename):
    """Parse the xml document.

    Args:
        filename (str): path to the filename.
    """
    self.filename = filename
    p = etree.XMLParser(huge_tree=True)
    self.etree = etree.parse(filename, p)

print_element(element, buffer)

Write the text of the element to the buffer.

Parameters:

Name Type Description Default
element etree._Element required
buffer required
Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
445
446
447
448
449
450
451
452
453
def print_element(self, element, buffer):
    """Write the text of the element to the buffer.

    Args:
        element (etree._Element):
        buffer ():
    """
    if element is not None and element.text is not None:
        buffer.write(element.text)

print_file(file_)

Print a xml file to stdout.

Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
455
456
457
458
459
460
461
462
def print_file(self, file_):
    """Print a xml file to stdout."""
    if file_.endswith(".xml"):
        self.parse_file(file_)
        try:
            sys.stdout.write(self.process_file().getvalue())
        except BrokenPipeError:
            pass

process_file()

Process the given file, adding the text into buffer.

Returns the buffer

Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
def process_file(self):
    """Process the given file, adding the text into buffer.

    Returns the buffer
    """
    buffer = StringIO()

    self.handle_hyph()
    if self.dependency:
        self.print_element(self.etree.find(".//dependency"), buffer)
    elif self.disambiguation:
        self.print_element(self.etree.find(".//disambiguation"), buffer)
    else:
        for paragraph in self.etree.findall(".//p"):
            if self.is_correct_lang(
                self.get_element_language(paragraph, self.get_lang())
            ) and self.visit_this_node(paragraph):
                self.collect_text(paragraph, self.get_lang(), buffer)

    return buffer

visit_children(element, textlist, parentlang)

Visit the children of element, adding their content to textlist.

Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
def visit_children(self, element, textlist, parentlang):
    """Visit the children of element, adding their content to textlist."""
    for child in element:
        if child.tag != "correct":
            if child.tag == "errorlang" and self.noforeign and self.typos:
                pass
            elif child.tag == "errorlang" and self.noforeign:
                self.get_contents(child.tail, textlist, parentlang)
            elif self.visit_error_inline(child):
                self.collect_inline_errors(
                    child, textlist, self.get_element_language(child, parentlang)
                )
            elif self.visit_error_not_inline(child):
                self.collect_not_inline_errors(child, textlist)
            else:
                self.visit_nonerror_element(
                    child, textlist, self.get_element_language(element, parentlang)
                )

visit_error_inline(element)

Determine whether element should be visited.

Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
377
378
379
380
381
382
383
def visit_error_inline(self, element):
    """Determine whether element should be visited."""
    return (
        element.tag.startswith("error")
        and not self.one_word_per_line
        and (self.correction or self.include_this_error(element))
    )

visit_error_not_inline(element)

Determine whether element should be visited.

Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
368
369
370
371
372
373
374
375
def visit_error_not_inline(self, element):
    """Determine whether element should be visited."""
    return (
        element.tag.startswith("error")
        and self.one_word_per_line
        and not self.error_filtering
        or self.include_this_error(element)
    )

visit_nonerror_element(element, textlist, parentlang)

Visit and extract text from non error element.

Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
345
346
347
348
349
350
351
352
353
def visit_nonerror_element(self, element, textlist, parentlang):
    """Visit and extract text from non error element."""
    if not self.typos:
        self.get_contents(
            element.text, textlist, self.get_element_language(element, parentlang)
        )
    self.visit_children(element, textlist, parentlang)
    if not self.typos:
        self.get_contents(element.tail, textlist, parentlang)

visit_this_node(element)

Return True if the element should be visited.

Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
355
356
357
358
359
360
361
362
363
364
365
366
def visit_this_node(self, element):
    """Return True if the element should be visited."""
    return (
        self.all_paragraphs
        or (
            self.paragraph is True
            and (element.get("type") is None or element.get("type") == "text")
        )
        or (self.title is True and element.get("type") == "title")
        or (self.listitem is True and element.get("type") == "listitem")
        or (self.table is True and element.get("type") == "tablecell")
    )

find_files(targets, extension)

Search for files with extension in targets.

Parameters:

Name Type Description Default
targets list of str

files or directories

required
extension str

interesting files has this extension.

required

Yields:

Type Description
str

path to the interesting file

Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
def find_files(targets, extension):
    """Search for files with extension in targets.

    Args:
        targets (list of str): files or directories
        extension (str): interesting files has this extension.

    Yields:
        (str): path to the interesting file
    """
    for target in targets:
        if os.path.exists(target):
            if os.path.isfile(target) and target.endswith(extension):
                yield target
            elif os.path.isdir(target):
                for root, _, files in os.walk(target):
                    for xml_file in files:
                        if xml_file.endswith(extension):
                            yield os.path.join(root, xml_file)
        else:
            print(f"{target} does not exist", file=sys.stderr)

main()

Set up the XMLPrinter class with the given command line options.

Process the given files and directories Print the output to stdout

Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
@suppress_broken_pipe_msg
def main():
    """Set up the XMLPrinter class with the given command line options.

    Process the given files and directories
    Print the output to stdout
    """
    args = parse_options()

    xml_printer = XMLPrinter(
        lang=args.lang,
        all_paragraphs=args.all_paragraphs,
        title=args.title,
        listitem=args.list,
        table=args.table,
        correction=args.corrections,
        error=args.error,
        errorort=args.errorort,
        errorortreal=args.errorortreal,
        errormorphsyn=args.errormorphsyn,
        errorsyn=args.errorsyn,
        errorlex=args.errorlex,
        errorlang=args.errorlang,
        noforeign=args.noforeign,
        withforeign=args.withforeign,
        errorformat=args.errorformat,
        typos=args.typos,
        print_filename=args.print_filename,
        one_word_per_line=args.one_word_per_line,
        dependency=args.dependency,
        disambiguation=args.disambiguation,
        hyph_replacement=args.hyph_replacement,
    )

    for filename in find_files(args.targets, ".xml"):
        xml_printer.print_file(filename)

parse_options()

Parse the options given to the program.

Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
def parse_options():
    """Parse the options given to the program."""
    parser = argparse.ArgumentParser(
        parents=[argparse_version.parser],
        description="Print the contents of a corpus in XML format\n\
        The default is to print paragraphs with no type (=text type).",
    )

    parser.add_argument(
        "-l",
        dest="lang",
        help="Print only elements in language LANG. Default \
                        is all langs.",
    )
    parser.add_argument(
        "-T", dest="title", action="store_true", help="Print paragraphs with title type"
    )
    parser.add_argument(
        "-L", dest="list", action="store_true", help="Print paragraphs with list type"
    )
    parser.add_argument(
        "-t", dest="table", action="store_true", help="Print paragraphs with table type"
    )
    parser.add_argument(
        "-a", dest="all_paragraphs", action="store_true", help="Print all text elements"
    )

    parser.add_argument(
        "-c",
        dest="corrections",
        action="store_true",
        help="Print corrected text instead of the original \
                        typos & errors",
    )
    parser.add_argument(
        "-C",
        dest="error",
        action="store_true",
        help="Only print unclassified (§/<error..>) \
                        corrections",
    )
    parser.add_argument(
        "-ort",
        dest="errorort",
        action="store_true",
        help="Only print ortoghraphic, non-word \
                        ($/<errorort..>) corrections",
    )
    parser.add_argument(
        "-ortreal",
        dest="errorortreal",
        action="store_true",
        help="Only print ortoghraphic, real-word \
                        (¢/<errorortreal..>) corrections",
    )
    parser.add_argument(
        "-morphsyn",
        dest="errormorphsyn",
        action="store_true",
        help="Only print morphosyntactic \
                        (£/<errormorphsyn..>) corrections",
    )
    parser.add_argument(
        "-syn",
        dest="errorsyn",
        action="store_true",
        help="Only print syntactic (¥/<errorsyn..>) \
                        corrections",
    )
    parser.add_argument(
        "-lex",
        dest="errorlex",
        action="store_true",
        help="Only print lexical (€/<errorlex..>) \
                        corrections",
    )
    parser.add_argument(
        "-format",
        dest="errorformat",
        action="store_true",
        help="Only print format (‰/<errorformat..>) \
                        corrections",
    )
    parser.add_argument(
        "-foreign",
        dest="errorlang",
        action="store_true",
        help="Only print foreign (∞/<errorlang..>) \
                        corrections",
    )
    parser.add_argument(
        "-noforeign",
        dest="noforeign",
        action="store_true",
        help="Do not print anything from foreign \
                        (∞/<errorlang..>) corrections",
    )
    parser.add_argument(
        "-withforeign",
        dest="withforeign",
        action="store_true",
        help="When printing corrections: include foreign text instead of nothing",
    )
    parser.add_argument(
        "-typos",
        dest="typos",
        action="store_true",
        help="Print only the errors/typos in the text, with \
                        corrections tab-separated",
    )
    parser.add_argument(
        "-f",
        dest="print_filename",
        action="store_true",
        help="Add the source filename as a comment after each \
                        error word.",
    )
    parser.add_argument(
        "-S",
        dest="one_word_per_line",
        action="store_true",
        help="Print the whole text one word per line; \
                        typos have tab separated corrections",
    )
    parser.add_argument(
        "-dis",
        dest="disambiguation",
        action="store_true",
        help="Print the disambiguation element",
    )
    parser.add_argument(
        "-dep",
        dest="dependency",
        action="store_true",
        help="Print the dependency element",
    )
    parser.add_argument(
        "-hyph",
        dest="hyph_replacement",
        default="",
        help="Replace hyph tags with the given argument",
    )

    parser.add_argument(
        "targets",
        nargs="+",
        help="Name of the files or directories to process. \
                        If a directory is given, all files in this directory \
                        and its subdirectories will be listed.",
    )

    args = parser.parse_args()
    return args

suppress_broken_pipe_msg(function)

Suppress message after a broken pipe error.

This code is fetched from: http://stackoverflow.com/questions/14207708/ioerror-errno-32-broken-pipe-python

Parameters:

Name Type Description Default
function function

the function that should be wrapped by this function.

required
Source code in /home/anders/projects/CorpusTools/corpustools/ccat.py
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
def suppress_broken_pipe_msg(function):
    """Suppress message after a broken pipe error.

    This code is fetched from:
    http://stackoverflow.com/questions/14207708/ioerror-errno-32-broken-pipe-python

    Args:
        function (function): the function that should be wrapped by this function.
    """

    @wraps(function)
    def wrapper(*args, **kwargs):
        try:
            return function(*args, **kwargs)
        except SystemExit:
            raise
        except:
            print_exc()
            sys.exit(1)
        finally:
            try:
                sys.stdout.flush()
            finally:
                try:
                    sys.stdout.close()
                finally:
                    try:
                        sys.stderr.flush()
                    finally:
                        sys.stderr.close()

    return wrapper