stanford corenlp的中文切词有时不尽如意,那我们就需要实现一个自定义切词类,来完全满足我们的私人定制(加各种词典干预)。上篇文章《IKAnalyzer》介绍了IKAnalyzer的自由度,本篇文章就说下怎么把IKAnalyzer作为corenlp的切词工具。

stanford corenlp的TokensRegex》提到了corenlp的配置CoreNLP-chinese.properties,其中customAnnotatorClass.segment就是用于指定切词类的,在这里我们只需要模仿ChineseSegmenterAnnotator来实现一个自己的Annotator,并设置在配置文件中即可。

customAnnotatorClass.segment = edu.stanford.nlp.pipeline.ChineseSegmenterAnnotator

下面是我的实现:

public class IKSegmenterAnnotator extends ChineseSegmenterAnnotator {
public IKSegmenterAnnotator() {
super();
} public IKSegmenterAnnotator(boolean verbose) {
super(verbose);
} public IKSegmenterAnnotator(String segLoc, boolean verbose) {
super(segLoc, verbose);
} public IKSegmenterAnnotator(String segLoc, boolean verbose, String serDictionary, String sighanCorporaDict) {
super(segLoc, verbose, serDictionary, sighanCorporaDict);
} public IKSegmenterAnnotator(String name, Properties props) {
super(name, props);
} private List<String> splitWords(String str) {
try {
List<String> words = new ArrayList<String>();
IKSegmenter ik = new IKSegmenter(new StringReader(str), true);
Lexeme lex = null;
while ((lex = ik.next()) != null) {
words.add(lex.getLexemeText());
}
return words;
} catch (IOException e) {
//LOGGER.error(e.getMessage(), e);
System.out.println(e);
List<String> words = new ArrayList<String>();
words.add(str);
return words;
}
} @Override
public void runSegmentation(CoreMap annotation) {
//0 2
// A BC D E
// 1 10 1 1
// 0 12 3 4
// 0, 0+1 , String text = annotation.get(CoreAnnotations.TextAnnotation.class);
List<CoreLabel> sentChars = annotation.get(ChineseCoreAnnotations.CharactersAnnotation.class);
List<CoreLabel> tokens = new ArrayList<CoreLabel>();
annotation.set(CoreAnnotations.TokensAnnotation.class, tokens); //List<String> words = segmenter.segmentString(text);
List<String> words = splitWords(text);
System.err.println(text);
System.err.println("--->");
System.err.println(words); int pos = 0;
for (String w : words) {
CoreLabel fl = sentChars.get(pos);
fl.set(CoreAnnotations.ChineseSegAnnotation.class, "1");
if (w.length() == 0) {
continue;
}
CoreLabel token = new CoreLabel();
token.setWord(w);
token.set(CoreAnnotations.CharacterOffsetBeginAnnotation.class, fl.get(CoreAnnotations.CharacterOffsetBeginAnnotation.class));
pos += w.length();
fl = sentChars.get(pos - 1);
token.set(CoreAnnotations.CharacterOffsetEndAnnotation.class, fl.get(CoreAnnotations.CharacterOffsetEndAnnotation.class));
tokens.add(token);
}
}
}

在外面为IKAnalyzer初始化词典,指定扩展词典和删除词典

        //为ik初始化词典,删除干扰词
Dictionary.initial(DefaultConfig.getInstance());
String delDic = System.getProperty(READ_IK_DEL_DIC, null);
BufferedReader reader = new BufferedReader(new FileReader(delDic));
String line = null;
List<String> delWords = new ArrayList<>();
while ((line = reader.readLine()) != null) {
delWords.add(line);
}
Dictionary.getSingleton().disableWords(delWords);

  

  

  

stanford corenlp自定义切词类的更多相关文章

  1. stanford corenlp的TokensRegex

    最近做一些音乐类.读物类的自然语言理解,就调研使用了下Stanford corenlp,记录下来. 功能 Stanford Corenlp是一套自然语言分析工具集包括: POS(part of spe ...

  2. 用 Python 和 Stanford CoreNLP 进行中文自然语言处理

    实验环境:Windows 7 / Python 3.6.1 / CoreNLP 3.7.0 一.下载 CoreNLP 在 Stanford NLP 官网 下载最新的模型文件: CoreNLP 完整包 ...

  3. 开源中文分词工具探析(五):Stanford CoreNLP

    CoreNLP是由斯坦福大学开源的一套Java NLP工具,提供诸如:词性标注(part-of-speech (POS) tagger).命名实体识别(named entity recognizer ...

  4. Stanford CoreNLP使用需要注意的一点

    1.Stanford CoreNLP maven依赖,jdk依赖1.8 <dependency> <groupId>edu.stanford.nlp</groupId&g ...

  5. 开源中文分词工具探析(六):Stanford CoreNLP

    CoreNLP是由斯坦福大学开源的一套Java NLP工具,提供诸如:词性标注(part-of-speech (POS) tagger).命名实体识别(named entity recognizer ...

  6. Stanford Corenlp学习笔记——词性标注

    使用Stanford Corenlp对中文进行词性标注 语言为Scala,使用的jar的版本是3.6.0,而且是手动添加jar包,使用sbt添加其他版本的时候出现了各种各样的问题 添加的jar包有5个 ...

  7. Eclipse下使用Stanford CoreNLP的方法

    源码下载地址:CoreNLP官网. 目前release的CoreNLP version 3.5.0版本仅支持java-1.8及以上版本,因此有时需要为Eclipse添加jdk-1.8配置,配置方法如下 ...

  8. Stanford CoreNLP 3.6.0 中文指代消解模块调用失败的解决方案

    当前中文指代消解领域比较活跃的研究者是Chen和Vincent Ng,这两个人近两年在AAAI2014, 2015发了一些相关的文章,研究领域跨越零指代.代词指代.名词指代等,方法也不是很复杂,集中于 ...

  9. 【转载】Stanford CoreNLP Typed Dependencies

    总结自Stanford typed dependencies manual 原文链接:http://www.jianshu.com/p/5c461cf096c4 依存关系描述句子中词与词之间的各种语法 ...

随机推荐

  1. JavaScript权威指南 - 函数

    函数本身就是一段JavaScript代码,定义一次但可能被调用任意次.如果函数挂载在一个对象上,作为对象的一个属性,通常这种函数被称作对象的方法.用于初始化一个新创建的对象的函数被称作构造函数. 相对 ...

  2. 分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间)

    分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间) 很多时候我们都需要计算数据库中各个表的数据量和每行记录所占用空间 这里共享一个脚本 CREATE TABLE #tab ...

  3. C语言 · 矩形面积交

    问题描述 平面上有两个矩形,它们的边平行于直角坐标系的X轴或Y轴.对于每个矩形,我们给出它的一对相对顶点的坐标,请你编程算出两个矩形的交的面积. 输入格式 输入仅包含两行,每行描述一个矩形. 在每行中 ...

  4. Socket聊天程序——初始设计

    写在前面: 可能是临近期末了,各种课程设计接踵而来,最近在csdn上看到2个一样问答(问题A,问题B),那就是编写一个基于socket的聊天程序,正好最近刚用socket做了一些事,出于兴趣,自己抽了 ...

  5. $.extend()的实现源码 --(源码学习1)

    目标: $.extend({         add:function(a,b){             return a + b;         }     }) console.log($.a ...

  6. 关于 devbridge-autocomplete 插件多选操作的实现方法

    目前据我所知最好用的 autocomplete 插件就是 jquery-ui 的 autocomplete 以及 devbridge 的 autocomplete 插件. 我最终选择了 devbrid ...

  7. AFNetworking 3.0 源码解读(十一)之 UIButton/UIProgressView/UIWebView + AFNetworking

    AFNetworking的源码解读马上就结束了,这一篇应该算是倒数第二篇,下一篇会是对AFNetworking中的技术点进行总结. 前言 上一篇我们总结了 UIActivityIndicatorVie ...

  8. log4net使用手册

    1. log4net简介 log4net是.Net下一个非常优秀的开源日志记录组件.log4net记录日志的功能非常强大.它可以将日志分不同的等级,以不同的格式,输出到不同的媒介.Java平台下,它还 ...

  9. docker – 你应该知道的10件事

      容器并不是一个全新的技术,但这并不妨碍Docker如风暴一样席卷整个世界. 如果你在IT圈里,你一定听说过Docker.就算与其他热门技术,如:Puppet/Chef,Hadoop或者MongoD ...

  10. 图解DevExpress RichEditControl富文本的使用,附源码及官方API

    9点半了,刚写到1.2.   该回家了,明天继续写完. 大家还需要什么操作,留言说一下,没有的我明天继续加. 好久没有玩DevExpress了,今天下载了一个玩玩,发现竟然更新到14.2.5了..我去 ...