-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinyin_utils.py
161 lines (127 loc) · 4.57 KB
/
pinyin_utils.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
# pinyin_utils.py
import re, pytest, pprint
hConv = {} # cached conversions
__version__ = "0.3.0"
# ---------------------------------------------------------------------------
def getVersion():
return __version__
# ---------------------------------------------------------------------------
def convertPinyin(s, *, debug=False):
assert type(s) == str
retval = ''
for match in re.finditer('([^a-z]*)([a-z]+)([0-5])?', s):
new = None
(other, base, tone) = match.groups()
assert tone is None or type(tone) == str
if (tone is None) or (tone == '') or (tone == '5') or (tone == '0'):
# --- There is no 'v' in pinyin, so this is safe
# By convention, 'v' means 'ü'
new = base.replace('v','ü')
if debug:
print(f"CONV '{base}' => '{new}' - neutral tone")
else:
index = int(tone) - 1
if base in hConv:
new = hConv[base][index]
if debug:
print(f"CONV '{base}' => '{new}' - found in hConv")
else:
# --- Set these:
# ch is the vowel we want to add accent to
# pos is the position in string 'base' where it's found
(ch, pos) = (None, -1) # set these
# --- check for 2 common exceptions
posIU = base.find('iu')
posIO = base.find('io')
if posIU != -1:
(ch, pos) = ('u', posIU + 1)
if debug:
print("...IU exception")
elif posIO != -1:
(ch, pos) = ('o', posIO + 1)
if debug:
print("...IO exception")
else:
# --- Find lowest vowel and position
for vowel in ('a','e','i','o','u','ü','v'):
pos = base.find(vowel)
if pos > -1:
ch = base[pos]
if debug:
print(f"...found vowel '{vowel}' at pos {pos}")
break # from this "for vowel" loop
if not ch or (pos == -1):
raise Exception(f"No vowel found in string '{base}'")
# --- fill in hConv so it's quicker to find next time
new = strWithCharReplaced(base, pos, toned(ch, index))
hConv[base] = _getPinyinList(base, ch, pos)
if debug:
print(f"CONV '{base}' => '{new}' - add to hConv")
retval += other + new
return retval
# ---------------------------------------------------------------------------
def toned(ch, index):
assert (len(ch) == 1) and (ch in ('a','e','i','o','u','ü','v'))
assert (index >= 0) and (index < 4)
if ch == 'a':
return ('ā', 'á', 'ǎ', 'à')[index]
elif ch == 'e':
return ('ē', 'é', 'ě', 'è')[index]
elif ch == 'i':
return ('ī', 'í', 'ǐ', 'ì')[index]
elif ch == 'o':
return ('ō', 'ó', 'ǒ', 'ò')[index]
elif ch == 'u':
return ('ū', 'ú', 'ǔ', 'ù')[index]
else:
return ('ǖ', 'ǘ', 'ǚ', 'ǜ')[index]
# ---------------------------------------------------------------------------
def strWithCharReplaced(s, pos, newch):
assert type(s) == str
assert pos >= 0
assert pos < len(s)
return s[:pos] + newch + s[pos+1:]
# ---------------------------------------------------------------------------
def _getPinyinList(untoned, ch, pos):
lPinyin = []
for i in (0,1,2,3):
lPinyin.append(strWithCharReplaced(untoned, pos, toned(ch, i)))
return lPinyin
# ---------------------------------------------------------------------------
# Unit Tests
# ---------------------------------------------------------------------------
def test_1():
assert strWithCharReplaced('abc', 0, 'X') == 'Xbc'
assert strWithCharReplaced('abc', 1, 'X') == 'aXc'
assert strWithCharReplaced('abc', 2, 'X') == 'abX'
with pytest.raises(AssertionError):
strWithCharReplaced(42, 1, 'X') == 'aXc'
with pytest.raises(AssertionError):
strWithCharReplaced('abc', 3, 'X') == 'aXc'
with pytest.raises(AssertionError):
strWithCharReplaced('abc', -1, 'X') == 'aXc'
def test_2():
assert toned('a', 0) == 'ā'
assert toned('o', 2) == 'ǒ'
with pytest.raises(AssertionError):
toned('x', 2)
with pytest.raises(AssertionError):
toned('a', 4)
with pytest.raises(AssertionError):
toned('a', -1)
def test_3():
print()
assert convertPinyin('liu3') == 'liǔ'
assert convertPinyin('wo3') == 'wǒ'
assert convertPinyin('xiong2') == 'xióng'
assert convertPinyin('san1ming2zhi4') == 'sānmíngzhì'
# --- Do each one again - it should get them from cache
assert convertPinyin('liu3') == 'liǔ'
assert convertPinyin('wo3') == 'wǒ'
assert convertPinyin('xiong2') == 'xióng'
assert convertPinyin('san1ming2zhi4') == 'sānmíngzhì'
def not_test_4():
global hConv
# --- This only works if your console support unicode
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(hConv)