-
Notifications
You must be signed in to change notification settings - Fork 5
/
pinyin.py
69 lines (50 loc) · 1.84 KB
/
pinyin.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
# coding=utf-8
# char codes ref from: http://www.math.nus.edu.sg/aslaksen/read.shtml
TONES = { "1a":"ā", "2a":"á", "3a":"ǎ", "4a":"à",
"1e":"ē", "2e":"é", "3e":"ě", "4e":"è",
"1i":"ī", "2i":"í", "3i":"ǐ", "4i":"ì",
"1o":"ō", "2o":"ó", "3o":"ǒ", "4o":"ò",
"1u":"ū", "2u":"ú", "3u":"ǔ", "4u":"ù",
"1v":"ǖ", "2v":"ǘ", "3v":"ǚ", "4v":"ǜ" }
# using v for the umlauded u
def convert(s):
word_list = []
ret_string = ""
tmp = ""
# split the string by spaces
words = s.split(" ")
# "zhong1 guo2" -> [ ['1', 'zhong'], ['2', 'guo'] ]
for word in words:
word_list.append([word[len(word)-1], word[0:len(word)-1]])
# do the searchy stuff
for word in word_list:
tone = word[0]
pinyin = word[1].lower()
if tone == "5" or pinyin == "":
break
if pinyin.find("a") > -1:
tmp = pinyin.replace("a", TONES[tone+"a"])
elif pinyin.find("e") > -1:
tmp = pinyin.replace("e", TONES[tone+"e"])
elif pinyin.find("ou") > -1:
tmp = pinyin.replace("o", TONES[tone+"o"]+"u")
elif pinyin.find("io") > -1:
tmp = pinyin.replace("io", "i"+TONES[tone+"o"])
elif pinyin.find("iu") > -1:
tmp = pinyin.replace("iu", "i"+TONES[tone+"u"])
elif pinyin.find("ui") > -1:
tmp = pinyin.replace("ui", "u"+TONES[tone+"i"])
elif pinyin.find("uo") > -1:
tmp = pinyin.replace("uo", "u"+TONES[tone+"o"])
elif pinyin.find("i") > -1:
tmp = pinyin.replace("i", TONES[tone+"i"])
elif pinyin.find("o") > -1:
tmp = pinyin.replace("o", TONES[tone+"o"])
elif pinyin.find("u:") > -1:
tmp = pinyin.replace("u:", TONES[tone+"v"])
elif pinyin.find("u") > -1:
tmp = pinyin.replace("u", TONES[tone + "u"])
else:
tmp = pinyin
ret_string += tmp + " "
return ret_string