想把 CC-CEDICT 源文件转成字典格式的同学,可以利用我之前写的解析代码和拼音处理函数。
更新:补充 ḿ、m̀ 两个鼻音,比如“呒”字。
更新:补充儿化音。
def pinyinify(pystr):
c1s = {
'A': ['Ā', 'Á', 'Ǎ', 'À', 'A'],
'O': ['Ō', 'Ó', 'Ǒ', 'Ò', 'O'],
'E': ['Ē', 'É', 'Ě', 'È', 'E'],
'a': ['ā', 'á', 'ǎ', 'à', 'a'],
'o': ['ō', 'ó', 'ǒ', 'ò', 'o'],
'e': ['ē', 'é', 'ě', 'è', 'e'],
}
c2s = {
'i': ['ī', 'í', 'ǐ', 'ì', 'i'],
'u': ['ū', 'ú', 'ǔ', 'ù', 'u'],
'ü': ['ǖ', 'ǘ', 'ǚ', 'ǜ', 'ü'],
}
rms = {'r5': 'r', 'm2': 'ḿ', 'm4': 'm̀'}
pys = pystr.split()
results = list()
for pyo in pys:
py = pyo[:]
if py in rms:
result = rms[py]
results.append(result)
continue
if py[-1] not in ('1', '2', '3', '4', '5'):
result = py
results.append(result)
continue
py = py.replace('u:', 'ü')
n = int(py[-1]) - 1
flag = 0
for c in py[:-1]:
if c in c1s:
result = py[:-1].replace(c, c1s[c][n])
results.append(result)
flag = 1
break
if flag == 1:
continue
for c in reversed(py[:-1]):
if c in c2s:
result = py[:-1].replace(c, c2s[c][n])
results.append(result)
flag = 1
break
if flag == 1:
continue
result = pyo
results.append(result)
return ' '.join(results)