为什么枚举要实现接口?
397
2022-08-31
Python3之常用包汇总(python中包)
Python包网站: 繁体与简体转换(install hanziconv
示例:
from hanziconv import HanziConvdef switch_hanzi(keyword): """ 汉字转换(简体<>繁体) :return: """ simple_chinese = HanziConv.toSimplified(keyword) if simple_chinese == keyword: # 简体 tradition_chinese = HanziConv.toTraditional(keyword) if tradition_chinese == simple_chinese: return return tradition_chinese else: return simple_chinesekeyword = '生活'print(switch_hanzi(keyword))keyword = '中國'print(switch_hanzi(keyword))
2. 汉字转拼音(install pypinyin
>>> from pypinyin import pinyin, lazy_pinyin, Style>>> pinyin('中心')[['zhōng'], ['xīn']]>>> pinyin('中心', heteronym=True) # 启用多音字模式[['zhōng', 'zhòng'], ['xīn']]>>> pinyin('中心', style=Style.FIRST_LETTER) # 设置拼音风格[['z'], ['x']]>>> pinyin('中心', style=Style.TONE2, heteronym=True)[['zho1ng', 'zho4ng'], ['xi1n']]>>> pinyin('中心', style=Style.TONE3, heteronym=True)[['zhong1', 'zhong4'], ['xin1']]>>> pinyin('中心', style=Style.BOPOMOFO) # 注音风格[['ㄓㄨㄥ'], ['ㄒㄧㄣ']]>>> lazy_pinyin('中心') # 不考虑多音字的情况['zhong', 'xin']
3. 拼音转汉字(install Pinyin2Hanzi
示例:
from Pinyin2Hanzi import DefaultHmmParamsfrom Pinyin2Hanzi import viterbihmmparams = DefaultHmmParams()## 2个候选result = viterbi(hmm_params=hmmparams, observations=('ni', 'zhi', 'bu', 'zhi', 'dao'), path_num = 2)for item in result: print(item.score, item.path)'''输出1.3155294593897203e-08 ['你', '知', '不', '知', '道']3.6677865125992192e-09 ['你', '只', '不', '知', '道']'''
def get_similar_words(word, num=3): """ 获取相似词 :param word: 单词 :param num: 返回词汇数量 """ word_pinyin = lazy_pinyin(word) dagparams = DefaultDagParams() result = dag(dagparams, tuple(word_pinyin), path_num=num) # 拼接,去重 words = set() for item in result: print(item.path) new_word = item.path[0] if len(item.path) == 1 else ''.join(item.path) words.add(new_word) similar_words = words - set([word]) return list(similar_words)
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~