[Trennmuster] Skript zum Erstellen einer Wortliste für Ligatursatzmuster

Lukas Sommer sommerluk at gmail.com
Fr Aug 26 14:16:58 CEST 2016


Hallo.

Anbei die Dokumentation und eine aktualisierte Version des Skripts
(mit genauerer Filterung bei Doppeldeutigkeiten und mit Querverweisen
auf die Dokumentation). Dabei habe ich die Endung .txt angehängt,
damit die Mailingliste die Datei nicht zurückweist.

Als Dateinamen schlage ich vor:
wortliste/skripte/python/ligatures/LIESMICH.md
wortliste/skripte/python/ligatures/prepare-ligature-wordlist.py

Das aktualisierte Skript habe ich mit der aktuellen Git-Version der
Wortliste getestet. Es gibt nur noch ein Wort, das als inkonsistent
gefiltert wird:

sodass;-2-;-3-;so=dass;so-dass

Bei der Liste pre-1901 wird überhaupt kein Wort als inkonsistent
gefiltert, aber einige wenige Wörter mit nicht klassifizierten
Trennstellen:

accomodiren;ac<co<mo·di-ren
achtungswerther;ach-tungs>wer·ther
Beurtheiler;Be<ur<thei·ler
demüthig;de·müt·hig
desavouiren;de<sa-voui·ren
kritisirte;kri-ti-sir·te



-- 
Lukas Sommer
-------------- nächster Teil --------------
#!/usr/bin/env python
# -*- coding: utf8 -*-


# This script is for Python 2.7


u"""
Make patgen wordlist for ligature patterns.

See LIESMICH.md for a detailed documentation (german).
"""


u"""
The MIT License (MIT)

Copyright (c) 2016 Lukas Sommer

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""


import codecs
import re
import argparse


def make_patgen_input():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        'input',
        help=u"wordlist (UTF8 encoded) in "
             "the file format of the Trennmuster project.")
    parser.add_argument(
        'output',
        help=u"wordlist (UTF8 encoded) as "
             "simple word list with a dash that marks all positions where "
             "ligatures have to be suppressed. This file can be used as "
             "input for patgen. Note however that it is UTF8 encoded, "
             "therefor you have to convert it to a single-byte encoding "
             "that patgen understands (e.g. ISO 8859-15) before using it "
             "in patgen. Attention: Existing files will be overwritten.")
    parser.add_argument(
        'log',
        help=u"log (UTF8 encoded) of the operations. "
             "Attention: Existing files will be overwritten.")
    args = parser.parse_args()

    # open the files
    wordlist_file = codecs.open(args.input, "rb", "UTF8", "strict")
    patgen_file = codecs.open(args.output, "wb", "UTF8", "strict")
    log_file = codecs.open(args.log, "wb", "UTF8", "strict")

    # initialize variables for log data
    double_meaning = []
    unclassified = []
    inconsistent = []

    # process data from the word list
    print u"Please wait while processing data..."
    for line in wordlist_file:
        # remove comments
        simplified_line = line.split(u"#")[0]
        # test for non-classified things: U+00B7 MIDDLE DOT
        if u"·" in simplified_line:
            unclassified.append(line)
            continue
        # remove whitespace
        simplified_line = simplified_line.strip()
        # remove information about special hyphenations following 1901
        # orthography (Not useful because most programs makes the
        # hyphenation automatically, but scripts access normally to the
        # non-hyphenated underlying data.)
        # We use *? instead of *, because * is greedy,
        # but *? is reluctant/non-greedy, which makes sure that we do not
        # match various blocks at the same time.
        simplified_line = re.sub(
            ur"(\{)(.*?)/.*?\}",
            ur"\2",
            simplified_line)
        # remove placeholder content of empty fields (like “-2-” for
        # an empty field at column 2)
        simplified_line = re.sub(
            ur"\-[0-9]\-",
            ur"",
            simplified_line)
        # remove hyphenations within morphemes (not useful for
        # ligature setting)
        simplified_line = simplified_line.replace(u"-", u"")
        # remove markers for bad hyphenation quality
        # (not useful for ligature setting)
        simplified_line = simplified_line.replace(u".", u"")
        # use “-” as sign for morpheme boundaries (“<”, “>”, “=”)
        # (“-” is guaranteed to not occur in the string, because it is
        # yet filtered out), which will later also be recognized by patgen.
        simplified_line = simplified_line.replace(u"<", u"-")
        simplified_line = simplified_line.replace(u">", u"-")
        simplified_line = simplified_line.replace(u"=", u"-")
        # remove duplicate morpheme boundaries
        while simplified_line.count(u"--") > 0:
            simplified_line = simplified_line.replace(u"--", u"-")
        # test for double meaning
        inconsistent_double_meaning = False
        stop_loop = False
        while not stop_loop:
            my_match_object = re.search(
                ur"\[(.*?)/(.*?)\]",
                simplified_line)
            if my_match_object:
                # double meaning found
                if my_match_object.group(1) == my_match_object.group(2):
                    # ligature setting is identical for both meaning
                    # We substitute it by a single meaning.
                    simplified_line = re.sub(
                        ur"\[(.*?)/(.*?)\]",
                        ur"\1",
                        simplified_line)
                else:
                    # ligature setting is not identical for both meaning
                    inconsistent_double_meaning = True
                    stop_loop = True
            else:
                # no double meaning found
                stop_loop = True
        if inconsistent_double_meaning:
            double_meaning.append(line)
            continue
        # transform into an array
        my_array = simplified_line.split(u";")
        # remove the first column (does not contain ligature data,
        # but only the non-hyphenated word)
        if len(my_array) > 0:
            my_array.pop(0)
        # remove empty fields
        while u"" in my_array:
            my_array.remove(u"")
        # nothing to do if there is no data (like on empty lines)
        if len(my_array) == 0:
            continue
        # test if there are differences between the remaining fields
        if len(my_array) != my_array.count(my_array[0]):
            inconsistent.append(line)
            continue
        # write to file
        patgen_file.write(my_array[0].lower() + u"\n")

    # close wordlist_file and patgen_file
    wordlist_file.close()
    patgen_file.close()

    # write log
    log_file.write(
        u"The following entries "
        u"with ambiguous morpheme boundaries "
        u"have been ignored:\n")
    for entry in double_meaning:
        log_file.write(entry)
    log_file.write(
        u"\n\nThe following entries "
        u"with unclassified hyphenation points "
        u"have been ignored:\n")
    for entry in unclassified:
        log_file.write(entry)
    log_file.write(
        u"\n\nThe following entries "
        u"with inconsistencies between different orthographies "
        u"have been ignored:\n")
    for entry in inconsistent:
        log_file.write(entry)

    # close log file
    log_file.close()

    # exit
    print("Done.")


make_patgen_input()
-------------- nächster Teil --------------
Ein Dateianhang mit Binärdaten wurde abgetrennt...
Dateiname   : LIESMICH.md
Dateityp    : text/x-markdown
Dateigröße  : 10583 bytes
Beschreibung: nicht verfügbar
URL         : <https://listi.jpberlin.de/pipermail/trennmuster/attachments/20160826/1821c329/attachment.bin>


Mehr Informationen über die Mailingliste Trennmuster