stanford corenlp的TokensRegex
最近做一些音乐类、读物类的自然语言理解,就调研使用了下Stanford corenlp,记录下来。
功能
Stanford Corenlp是一套自然语言分析工具集包括:
- POS(part of speech tagger)-标注词性
- NER(named entity recognizer)-实体名识别
- Parser树-分析句子的语法结构,如识别出短语词组、主谓宾等
- Coreference Resolution-指代消解,找出句子中代表同一个实体的词。下文的I/my,Nader/he表示的是同一个人

- Sentiment Analysis-情感分析
- Bootstrapped pattern learning-自展的模式学习(也不知道翻译对不对,大概就是可以无监督的提取一些模式,如提取实体名)
- Open IE(Information Extraction)-从纯文本中提取有结构关系组,如"Barack Obama was born in Hawaii" =》 (Barack Obama; was born in; Hawaii)
需求
语音交互类的应用(如语音助手、智能音箱echo)收到的通常是口语化的自然语言,如:我想听一个段子,给我来个牛郎织女的故事,要想精确的返回结果,就需要提出有用的主题词,段子/牛郎织女/故事。看了一圈就想使用下corenlp的TokensRegex,基于tokens序列的正则表达式。因为它提供的可用的工具有:正则表达式、分词、词性、实体类别,另外还可以自己指定实体类别,如指定牛郎织女是READ类别的实体。
Pattern语法
规则格式
{
// ruleType is "text", "tokens", "composite", or "filter"
ruleType: "tokens",//tokens是基于切词用于tokens正则,text是文本串用于文本正则,composite/filter还没搞明白
// pattern to be matched
pattern: ( ( [ { ner:PERSON } ]) /was/ /born/ /on/ ([ { ner:DATE } ]) ),
// value associated with the expression for which the pattern was matched
// matched expressions are returned with "DATE_OF_BIRTH" as the value
// (as part of the MatchedExpression class)
result: "DATE_OF_BIRTH"
}
除了上面的字段外还有action/name/stage/active/priority等,可以参考文后的文献。
ruleTypes是tokens,pattern中的基本元素是token,整体用(),1个token用[<expression>],1个expression用{tag:xx;ner:xx}来表述
ruleTypes是text,pattern就是常规的正则表达式,基本元素就是字符了,整体用//包围
实例
corenlp提供了单条/多条正则表达式的提取,本文就介绍从文件中加载规则来拦截我们需要的文本,并从中提取主题词。
依赖包
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-corenlp</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-corenlp</artifactId>
<version>3.4.1</version>
<classifier>models</classifier>
</dependency>
<!--中文支持-->
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-corenlp</artifactId>
<version>3.6.0</version>
<classifier>models-chinese</classifier>
</dependency>
属性配置CoreNLP-chinese.properties(可以参考stanford-corenlp-models-chinese中的配置)
annotators = segment, ssplit, pos, ner, regexner, parse
regexner.mapping = regexner.txt//自定义的实体正则表达式文件 customAnnotatorClass.segment = edu.stanford.nlp.pipeline.ChineseSegmenterAnnotator segment.model = edu/stanford/nlp/models/segmenter/chinese/pku.gz
segment.sighanCorporaDict = edu/stanford/nlp/models/segmenter/chinese
segment.serDictionary = edu/stanford/nlp/models/segmenter/chinese/dict-chris6.ser.gz
segment.sighanPostProcessing = true ssplit.boundaryTokenRegex = [.]|[!?]+|[。]|[!?]+ //句子切分符 pos.model = edu/stanford/nlp/models/pos-tagger/chinese-distsim/chinese-distsim.tagger ner.model = edu/stanford/nlp/models/ner/chinese.misc.distsim.crf.ser.gz
ner.applyNumericClassifiers = false
ner.useSUTime = false parse.model = edu/stanford/nlp/models/lexparser/chinesePCFG.ser.gz
corenlp中对文本的一次处理称为一个pipeline,annotators代表一个处理节点,如segment切词、ssplit句子切割(将一段话分为多个句子)、pos词性、ner实体命名、regexner是用自定义正则表达式来标注实体类型、parse是句子结构解析。后面就是各annotator的属性。
自定义的规则文件
regexner.txt(将'牛郎织女'的实体类别识别为READ)
牛郎织女 READ
rule.txt(tokensregex规则)
$TYPE="/笑话|故事|段子|口技|谜语|寓言|评书|相声|小品|唐诗|古诗|宋词|绕口令|故事|小说/ | /脑筋/ /急转弯/"
//单类型
{
ruleType: "tokens",
pattern: ((?$type $TYPE)),
result: Format("%s;%s;%s", "", $$type.text.replace(" ",""), "")
}
(?type xx)代表一个命名group,提取该group将结果组装成xx;xx;xx形式返回
代码
//加载tokens正则表达
CoreMapExpressionExtractor extractor = CoreMapExpressionExtractor.createExtractorFromFile(TokenSequencePattern.getNewEnv(), "rule.txt");
//创建pipeline
StanfordCoreNLP coreNLP = new StanfordCoreNLP("CoreNLP-chinese.properties");
//处理文本
Annotation annotation = coreNLP.process("听个故事");
List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
CoreMap sentence = sentences.get(0); //获得第一个句子分析结果
//过一遍tokens正则
List<MatchedExpression> matchedExpressions = extractor.extractExpressions(sentence);
for (MatchedExpression match : matchedExpressions) {
System.out.println("Matched expression: " + match.getText() + " with value " + match.getValue());
}
想看下分析结果,如切词、词性、实体名,可以使用下面的函数
private void debug(CoreMap sentence) {
// 从CoreMap中取出CoreLabel List,逐一打印出来
List<CoreLabel> tokens = sentence.get(CoreAnnotations.TokensAnnotation.class);
System.out.println("字/词" + "\t " + "词性" + "\t " + "实体标记");
System.out.println("-----------------------------");
for (CoreLabel token : tokens) {
String word = token.getString(CoreAnnotations.TextAnnotation.class);
String pos = token.getString(CoreAnnotations.PartOfSpeechAnnotation.class);
String ner = token.getString(CoreAnnotations.NamedEntityTagAnnotation.class);
System.out.println(word + "\t " + pos + "\t " + ner);
}
}
功能还是很强大的,毕竟可以用的东西多了,遇到问题时方法就多了。
参考文献
TokensRegex: http://nlp.stanford.edu/software/tokensregex.shtml
SequenceMatchRules: http://nlp.stanford.edu/nlp/javadoc/javanlp-3.5.0/edu/stanford/nlp/ling/tokensregex/SequenceMatchRules.html
Regexner: http://nlp.stanford.edu/software/regexner.html
stanford corenlp的TokensRegex的更多相关文章
- stanford corenlp自定义切词类
stanford corenlp的中文切词有时不尽如意,那我们就需要实现一个自定义切词类,来完全满足我们的私人定制(加各种词典干预).上篇文章<IKAnalyzer>介绍了IKAnalyz ...
- 用 Python 和 Stanford CoreNLP 进行中文自然语言处理
实验环境:Windows 7 / Python 3.6.1 / CoreNLP 3.7.0 一.下载 CoreNLP 在 Stanford NLP 官网 下载最新的模型文件: CoreNLP 完整包 ...
- 开源中文分词工具探析(五):Stanford CoreNLP
CoreNLP是由斯坦福大学开源的一套Java NLP工具,提供诸如:词性标注(part-of-speech (POS) tagger).命名实体识别(named entity recognizer ...
- Stanford CoreNLP使用需要注意的一点
1.Stanford CoreNLP maven依赖,jdk依赖1.8 <dependency> <groupId>edu.stanford.nlp</groupId&g ...
- 开源中文分词工具探析(六):Stanford CoreNLP
CoreNLP是由斯坦福大学开源的一套Java NLP工具,提供诸如:词性标注(part-of-speech (POS) tagger).命名实体识别(named entity recognizer ...
- Stanford Corenlp学习笔记——词性标注
使用Stanford Corenlp对中文进行词性标注 语言为Scala,使用的jar的版本是3.6.0,而且是手动添加jar包,使用sbt添加其他版本的时候出现了各种各样的问题 添加的jar包有5个 ...
- Eclipse下使用Stanford CoreNLP的方法
源码下载地址:CoreNLP官网. 目前release的CoreNLP version 3.5.0版本仅支持java-1.8及以上版本,因此有时需要为Eclipse添加jdk-1.8配置,配置方法如下 ...
- Stanford CoreNLP 3.6.0 中文指代消解模块调用失败的解决方案
当前中文指代消解领域比较活跃的研究者是Chen和Vincent Ng,这两个人近两年在AAAI2014, 2015发了一些相关的文章,研究领域跨越零指代.代词指代.名词指代等,方法也不是很复杂,集中于 ...
- 【转载】Stanford CoreNLP Typed Dependencies
总结自Stanford typed dependencies manual 原文链接:http://www.jianshu.com/p/5c461cf096c4 依存关系描述句子中词与词之间的各种语法 ...
随机推荐
- JS核心系列:理解 new 的运行机制
和其他高级语言一样 javascript 中也有 new 运算符,我们知道 new 运算符是用来实例化一个类,从而在内存中分配一个实例对象. 但在 javascript 中,万物皆对象,为什么还要通过 ...
- X86和X86_64和X64有什么区别?
x86是指intel的开发的一种32位指令集,从386开始时代开始的,一直沿用至今,是一种cisc指令集,所有intel早期的cpu,amd早期的cpu都支持这种指令集,ntel官方文档里面称为&qu ...
- CSS 特殊属性介绍之 pointer-events
首先看一下 MDN 上关于 pointer-events 的介绍: CSS属性 pointer-events 允许作者控制特定的图形元素在何时成为鼠标事件的 target.当未指定该属性时,SVG 内 ...
- 如何利用ansible callback插件对执行结果进行解析
最近在写一个批量巡检工具,利用ansible将脚本推到各个机器上执行,然后将执行的结果以json格式返回来. 如下所示: # ansible node2 -m script -a /root/pyth ...
- [Nginx笔记]关于线上环境CLOSE_WAIT和TIME_WAIT过高
运维的同学和Team里面的一个同学分别遇到过Nginx在线上环境使用中会遇到TIME_WAIT过高或者CLOSE_WAIT过高的状态 先从原因分析一下为什么,问题就迎刃而解了. 首先是TIME_WAI ...
- 编写高质量代码:改善Java程序的151个建议(第6章:枚举和注解___建议88~92)
建议88:用枚举实现工厂方法模式更简洁 工厂方法模式(Factory Method Pattern)是" 创建对象的接口,让子类决定实例化哪一个类,并使一个类的实例化延迟到其它子类" ...
- 设计模式C#合集--单例模式
单例模式 代码: 第一种: private static Singleton singleton = null; private Singleton() { } public static Singl ...
- C# 工厂模式+虚方法(接口、抽象方法)实现多态
面向对象语言的三大特征之一就是多态,听起来多态比较抽象,简而言之就是同一行为针对不同对象得到不同的结果,同一对象,在不同的环境下得到不同的状态. 实例说明: 业务需求:实现一个打开文件的控制台程序的d ...
- win10上部署Hadoop-2.7.3——非Cygwin、非虚拟机
开始接触Hadoop,听人说一般都是在Lunix下部署Hadoop,但是本人Lunix不是很了解,所以Google以下如何在Win10下安装Hadoop(之后再在Lunix下弄),找到不少文章,以下是 ...
- Java 中获取类路径 classpath 的方法
System.out.println("++++++++++++++++++++++++"); String path = System.getProperty("jav ...