温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何使用HanLP增强Elasticsearch分词功能

发布时间:2021-12-16 11:53:11 来源:亿速云 阅读:421 作者:小新 栏目:开发技术

这篇文章主要介绍如何使用HanLP增强Elasticsearch分词功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

Elasticsearch 默认对中文分词是按“字”进行分词的,这是肯定不能达到我们进行分词搜索的要求的。官方有一个 SmartCN 中文分词插件,另外还有一个 IK 分词插件使用也比较广。但这里,我们采用 HanLP 这款 自然语言处理工具 来进行中文分词。

Elasticsearch

Elasticsearch 的默认分词效果是惨不忍睹的。

    GET /_analyze?pretty
    {      "text" : ["广州亿速云计算科技有限公司"]
    }

输出:

{  "tokens": [
    {      "token": "广",      "start_offset": 0,      "end_offset": 1,      "type": "<IDEOGRAPHIC>",      "position": 0
    },
    {      "token": "州",      "start_offset": 1,      "end_offset": 2,      "type": "<IDEOGRAPHIC>",      "position": 1
    },
    {      "token": "亿",      "start_offset": 2,      "end_offset": 3,      "type": "<IDEOGRAPHIC>",      "position": 2
    },
    {      "token": "速",      "start_offset": 3,      "end_offset": 4,      "type": "<IDEOGRAPHIC>",      "position": 3
    },
    {      "token": "云",      "start_offset": 4,      "end_offset": 5,      "type": "<IDEOGRAPHIC>",      "position": 4
    },
    {      "token": "计",      "start_offset": 5,      "end_offset": 6,      "type": "<IDEOGRAPHIC>",      "position": 5
    },
    {      "token": "算",      "start_offset": 6,      "end_offset": 7,      "type": "<IDEOGRAPHIC>",      "position": 6
    },
    {      "token": "科",      "start_offset": 7,      "end_offset": 8,      "type": "<IDEOGRAPHIC>",      "position": 7
    },
    {      "token": "技",      "start_offset": 8,      "end_offset": 9,      "type": "<IDEOGRAPHIC>",      "position": 8
    },
    {      "token": "有",      "start_offset": 9,      "end_offset": 10,      "type": "<IDEOGRAPHIC>",      "position": 9
    },
    {      "token": "限",      "start_offset": 10,      "end_offset": 11,      "type": "<IDEOGRAPHIC>",      "position": 10
    },
    {      "token": "公",      "start_offset": 11,      "end_offset": 12,      "type": "<IDEOGRAPHIC>",      "position": 11
    },
    {      "token": "司",      "start_offset": 12,      "end_offset": 13,      "type": "<IDEOGRAPHIC>",      "position": 12
    }
  ]
}

可以看到,默认是按字进行分词的。

elasticsearch-hanlp

HanLP

HanLP  是一款使用 Java 实现的优秀的,具有如下功能:

  • 中文分词

  • 词性标注

  • 命名实体识别

  • 关键词提取

  • 自动摘要

  • 短语提取

  • 拼音转换

  • 简繁转换

  • 文本推荐

  • 依存句法分析

  • 语料库工具

安装 elasticsearch-hanlp(安装见:https://github.com/hualongdata/hanlp-ext/tree/master/es-plugin)插件以后,我们再来看看分词效果。

    GET /_analyze?pretty
    {      "analyzer" : "hanlp",      "text" : ["广州亿速云计算科技有限公司"]
    }

输出:

{  "tokens": [
    {      "token": "广州",      "start_offset": 0,      "end_offset": 2,      "type": "ns",      "position": 0
    },
    {      "token": "亿速云",      "start_offset": 2,      "end_offset": 5,      "type": "nr",      "position": 1
    },
    {      "token": "计算",      "start_offset": 5,      "end_offset": 7,      "type": "nr",      "position": 2
    },
    {      "token": "科技",      "start_offset": 7,      "end_offset": 9,      "type": "n",      "position": 3
    },
    {      "token": "有限公司",      "start_offset": 9,      "end_offset": 13,      "type": "nis",      "position": 4
    }
  ]
}

以上是“如何使用HanLP增强Elasticsearch分词功能”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI