您好,登錄后才能下訂單哦!
這篇文章主要介紹如何安裝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)代漢語補充詞庫.txt; 全國地名大全.txt ns; 人名詞典.txt; 機構(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等)上運行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)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。