from pypinyin import pinyin, Style
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
# 带声调的(默认)
def yinjie(word):
pinyin_list = []
# heteronym=True开启多音字
for old_pinyin_list in pinyin(word, heteronym=True, style=Style.TONE2):
for old_pinyin in old_pinyin_list:
s = ''
num = ''
for j in old_pinyin:
if is_number(j):
num = j
else:
s += j
s += num
pinyin_list.append(s)
return pinyin_list