一:pypinyin
作用:汉字转拼音
1
pip3 install pypinyin
使用
from pypinyin import lazy_pinyin,TONE2,TONE
res = lazy_pinyin('先帝创业未半')
print(res) #['xian', 'di', 'chuang', 'ye', 'wei', 'ban']
res = lazy_pinyin('先帝创业未半',style=TONE2)
print(res) #['xia1n', 'di4', 'chua4ng', 'ye4', 'we4i', 'ba4n']
print(''.join(res))#xia1ndi4chua4ngye4we4iba4n
res = lazy_pinyin('先帝创业未半',style=TONE)
print(res) #['xiān', 'dì', 'chuàng', 'yè', 'wèi', 'bàn']
二:jieba
作用:把一句话分成词组
1
pip3 install jieba
使用
import jieba
res = jieba.cut('皇帝打天下打到一半就挂了')
print(res)#<generator object Tokenizer.cut at 0x000001544D390888>
res = list(jieba.cut