[Elasticsearch] Nori 사전 빌드하기.

Elastic/Elasticsearch 2021. 5. 11. 20:51

[추가 사항]

https://issues.apache.org/jira/browse/SOLR-12655?focusedCommentId=16604160&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel&fbclid=IwAR3jRIpaCQ497v-qhofkc3DVmNabPab1ErDQhXnOsA0LNoqpHypa5cUSpy0#comment-16604160

 

아래 발생한 오류는 UnknownDictionaryBuilder.java 에서 아래 코드 수정으로 해결 되었습니다.

기본적으로 ivy.xml, build,xml 에 보면 사전 버전 정보가 들어가 있습니다.

이 사전이 변경 되면서 POS tag list 가 달라 졌는데요. 이 영향으로 에러가 발생 하게 됩니다.

private static final String NGRAM_DICTIONARY_ENTRY = "NGRAM,1801,3561,3668,SY,*,*,*,*,*,*,*";

코드를 수정 하지 않으려면 사전 버전을 맞춰서 사용 하시면 됩니다.

 

Elasticsearch User Group 에 #유정인 님이 도움 주셨습니다.


https://github.com/jimczi/nori/blob/master/how-to-custom-dict.asciidoc

https://bitbucket.org/eunjeon/mecab-ko/src/mecab-0.996/
https://bitbucket.org/eunjeon/mecab-ko-dic/src/v2.1.1/

$ git clone https://bitbucket.org/eunjeon/mecab-ko.git
$ git checkout tags/mecab-0.996
$ ./configure
$ make
$ sudo make install
$ mecab -v

https://bitbucket.org/eunjeon/mecab-ko-dic/downloads/

$ wget https://bitbucket.org/eunjeon/mecab-ko-dic/downloads/mecab-ko-dic-2.1.1-20180720.tar.gz
$ tar -xvzf mecab-ko-dic-2.1.1-20180720.tar.gz
$ cd mecab-ko-dic-2.1.1-20180720
$ brew install autoconf automake libtool
$ autoreconf
$ ./configure
$ make
$ sudo make install
$ ./tools/add-userdic.sh

$ tar cvzf custom-mecab-ko-dic.tar.gz mecab-ko-dic-2.1.1-20180720
$ git clone https://github.com/apache/lucene.git
$ git checkout tags/releases/lucene-solr/8.8.1
$ vi lucene/analysis/nori/ivy.xml

~       <!--artifact name="mecab-ko-dic" type=".tar.gz" url="https://bitbucket.org/eunjeon/mecab-ko-dic/downloads/mecab-ko-dic-2.0.3-20170922.tar.gz" /-->
+         <artifact name="mecab-ko-dic" type=".tar.gz" url="file:///Users/mzc02-henryjeong/Temp/fastcampus/analysis-nori/custom-mecab-ko-dic.tar.gz" />

 

$ vi lucene/analysis/nori/build.xml

~   <!--property name="dict.version" value="mecab-ko-dic-2.0.3-20170922" /-->
+   <property name="dict.version" value="mecab-ko-dic-2.1.1-20180720" />

 

$ cd lucene/analysis/nori
// apache ant 설치
$ mkdir -p ~/.ant/lib
$ ant ivy-bootstrap
$ ant regenerate
build-dict:
[delete] Deleting /Users/mzc02-henryjeong/Temp/analysis-nori/lucene/lucene/analysis/nori/src/resources/org/apache/lucene/analysis/ko/dict/TokenInfoDictionary$buffer.dat
[delete] Deleting /Users/mzc02-henryjeong/Temp/analysis-nori/lucene/lucene/analysis/nori/src/resources/org/apache/lucene/analysis/ko/dict/TokenInfoDictionary$fst.dat
[delete] Deleting /Users/mzc02-henryjeong/Temp/analysis-nori/lucene/lucene/analysis/nori/src/resources/org/apache/lucene/analysis/ko/dict/TokenInfoDictionary$posDict.dat
[delete] Deleting /Users/mzc02-henryjeong/Temp/analysis-nori/lucene/lucene/analysis/nori/src/resources/org/apache/lucene/analysis/ko/dict/TokenInfoDictionary$targetMap.dat
[java] Exception in thread "main" java.lang.AssertionError
[java] at org.apache.lucene.analysis.ko.util.BinaryDictionaryWriter.put(BinaryDictionaryWriter.java:112)
[java] at org.apache.lucene.analysis.ko.util.UnknownDictionaryWriter.put(UnknownDictionaryWriter.java:39)
[java] at org.apache.lucene.analysis.ko.util.UnknownDictionaryBuilder.readDictionaryFile(UnknownDictionaryBuilder.java:71)
[java] at org.apache.lucene.analysis.ko.util.UnknownDictionaryBuilder.readDictionaryFile(UnknownDictionaryBuilder.java:47)
[java] at org.apache.lucene.analysis.ko.util.UnknownDictionaryBuilder.build(UnknownDictionaryBuilder.java:41)
[java] at org.apache.lucene.analysis.ko.util.DictionaryBuilder.build(DictionaryBuilder.java:39)
[java] at org.apache.lucene.analysis.ko.util.DictionaryBuilder.main(DictionaryBuilder.java:52)

BUILD FAILED
$ git status .
HEAD detached at releases/lucene-solr/8.8.1
Changes not staged for commit:
(use "git add/rm ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: build.xml
modified: ivy.xml
deleted: src/resources/org/apache/lucene/analysis/ko/dict/CharacterDefinition.dat
deleted: src/resources/org/apache/lucene/analysis/ko/dict/ConnectionCosts.dat
modified: src/resources/org/apache/lucene/analysis/ko/dict/TokenInfoDictionary$buffer.dat
modified: src/resources/org/apache/lucene/analysis/ko/dict/TokenInfoDictionary$fst.dat
modified: src/resources/org/apache/lucene/analysis/ko/dict/TokenInfoDictionary$posDict.dat
modified: src/resources/org/apache/lucene/analysis/ko/dict/TokenInfoDictionary$targetMap.dat
deleted: src/resources/org/apache/lucene/analysis/ko/dict/UnknownDictionary$buffer.dat
deleted: src/resources/org/apache/lucene/analysis/ko/dict/UnknownDictionary$posDict.dat
deleted: src/resources/org/apache/lucene/analysis/ko/dict/UnknownDictionary$targetMap.dat
$ ant jar
...중략...
-jar-core:
[jar] Building jar: /Users/mzc02-henryjeong/Temp/analysis-nori/lucene/lucene/build/analysis/nori/lucene-analyzers-nori-8.8.1-SNAPSHOT.jar
...중략...

/Users/mzc02-henryjeong/Works/app/apache-ant-1.10.10

일단 시간이 별로 없어서 이 정도까지만 테스트 하고 오류는 나중에 심각 하게 살펴 보겠습니다.

Arirang 만 잘 해도 되는데 Nori 도 할 줄 알아야 하니까...
근데 사전 관리 방식은 Arirang 이 편하고 좋습니다.

사실 한자 사전 고치려다가 여기까지 왔네요.

: