###########################拼音音调转数字#############################################
#encoding:utf-8
#参考https://blog.csdn.net/wanghuafengc/article/details/19829119
# map (final) constanant+tone to tone+constanan
mapConstTone2ToneConst = {'n1': '1n',
'n2': '2n',
'n3': '3n',
'n4': '4n',
'ng1': '1ng',
'ng2': '2ng',
'ng3': '3ng',
'ng4': '4ng',
'r1': '1r',
'r2': '2r',
'r3': '3r',
'r4': '4r'}
# map vowel+vowel+tone to vowel+tone+vowel
mapVowelVowelTone2VowelToneVowel = {'ai1': 'a1i',
'ai2': 'a2i',
'ai3': 'a3i',
'ai4': 'a4i',
'ao1': 'a1o',
'ao2': 'a2o',
'ao3': 'a3o',
'ao4': 'a4o',
'ei1': 'e1i',
'ei2': 'e2i',
'ei3': 'e3i',
'ei4': 'e4i',
'ou1': 'o1u',
'ou2': 'o2u',
'ou3': 'o3u',
'ou4': 'o4u'}
# map vowel-number combination to unicode
mapVowelTone2Unicode = {'a1': 'ā',
'a2': 'á',
'a3': 'ǎ',
'a4': 'à',
'e1': 'ē',
'e2': 'é',
'e3': 'ě',
'e4': 'è',
'i1': 'ī',
'i2': 'í',
'i3': 'ǐ',
'i4': 'ì',
'o1': 'ō',
'o2': 'ó',
'o3': 'ǒ',
'o4': 'ò',
'u1': 'ū',
'u2': 'ú',
'u3': 'ǔ',
'u4': 'ù',
'v1': 'ǜ',
'v2': 'ǘ',
'v3': 'ǚ',
'v4': 'ǜ',
}
def ConvertToneNumbersPinyin(lineIn):
assert type(lineIn) is str
lineOut = lineIn
# mapVowelTone2Unicode
for x, y in mapVowelTone2Unicode.items():
lineOut = lineOut.replace(y, x).replace(y.upper(), x.upper())
# mapVowelVowelTone2VowelToneVowel
for x, y in mapVowelVowelTone2VowelToneVowel.items():
lineOut = lineOut.replace(y, x).replace(y.upper(), x.upper())
# first transform
for x, y in mapConstTone2ToneConst.items():
lineOut = lineOut.replace(y, x).replace(y.upper(), x.upper())
return lineOut.replace('Ü', 'V').replace('ü', 'v')
if __name__ == '__main__':
file=open("jyc.txt","r")
for line in file:
#lineIn = "shēn-qiǎn"
#lineOut = ConvertToneNumbersPinyin(lineIn)
lineOut = ConvertToneNumbersPinyin(line)
print lineOut
#############################统计###########################################################
#coding:utf-8
import re
file=open("jyc-num.txt",'r')
def pz(num):
if int(num)==3 or int(num)==4:
return "z"
else:
return "p"
pcount=0
zcount=0
pzcount=0
ppcount=0
zzcount=0
for line in file:
result=re.findall(r"\d",line)
resultlen=len(result)
first=result[resultlen/2-1]
last=result[-1]
fy=pz(first)
ly=pz(last)
if fy=="z":
zcount+=1
else:
pcount+=1
if ly=="p":
zcount+=1
else:
pcount+1
if fy==ly and fy=="p":
ppcount+=1
if fy==ly and fy=="z":
zzcount+=1
if fy!=ly:
pzcount+=1
print "平",pcount
print "仄",zcount
print "平仄",pzcount
print "平平",ppcount
print "仄仄",zzcount











网友评论