Python-Skript Update.
[wortliste.git] / skripte / python / edit_tools / abgleich_sprachvarianten.py
blob5e138faabeb17d01d09da0210731743c96b7bda3
1 #!/usr/bin/env python
2 # -*- coding: utf8 -*-
3 # :Copyright: © 2014 Günter Milde.
4 # Released without warranty under the terms of the
5 # GNU General Public License (v. 2 or later)
6 # :Id: $Id: $
8 # Abgleich der Trennstellen zwischen Sprachvarianten
9 # ====================================================
11 # * Übertragen von kategorisierten Trennstellen zwischen Sprachvarianten
12 # desselben Wortes, und/oder
14 # * Zusammenfassen von Feldern mit gleichem Inhalt wenn das Ergebnis ein
15 # wohlgeformter Eintrag ist.
17 # * Ergänzen von Formen mit SS statt ß.
19 # ::
21 import re, sys, codecs, copy
22 from wortliste import WordFile, WordEntry, join_word, udiff, sprachabgleich
25 # Zusammenfassen von Feldern mit gleichem Inhalt z.B.
27 # hallo;-2-;hal-lo;hal-o --> hallo;hal-lo
29 # in allen Einträgen von `wortliste`.
30 # Siehe ``WordEntry.conflate_fields()`` in wortliste.py.
32 # Anwendung 2012-03-13
33 # (getestet mit ``texlua validate.lua < ../wortliste``)
35 # ========= ====== =======
36 # Typ Vorher Nachher
37 # --------- ------ -------
38 # ua 371807 374614
39 # uxtr 41156 38349
40 # ========= ====== =======
42 # ::
44 if __name__ == '__main__':
46 # sys.stdout mit UTF8 encoding.
47 sys.stdout = codecs.getwriter('UTF-8')(sys.stdout)
49 # Die `Wortliste`::
51 wordfile = WordFile('../../../wortliste') # ≅ 400 000 Einträge/Zeilen
52 wortliste = list(wordfile)
53 wortliste_neu = []
55 wordfile.seek(0) # Pointer zurücksetzen
56 words = wordfile.asdict()
58 for oldentry in wortliste:
59 if len(oldentry) <= 2:
60 # Ggf. Ergänzen der GROSS-Variante:
61 if (u'ß' in oldentry[0]
62 and oldentry[0].replace(u'ß', u'ss') not in words
63 and oldentry[0].replace(u'ß', u'ss').lower() not in words
64 and oldentry[0].replace(u'ß', u'ss').title() not in words
66 entry = WordEntry(oldentry[0].replace(u'ß', u'ss')
67 + u';-2-;-3-;-4-;'
68 + oldentry[1].replace(u'ß', u'ss'))
69 wortliste_neu.append(entry)
70 wortliste_neu.append(oldentry)
71 continue
72 entry = copy.copy(oldentry)
73 sprachabgleich(entry)
74 # Sprachabgleich mit ß-Form (Strassenschild vs. Straßenschild)
75 if oldentry == entry and u'ss' in entry[0]:
76 # Vergleichseintrag für Sprachabgleich finden:
77 for field in entry[1:]:
78 if not field.startswith(u'-'):
79 break # ``field`` ist jetzt erstes nichtleeres Feld
80 sz_key = join_word(field.replace(u'ss', u'ß'))
81 try:
82 vergleichseintrag = words[sz_key]
83 sprachabgleich(entry, vergleichseintrag)
84 except KeyError: # sz-Variante fehlt
85 wort1901 = entry.get('de-x-GROSS,de-1901-x-GROSS')
86 if (wort1901 and 'ss' in wort1901
87 and not sz_key.title() in words):
88 sz_wort = wort1901.replace(u'ss', u'ß')
89 if (not u'/' in sz_wort and len(sz_key) > 3
90 and not u'Abk.' in entry.comment):
91 # print wort1901, "sz-Variante fehlt", sz_key
92 # print u'%s;-2-;%s;-4-' % (join_word(sz_wort), sz_wort)
93 oldentry = WordEntry(u'%s;-2-;%s;-4-' %
94 (join_word(sz_wort), sz_wort))
96 if oldentry == entry and u'ß' in entry[0]:
97 try:
98 sprachabgleich(entry, words[entry[0].replace(u'ß', u'ss')])
99 except KeyError:
100 # Ergänzen der GROSS-Variante
101 if entry.get('de-1996') is None:
102 oldentry = WordEntry(u';'.join(
103 [entry[0].replace(u'ß', u'ss'),
104 u'-2-;-3-',
105 entry[2].replace(u'ß', u'ss'),
106 entry[2].replace(u'ß', u'ss')]))
107 elif entry.get('de-1996') is None:
108 # Dämmmassnahmen;-2-;-3-;-4-;-5-;-6-;Dämm==mass=nah-men;-8-
109 oldentry = WordEntry(entry[0].replace(u'ß', u'ss')
110 + u';-2-;-3-;-4-;-5-;-6-;'
111 + entry[3].replace(u'ß', u'ss')
112 + u'-8-')
113 else:
114 oldentry = WordEntry(u';'.join(
115 [entry[0].replace(u'ß', u'ss'),
116 u'-2-;-3-;-4-;-5-',
117 entry[2].replace(u'ß', u'ss'),
118 entry[3].replace(u'ß', u'ss'),
119 u'-8-']))
121 wortliste_neu.append(oldentry)
122 entry.conflate_fields()
123 wortliste_neu.append(entry)
125 words = None # Speicher freigeben
127 # Patch erstellen::
129 patch = udiff(wortliste, wortliste_neu, 'wortliste', 'wortliste-neu',
130 encoding=wordfile.encoding)
131 if patch:
132 # print patch
133 patchfile = open('../../../wortliste.patch', 'w')
134 patchfile.write(patch + '\n')
135 print '"wortliste.patch" geschrieben'
136 else:
137 print 'empty patch'