最近考虑做些英文词语词干化的工作,听说coreNLP这个工具不错,就拿来用了。
coreNLP是斯坦福大学开发的一套关于自然语言处理的工具(toolbox),使用简单功能强大,有;命名实体识别、词性标注、词语词干化、语句语法树的构造还有指代关系等功能,使用起来比较方便。
coreNLP是使用Java编写的,运行环境需要在JDK1.8,1.7貌似都不支持。这是需要注意的
coreNLP官方文档不多,但是给的几个示例文件也差不多能摸索出来怎么用,刚才摸索了一下,觉得还挺顺手的。
环境:
window7 64位
JDK1.8
需要引进的ar包:
说明:这里只是测试了英文的,所以使用的Stanford-corenlp-3.6.0.models.jar文件,如果使用中文的需要在官网上下对应中文的model jar包,然后引进项目即可。
直接看代码比较简单:
package com.luchi.corenlp; import java.util.List; import java.util.Map; import java.util.Properties; import edu.stanford.nlp.hcoref.CorefCoreAnnotations.CorefChainAnnotation; import edu.stanford.nlp.hcoref.data.CorefChain; import edu.stanford.nlp.ling.CoreAnnotations; import edu.stanford.nlp.ling.CoreAnnotations.LemmaAnnotation; import edu.stanford.nlp.ling.CoreAnnotations.NamedEntityTagAnnotation; import edu.stanford.nlp.ling.CoreAnnotations.PartOfSpeechAnnotation; import edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation; import edu.stanford.nlp.ling.CoreAnnotations.TextAnnotation; import edu.stanford.nlp.ling.CoreAnnotations.TokensAnnotation; import edu.stanford.nlp.ling.CoreLabel; import edu.stanford.nlp.pipeline.Annotation; import edu.stanford.nlp.pipeline.StanfordCoreNLP; import edu.stanford.nlp.semgraph.SemanticGraph; import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations; import edu.stanford.nlp.trees.Tree; import edu.stanford.nlp.trees.TreeCoreAnnotations.TreeAnnotation; import edu.stanford.nlp.util.CoreMap; public class TestNLP { public void test(){ //构造一个StanfordCoreNLP对象,配置NLP的功能,如lemma是词干化,ner是命名实体识别等 Properties props = new Properties(); props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref"); StanfordCoreNLP pipeline = new StanfordCoreNLP(props); // 待处理字符串 String text = "judy has been to china . she likes people there . and she went to Beijing ";// Add your text here! // 创造一个空的Annotation对象 Annotation document = new Annotation(text); // 对文本进行分析 pipeline.annotate(document); //获取文本处理结果 List<CoreMap> sentences = document.get(SentencesAnnotation.class); for(CoreMap sentence: sentences) { // traversing the words in the current sentence // a CoreLabel is a CoreMap with additional token-specific methods for (CoreLabel token: sentence.get(TokensAnnotation.class)) { // 获取句子的token(可以是作为分词后的词语) String word = token.get(TextAnnotation.class); System.out.println(word); //词性标注 String pos = token.get(PartOfSpeechAnnotation.class); System.out.println(pos); // 命名实体识别 String ne = token.get(NamedEntityTagAnnotation.class); System.out.println(ne); //词干化处理 String lema=token.get(LemmaAnnotation.class); System.out.println(lema); } // 句子的解析树 Tree tree = sentence.get(TreeAnnotation.class); tree.pennPrint(); // 句子的依赖图 SemanticGraph graph = sentence.get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class); System.out.println(graph.toString(SemanticGraph.OutputFormat.LIST)); } // 指代词链 //每条链保存指代的集合 // 句子和偏移量都从1开始 Map<Integer, CorefChain> corefChains = document.get(CorefChainAnnotation.class); if (corefChains == null) { return; } for (Map.Entry<Integer,CorefChain> entry: corefChains.entrySet()) { System.out.println("Chain " + entry.getKey() + " "); for (CorefChain.CorefMention m : entry.getValue().getMentionsInTextualOrder()) { // We need to subtract one since the indices count from 1 but the Lists start from 0 List<CoreLabel> tokens = sentences.get(m.sentNum - 1).get(CoreAnnotations.TokensAnnotation.class); // We subtract two for end: one for 0-based indexing, and one because we want last token of mention not one following. System. out.println(" " + m + ", i.e., 0-based character offsets [" + tokens.get(m.startIndex - 1).beginPosition() + ", " + tokens.get(m.endIndex - 2).endPosition() + ")"); } } } public static void main(String[]args){ TestNLP nlp=new TestNLP(); nlp.test(); } }
具体的注释都给出来了,我们可以直接看结果就知道代码的作用了:
对于每个token的识别结果:
原句中的:
judy 识别结果为:词性为NN,也就是名词,命名实体对象识别结果为O,词干识别为Judy
注意到has识别的词干已经被识别出来了,是“have”
而Beijing的命名实体标注识别结果为“Location”,也就意味着识别出了地名
然后我们看 解析树的识别(以第一句为例):
最后我们看一下指代的识别:
每个chain包含的是指代相同内容的词语,如chain1中两个she虽然在句子的不同位置,但是都指代的是第一句的“Judy”,这和原文的意思一致,表示识别正确,offsets表示的是该词语在句子中的位置
当然我只是用到了coreNLP的词干化功能,所以只需要把上面代码一改就可以处理词干化了,测试代码如下:
package com.luchi.corenlp; import java.util.List; import java.util.Properties; import edu.stanford.nlp.ling.CoreLabel; import edu.stanford.nlp.ling.CoreAnnotations.LemmaAnnotation; import edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation; import edu.stanford.nlp.ling.CoreAnnotations.TokensAnnotation; import edu.stanford.nlp.pipeline.Annotation; import edu.stanford.nlp.pipeline.StanfordCoreNLP; import edu.stanford.nlp.util.CoreMap; public class Lemma { // 词干化 public String stemmed(String inputStr) { Properties props = new Properties(); props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref"); StanfordCoreNLP pipeline = new StanfordCoreNLP(props); Annotation document = new Annotation(inputStr); pipeline.annotate(document); List<CoreMap> sentences = document.get(SentencesAnnotation.class); String outputStr = ""; for (CoreMap sentence : sentences) { // traversing the words in the current sentence // a CoreLabel is a CoreMap with additional token-specific methods for (CoreLabel token : sentence.get(TokensAnnotation.class)) { String lema = token.get(LemmaAnnotation.class); outputStr += lema+" "; } } return outputStr; } public static void main(String[]args){ Lemma lemma=new Lemma(); String input="jack had been to china there months ago. he likes china very much,and he is falling love with this country"; String output=lemma.stemmed(input); System.out.print("原句 :"); System.out.println(input); System.out.print("词干化:"); System.out.println(output); } }
输出结果为:
结果还是很准确的
相关推荐
使用stanford-corenlp的python接口,实现自动保存分词(词性标注)结果的python脚本。 自己更改目录及配置端口即可使用。
C#下调用Stanford CoreNLP,实现中文分词,命名实体识别,词性判断.附件中只包含如何调用,需自行从stanford官网下载模型和语料
我们努力支持Stanford CoreNLP使用的所有语言。 注释者 AR zh 恩 fr 德 es 标记化/细分 :check_mark: :check_mark: :check_mark: :check_mark: :check_mark: 句子拆分 :check_mark:
Stanford CoreNLP是一套稳定且经过测试的自然语言处理工具,已被学术界,行业和政府中的各个团体广泛使用。 这些工具使用基于规则的,概率机器学习和深度学习组件。 Stanford CoreNLP代码用Java编写,并根据GNU...
斯坦福大学的NLP包,可用于用于BLEU、CIDER、SPICE等指标的计算,以及一些自然语言处理相关内容,使用COCO数据集评价函数必备。
使用斯坦福 CoreNLP 的情感分析服务器 这是为 Koding Global Hackathon 创建的子项目。 它用于分析使用斯坦福 CoreNLP 库发送的情感推文。 推文像 /main/analyzeTweet.htm?tweets=xyz 一样发送(每条推文用新行分隔...
作为依赖项才能使用语言模型。 所有函数都在com.databricks.spark.corenlp.functions下com.databricks.spark.corenlp.functions 。 cleanxml :清理文档中的 XML 标签并返回清理过的文档。 tokenize :将句子...
corenlp中的词性标注缩写含义使用的是Penn Treebank tag set,这是全部对应缩写的含义,这份资源下载自https://www.eecis.udel.edu/~vijay/cis889/ie/pos-set.pdf
使用stanford-chinese-corenlp-2018-10-05-models.jar包中的中文语言模型,您需要在Java代码中加载相应的模型,然后使用Stanford CoreNLP库提供的API调用来处理中文文本。这些功能可用于处理中文文本数据的各种应用...
Java 1.8+(使用命令: java -version检查)( ) 斯坦福大学CoreNLP() Py版本 CoreNLP版本 v3.7.0.1 v3.7.0.2 CoreNLP 3.7.0 v3.8.0.1 CoreNLP 3.8.0 v3.9.1.1 CoreNLP 3.9.1 安装 pip install ...
corenlp-java-server 斯坦福CoreNLP解析器的简单Java REST API包装器基本原理为了有效地“玩弄” ,让它作为一个独立的进程运行,并使用简单的REST API对其进行查询是有意义的。 其他语言的现有包装器的一个问题是,...
stanford-chinese-corenlp相关资源,与core-full配合使用可进行中文文本分析
python-stanford-corenlp:使用双向服务器-客户端接口的CoreNLP的Python接口
Stanford-CoreNLP Python包装器使用Jpype的Stanford-CoreNLP版本3.9.1的Python包装器如何安装克隆存储库 git clone https://github.com/plotlabs/stanfordcorenlp-python.git运行以下命令来编译Java代码: export ...
pointer-generator(https://github.com/atulkum/pointer_summarizer)训练使用的CNN/Dailymail数据处理工具
#CoreNLP 文本分类引擎该引擎使用斯坦福的 CoreNLP 库对字符串进行分类。 引擎将根据示例数据将查询字符串放入一个类中。 ##样本数据数据文件的格式应为分类::文本。 其中 text 可以是任意字符串(带或不带空格)...
CoreNLP的官方Python接口,使用一个双向服务器 - 客户端接口
py-corenlp Stanford CoreNLP的Python包装器。 这只是封装了CoreNLP 3.6.0随附的服务器中的API。 有关详细信息,请参见CoreNLP服务器。 安装 pip install pycorenlp 用法 首先,请确保您正在运行Stanford CoreNLP...
这是使用Stanford CoreNLP的julia软件包。 使用该软件包需要CoreNLP服务器。 斯坦福大学CoreNLP可以从以下下载: ://stanfordnlp.github.io/CoreNLP/ 每当使用软件包时,都必须启动服务器。 可以在以下网页上找到...
stanford_corenlp_py 截至发表时,是开源情感分析领域的最新技术。 现有 Python 包装器使用运行 Java 管道; 然而,这些代码库中的大部分都是围绕子进程/套接字管理的,并且大体上重新发明了一个已经成熟的 Python...