关键词近义词/同义词去重
关键词近义词/同义词去重python脚本
-
通过结巴分词结合百度停止词库对txt文本中的关键词进行分词、去停止词,结果组成新的关键词,然后对整个txt文件关键词进行去重,最后输出结果为处理后的关键词或对应的输入关键词。实例如下:
-
关键词:
哈士奇价格
哈士奇价格多少
哈士奇价格怎么样
金毛价格 -
结果:
哈士奇价格
金毛价格 -
脚本如下:
import jieba# 加载百度停用词表
def load_stopwords(file_path):stopwords = set()with open(file_path, 'r', encoding='utf-8') as file:for line in file:stopwords.add(line.strip())return stopwords# 对文本进行分词并去除停用词
def segment_text(text, stopwords):words = jieba.cut(text)filtered_words = [word for word in words if word not in stopwords and len(word) > 1]return filtered_words# 主函数
def main():stopwords = load_stopwords('baidu_stopwords.txt') # 百度停用词表文件路径input_file_path = 'input_text.txt' # 输入文本文件路径output_file_path = 'output_keywords.txt' # 输出关键词文本文件路径# 读取输入文本with open(input_file_path, 'r', encoding='utf-8') as input_file:lines = input_file.readlines()# 逐行处理文本unique_keywords = set()for line in lines:keywords = segment_text(line, stopwords)unique_keywords.add("".join(keywords))# 保存去重后的关键词到输出文本文件with open(output_file_path, 'w', encoding='utf-8') as output_file:output_file.write("\n".join(unique_keywords))if __name__ == "__main__":main()
如有不明白,可联系我:(vx)zhang514221
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
