3 # :Copyright: © 2014 Günter Milde.
4 # Released without warranty under the terms of the
5 # GNU General Public License (v. 2 or later)
8 # long_s_conversion.py: Demonstrator für die Lang-S Wandlung
9 # =============================================================================
11 u
"""Rund-S nach Lang-S Wandlung über "hyphenation patterns"."""
13 import os
, sys
, codecs
, re
, optparse
14 from hyphenation
import Hyphenator
20 # Lang-s Pseudo-Trennmuster mit "s-" statt "ſ" (ausſagen -> auss-agen)
21 # (mit "make de-x-long-s" im Wurzelverzeichnis der wortliste generiert)::
23 default_pfile
= os
.path
.normpath(os
.path
.join(os
.path
.dirname(__file__
),
24 '../../../de-long-s/de-x-long-s.pat'))
29 # ſ steht auch am Ende von Abkürzungen, wenn es im abgekürzten Wort steht
30 # (Abſ. - Abſatz/Abſender, (de)creſc. - (de)creſcendo, daſ. - daſelbst ...)
31 # s steht auch in der Mitte von Abkürzungen, wenn es im abgekürzten Wort steht
32 # (Ausg. - Ausgang/Ausgabe, Hrsg. - Herausgeber, ...)
35 exceptions
= (u
'Abſ', # Abſatz/Abſender
38 # u'daſ', # da<ſelbſt (nicht von Artikel "das" zu unterscheiden!)
39 u
'Diſſ', # Diſſertation
41 u
'Maſſ', # Maſſachuſetts
42 # u'Miſſ', # Miſſiſippi (nicht von Miſs (Frln.) zu unterscheiden)
45 exceptions
= dict((ex
.replace(u
'ſ', u
's'), ex
) for ex
in exceptions
)
47 # Konvertierung mit Hyphenator::
49 def transformiere(wort
, hyphenator
):
53 if wort
in exceptions
:
54 return exceptions
[wort
]
56 wort
= hyphenator
.hyphenate_word(wort
, hyphen
=u
'-', lmin
=1, rmin
=1)
58 # Wandle "s-" in "ſ" (auss-agen -> ausſagen):
60 return wort
.replace(u
's-', u
'ſ').replace(u
'S-', u
'S')
62 def transformiere_text(text
, hyphenator
):
63 # Text zerlegen: finde (ggf. leere) Folgen von nicht-Wort-Zeichen
64 # gefolgt von Wort-Zeichen. Der Iterator liefert Match-Objekte, mit
65 # den Untergruppen 0: nicht-Wort und 1: Wort.
66 it
= re
.finditer(r
"([\W0-9_]*)(\w*)", text
, flags
=re
.UNICODE
)
67 # Konvertierung und Zusammenfügen
68 parts
= [match
.groups()[0] # nicht-Wort Zeichen
69 + transformiere(match
.groups()[1], hyphenator
)
71 return u
''.join(parts
)
73 if __name__
== '__main__':
75 usage
= u
'%prog [options] [words to be transformed]\n\n' + __doc__
77 parser
= optparse
.OptionParser(usage
=usage
)
78 parser
.add_option('-f', '--pattern-file', dest
='pattern_file',
79 help='Pattern file, Default "%s"' % default_pfile
,
80 default
=default_pfile
)
81 parser
.add_option('-t', '--test', action
="store_true", default
=False,
82 help='Vergleiche Eingabe mit Rekonstruktion, '
85 (options
, args
) = parser
.parse_args()
88 h_Latf
= Hyphenator(options
.pattern_file
)
90 # sys.stdout mit UTF8 encoding.
91 sys
.stdout
= codecs
.getwriter('utf8')(sys
.stdout
)
94 lines
= [' '.join(args
).decode('utf8')]
96 lines
= (line
.decode('utf8') for line
in sys
.stdin
)
100 line2
= transformiere_text(line
.replace(u
'ſ', u
's'), h_Latf
)
102 print line
.strip(), '->', line2
,
106 print transformiere_text(line
, h_Latf
),