python同义词替换的实现(jieba分词)
TihuanWords.txt文档格式
注意:同一行的词用单个空格隔开,每行第一个词为同行词的替换词。
年休假 年假 年休
究竟 到底
回家场景 我回来了
代码
import jieba def replaceSynonymWords(string1): # 1读取同义词表,并生成一个字典。 combine_dict = {} # synonymWords.txt是同义词表,每行是一系列同义词,用空格分割 for line in open("TihuanWords.txt", "r", encoding='utf-8'): seperate_word = line.strip().split(" ") num = len(seperate_word) for i in range(1, num): combine_dict[seperate_word[i]] = seperate_word[0] print(seperate_word) print(combine_dict) # 2提升某些词的词频,使其能够被jieba识别出来 jieba.suggest_freq("年休假", tune=True) # 3将语句切分成单词 seg_list = jieba.cut(string1, cut_all=False) f = "/".join(seg_list).encode("utf-8") f = f.decode("utf-8") print(f) # 4返回同义词替换后的句子 final_sentence = " " for word in f.split('/'): if word in combine_dict: word = combine_dict[word] final_sentence += word else: final_sentence += word # print final_sentence return final_sentence string1 = '年休到底放几天?' print(replaceSynonymWords(string1))
结果
相关推荐
zhangchaoming 2019-12-26
aspaspx 2019-10-21
tangh 2019-04-27
中草药代码 2009-07-29
wenchanter 2019-06-21
windsky 2011-08-17
iceghostsc 2016-05-15
无限未来 2015-07-21
enjoyandenjoy 2013-11-26
盖世小可爱 2013-06-22
kkikiako 2013-06-21
DCLCSDN 2013-01-10
lester 2012-11-02
anshenoracle 2012-05-22
oracleKing 2012-03-20
oracleerp 2011-08-28
执米之手与米偕老 2011-04-11
午后红茶 2010-06-29