-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcyrconv.py
369 lines (304 loc) · 11 KB
/
cyrconv.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
363
364
365
366
367
368
#! /usr/bin/python3
"""
Converts Cyrillic script to Latin.
Use:
cyrillic_text = 'На ливади коњ ућустечен и расћустечен!'
converted = CirConv(text=cyrillic_text)
converted.convert_to_latin()
# Also: converted.convert_to_cyrillic()
print(converted.result)
> Na livadi konj ućustečen i rasćustečen!
"""
#
# Copyright (C) 2011 Romeo Mlinar (mlinar [a] languagebits.com)
#
# 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/>.
__version__ = '1.5'
__url__ = "https://gitorious.org/dtknv"
__author__ = "Romeo Mlinar"
__license__ = "GNU General Public License v. 3"
import os
import codecs
import json
cyr = {'А':'A', 'Б':'B', 'В':'V', 'Г':'G', 'Д':'D', 'Е':'E',
'Ж':'Ž', 'З':'Z', 'И':'I', 'Ј':'J', 'К':'K', 'Л':'L',
'М':'M', 'Н':'N', 'Њ':'Nj','О':'O', 'П':'P', 'Р':'R',
'С':'S', 'Т':'T', 'Ћ':'Ć', 'У':'U', 'Ф':'F', 'Х':'H',
'Ц':'C', 'Ч':'Č', 'Џ':'Dž','Ш':'Š', 'Ђ':'Đ', 'Љ':'Lj',
'а':'a', 'б':'b', 'в':'v', 'г':'g', 'д':'d', 'е':'e',
'ж':'ž', 'з':'z', 'и':'i', 'ј':'j', 'к':'k', 'л':'l',
'љ':'lj','м':'m', 'н':'n', 'њ':'nj', 'о':'o', 'п':'p',
'р':'r', 'с':'s', 'т':'t', 'ћ':'ć', 'у':'u', 'ф':'f',
'х':'h', 'ц':'c', 'ч':'č', 'џ':'dž','ш':'š', 'ђ':'đ'}
lat_resolutions = {'NJ':'Њ',
'Nj':'Њ',
'nJ':'нЈ',
'LJ':'Љ',
'Lj':'љ',
'lJ':'лЈ',
'DŽ':'Џ',
'Dž':'Џ',
'dŽ':'дЖ',}
two_char = {'Њ':'NJ', 'Џ':'DŽ', 'Љ':'LJ'}
# Characters that can follow capital letter. TODO: Options
# for this?
INTERPUNCTION_CAPLETTER = "!?.'„“" + '"' + " " + "»«–…"
standard_exc = \
{'injekci': 'инјекци',
'konjuga': 'конјуга',
'nadživlj': 'надживљ',
'nadžnje': 'наджње',
'odživlj': 'одживљ',
'odživljen': 'одживљен',
'podžnjeti': 'поджњети'}
class Replace:
"""
Loads and saves strings that need to have different
conversion rules.
"""
def __init__(self, f=False):
"""
Load and save file strings
"""
pass
def load(self, f):
"""
Load a JSON file.
"""
return(self._load(f))
def _load(self, f):
"""
Load a JSON file.
"""
# TODO: Add more elaborate check and
# introduce a warning.
with open(f, mode='r', encoding='utf-8') as f:
c = json.load(f)
return(c)
def save(self, f, exc):
"""
Save a JSON file.
"""
with open(f, mode='w', encoding='utf-8') as f:
json.dump(exc, f)
class CirConv:
"""
Converts Cyrillic script to Latin and vice versa.
"""
def __init__(self, text='', stats=False, exception_files=[],
variants=False,
path=False):
"""
text - text to be converted
stats - true if statistics is to be calaculated
exceptions - list of files with the exception strings
"""
# Raise TypeError if 'text' is not a character
# object.
if not isinstance(text, str):
raise TypeError('CirConv accepts text only, %s is rejected.' \
% type(text))
# Variables
self.path = path
self.text = text
self.exception_elements = []
# Exceptions strings. Don't load if path is
# not present.
if path and len(exception_files):
self.load_exceptions(exception_files)
else:
self.exception_elements.append(standard_exc)
# Variants?
if variants and len(exception_files):
self._make_variants()
# Make character maps.
self._make_charkeys()
def load_exceptions(self, flist):
"""
Load exceptions strings from flist files.
"""
self.exception_elements = []
if isinstance(flist, str):
f = os.path.join(self.path, flist)
exc_content = self._load_exc_file(f)
if exc_content:
self.exception_elements.append(exc_content)
else:
paths = [os.path.join(self.path, i) for i in flist]
for f in paths:
exc_content = self._load_exc_file(f)
if exc_content:
self.exception_elements.append(exc_content)
def _load_exc_file(self, f):
"""
Load exception file or return false if
there was an error.
"""
try:
exc_content = Replace().load(f)
except:
exc_content = False
return(exc_content)
def _make_variants(self):
"""
Make variants of the words.
TODO: finish this
"""
pass
# variants = []
# for word in words:
# variants.append(word.upper())
# variants.append(word.capitalize())
# return variants
def convert_to_latin(self):
"""
Convert the text and place it into .result. No return.
"""
self.result = self._charreplace(self.text, mode='tolat')
def convert_to_cyrillic(self):
"""
Convert the text and place it into .result. No return.
"""
self.result = self._charreplace(self.text, mode='tocyr')
def convert(self, text, prepare=False):
"""
If text is in Cyrillic, convert it to Latin and
vice versa.
Return the text.
Does not use public text or conversion functions.
"""
if self.is_all_cyrillic(text):
return self._charreplace(text, mode='tolat')
elif self.is_all_latin(text):
return self._charreplace(text, mode='tocyr')
else:
raise ValueError("Method does not accept mixed-script text.")
def is_all_cyrillic(self, text=None):
"""
Return true if all chars are Cyrillic.
"""
return self._is_all_textscript(text, 'lat')
def is_all_latin(self, text=None):
"""
Return true if all chars are Latin.
"""
return self._is_all_textscript(text, 'cyr')
def _is_all_textscript(self, text, script):
"""
Return true if all chars are Cyrillic/Latin
"""
# Check if the argumets are valid
if script not in ('lat', 'cyr'):
ValueError('script must be "lat" or "cyr"')
# If no text is provide, check self.text
if text == None:
text = self.text
# Character sets
characters = getattr(self, 'charmap_to%s' % script)
for i in text:
if i not in characters.keys():
return False
return True
def _make_charkeys(self):
"""
Make dictionaries for character replacement.
"""
self.charmap_tolat = cyr
self.charmap_tocyr = dict([v,k] for k,v in cyr.items())
def _prepare_for_cyrillic(self, text):
"""
Prepare text for conversion to Cyrillic.
For example, capitalised "NJEGOŠ" is "ЊЕГОШ". The conversion
without this method would be invalid "НЈЕГОШ".
Uses lat_resolutions dictionary.
"""
lat_keys = lat_resolutions.keys()
for letter in lat_keys:
if letter in text:
text = text.replace(letter, lat_resolutions[letter])
return text
def _prepare_for_latin(self, text):
"""
Prepare text for conversion to Latin.
For example, capitalised "ЊЕГОШ" is "NJEGOŠ". The conversion
without this method would be invalid "NjEGOŠ". The first form
is required by the grammar of Serbian.
"""
for letter in two_char.keys():
for i in range(text.count(letter)):
letter_position = text.find(letter)
if self._cap_check(text, letter_position):
text = text.replace(letter, two_char[letter])
return text
def _cap_check(self, text, position):
"""
Returns true is the character at position+1 is capitalised.
This method should contain more detailed checks.
"""
text_check = False
try:
# In case the letter in quesiton is at the end
# of a sentence:
if (text[position+1] in INTERPUNCTION_CAPLETTER) and \
text[position-1].isupper():
return(text[position-1].isupper())
except:
pass
try:
# Is the letter at the end of a word?
# I.e. KONJ.
text_check = text[position+1].isupper()
except IndexError:
# Probably is:
text_check = text[position-1].isupper()
return(text_check)
def _excreplace(self, text):
"""
Replace custom strings.
"""
print("I got", text)
# Go throught self.exceptions list, that holds
# all dictionaries correspondng to files loaded
# by Replace in __init__.
for exception_dictionary in self.exception_elements:
# Go through all keys of a dictionary.
for string_search in exception_dictionary.keys():
# If key is found in text, replace it
# by the corresponding value.
if string_search in text:
text = text.replace(string_search,
exception_dictionary[string_search])
return text
def _charreplace(self, text, mode):
"""
Replace characters in the input text.
"""
# Replace custom strings ("exceptions")
text = self._excreplace(text)
# Create lists and dictionary
if mode == 'tocyr':
charkeys = self.charmap_tocyr.keys()
charmap = self.charmap_tocyr
text = self._prepare_for_cyrillic(text)
elif mode == 'tolat':
charkeys = self.charmap_tolat.keys()
charmap = self.charmap_tolat
text = self._prepare_for_latin(text)
else:
raise ValueError("Mode must be 'tocyr' or 'tolat'.")
# Replace the characters
for letter in charkeys:
if letter in text:
text = text.replace(letter, charmap[letter])
return text