-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslit.py
363 lines (323 loc) · 13.2 KB
/
translit.py
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/python
# -*- coding: utf8 -*-
# Copyright (C) 2010-2011 Felix Herrmann <[email protected]>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Dieses Programm ist Freie Software: Sie können es unter den Bedingungen
# der GNU General Public License, wie von der Free Software Foundation,
# Version 3 der Lizenz oder (nach Ihrer Option) jeder späteren
# veröffentlichten Version, weiterverbreiten und/oder modifizieren.
#
# Dieses Programm wird in der Hoffnung, dass es nützlich sein wird, aber
# OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
# Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
# Siehe die GNU General Public License für weitere Details.
#
# Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
# Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
import sys
DSC = """
transliterate russian cyrillics into latin letters
"""
USAGE = """
call: "python translit.py '<cyrillics>'"
or: "python translit.py --file <pathToFile>"
optionally specify the type of the transliteration:
german scientific (default): "--type de"
iso: "--type iso"
american: "--type us"
examples:
"python translit.py --type iso 'русские слова'"
"python translit.py --file /home/user/doc/file.txt --type us"
"python translit.py --file /home/user/doc/file.txt"
call "python translit.py --help" to see this text
"""
ART = """
_ _
_ | (_)_
| |_ ____ ____ ____ ___| |_| |_ ____ _ _
| _) / ___) _ | _ \ /___) | | _) | _ \| | | |
| |__| | ( ( | | | | |___ | | | |__ _| | | | |_| |
\___)_| \_||_|_| |_(___/|_|_|\___|_) ||_/ \__ |
|_| (____/
"""
class Translit:
def get_args(self):
args = {}
args["inputStr"] = u""
args["type"] = None
if len(sys.argv) < 2:
print ART
print DSC
print USAGE
exit()
for cnt, arg in enumerate( sys.argv ):
if arg == "--help" or arg == "-h":
print USAGE
exit()
if arg == "--type" or arg == "-t":
args["type"] = sys.argv[cnt+1]
if arg == "--file" or arg == "-f":
try:
inputFile = open( sys.argv[cnt+1] , "r")
for line in inputFile:
args["inputStr"] += line.decode("utf-8")
inputFile.close()
return args
except:
print "Datei nicht lesbar oder nicht vorhanden. Beende Ausführung."
exit()
else:
if cnt == len(sys.argv)-1:
args["inputStr"] += arg.decode("utf-8")
#print args["inputStr"]
#else:
# if arg != sys.argv[0]:
#
return args
def translit(self, transtype, string):
# returns the transliteration of the given utf-8-string
#transtype = params["transtype"]
#string = params["inputStr"]
output = u""
prevLetter = u""
if transtype == "de" or transtype == None:
transDict = self.get_transDE()
if transtype == "iso":
transDict = self.get_transISO()
if transtype == "duden":
transDict = self.get_transDUDEN()
if transtype == "us":
transDict = self.get_transUS()
if transtype == "kritika":
transDict = self.get_transKritika()
for char in string:
try:
if transDict[char] == "usEx":
ex, prevLetter = self.usEx(char, prevLetter, self.get_vocalList())
output += ex
elif transDict[char] == "kritikaEx":
ex, prevLetter = self.kritikaEx(char, prevLetter, self.get_vocalList())
output += ex
else:
output += transDict[char]
prevLetter = char
except:
output += char
prevLetter = char
return output
def usEx(self, char, lastLetter, vocalList):
"tranliterate 'e' at the beginning of a word or after previous letter is in vocalList as 'ye'"
if char==u'Е' and lastLetter==' ' or char==u'Е' and lastLetter=='' or char==u'Е' and lastLetter in vocalList:
return 'Ye', char
else:
if char==u'е' and lastLetter==' ' or char==u'е' and lastLetter=='' or char==u'е' and lastLetter in vocalList:
lastLetter = char
return 'ye', char
def kritikaEx(self, char, lastLetter, vocalList):
"tranliterate 'i' at the beginning of a word or after previous letter is in vocalList as 'ie'"
if char==u'Е' and lastLetter==' ' or char==u'Е' and lastLetter=='' or char==u'Е' and lastLetter in vocalList:
return 'Ie', char
else:
if char==u'е' and lastLetter==' ' or char==u'е' and lastLetter=='' or char==u'е' and lastLetter in vocalList:
lastLetter = char
return 'ie', char
def get_vocalList(self):
"list the vocals and the soft/hard sign to check for exceptions"
return [u'a',u'А',
u'о', u'О',
u'y', u'У',
u'э', u'Э',
u'ы', u'Ы',
u'я', u'Я',
u'ё', u'Ё',
u'ю', u'Ю',
u'е', u'Е',
u'и', u'И',
u'ь', u'Ь',
u'ъ', u'Ъ',
]
def print_transDict(self, dict):
for values in dict.iteritems():
print values[0] + ":" + values[1]
def get_transISO(self):
"iso9 transliteration"
iso = self.get_transDE()
iso[u'Х'] = u'H'
iso[u'х'] = u'h'
iso[u'Щ'] = u'Ŝ'
iso[u'щ'] = u'ŝ'
iso[u'Э'] = u'È'
iso[u'э'] = u'è'
iso[u'Ю'] = u'Û'
iso[u'ю'] = u'û'
iso[u'Я'] = u'Â'
iso[u'я'] = u'â'
return iso
def get_transDE(self):
"scientific transliteration: return the dictionary with mappings 'russian' : 'transliteration'"
return {
u'А':u'A', u'а':u'a',
u'Б':u'B', u'б':u'b',
u'В':u'V', u'в':u'v',
u'Г':u'G', u'г':u'g',
u'Д':u'D', u'д':u'd',
u'Е':u'E', u'е':u'e',
u'Ё':u'Ё', u'ё':u'ё',
u'Ж':u'Ž', u'ж':u'ž',
u'З':u'Z', u'з':u'z',
u'И':u'I', u'и':u'i',
u'Й':u'J', u'й':u'j',
u'К':u'K', u'к':u'k',
u'Л':u'L', u'л':u'l',
u'М':u'M', u'м':u'm',
u'Н':u'N', u'н':u'n',
u'О':u'O', u'о':u'o',
u'П':u'P', u'п':u'p',
u'Р':u'R', u'р':u'r',
u'С':u'S', u'с':u's',
u'Т':u'T', u'т':u't',
u'У':U'u', u'у':u'u',
u'Ф':u'F', u'ф':u'f',
u'Х':u'Ch',u'х':u'ch',
u'Ц':u'C', u'ц':u'c',
u'Ч':u'Č', u'ч':u'č',
u'Ш':u'Š', u'ш':u'š',
u'Щ':u'Šč',u'щ':u'šč',
u'Ъ':u"’’ ",u'ъ':u"’’",# to-do: Zollzeichen ersetzen durch Hochkomma
u'Ы':u'Y', u'ы':u'y',
u'Ь':u"’", u'ь':u"’",
u'Э':u'Ė', u'э':u'ė',
u'Ю':u'Ju',u'ю':u'ju',
u'Я':u'Ja',u'я':u'ja',
}
def get_transDUDEN(self):
"TO-DO: German Duden transliteration: return the dictionary with mappings 'russian' : 'transliteration'"
return {
u'А':u'A', u'а':u'a',
u'Б':u'B', u'б':u'b',
u'В':u'V', u'в':u'v',
u'Г':u'G', u'г':u'g',
u'Д':u'D', u'д':u'd',
u'Е':u'E', u'е':u'e',
u'Ё':u'Ё', u'ё':u'ё',
u'Ж':u'Ž', u'ж':u'ž',
u'З':u'Z', u'з':u'z',
u'И':u'I', u'и':u'i',
u'Й':u'J', u'й':u'j',
u'К':u'K', u'к':u'k',
u'Л':u'L', u'л':u'l',
u'М':u'M', u'м':u'm',
u'Н':u'N', u'н':u'n',
u'О':u'O', u'о':u'o',
u'П':u'P', u'п':u'p',
u'Р':u'R', u'р':u'r',
u'С':u'S', u'с':u's',
u'Т':u'T', u'т':u't',
u'У':u'u', u'у':u'u',
u'Ф':u'F', u'ф':u'f',
u'Х':u'Ch',u'х':u'ch',
u'Ц':u'C', u'ц':u'c',
u'Ч':u'Č', u'ч':u'č',
u'Ш':u'Š', u'ш':u'š',
u'Щ':u'Šč',u'щ':u'šč',
u'Ъ':u"''",u'ъ':u"''",
u'Ы':u'Y', u'ы':u'y',
u'Ь':u"'", u'ь':u"'",
u'Э':u'Ė', u'э':u'ė',
u'Ю':u'Ju',u'ю':u'ju',
u'Я':u'Ja',u'я':u'ja',
}
def get_transUS(self):
"TO-DO: US transliteration: return the dictionary with mappings 'russian' : 'transliteration'"
return {
u'А':u'A', u'а':u'a',
u'Б':u'B', u'б':u'b',
u'В':u'V', u'в':u'v',
u'Г':u'G', u'г':u'g',
u'Д':u'D', u'д':u'd',
u'Е':u"usEx", u'е':u"usEx",
u'Ё':u'E', u'ё':u'e',
u'Ж':u'Zh', u'ж':u'zh',
u'З':u'Z', u'з':u'z',
u'И':u'I', u'и':u'i',
u'Й':u'Y', u'й':u'y',
u'К':u'K', u'к':u'k',
u'Л':u'L', u'л':u'l',
u'М':u'M', u'м':u'm',
u'Н':u'N', u'н':u'n',
u'О':u'O', u'о':u'o',
u'П':u'P', u'п':u'p',
u'Р':u'R', u'р':u'r',
u'С':u'S', u'с':u's',
u'Т':u'T', u'т':u't',
u'У':u'U', u'у':u'u',
u'Ф':u'F', u'ф':u'f',
u'Х':u'Kh',u'х':u'kh',
u'Ц':u'Ts', u'ц':u'ts',
u'Ч':u'Ch', u'ч':u'ch',
u'Ш':u'Sh', u'ш':u'sh',
u'Щ':u'Shch',u'щ':u'shch',
u'Ъ':u"",u'ъ':u"",
u'Ы':u'Y', u'ы':u'y',
u'Ь':u"", u'ь':u"",
u'Э':u'E', u'э':u'e',
u'Ю':u'Yu',u'ю':u'yu',
u'Я':u'Ya',u'я':u'ya',
}
def get_transKritika(self):
"Kritika-Style transliteration: return the dictionary with mappings 'russian' : 'transliteration'"
return {
u'А':u'A', u'а':u'a',
u'Б':u'B', u'б':u'b',
u'В':u'V', u'в':u'v',
u'Г':u'G', u'г':u'g',
u'Д':u'D', u'д':u'd',
u'Е':u"E", u'е':u"e",
u'Ё':u'E', u'ё':u'e',
u'Ж':u'Zh', u'ж':u'zh',
u'З':u'Z', u'з':u'z',
u'И':u'I', u'и':u'i',
u'Й':u'I', u'й':u'i',
u'К':u'K', u'к':u'k',
u'Л':u'L', u'л':u'l',
u'М':u'M', u'м':u'm',
u'Н':u'N', u'н':u'n',
u'О':u'O', u'о':u'o',
u'П':u'P', u'п':u'p',
u'Р':u'R', u'р':u'r',
u'С':u'S', u'с':u's',
u'Т':u'T', u'т':u't',
u'У':u'U', u'у':u'u',
u'Ф':u'F', u'ф':u'f',
u'Х':u'Kh',u'х':u'kh',
u'Ц':u'Ts', u'ц':u'ts',
u'Ч':u'Ch', u'ч':u'ch',
u'Ш':u'Sh', u'ш':u'sh',
u'Щ':u'Shch',u'щ':u'shch',
u'Ъ':u"",u'ъ':u"",
u'Ы':u'Y', u'ы':u'y',
u'Ь':u"’", u'ь':u"’",
u'Э':u'E', u'э':u'e',
u'Ю':u'Iu',u'ю':u'iu',
u'Я':u'Ia',u'я':u'ia',
}
def main(self):
args = self.get_args()
print self.translit(args["type"], args["inputStr"])
#params["type"]
#string = params["inputStr"]
if __name__ == "__main__":
translit = Translit()
translit.main()