python synonyms文本处理:计算词语相似度,提取段落关键词

2023-06-1415:01:58后端程序开发Comments2,033 views1字数 2229阅读模式

python中的synonyms库可以处理中文文本,例如找到词语的相近词,计算词语之间、句子之间的相似度,提取某段新闻的关键词等。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

我们来看一个简单的示例。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

1.安装与下载文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

首先要安装synonyms库,并下载词汇文件words.vector.gz。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

pip install -U synonyms文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

python synonyms文本处理:计算词语相似度,提取段落关键词文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

python synonyms文本处理:计算词语相似度,提取段落关键词文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

python -c "import synonyms" #下载word vector文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

词汇文件下载地址:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

https://gitee.com/chatopera/cskefu/attach_files/610602/download/words.vector.gz文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

#查看当前库的信息文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

synonyms.describe()#从描述中能够看到当前的词汇量。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

2.寻找相近词文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

import synonyms #首次使用时会自动下载words.vector.gz文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

#synonyms.nearby(“str”, size = 10)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

可以自动返回与该词语相近的词以及相关的程度(取值在0到1之间),size为相近词的个数,默认为10个。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

print("快乐: ", synonyms.nearby("快乐"))文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

print("平凡: ", synonyms.nearby("平凡"))文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

快乐:  (['快乐', '欢乐', '幸福', '欢笑', '青春', '喜悦', '无忧无虑', '喜乐', '纯真', '圣洁'], [1.0, 0.7718689, 0.7449047, 0.67883134, 0.67068595, 0.6529962, 0.6448968, 0.6370579, 0.6242799, 0.6103234])
平凡:  (['平凡', '理想', '孤独', '默默无闻', '朴素', '纯洁', '平庸', '无私', '平凡人', '张扬'], [1.0, 0.6413613, 0.60000455, 0.5960809, 0.59234023, 0.57126236, 0.5700199, 0.5690233, 0.56774443, 0.54518956])文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

synonyms.display("财富",size=12)#打印12个相近词文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

'财富'近义词:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

  1. 财富:1.0文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

  2. 储蓄:0.631402文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

  3. 社会财富:0.6189183文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

  4. 私人银行:0.56370497文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

  5. 投资理财:0.5611471文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

  6. 金融资产:0.55666083文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

  7. 银行存款:0.52110213文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

  8. 资产投资:0.5142404文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

  9. 增值:0.5110868文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

  10. 中产阶层:0.50663656文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

  11. 金融投资:0.50064766文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

  12. 创造财富:0.49771798文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

从结果中可以看到与“财富”相近的词语以及各词的分数。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

3.计算两个句子的相关度文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

synonyms.compare()函数用于计算两个句子的相似度,越接近1表示相似度越高,seg选择是否对句子进行分词,默认true。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

sen1 = "明月几时有?把酒问青天。"文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

sen2 = "举杯邀明月,对影成三人。"文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

r = synonyms.compare(sen1, sen2, seg=True)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

print("sentence1:",sen1)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

print("sentence2:",sen2)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

print("相似度",r)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

python synonyms文本处理:计算词语相似度,提取段落关键词文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

python synonyms文本处理:计算词语相似度,提取段落关键词文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

python synonyms文本处理:计算词语相似度,提取段落关键词文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

4.获取词向量文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

synonyms.v(word)中输入具体的词语,可以查看该词语的数值向量形式。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

print("蓝天",synonyms.v("蓝天"))文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

synonyms.sv(sentence , ignore=False)查看句子的向量,当句子为词表外的句子时,ignore=False会随机生成一个向量。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

sentence1= "蓝天白云,青山绿水,还有清风吹斜阳。"文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

print(sentence1,synonyms.sv(sentence1,ignore=False))python synonyms文本处理:计算词语相似度,提取段落关键词文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

5.中文分词文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

sentence2="我家院子里有两棵树,一棵是枣树,另一棵也是枣树。"文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

print(sentence2,synonyms.seg(sentence2))文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

分词后将得到词语以及其对应的词性。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

(['我家', '院子', '里', '有', '两棵树', ',', '一棵', '是', '枣树', ',', '另', '一棵', '也', '是', '枣树', '。'], ['r', 'n', 'f', 'v', 'ns', 'x', 'm', 'v', 'n', 'x', 'r', 'm', 'd', 'v', 'n', 'x'])文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

6.提取关键词文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

synonyms.keywords(sentence [, topK=5, withWeight=False])文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

默认提取前5个重要的关键词。可以通过withWeight手动设置权重。比如提取某篇新闻的关键词。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

sentence3 ="9月15日以来,台积电、高通、三星等华为的重要合作伙伴,只要没有美国的相关许可证,都无法供应芯片给华为,而中芯国际等国产芯片企业,也因采用美国技术,而无法供货给华为。目前华为部分型号的手机产品出现货少的现象,若该形势持续下去,华为手机业务将遭受重创。"文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

keywords = synonyms.keywords(sentence3)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

print("sentence:",sentence3)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

print("keywords",keywords)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

keywords ['华为', '芯片', '手机', '15', '台积电']文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

设置参数topK=10,提取10个关键词。python synonyms文本处理:计算词语相似度,提取段落关键词文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47036.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/47036.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定