]> git.phdru.name Git - m_lib.git/blob - m_lib/rus/rus2lat.py
Add requirements.txt and setup.cfg
[m_lib.git] / m_lib / rus / rus2lat.py
1 #! /usr/bin/env python
2 # -*- coding: koi8-r -*-
3
4 #
5 # Rus -> Lat transliteration (koi2lat and win2lat)
6 #
7
8 koi2lat_d = {
9    "�": "A",
10    "�": "B",
11    "�": "V",
12    "�": "G",
13    "�": "D",
14    "�": "E",
15    "�": "Zh",
16    "�": "Z",
17    "�": "I",
18    "�": "Y",
19    "�": "K",
20    "�": "L",
21    "�": "M",
22    "�": "N",
23    "�": "O",
24    "�": "P",
25    "�": "R",
26    "�": "S",
27    "�": "T",
28    "�": "U",
29    "�": "F",
30    "�": "H",
31    "�": "Ts",
32    "�": "Ch",
33    "�": "Sh",
34    "�": "Sh",
35    "�": "'",
36    "�": "'",
37    "�": "Y",
38    "�": "E",
39    "�": "Yu",
40    "�": "Ya",
41    "�": "a",
42    "�": "b",
43    "�": "v",
44    "�": "g",
45    "�": "d",
46    "�": "e",
47    "�": "zh",
48    "�": "z",
49    "�": "i",
50    "�": "y",
51    "�": "k",
52    "�": "l",
53    "�": "m",
54    "�": "n",
55    "�": "o",
56    "�": "p",
57    "�": "r",
58    "�": "s",
59    "�": "t",
60    "�": "u",
61    "�": "f",
62    "�": "h",
63    "�": "ts",
64    "�": "ch",
65    "�": "sh",
66    "�": "sh",
67    "�": "'",
68    "�": "'",
69    "�": "y",
70    "�": "e",
71    "�": "yu",
72    "�": "ya"
73 }
74
75 def make_xxx2lat(encoding="cp1251"):
76    d = {}
77    for k, v in koi2lat_d.items():
78       k = unicode(k, "koi8-r").encode(encoding)
79       d[k] = v
80    return d
81
82
83 from m_lib.lazy.dict import LazyDictInitFunc
84 win2lat_d = LazyDictInitFunc(make_xxx2lat, encoding="cp1251")
85
86
87 def rus2lat(instr, rus2lat_d = koi2lat_d):
88    out = []
89    for c in instr:
90       out.append(rus2lat_d.get(c, c))
91    return ''.join(out)
92
93
94 koi2lat = rus2lat
95
96 def win2lat(instr):
97    return rus2lat(instr, win2lat_d)
98
99
100 if __name__ == "__main__":
101    Test = "�������� ����� �����������. ��� xyz ��� ����� �����"
102    print "Test:", Test
103    print "����:", koi2lat(Test)
104    print "����:", win2lat(unicode(Test, "koi8-r").encode("cp1251"))