共計 3878 個字符,預(yù)計需要花費 10 分鐘才能閱讀完成。
這篇文章主要介紹如何安裝 elasticsearch 中文切詞插件 hanlp,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
hanlp 好處的, 就是它的 data 字典比較齊全.
github 上有國人寫 hanlp 支持 es 的插件
https://github.com/pengcong90/elasticsearch-analysis-hanlp
下載它的安裝
release
包
下載發(fā)現(xiàn)解壓按它的安裝要求總找不到 hanlp.properties 文件
將源碼 git 下來, 發(fā)現(xiàn)路徑有問題.
package org.elasticsearch.index.analysis;import com.hankcs.hanlp.HanLP;import com.hankcs.hanlp.utility.Predefine;import com.hankcs.lucene4.HanLPIndexAnalyzer;import org.elasticsearch.common.inject.Inject;import org.elasticsearch.common.inject.assistedinject.Assisted;import org.elasticsearch.common.settings.Settings;import org.elasticsearch.env.Environment;import org.elasticsearch.index.IndexSettings;/**
*/public class HanLPAnalyzerProvider extends AbstractIndexAnalyzerProvider HanLPIndexAnalyzer { private final HanLPIndexAnalyzer analyzer; private static String sysPath = String.valueOf(System.getProperties().get(user.dir)); @Inject
public HanLPAnalyzerProvider(IndexSettings indexSettings, Environment env, @Assisted String name, @Assisted Settings settings) { super(indexSettings, name, settings); // 原來路徑
//Predefine.HANLP_PROPERTIES_PATH = sysPath.substring(0, sysPath.length()-4) + /plugins/analysis-hanlp/hanlp.properties
// 修改后正確路徑
Predefine.HANLP_PROPERTIES_PATH = sysPath + /plugins/analysis-hanlp/hanlp.properties
analyzer = new HanLPIndexAnalyzer(true);
}
public static HanLPAnalyzerProvider getIndexAnalyzerProvider(IndexSettings indexSettings, Environment env, String name, Settings settings) { return new HanLPAnalyzerProvider(indexSettings, env, name, settings);
}
public static HanLPAnalyzerProvider getSmartAnalyzerProvider(IndexSettings indexSettings, Environment env, String name, Settings settings) { return new HanLPAnalyzerProvider(indexSettings, env, name, settings);
} @Override
public HanLPIndexAnalyzer get() { return this.analyzer;
}
}
因為它的 hanlp 版本是 1.2.8, 最新版本是 1.5.4
修改 pom.xml 為
dependency
groupId com.hankcs /groupId
artifactId hanlp /artifactId
version portable-1.5.4 /version
!-- systemPath ${pom.basedir}/lib/hanlp-1.2.8.jar /systemPath --
!-- scope system /scope --
/dependency
打包編譯
在 $ES_HOME 下 /plugins 建立 analysis-hanlp 文件
目錄下結(jié)構(gòu)為
hanlp.properties 屬性 (可以直接從
https://github.com/hankcs/HanLP
的 realease 下載修改 root 路徑就行了 )
#本配置文件中的路徑的根目錄,根目錄 + 其他路徑 = 完整路徑(支持相對路徑,請參考:https://github.com/hankcs/HanLP/pull/254)#Windows 用戶請注意,路徑分隔符統(tǒng)一使用 /root=/opt/elasticsearch-5.5.1/plugins/analysis-hanlp#核心詞典路徑 CoreDictionaryPath=data/dictionary/CoreNatureDictionary.txt#2 元語法詞典路徑 BiGramDictionaryPath=data/dictionary/CoreNatureDictionary.ngram.txt#停用詞詞典路徑 CoreStopWordDictionaryPath=data/dictionary/stopwords.txt#同義詞詞典路徑 CoreSynonymDictionaryDictionaryPath=data/dictionary/synonym/CoreSynonym.txt#人名詞典路徑 PersonDictionaryPath=data/dictionary/person/nr.txt#人名詞典轉(zhuǎn)移矩陣路徑 PersonDictionaryTrPath=data/dictionary/person/nr.tr.txt#繁簡詞典根目錄 tcDictionaryRoot=data/dictionary/tc#自定義詞典路徑,用; 隔開多個自定義詞典,空格開頭表示在同一個目錄,使用“文件名 詞性”形式則表示這個詞典的詞性默認(rèn)是該詞性。優(yōu)先級遞減。#另外 data/dictionary/custom/CustomDictionary.txt 是個高質(zhì)量的詞庫,請不要刪除。所有詞典統(tǒng)一使用 UTF- 8 編碼。CustomDictionaryPath=data/dictionary/custom/CustomDictionary.txt; 現(xiàn)代漢語補(bǔ)充詞庫.txt; 全國地名大全.txt ns; 人名詞典.txt; 機(jī)構(gòu)名詞典.txt; 上海地名.txt ns;data/dictionary/person/nrf.txt nrf;#CRF 分詞模型路徑 CRFSegmentModelPath=data/model/segment/CRFSegmentModel.txt#HMM 分詞模型 HMMSegmentModelPath=data/model/segment/HMMSegmentModel.bin#分詞結(jié)果是否展示詞性 ShowTermNature=true#IO 適配器,實現(xiàn) com.hankcs.hanlp.corpus.io.IIOAdapter 接口以在不同的平臺(Hadoop、Redis 等)上運(yùn)行 HanLP# 默認(rèn)的 IO 適配器如下,該適配器是基于普通文件系統(tǒng)的。#IOAdapter=com.hankcs.hanlp.corpus.io.FileIOAdapter
plugin-descriptor.properties 和 plugin-security.policy 屬性按
elasticsearch-analysis-hanlp
的 release 包屬性修改.
修改 ES 啟動, 并啟動
vim /opt/elasticsearch-5.5.1config/jvm.options # 新增 -Djava.security.policy=/opt/elasticsearch-5.5.1/plugins/analysis-hanlp/plugin-security.policy
測試安裝成功否命令
GET /_analyze?analyzer=hanlp-index pretty=true {
text : 公安部:各地校車將享最高路權(quán) }
data 字典文件從
https://github.com/hankcs/HanLP/releases
下載, 解壓就行了.
以上是“如何安裝 elasticsearch 中文切詞插件 hanlp”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注丸趣 TV 行業(yè)資訊頻道!