java 文本分析 关键词提取,利用ANSJ进行文本关键词提取
以前已经提到过使用FudanNLP进行新闻关键词提取,无奈组长不满意因而换成了ictclas,在个人ubuntu13.04上面ictclas跑得很好,惋惜到别人的机器上就报错,没办法,只好再一次换工具,通过晓阳童鞋推荐,此次换成了ANSJ,听说这个工具就是早先ictclas的JAVA版本。java
这个工具的Github地址是这里:https://github.com/ansjsun/ansj_seg 须要看源码的本身前往。下面说说如何使用这个工具提取关键词。git
1-下载JAR包github
显然下载JAR包是最省事的方法,固然你也能够把Github上面的工程加进来,无奈JAR包找了很久未找到,最后只好向做者索取,目前已经上传到CSDN了,欢迎下载:http://download.csdn.net/detail/jj12345jj198999/6020541ubuntu
2-自定义keyword类网络
虽然ANSJ中已经实现了关键词提取,不过输出时并无给出每个关键词的权重,因而只好手动修改keyword类,好在Github上面的源码中已经定义了权重成员,咱们只须要增长一个Get函数便可。app
publicclassKeywordimplementsComparable {
privateString name;
privatedoublescore;
privatedoubleidf;
privateintfreq;
publicKeyword(String name,intdocFreq,intweight) {
this.name = name;
this.idf = Math.log(10000+10000.0/ (docFreq +1));
this.score = idf * weight;
freq++;
}
publicvoidupdateWeight(intweight) {
this.score += weight * idf;
freq++;
}
publicintcompareTo(Keyword o) {
if(this.score
return1;
} else{
return-1;
}
}
publicbooleanequals(Object obj) {
// TODO Auto-generated method stub
if(objinstanceofKeyword) {
Keyword k = (Keyword) obj;
returnk.name.equals(name);
} else{
returnfalse;
}
}
publicString toString() {
returnname;
}
//look here ******************************************************
publicdoublegetScore(){
returnscore;
}
publicintgetFreq() {
returnfreq;
}
}
除了这个以外,咱们还须要原封不动地把keywordcomputer类重抄一遍,随叫这两个类息息相关呢,要么都在JAR中要么就一块儿跳出来。对于这个类,实在不知道计算keyword时为什么要知道title的长度,做者告诉我这不是他写的也不知道原因,我猜Github上面添加这个模块的人确定认为出如今标题中的词须要赋予较大的权重吧,也许这个想法是对的。
importjava.util.ArrayList;
importjava.util.Collection;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
importjava.util.TreeSet;
importorg.ansj.app.newWord.LearnTool;
importorg.ansj.domain.Term;
importorg.ansj.recognition.NatureRecognition;
importorg.ansj.splitWord.analysis.NlpAnalysis;
publicclassKeyWordComputer {
privateintnKeyword =10;
//default constructor keyword number=10
publicKeyWordComputer() {
nKeyword = 10;
}
// constructor set keyword number
publicKeyWordComputer(intnKeyword) {
this.nKeyword = nKeyword;
}
//get keywords object list
privateList computeArticleTfidf(String content,inttitleLength) {
Map tm = newHashMap();
LearnTool learn = newLearnTool();
List parse = NlpAnalysis.parse(content, learn);
parse = NlpAnalysis.parse(content, learn);
for(Term term : parse) {
intweight = getWeight(term, content.length(), titleLength);
if(weight ==0)
continue;
Keyword keyword = tm.get(term.getName());
if(keyword ==null) {
keyword = newKeyword(term.getName(), term.getNatrue().allFrequency, weight);
tm.put(term.getName(), keyword);
} else{
keyword.updateWeight(1);
}
}
TreeSet treeSet = newTreeSet(tm.values());
ArrayList arrayList = newArrayList(treeSet);
if(treeSet.size()
returnarrayList;
} else{
returnarrayList.subList(0, nKeyword);
}
}
//get keywords,need title and content
publicCollection computeArticleTfidf(String title, String content) {
returncomputeArticleTfidf(title +"\t"+ content, title.length());
}
//get keywords, just need content
publicCollection computeArticleTfidf(String content) {
returncomputeArticleTfidf(content,0);
}
//get keywords weight
privateintgetWeight(Term term,intlength,inttitleLength) {
if(term.getName().matches("(?s)\\d.*")) {
return0;
}
if(term.getName().trim().length() <2) {
return0;
}
String pos = term.getNatrue().natureStr;
if(!pos.startsWith("n") ||"num".equals(pos)) {
return0;
}
intweight =0;
if(titleLength > term.getOffe()) {
return20;
}
// position
doubleposition = (term.getOffe() +0.0) / length;
if(position <0.05)
return10;
weight += (5-5* position);
returnweight;
}
}
3-testdom
最后就是写一个类测试,import一堆东西ide
packagecom.zengxiaosen;importjava.io.BufferedReader;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.util.Iterator;/*** Created by zengxiaosen on 16/10/17.* 提取关键词语*/public classKeyWordtest {
public static voidmain(String[] args) throwsIOException{
String filePath = "./test-utf8.TXT";String tt = newString();BufferedReader in = newBufferedReader(newInputStreamReader(newFileInputStream(filePath)));String str;while((str = in.readLine()) != null){
tt += str;}
test1(tt);}
public static voidtest1(String content){
KeyWordComputer key = newKeyWordComputer(10);Iterator it = key.computeArticleTfidf(content).iterator();while(it.hasNext()){
Keyword key2 = (Keyword)it.next();System.out.println(key2.toString()+key2.getScore());}
}
}
在测试时有一点须要注意,ANSJ目前只支持UTF-8格式的字符串,其余格式的运行时会报JAVA GC错误,做者说由于处理过程当中是乱码,程序觉得都是新词致使内存崩溃了,但愿他在之后的版本中加以改进吧。最后给出输出结果,这里是按照权重排序的:
init ambiguity waring :library/ambiguity.dic because : not find that file or can not to read !
init core library ok use time :3983
init ngram ok use time :2023
屌丝528.8693014046396
李毅202.62858475668492
网络174.9965471938941
球迷110.52413506982782
群体110.52413506982782
人人110.52413506982782
名号101.31379048067551
高富帅92.10390216212956
满屏92.10390216212954
网友92.1034458915232
*************************
社会主义1446.0241004969153
社会1326.289620837935
中国1096.0347881537828
人民1049.9792831633652
文化874.9827359694709
经济874.9827359694709
特点847.3517022020139
制度801.2999792562523
体系746.0379117213383
国家598.6723982949011
java 文本分析 关键词提取,利用ANSJ进行文本关键词提取相关推荐
- 对爬取的电影《唐人街探案3》豆瓣短评做文本分析,并利用stylecloud制作词云图
上一节,我们爬取了豆瓣上电影<唐人街探案3>的评论,并将评论保存成了txt文档,爬虫过程可参考:爬取某瓣上电影<唐人街探案3>的评论 本节,我们将对电影短评做文本分析,并利用s ...
- 自然语言处理文本分析_通过自然语言处理释放文本分析的力量
自然语言处理文本分析 深度学习 , 自然语言处理 (Deep Learning, Natural Language Processing) Natural language is a language ...
- 利用Ansj进行新闻关键词提取
之前已经提到过使用FudanNLP进行新闻关键词提取,无奈组长不满意于是换成了ictclas,在我的ubuntu13.04上面ictclas跑得很好,可惜到别人的机器上就报错,没办法,只好再一次换工具 ...
- python中文文本分析_python使用snownlp进行中文文本处理以及分词和情感分析 - pytorch中文网...
SnowNLP: 一个简单的中文文本处理库 SnowNLP是一个python写的类库,可以方便的处理中文文本内容,是受到了TextBlob的启发而写的,由于现在大部分的自然语言处理库基本都是针对英文的 ...
- 自然语言处理︱简述四大类文本分析中的“词向量”(文本词特征提取)
笔者在看各种NLP的论文.文献.博客之中发现在应用过程中,有种类繁多的词向量的表达.笔者举例所看到的词向量有哪些. 词向量一般被看做是文档的特征,不同词向量有不同的用法,本文介绍了四类词向量: Has ...
- python中文文本分析_基于CNN的中文文本分类算法(可应用于垃圾邮件过滤、情感分析等场景)...
基于cnn的中文文本分类算法 简介 参考IMPLEMENTING A CNN FOR TEXT CLASSIFICATION IN TENSORFLOW实现的一个简单的卷积神经网络,用于中文文本分类任 ...
- 数据分析中,文本分析远比数值型分析重要!(上)
温馨提示:本文是<数据分析中,文本分析远比数值型分析重要!>的上篇,聊的是文本分析的一些基本知识,下篇将以一个实际案例来聊聊基于大数据的文本分析是如何应用在商业场景中的,将于明天推送,敬请 ...
- 英雄联盟手游有EDG那么牛吗?——基于评论的文本分析证据
注:本科课程作业,水平有限.Python代码东拼西凑但是可以跑哈哈哈,需要可私聊1310778864@qq.com 内 容 摘 要 本文选取了电子竞技这一较为新颖的范畴,以中国战队EDG夺得英雄联盟全 ...
- 教你利用python处理文本
我是个C/C++ Coder,并不是专业写python的,python lua其实属于一类语言,只不要python的库更多,强大之处并不完全是python语言特性,而且扩展库比较多,请允许我黑一下py ...
最新文章
- SCI论文投稿全程模板
- 【读书笔记】代码可为维护性标准(一)
- Java多线程之线程并发库阻塞队列的应用
- 针对十类数据从业人员,最好的工具推荐
- angular 模块构建_我如何在Angular 4和Magento上构建人力资源门户
- 【Clickhouse】Clickhouse 外部存储引擎之 MySQL
- mysql删除记录后id不连续_小水玩转Mysql---Mysql跟踪sql记录
- 比特币总量是2100万吗?为什么?
- 《Java Web开发入门很简单》学习笔记
- 网络中各层协议(7层)
- IOS 本地图片加载
- 非合作博弈篇——非合作博弈的矩阵表示
- Hutool - 身份证验证及其信息解读封装
- 浏览器可下载的网页链接(百度云链接等),而迅雷(等外置软件)下载失败
- python数据透视表对各列统计_python pandas数据分析基础入门2——(数据格式转换、排序、统计、数据透视表)...
- vue + echarts 实现以中国为中心的世界3D地图
- MATLAB学习笔记 plotyy双y轴
- 华为ensp配置AC+AP多个SSID信号
- QT警告Slots named on_foo_bar are error prone
- mtatlas mysql_MHA-Atlas-MySQL高可用(下)