几种提取关键词的算法(6)

一篇只讲常用算法以及几个简单的实现------原理https://blog.csdn.net/Sakura55/article/details/85122966

具体的算法实现
大杂烩:https://github.com/yongzhuo/nlp_xiaojiang/blob/master/FeatureProject/sentence_sim_feature.py(已经把代码复制到 实训下的短文本相似度计算)

https://blog.csdn.net/rensihui/article/details/82416085?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.nonecase

https://blog.csdn.net/baidu_26550817/article/details/80171532

gensim实现LDA(Latent Dirichlet Allocation)算法提取主题词(topic)
https://blog.csdn.net/limengmingx/article/details/82900781?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.nonecase
主题词有点类似摘要,没有单独的这一算法的运行结果,不知道丢哪去了。

Keras框架(二):实现文本相似度的几种模型(代码)
https://blog.csdn.net/qq_24140919/article/details/89469318?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TextRank算法是一种无监督的关键词提取算法,它利用图论中的PageRank算法进行关键词的计算。其基本思想是:将文本中的每个句子看成图中的一个节点,句子之间的相似度看成边,通过迭代的方式计算出每个节点的重要性,从而得到关键词。 TextRank算法提取关键词的流程: 1. 对文本进行分句,将每个句子看成图中的一个节点; 2. 对每个句子进行分词,去除停用词和无意义的词,只保留名词、动词、形容词等关键词; 3. 构建句子之间的相似度矩阵,相似度的计算可以使用余弦相似度等算法; 4. 将相似度矩阵转化为权重矩阵,使用PageRank算法进行迭代计算,得到每个节点的重要性得分; 5. 对每个句子的重要性得分进行排序,取得分最高的几个句子作为关键词。 下面是使用Python实现TextRank算法提取关键词的示例代码: ```python import jieba import jieba.analyse import numpy as np import networkx as nx # 加载停用词 stopwords = set() with open('stopwords.txt', 'r', encoding='utf-8') as f: for line in f: stopwords.add(line.strip()) # 加载文本数据 with open('input.txt', 'r', encoding='utf-8') as f: text = f.read() # 分句 sentences = text.split('。') # 分词,并去除无意义的词 keywords = [] for sentence in sentences: words = jieba.analyse.textrank(sentence, topK=10, withWeight=True, allowPOS=('n', 'v', 'a')) words = [(word, weight) for word, weight in words if word not in stopwords] keywords.extend(words) # 构建相似度矩阵 similarity_matrix = np.zeros((len(sentences), len(sentences))) for i in range(len(sentences)): for j in range(len(sentences)): if i != j: s1 = set(jieba.cut(sentences[i])) s2 = set(jieba.cut(sentences[j])) similarity_matrix[i][j] = len(s1 & s2) / (len(s1) + len(s2)) # 构建权重矩阵,使用PageRank算法进行计算 nx_graph = nx.from_numpy_matrix(similarity_matrix) scores = nx.pagerank(nx_graph) # 按得分排序,取前N个关键词 keywords = sorted(keywords, key=lambda x: scores[sentences.index(x[0])], reverse=True)[:10] # 输出结果 with open('output.txt', 'w', encoding='utf-8') as f: for keyword, weight in keywords: f.write(keyword + '\t' + str(weight) + '\n') ``` 需要注意的是,这里使用了jieba库的`textrank`函数进行关键词提取,也可以使用其他方法进行分词和关键词提取。同时,相似度矩阵的计算方法也可以根据具体需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值