日常工作中,有涉及到单个字,词语,句子等文本转成拼音,且有时带声调的拼音文本做数据分析时不太好处理,遂当前有个需求: 批量将文本转换为拼音,且声调转成数字,数字放在单个字拼音后
1、先处理文本转拼音,拼音声调转成数字,数字放在拼音串后面
如:练习 —————》 lian4 xi2
本次主要将网上学习代码结合自己实际需求做整理
①常用的文本转拼音用 pypinyin这个库,具体代码为:
#coding:utf-8
import pypinyin
import re
from string import digits
def ConvertToneNumbersPinyin(lineIn):
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': 'ǜ',
}
assert type(lineIn) is str
lineOut = lineIn
# mapVowelTone2Unicode
for x, y in mapVowelTone2Unicode.items():
lineOut = lineOut.replace(y, x).replace(y.upper(), x.upper())
return lineOut.replace('Ü', 'V').replace('ü', 'v')
def convertDigit(word):
#中文转换为拼音
s = ''
for i in pypinyin.pinyin(word,heteronym=True):
s = s+''.join(i) + " "
#拼音声调转数字
result = ConvertToneNumbersPinyin(s)
#声调数字放拼音后
result2=result.split()
value = ''
for i in result2:
if bool(re.search(r'\d',i)) is True:
aa = re.sub("\D","",i)
else:
pass
bb=i.translate(str.maketrans("","",digits))
dd = bb + aa
value += ' '+dd
print(value)
if __name__ == "__main__":
convertDigit("练习")
测试“练习”这个词语的拼音效果
看来是成功的。
2、批量处理数据,将待转的文本放在excel表中,通过读取表中数据,将测试结果写入表中对应列
本次读写excel文件用的传统的库:xlrd,xlwt ,后续建议使用openpyxl,处理数据量比较大的文件时,优势更明显点。
我的表是这样的:
import xlrd,xlwt,os
#读取excel
excel_path = r'F:\TestScript\待转拼音文本.xlsx'
old_excel = xlrd.open_workbook(excel_path)
old_sheet = old_excel.sheet_by_index(0)
#读取第2列数据
TxtResult = old_sheet.col_values(1)
后面就是将处理好的结果放到excel表中
处理完结果:
贴上完整的代码:
#encoding:utf-8
import pypinyin
import re
from string import digits
import xlrd,xlwt,os
def ConvertToneNumbersPinyin(lineIn):
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': 'ǜ',
}
assert type(lineIn) is str
lineOut = lineIn
# mapVowelTone2Unicode
for x, y in mapVowelTone2Unicode.items():
lineOut = lineOut.replace(y, x).replace(y.upper(), x.upper())
return lineOut.replace('Ü', 'V').replace('ü', 'v')
def convertDigit(word):
#中文转换为拼音
s = ''
for i in pypinyin.pinyin(word,heteronym=True):
s = s+''.join(i) + " "
#拼音声调转数字
result = ConvertToneNumbersPinyin(s)
#声调数字放拼音后
result2=result.split()
value = ''
for i in result2:
global aa,bb,dd
if bool(re.search(r'\d',i)) is True:
aa = re.sub("\D","",i)
else:
pass
bb=i.translate(str.maketrans("","",digits))
dd = bb + aa
value += ' '+dd
return value
def readExcel():
#读取excel
excel_path = r'F:\TestScript\待转拼音文本.xlsx'
old_excel = xlrd.open_workbook(excel_path)
old_sheet = old_excel.sheet_by_index(0)
#读取第2列数据
TxtResult = old_sheet.col_values(1)
#新建表格保存数据
new_workbook = xlwt.Workbook()
new_sheet = new_workbook.add_sheet('拼音识别')
new_sheet.write(0,0,'编号')
new_sheet.write(0,1,'待识别文本')
new_sheet.write(0,2,'拼音文本')
for i in range(1,len(TxtResult)):
pinResult = convertDigit(TxtResult[i])
print(TxtResult[i],pinResult)
if TxtResult[i] is None or TxtResult[i] == 'null':
continue
else:
new_sheet.write(i,0,i)
new_sheet.write(i,1,TxtResult[i])
new_sheet.write(i,2,pinResult)
new_workbook.save(r'F:\TestScript\转换结果.xlsx')
print('已完成')
os.system('pause')
if __name__ == "__main__":
readExcel()
目前问题:因为汉字有多音字,pypinyin这个库会将单个字的所有读音都会返回,若想根据字词语境选择合适读音,可能需要找其他的库,或者其他处理方式。
本次主要还是分享中文转拼音,声调转数字,数字放拼音串后面。