概要
Elasticsearchの日本語の形態素解析をする際に利用されるkuromojiは非常に便利ですが、その辞書であるIPADICは更新が止まっているためやや古い状態です。
その辞書を更新してくださった方がいらっしゃり、neologdとして公開されているためそれを導入して新語でもきちんと解析できるようにします。
環境
- Elasticsearch 2.3.2
- kuromoji 2.3.2
- neologd 5.5.0-20160411
プラグインインストール
kuromoji
# ./bin/plugin install analysis-kuromoji
neologd
./bin/plugin install org.codelibs/elasticsearch-analysis-kuromoji-neologd/2.3.0
最新版だと以下の様なwarningが出るので「y」でインストールを進めてください。
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: plugin requires additional permissions @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ * java.lang.RuntimePermission getClassLoader * java.lang.reflect.ReflectPermission suppressAccessChecks See http://docs.oracle.com/javase/8/docs/technotes/guides/security/permissions.html for descriptions of what these permissions allow and the associated risks. Continue with installation? [y/N]y
インストールしたら再起動します。
$ sudo service elasticsearch restart
アナライザの設定
kuromoji
$ curl -XPUT 'http://localhost:9200/kuromoji/' -d'
{
"settings": {
"index":{
"analysis":{
"analyzer" : {
"default" : {
"tokenizer" : "kuromoji_tokenizer"
}
}
}
}
}
}
'
neologd
kuromoji_neologd_tokenizerというトークナイザーを使います。
$ curl -XPUT 'http://localhost:9200/neologd/' -d'
{
"settings": {
"index":{
"analysis":{
"analyzer" : {
"default" : {
"tokenizer" : "kuromoji_neologd_tokenizer"
}
}
}
}
}
}
'
動作確認
こんにちは世界
まずデフォルトのkuromojiから
$ curl 'localhost:9200/kuromoji/_analyze?pretty' -d 'こんにちは世界'
{
"tokens" : [ {
"token" : "こんにちは",
"start_offset" : 0,
"end_offset" : 5,
"type" : "word",
"position" : 0
}, {
"token" : "世界",
"start_offset" : 5,
"end_offset" : 7,
"type" : "word",
"position" : 1
} ]
}
ちゃんと解析出来てます。
次にneologd
$ curl 'localhost:9200/neologd/_analyze?pretty' -d 'こんにちは世界'
{
"tokens" : [ {
"token" : "こんにちは",
"start_offset" : 0,
"end_offset" : 5,
"type" : "word",
"position" : 0
}, {
"token" : "世界",
"start_offset" : 5,
"end_offset" : 7,
"type" : "word",
"position" : 1
} ]
}
こちらも大丈夫です。
きゃりーぱみゅぱみゅ
次は芸能人などの固有名詞で試してみます。
$ curl 'localhost:9200/kuromoji/_analyze?pretty' -d 'きゃりーぱみゅぱみゅ'
{
"tokens" : [ {
"token" : "きゃ",
"start_offset" : 0,
"end_offset" : 2,
"type" : "word",
"position" : 0
}, {
"token" : "り",
"start_offset" : 2,
"end_offset" : 3,
"type" : "word",
"position" : 1
}, {
"token" : "ー",
"start_offset" : 3,
"end_offset" : 4,
"type" : "word",
"position" : 2
}, {
"token" : "ぱみゅぱみゅ",
"start_offset" : 4,
"end_offset" : 10,
"type" : "word",
"position" : 3
} ]
}
やはり辞書データが古いため、分割されてしまいます。
一方neologdは
$ curl 'localhost:9200/neologd/_analyze?pretty' -d 'きゃりーぱみゅぱみゅ'
{
"tokens" : [ {
"token" : "きゃりーぱみゅぱみゅ",
"start_offset" : 0,
"end_offset" : 10,
"type" : "word",
"position" : 0
} ]
}
ちゃんと1語で認識してくれてますね。
ラブライブ!
続いてアニメタイトルを試してみます。
$ curl 'localhost:9200/kuromoji/_analyze?pretty' -d 'ラブライブ!'
{
"tokens" : [ {
"token" : "ラブ",
"start_offset" : 0,
"end_offset" : 2,
"type" : "word",
"position" : 0
}, {
"token" : "ライブ",
"start_offset" : 2,
"end_offset" : 5,
"type" : "word",
"position" : 1
} ]
}
予測通りですがラブとライブで分割されました。
一方neologdは
$ curl 'localhost:9200/neologd/_analyze?pretty' -d 'ラブライブ!'
{
"tokens" : [ {
"token" : "ラブライブ!",
"start_offset" : 0,
"end_offset" : 6,
"type" : "word",
"position" : 0
} ]
}
これもきれいに認識されてます。