Elasticsearch:hanlp 中文分詞器


HanLP 中文分詞器是一個開源的分詞器,是專為Elasticsearch而設計的。它是基於HanLP,並提供了HanLP中大部分的分詞方式。它的源碼位於:

https://github.com/KennFalcon/elasticsearch-analysis-hanl

從Elasticsearch 5.2.2開始,一直有跟隨Elasticsearch的不同發行版而更新。

安裝

1) 方式一:

a. 下載對應的release安裝包,最新release包可從baidu盤下載(鏈接:https://pan.baidu.com/s/1mFPNJXgiTPzZeqEjH_zifw 密碼:i0o7)

b. 執行如下命令安裝,其中PATH為插件包絕對路徑:

./bin/elasticsearch-plugin install file://${PATH}

2)方式二:

a. 使用elasticsearch插件腳本安裝command如下:

./bin/elasticsearch-plugin install https://github.com/KennFalcon/elasticsearch-analysis-hanlp/releases/download/v7.4.2/elasticsearch-analysis-hanlp-7.4.2.zip

安裝完后,我們可以使用如下的方式來驗證我們的安裝是否成功:

    $ ./bin/elasticsearch-plugin list
    analysis-hanlp

如果我們安裝時成功的話,我們可以看到上面的輸出。

安裝數據包

release包中存放的為HanLP源碼中默認的分詞數據,若要下載完整版數據包,請查看HanLP Release。

數據包目錄:ES_HOME/plugins/analysis-hanlp

注:因原版數據包自定義詞典部分文件名為中文,這里的hanlp.properties中已修改為英文,請對應修改文件名

重啟Elasticsearch

注:上述說明中的ES_HOME為自己的ES安裝路徑,需要絕對路徑。

這一步非常重要。如果我們不重新啟動,新安裝的分詞器將不會工作。

熱更新

在本版本中,增加了詞典熱更新,修改步驟如下:

a. 在ES_HOME/plugins/analysis-hanlp/data/dictionary/custom目錄中新增自定義詞典
b. 修改hanlp.properties,修改CustomDictionaryPath,增加自定義詞典配置
c. 等待1分鍾后,詞典自動加載

注:每個節點都需要做上述更改

提供的分詞方式說明

  • hanlp: hanlp默認分詞
  • hanlp_standard: 標准分詞
  • hanlp_index: 索引分詞
  • hanlp_nlp: NLP分詞
  • hanlp_n_short: N-最短路分詞
  • hanlp_dijkstra: 最短路分詞
  • hanlp_crf: CRF分詞(已有最新方式)
  • hanlp_speed: 極速詞典分詞

我們來做一個簡單的例子:

    GET _analyze
    {
      "text": "美國阿拉斯加州發生8.0級地震",
      "tokenizer": "hanlp"
    }

那么顯示的結果為:

    {
      "tokens" : [
        {
          "token" : "美國",
          "start_offset" : 0,
          "end_offset" : 2,
          "type" : "nsf",
          "position" : 0
        },
        {
          "token" : "阿拉斯加州",
          "start_offset" : 2,
          "end_offset" : 7,
          "type" : "nsf",
          "position" : 1
        },
        {
          "token" : "發生",
          "start_offset" : 7,
          "end_offset" : 9,
          "type" : "v",
          "position" : 2
        },
        {
          "token" : "8.0",
          "start_offset" : 9,
          "end_offset" : 12,
          "type" : "m",
          "position" : 3
        },
        {
          "token" : "級",
          "start_offset" : 12,
          "end_offset" : 13,
          "type" : "q",
          "position" : 4
        },
        {
          "token" : "地震",
          "start_offset" : 13,
          "end_offset" : 15,
          "type" : "n",
          "position" : 5
        }
      ]
    }

更多詳細閱讀,請參閱鏈接https://github.com/KennFalcon/elasticsearch-analysis-hanlp


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM