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))

结果

python同义词替换的实现(jieba分词)

相关推荐