HanLP Analysis for Elasticsearch

基於 HanLP 的 Elasticsearch 中文分詞插件,核心功能:git

 

兼容 ES 5.x-7.x;github

內置詞典,無需額外配置便可使用;elasticsearch

支持用戶自定義詞典;測試

支持遠程詞典熱更新(待開發);spa

內置多種分詞模式,適合不一樣場景;插件

拼音過濾器(待開發);線程

簡繁體轉換過濾器(待開發)。xml

 

版本blog

插件版本和 ES 版本一致,直接下載對應版本的插件進行安裝便可。ip

 

·插件開發完成時,最新版本已經爲 6.5.2 了,因此我的只對典型的版本進行了測試;

·5.X 在 5.0.0、5.5.0 版本進行了測試;

·6.X 在 6.0.0、6.3.0、6.4.一、6.5.1 版本進行了測試;

·7.X 在 7.0.0 版本進行了測試。

 

安裝使用

下載編譯

git clone 對應版本的代碼,打開 pom.xml 文件,修改 <elasticsearch.version>6.5.1</elasticsearch.version> 爲須要的 ES 版本;而後使用 mvn package 生產打包文件,最終文件在 target/release 文件夾下。

 

打包完成後,使用離線方式安裝便可。

 

使用默認詞典

 

·在線安裝:.\elasticsearch-plugin install https://github.com/AnyListen/elasticsearch-analysis-hanlp/releases/download/vA.B.C/elasticsearch-analysis-hanlp-A.B.C.zip

·離線安裝:.\elasticsearch-plugin install file:///FILE_PATH/elasticsearch-analysis-hanlp-A.B.C.zip

離線安裝請把 FILE_PATH 更改成 zip 文件路徑;A、B、C 對應的是 ES 版本號。

 

使用自定義詞典

默認詞典是精簡版的詞典,可以知足基本需求,可是沒法使用感知機和 CRF 等基於模型的分詞器。

 

HanLP 提供了更加完整的詞典,請按需下載。

 

詞典下載後,解壓到任意目錄,而後修改插件安裝目錄下的 hanlp.properties 文件,只需修改第一行

 

root=D:/JavaProjects/HanLP/

爲 data 的父目錄便可,好比 data 目錄是 /Users/hankcs/Documents/data,那麼 root=/Users/hankcs/Documents/。

 

使用自定義配置文件

若是你在其餘地方使用了 HanLP,但願可以複用 hanlp.properties 文件,你只須要修改插件安裝目錄下的 plugin.properties 文件,將 configPath 配置爲已有的 hanlp.properties 文件地址便可。

 

內置分詞器

 

分析器(Analysis)

 

·hanlp_index:細粒度切分

·hanlp_smart:常規切分

·hanlp_nlp:命名實體識別

·hanlp_per:感知機分詞

·hanlp_crf:CRF分詞

·hanlp:自定義

 

分詞器(Tokenizer)

·hanlp_index:細粒度切分

·hanlp_smart:常規切分

·hanlp_nlp:命名實體識別

·hanlp_per:感知機分詞

·hanlp_crf:CRF分詞

·hanlp:自定義

 

自定義分詞器

插件有較爲豐富的選項容許用戶自定義分詞器,下面是可用的配置項:

 

案例展現:

 

# 建立自定義分詞器

PUT my_index

{

  "settings": {

    "analysis": {

      "analyzer": {

        "my_analyzer": {

          "type": "hanlp",

          "algorithm": "viterbi",

          "enableIndexMode": "true",

          "enableCustomDictionary": "true",

          "customDictionaryPath": "",

          "enableCustomDictionaryForcing": "false",

          "enableStopWord": "true",

          "stopWordDictionaryPath": "",

          "enableNumberQuantifierRecognize": "true",

          "enableNameRecognize": "true",

          "enableTranslatedNameRecognize": "true",

          "enableJapaneseNameRecognize": "true",

          "enableOrganizationRecognize": "true",

          "enablePlaceRecognize": "true",

          "enableTraditionalChineseMode": "false"

        }

      }

    }

  }

}

 

# 測試分詞器

POST my_index/_analyze

{

  "analyzer": "my_analyzer",

  "text": "張惠妹在上海市舉辦演唱會啦"

}

分詞速度(僅供參考)

 

藉助 _analyze API(1核1G單線程),經過改變分詞器類型,對 2W 字的文本進行分詞,如下爲從請求到返回的耗時:

相關文章
相關標籤/搜索