基于TextRank算法提取关键词——Java实现
来源:互联网 发布:php websocket server 编辑:程序博客网 时间:2024/05/23 00:04
依赖
<dependency> <groupId>com.janeluo</groupId> <artifactId>ikanalyzer</artifactId> <version>2012_u6</version> </dependency>
代码
import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.TokenStream;import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;import org.apache.lucene.analysis.tokenattributes.TypeAttribute;import org.wltea.analyzer.lucene.IKAnalyzer;import java.io.IOException;import java.io.StringReader;import java.util.*;/** * @author yuyufeng * @date 2017/11/3 */public class Demo { public static void main(String[] args) { List<String> keyWords = new ArrayList<>(); int k = 2; //窗口大小/2 float d = 0.85f; /** * 标点符号、常用词、以及“名词、动词、形容词、副词之外的词” */ Set<String> stopWordSet = new HashSet<String>(); stopWordSet.add("是"); stopWordSet.add("的"); stopWordSet.add("地"); stopWordSet.add("从"); stopWordSet.add("将"); stopWordSet.add("但"); stopWordSet.add("都"); stopWordSet.add("和"); stopWordSet.add("为"); stopWordSet.add("让"); stopWordSet.add("在"); stopWordSet.add("由"); stopWordSet.add("上"); String field = "PageRank近似于一个用户,是指在Internet上随机地单击链接将会到达特定网页的可能性。通常,能够从更多地方到达的网页更为重要,因此具有更高的PageRank。每个到其他网页的链接,都增加了该网页的PageRank。具有较高PageRank的网页一般都是通过更多其他网页的链接而提高的。"; Analyzer analyzer = new IKAnalyzer(true); TokenStream ts = null; //分词 try { ts = analyzer.tokenStream("myfield", new StringReader(field)); OffsetAttribute offset = (OffsetAttribute) ts.addAttribute(OffsetAttribute.class); CharTermAttribute term = (CharTermAttribute) ts.addAttribute(CharTermAttribute.class); TypeAttribute type = (TypeAttribute) ts.addAttribute(TypeAttribute.class); ts.reset(); while (ts.incrementToken()) { if (!stopWordSet.contains(term.toString())) { keyWords.add(term.toString()); } } ts.end(); } catch (IOException var14) { var14.printStackTrace(); } finally { if (ts != null) { try { ts.close(); } catch (IOException var13) { var13.printStackTrace(); } } } Map<String, Set<String>> relationWords = new HashMap<>(); //获取每个关键词 前后k个的组合 for (int i = 0; i < keyWords.size(); i++) { String keyword = keyWords.get(i); Set<String> keySets = relationWords.get(keyword); if (keySets == null) { keySets = new HashSet<>(); relationWords.put(keyword, keySets); } for (int j = i - k; j <= i + k; j++) { if (j < 0 || j >= keyWords.size() || j == i) { continue; } else { keySets.add(keyWords.get(j)); } } } /* for (String s : relationWords.keySet()) { System.out.print(s+" "); for (String s1 : relationWords.get(s)) { System.out.print(s1+" "); } System.out.println(); }*/ Map<String, Float> score = new HashMap<>(); float min_diff = 0.1f; //差值最小 int max_iter = 100;//最大迭代次数 //迭代 for (int i = 0; i < max_iter; i++) { Map<String, Float> m = new HashMap<>(); float max_diff = 0; for (String key : relationWords.keySet()) { Set<String> value = relationWords.get(key); //先给每个关键词一个默认rank值 m.put(key, 1 - d); //一个关键词的TextRank由其它成员投票出来 for (String other : value) { int size = relationWords.get(other).size(); if (key.equals(other) || size == 0) { continue; } else { m.put(key, m.get(key) + d / size * (score.get(other) == null ? 0 : score.get(other))); } }// System.out.println("m.get(key):"+m.get(key)+" score:"+(score.get(key) == null ? 0 : score.get(key))); max_diff = Math.max(max_diff, Math.abs(m.get(key) - (score.get(key) == null ? 0 : score.get(key)))); } score = m; if (max_diff <= min_diff) { System.out.println("迭代次数:" + i); break; } } List<Score> scores = new ArrayList<>(); for (String s : score.keySet()) { Score score1 = new Score(); score1.key = s; score1.significance = score.get(s); scores.add(score1); } scores.sort(new Comparator<Score>() { @Override public int compare(Score o1, Score o2) { if (o2.significance - o1.significance > 0) { return 1; } else { return -1; } } }); for (Score score1 : scores) { System.out.println(score1); } }}class Score { String key; float significance; @Override public String toString() { return "关键词=" + key + ", 重要程度=" + significance; }}
运行结果
迭代次数:11关键词=网页, 重要程度=2.8311346
关键词=链接, 重要程度=1.646728
关键词=pagerank, 重要程度=1.6038197
关键词=更多, 重要程度=1.2489531
关键词=到达, 重要程度=1.1083827
关键词=具有, 重要程度=0.98187566
关键词=其他, 重要程度=0.9651773
关键词=用户, 重要程度=0.81595975
关键词=指在, 重要程度=0.8086006
关键词=internet, 重要程度=0.80388165
关键词=一个, 重要程度=0.787644
关键词=随机, 重要程度=0.7764552
关键词=单击, 重要程度=0.76052386
关键词=将会, 重要程度=0.71690917
关键词=能够, 重要程度=0.7066941
关键词=可能性, 重要程度=0.70503104
关键词=更高, 重要程度=0.7045265
关键词=每个, 重要程度=0.7005399
关键词=特定, 重要程度=0.6963727
关键词=通过, 重要程度=0.69495517
关键词=因此, 重要程度=0.69311315
关键词=通常, 重要程度=0.69245243
关键词=该, 重要程度=0.6918771
关键词=一般, 重要程度=0.6895788
关键词=都是, 重要程度=0.686642
关键词=到, 重要程度=0.68152785
关键词=更为重要, 重要程度=0.68064004
关键词=地方, 重要程度=0.6771895
关键词=近似于, 重要程度=0.6137907
关键词=而, 重要程度=0.594995
关键词=增加了, 重要程度=0.5508093
关键词=较高, 重要程度=0.5392841
关键词=提高, 重要程度=0.44995427
- 基于TextRank算法提取关键词——Java实现
- TextRank算法提取关键词的Java实现
- TextRank算法提取关键词的Java实现
- TextRank 算法提取关键词的 Java 实现
- 基于TextRank的关键词提取算法
- 关键词提取算法-TextRank
- 关键词提取之TextRank算法
- textrank 关键词提取-python实现
- 基于TextRank的关键词、短语、摘要提取
- 基于TextRank的关键词、短语、摘要提取
- [NLP技术]关键词提取算法-TextRank
- textrank关键词提取
- textrank提取文档关键词
- textrank算法原理与提取关键词、自动提取摘要PYTHON
- 短文本关键词提取算法RAKE & TextRank及改进
- TextRank算法抽取关键词
- 笔记-TextRank与关键词提取
- 文章提取关键词_jieba(IF-IDF/TextRank)
- 消息队列的使用场景
- Oracle表分区分为四种:范围分区,散列分区,列表分区和复合分区
- 数组题目练习
- 测试建模:功能列表(Function List)
- 获取F7值对象的详细信息
- 基于TextRank算法提取关键词——Java实现
- javamail 发邮件
- URI编码与解码
- Wireshark初探之基本介绍和TCP 3次握手数据包捕捉分析
- 微信小程序UI组件、开发框架、实用库...
- AVL二叉平衡查找树
- 【LeetCode 7】Reverse Integer(Python)
- Intrinsic function
- 【opencv】opencv3.3的DNN模块调用caffe训练结果——【caffe学习五】