基于HanLP,支持包括Solr(7.x)在内的任何基于Lucene(7.x)的系统。

Maven

<dependency>

<groupId>com.hankcs.nlp</groupId>

<artifactId>hanlp-lucene-plugin</artifactId>

<version>1.1.6</version>

</dependency>

Solr快速上手

1.将hanlp-portable.jar和hanlp-lucene-plugin.jar共两个jar放入${webapp}/WEB-INF/lib下。(或者使用mvn package对源码打包,拷贝target/hanlp-lucene-plugin-x.x.x.jar到${webapp}/WEB-INF/lib下)

2. 修改solr core的配置文件${core}/conf/schema.xml:

<fieldType name="text_cn" class="solr.TextField">

<analyzer type="index">

<tokenizer class="com.hankcs.lucene.HanLPTokenizerFactory" enableIndexMode="true"/>

</analyzer>

<analyzer type="query">

<!-- 切记不要在query中开启index模式 -->

<tokenizer class="com.hankcs.lucene.HanLPTokenizerFactory" enableIndexMode="false"/>

</analyzer>

</fieldType>

<!-- 业务系统中需要分词的字段都需要指定type为text_cn -->

<field name="my_field1" type="text_cn" indexed="true" stored="true"/>

<field name="my_field2" type="text_cn" indexed="true" stored="true"/>

· 如果你的业务系统中有其他字段,比如location,summary之类,也需要一一指定其type="text_cn"。切记,否则这些字段仍旧是solr默认分词器。

· 另外,切记不要在query中开启indexMode,否则会影响PhaseQuery。indexMode只需在index中开启一遍即可。

高级配置

目前本插件支持如下基于schema.xml的配置:

更高级的配置主要通过class path下的hanlp.properties进行配置,请阅读HanLP自然语言处理包文档以了解更多相关配置,如:

0.用户词典

1.词性标注

2.简繁转换

3.……

停用词与同义词

推荐利用Lucene或Solr自带的filter实现,本插件不会越俎代庖。 一个示例配置如下:

调用方法

在Query改写的时候,可以利用HanLPAnalyzer分词结果中的词性等属性,如

String text = "中华人民共和国很辽阔";

for (int i = 0; i < text.length(); ++i)

{

System.out.print(text.charAt(i) + "" + i + " ");

}

System.out.println();

Analyzer analyzer = new HanLPAnalyzer();

TokenStream tokenStream = analyzer.tokenStream("field", text);

tokenStream.reset();

while (tokenStream.incrementToken())

{

CharTermAttribute attribute = tokenStream.getAttribute(CharTermAttribute.class);

// 偏移量

OffsetAttribute offsetAtt = tokenStream.getAttribute(OffsetAttribute.class);

// 距离

PositionIncrementAttribute positionAttr = tokenStream.getAttribute(PositionIncrementAttribute.class);

// 词性

TypeAttribute typeAttr = tokenStream.getAttribute(TypeAttribute.class);

System.out.printf("[%d:%d %d] %s/%s\n", offsetAtt.startOffset(), offsetAtt.endOffset(), positionAttr.getPositionIncrement(), attribute, typeAttr.type());

}

在另一些场景,支持以自定义的分词器(比如开启了命名实体识别的分词器、繁体中文分词器、CRF分词器等)构造HanLPTokenizer,比如:

tokenizer = new HanLPTokenizer(HanLP.newSegment()

.enableJapaneseNameRecognize(true)

.enableIndexMode(true), null, false);

tokenizer.setReader(new StringReader("林志玲亮相网友:确定不是波多野结衣?"));

文章摘自:2019 github

HanLP中文分词Lucene插件的更多相关文章

  1. 全文检索Solr集成HanLP中文分词

    以前发布过HanLP的Lucene插件,后来很多人跟我说其实Solr更流行(反正我是觉得既然Solr是Lucene的子项目,那么稍微改改配置就能支持Solr),于是就抽空做了个Solr插件出来,开源在 ...

  2. 全文检索Solr集成HanLP中文分词【转】

    以前发布过HanLP的Lucene插件,后来很多人跟我说其实Solr更流行(反正我是觉得既然Solr是Lucene的子项目,那么稍微改改配置就能支持Solr),于是就抽空做了个Solr插件出来,开源在 ...

  3. Elasticsearch:hanlp 中文分词器

    HanLP 中文分词器是一个开源的分词器,是专为Elasticsearch而设计的.它是基于HanLP,并提供了HanLP中大部分的分词方式.它的源码位于: https://github.com/Ke ...

  4. 【自定义IK词典】Elasticsearch之中文分词器插件es-ik的自定义词库

    Elasticsearch之中文分词器插件es-ik 针对一些特殊的词语在分词的时候也需要能够识别 有人会问,那么,例如: 如果我想根据自己的本家姓氏来查询,如zhouls,姓氏“周”.      如 ...

  5. Elasticsearch之中文分词器插件es-ik(博主推荐)

    前提 什么是倒排索引? Elasticsearch之分词器的作用 Elasticsearch之分词器的工作流程 Elasticsearch之停用词 Elasticsearch之中文分词器 Elasti ...

  6. Elasticsearch之中文分词器插件es-ik的自定义热更新词库

    不多说,直接上干货! 欢迎大家,关注微信扫码并加入我的4个微信公众号:   大数据躺过的坑      Java从入门到架构师      人工智能躺过的坑         Java全栈大联盟       ...

  7. elasticsearch安装中文分词器插件smartcn

    原文:http://blog.java1234.com/blog/articles/373.html elasticsearch安装中文分词器插件smartcn elasticsearch默认分词器比 ...

  8. lucene6+HanLP中文分词

    1.前言 前一阵把博客换了个模版,模版提供了一个搜索按钮,这让我想起一直以来都想折腾的全文搜索技术,于是就用lucene6.2.1加上HanLP的分词插件做了这么一个模块CSearch.效果看这里:h ...

  9. 搜索引擎ElasticSearch系列(五): ElasticSearch2.4.4 IK中文分词器插件安装

    一:IK分词器简介  IK Analyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包.从2006年12月推出1.0版开始, IKAnalyzer已经推出了4个大版本.最初,它是以开源 ...

随机推荐

  1. python 实现程序重启

    def restart_program(): """Restarts the current program. Note: this function does not  ...

  2. selenium中的alter弹框

    from selenium import webdriverimport timedriver=webdriver.Chrome()driver.get('http://ui.imdsx.cn/uit ...

  3. mysql事件调用存储过程总结

    第一次写事件调用存储过程,在网上找了一些资料,特此做下总结,巩固一下: 事件调用存储过程主要有三种: (1)创建事件马上执行,调用存储过程 CREATE EVENT if not exists Eve ...

  4. fiddler模拟弱网测试点

    弱网: oSession[“request-trickle-delay”] = “300”; 注释的也很明白,Delay sends by 300ms per KB uploaded.上传1KB需要3 ...

  5. 精进之路之AQS及相关组件

    AQS ( AbstractQueuedSynchronizer)是一个用来构建锁和同步器的框架,使用AQS能简单且高效地构造出应用广泛的大量的同步器,比如我们提到的ReentrantLock,Sem ...

  6. Thymeleaf前后端传值 页面取值与js取值

    参考: Thymeleaf前后端传值 页面取值与js取值 Thymeleaf 与 Javascript Thymeleaf教程 (十二) 标签内,js中使用表达式 目的: 后端通过Model传值到前端 ...

  7. PHP 基于pdo的数据库操作类

    http://www.php.cn/php-weizijiaocheng-404645.html <?php class Pdodb{   protected $pdo;   protected ...

  8. 剑指Offer 62. 二叉搜索树的第k个结点 (二叉搜索树)

    题目描述 给定一棵二叉搜索树,请找出其中的第k小的结点.例如, (5,3,7,2,4,6,8)    中,按结点数值大小顺序第三小结点的值为4. 例如, 5 / \ 3 7 / \ / \ 2 4 6 ...

  9. WEB学习笔记14-HTML5新特性的使用

    (1)定义文档类型声明 <!DOCTYPE html> (2)定义页面编码 <!—HTML 4.01中定义的设置页面编码的方式--> <meta http-equiv=& ...

  10. ssm登录与退出

    ssm整合比较好的实例 http://how2j.cn/k/ssm/ssm-tutorial/1137.html?tid=77#nowhere ssm登录后台用户检测(此实例注销有问题):http:/ ...