IK是基于字典的一款轻量级的中文分词工具包,可以通过elasticsearch的插件机制集成;

一、集成步骤

1.在elasticsearch的安装目录下的plugin下新建ik目录;

2.在github下载对应版本的ik插件;

https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v6.8.12

3.解压插件文件,并重启elasticsearch,可以看到如下已经加载了ik插件;

[2022-01-11T15:22:54,341][INFO ][o.e.p.PluginsService     ] [4EvvJl1] loaded plugin [analysis-ik]

二、体验IK的分析器

IK提供了ik_smart和ik_max_word两个分析器;

ik_max_word分析器会最大程度的对文本进行分词,分词的粒度还是比较细致的;

POST _analyze
{
"analyzer": "ik_max_word",
"text":"这次出差我们住的是闫团如家快捷酒店"
} {
"tokens" : [
{
"token" : "这次",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "出差",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "我们",
"start_offset" : 4,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "住",
"start_offset" : 6,
"end_offset" : 7,
"type" : "CN_CHAR",
"position" : 3
},
{
"token" : "的",
"start_offset" : 7,
"end_offset" : 8,
"type" : "CN_CHAR",
"position" : 4
},
{
"token" : "是",
"start_offset" : 8,
"end_offset" : 9,
"type" : "CN_CHAR",
"position" : 5
},
{
"token" : "闫",
"start_offset" : 9,
"end_offset" : 10,
"type" : "CN_CHAR",
"position" : 6
},
{
"token" : "团",
"start_offset" : 10,
"end_offset" : 11,
"type" : "CN_CHAR",
"position" : 7
},
{
"token" : "如家",
"start_offset" : 11,
"end_offset" : 13,
"type" : "CN_WORD",
"position" : 8
},
{
"token" : "快捷酒店",
"start_offset" : 13,
"end_offset" : 17,
"type" : "CN_WORD",
"position" : 9
}
]
}

ik_smart相对来说粒度会比较粗;

POST _analyze
{
"analyzer": "ik_smart",
"text":"这次出差我们住的是闫团如家快捷酒店"
} {
"tokens" : [
{
"token" : "这次",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "出差",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "我们",
"start_offset" : 4,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "住",
"start_offset" : 6,
"end_offset" : 7,
"type" : "CN_CHAR",
"position" : 3
},
{
"token" : "的",
"start_offset" : 7,
"end_offset" : 8,
"type" : "CN_CHAR",
"position" : 4
},
{
"token" : "是",
"start_offset" : 8,
"end_offset" : 9,
"type" : "CN_CHAR",
"position" : 5
},
{
"token" : "闫",
"start_offset" : 9,
"end_offset" : 10,
"type" : "CN_CHAR",
"position" : 6
},
{
"token" : "团",
"start_offset" : 10,
"end_offset" : 11,
"type" : "CN_CHAR",
"position" : 7
},
{
"token" : "如家",
"start_offset" : 11,
"end_offset" : 13,
"type" : "CN_WORD",
"position" : 8
},
{
"token" : "快捷酒店",
"start_offset" : 13,
"end_offset" : 17,
"type" : "CN_WORD",
"position" : 9
}
]
}

三、扩展ik字典

由于 闫团 是一个比较小的地方,ik的字典中并不包含导致分成两个单个的字符;我们可以将它添加到ik的字典中;

在ik的安装目录下config中新增my.dic文件,并将 闫团 放到文件中;完成之后修改IKAnalyzer.cfg.xml文件,添加新增的字典文件;

<properties>
<comment>IK Analyzer 扩展配置</comment>
<!--用户可以在这里配置自己的扩展字典 -->
<entry key="ext_dict">my.dic</entry>
<!--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords"></entry>
<!--用户可以在这里配置远程扩展字典 -->
<!-- <entry key="remote_ext_dict">words_location</entry> -->
<!--用户可以在这里配置远程扩展停止词字典-->
<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>

重启elasticsearch并重新执行查看已经将地名作为一个分词了;

POST _analyze
{
"analyzer": "ik_smart",
"text":"这次出差我们住的是闫团如家快捷酒店"
} {
"tokens" : [
{
"token" : "这次",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "出差",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "我们",
"start_offset" : 4,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "住",
"start_offset" : 6,
"end_offset" : 7,
"type" : "CN_CHAR",
"position" : 3
},
{
"token" : "的",
"start_offset" : 7,
"end_offset" : 8,
"type" : "CN_CHAR",
"position" : 4
},
{
"token" : "是",
"start_offset" : 8,
"end_offset" : 9,
"type" : "CN_CHAR",
"position" : 5
},
{
"token" : "闫团",
"start_offset" : 9,
"end_offset" : 11,
"type" : "CN_WORD",
"position" : 6
},
{
"token" : "如家",
"start_offset" : 11,
"end_offset" : 13,
"type" : "CN_WORD",
"position" : 7
},
{
"token" : "快捷酒店",
"start_offset" : 13,
"end_offset" : 17,
"type" : "CN_WORD",
"position" : 8
}
]
}

四、体验HanLP分析器及自定义字典

HanLP是由一系列模型与算法组成的Java工具包,它从中文分词开始,覆盖词性标注、命名实体识别、句法分析、文本分类等常用的NLP任务,提供了丰富的API,被广泛用于Lucene、Solr和ES等搜索平台。就分词算法来说,它支持最短路分词、N-最短路分词和CRF分词等分词算法。

从以下地址下载hanLP插件包

https://github.com/KennFalcon/elasticsearch-analysis-hanlp/releases/download/v7.9.2/elasticsearch-analysis-hanlp-7.9.2.zip

安装hanLP插件包

bin\elasticsearch-plugin install file:///c:/elasticsearch-analysis-hanlp-7.9.2.zip
-> Installing file:///c:/elasticsearch-analysis-hanlp-7.9.2.zip
-> Downloading file:///c:/elasticsearch-analysis-hanlp-7.9.2.zip
[=================================================] 100%??
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: plugin requires additional permissions @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* java.io.FilePermission plugins/analysis-hanlp/data/-#plus read,write,delete
* java.io.FilePermission plugins/analysis-hanlp/hanlp.cache#plus read,write,delete
* java.lang.RuntimePermission getClassLoader
* java.lang.RuntimePermission setContextClassLoader
* java.net.SocketPermission * connect,resolve
* java.util.PropertyPermission * read,write
See http://docs.oracle.com/javase/8/docs/technotes/guides/security/permissions.html
for descriptions of what these permissions allow and the associated risks. Continue with installation? [y/N]y
-> Installed analysis-hanlp

使用hanlp_standard分析器对文本进行分析

POST _analyze
{
"analyzer": "hanlp_standard",
"text":"这次出差我们住的是闫团如家快捷酒店"
} {
"tokens" : [
{
"token" : "这次",
"start_offset" : 0,
"end_offset" : 2,
"type" : "r",
"position" : 0
},
{
"token" : "出差",
"start_offset" : 2,
"end_offset" : 4,
"type" : "vi",
"position" : 1
},
{
"token" : "我们",
"start_offset" : 4,
"end_offset" : 6,
"type" : "rr",
"position" : 2
},
{
"token" : "住",
"start_offset" : 6,
"end_offset" : 7,
"type" : "vi",
"position" : 3
},
{
"token" : "的",
"start_offset" : 7,
"end_offset" : 8,
"type" : "ude1",
"position" : 4
},
{
"token" : "是",
"start_offset" : 8,
"end_offset" : 9,
"type" : "vshi",
"position" : 5
},
{
"token" : "闫团",
"start_offset" : 9,
"end_offset" : 11,
"type" : "nr",
"position" : 6
},
{
"token" : "如家",
"start_offset" : 11,
"end_offset" : 13,
"type" : "r",
"position" : 7
},
{
"token" : "快捷酒店",
"start_offset" : 13,
"end_offset" : 17,
"type" : "ntch",
"position" : 8
}
]
}

我们可以看到hanLP自动将 闫团 分成一个词了;

执行如下测试,可以看到hanLP没有将 小地方作为一个分词;

POST _analyze
{
"analyzer": "hanlp_standard",
"text":"闫团是一个小地方"
} {
"tokens" : [
{
"token" : "闫团",
"start_offset" : 0,
"end_offset" : 2,
"type" : "nr",
"position" : 0
},
{
"token" : "是",
"start_offset" : 2,
"end_offset" : 3,
"type" : "vshi",
"position" : 1
},
{
"token" : "一个",
"start_offset" : 3,
"end_offset" : 5,
"type" : "mq",
"position" : 2
},
{
"token" : "小",
"start_offset" : 5,
"end_offset" : 6,
"type" : "a",
"position" : 3
},
{
"token" : "地方",
"start_offset" : 6,
"end_offset" : 8,
"type" : "n",
"position" : 4
}
]
}

为了自定义分词,我们在${ES_HOME}/plugins/analysis-hanlp/data/dictionary/custom下新建my.dic,并添加 小地方;

然后从插件安装包拷贝hanlp.properties文件放到如下位置${ES_HOME}/config/analysis-hanlp/hanlp.properties,并修改CustomDictionaryPath;

CustomDictionaryPath=data/dictionary/custom/CustomDictionary.txt; ModernChineseSupplementaryWord.txt; ChinesePlaceName.txt ns; PersonalName.txt; OrganizationName.txt; ShanghaiPlaceName.txt ns;data/dictionary/person/nrf.txt nrf;data/dictionary/custom/my.dic;

从起elasticsearch并执行测试

POST _analyze
{
"analyzer": "hanlp",
"text":"闫团是一个小地方"
} {
"tokens" : [
{
"token" : "闫团",
"start_offset" : 0,
"end_offset" : 2,
"type" : "nr",
"position" : 0
},
{
"token" : "是",
"start_offset" : 2,
"end_offset" : 3,
"type" : "vshi",
"position" : 1
},
{
"token" : "一个",
"start_offset" : 3,
"end_offset" : 5,
"type" : "mq",
"position" : 2
},
{
"token" : "小地方",
"start_offset" : 5,
"end_offset" : 8,
"type" : "n",
"position" : 3
}
]
}

elasticsearch之集成中文分词器的更多相关文章

  1. solr 7+tomcat 8 + mysql实现solr 7基本使用(安装、集成中文分词器、定时同步数据库数据以及项目集成)

    基本说明 Solr是一个开源项目,基于Lucene的搜索服务器,一般用于高级的搜索功能: solr还支持各种插件(如中文分词器等),便于做多样化功能的集成: 提供页面操作,查看日志和配置信息,功能全面 ...

  2. elasticsearch使用ik中文分词器

    elasticsearch使用ik中文分词器 一.背景 二.安装 ik 分词器 1.从 github 上找到和本次 es 版本匹配上的 分词器 2.使用 es 自带的插件管理 elasticsearc ...

  3. Elasticsearch系列---使用中文分词器

    前言 前面的案例使用standard.english分词器,是英文原生的分词器,对中文分词支持不太好.中文作为全球最优美.最复杂的语言,目前中文分词器较多,ik-analyzer.结巴中文分词.THU ...

  4. 如何在Elasticsearch中安装中文分词器(IK)和拼音分词器?

    声明:我使用的Elasticsearch的版本是5.4.0,安装分词器前请先安装maven 一:安装maven https://github.com/apache/maven 说明: 安装maven需 ...

  5. Elasticsearch:hanlp 中文分词器

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

  6. Elasticsearch:IK中文分词器

    Elasticsearch内置的分词器对中文不友好,只会一个字一个字的分,无法形成词语,比如: POST /_analyze { "text": "我爱北京天安门&quo ...

  7. 如何在Elasticsearch中安装中文分词器(IK+pinyin)

    如果直接使用Elasticsearch的朋友在处理中文内容的搜索时,肯定会遇到很尴尬的问题--中文词语被分成了一个一个的汉字,当用Kibana作图的时候,按照term来分组,结果一个汉字被分成了一组. ...

  8. ElasticSearch安装中文分词器IKAnalyzer

    # ElasticSearch安装中文分词器IKAnalyzer  本篇主要讲解如何在ElasticSearch中安装中文分词器IKAnalyzer,拆分的每个词都是我们熟知的词语,从而建立词汇与文档 ...

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

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

随机推荐

  1. 07- Vue3 UI Framework - Switch 组件

    为了更好的提升用户体验,我们这里再做一个很常用的开关组件 switch 返回阅读列表点击 这里 需求分析 开始之前我们先做一个简单的需求分析 switch 组件应分为选中/未被选中,两种状态 可以通过 ...

  2. Shell if 参数含义列表

    [ -a FILE ]  如果 FILE 存在则为真.   [ -b FILE ]  如果 FILE 存在且是一个块特殊文件则为真.   [ -c FILE ]  如果 FILE 存在且是一个字特殊文 ...

  3. Google Earth Engine 批量点击RUN任务,批量取消正在上传的任务

    本文内容参考自: https://blog.csdn.net/qq_21567935/article/details/89061114 https://blog.csdn.net/qq_2156793 ...

  4. CF975A Aramic script 题解

    Content 定义一个字符串的根为字符串中不同种类的字符按字典序非降序排列得到的字符串.例如 \(\texttt{aaa}\) 的词根为 \(\texttt{a}\),\(\texttt{babb} ...

  5. AcWing 1113. 红与黑

    1.题目描述 有一间长方形的房子,地上铺了红色.黑色两种颜色的正方形瓷砖. 你站在其中一块黑色的瓷砖上,只能向相邻(上下左右四个方向)的黑色瓷砖移动. 请写一个程序,计算你总共能够到达多少块黑色的瓷砖 ...

  6. Python 图片转字符画 学习笔记

    Python 图片转字符画 学习笔记 标签(空格分隔): Python 声明:此文章和所有代码是学习笔记,非原创,原文教程地址:https://www.shiyanlou.com/courses/37 ...

  7. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

  8. 【剑指Offer】数值的整数次方 解题报告(Python)

    [剑指Offer]数值的整数次方 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://www.nowcoder.com/ta/coding-interviews ...

  9. 广告投放效果难判断?集成华为DTM为您轻松实现!

    在进行广告投放时,你是否遇到这样的困扰: 花了一大笔预算去投放推广,不知道实际效果如何? 用户看到投放的广告,产生了哪些有价值的行为? 当前广告投放的渠道和类型等投放策略,是否需要调整? 此时我们就需 ...

  10. python学习第五天:python基础(string、list、tuple)

    首先,什么是sequence(序列)操作? 字符串的特性被称为sequence(序列) H o w a r e y o u ? 就好像存储在一个个连续的单元格里面,每个单元格存储一个字符,每个字符就是 ...