Elasticsearchで作る形態素解析サーバ

22
Elasticsearchで作る形態素解析サーバ 10elasticsearch勉強会

Transcript of Elasticsearchで作る形態素解析サーバ

Page 1: Elasticsearchで作る形態素解析サーバ

Elasticsearchで作る形態素解析サーバ

第10回elasticsearch勉強会

Page 2: Elasticsearchで作る形態素解析サーバ

第10回elasticsearch勉強会

■ 名前: 菅谷信介■ 所属: N2SM, Inc.■ オープンソース活動:

• Apache Portals (Jetspeed2) コミッタ• Codehausコミッタ • Seasar Projectコミッタ• CodeLibsプロジェクト運営              などなど・・・

■ Blog: http://www.chazine.com/■ Twitter: https://twitter.com/shinsuke_sugaya/

自己紹介

2

Page 3: Elasticsearchで作る形態素解析サーバ

第10回elasticsearch勉強会

まずは背景から…

3

Page 4: Elasticsearchで作る形態素解析サーバ

第10回elasticsearch勉強会

今回の話の背景

4

■ Elasticsearchにいろいろとデータを入れている

■ そのデータを使って、機械学習をいろいろと適用する

■ 日本語のテキストデータだと、形態素解析が必要

■ Java以外にもPythonを使ってみたり…■ Mecabとか使うなら環境作らないと…■ 動かしたいOSもLinuxだったり、Macだったり…■ Mecabとかチューニングどうするのだろう…

➔ 環境構築とかいろいろと面倒なことが多すぎる…

Page 5: Elasticsearchで作る形態素解析サーバ

第10回elasticsearch勉強会

!?

5

Page 6: Elasticsearchで作る形態素解析サーバ

第10回elasticsearch勉強会

ElasticsearchにAnalyze APIがあるではないか!

6

Page 7: Elasticsearchで作る形態素解析サーバ

第10回elasticsearch勉強会

Analyze APIhttps://www.elastic.co/guide/en/elasticsearch/reference/1.5/indices-analyze.html

7

■ Elasticsearchが提供するRESTなAPI■ 指定したAnalyzerでテキストを分解してくれる

$ curl -XGET 'localhost:9200/_analyze?analyzer=standard' -d 'this is a test'{ "tokens" : [ { "token" : "this", "start_offset" : 0, "end_offset" : 4, "type" : "<ALPHANUM>", "position" : 1 }, { "token" : "is", "start_offset" : 5, "end_offset" : 7,…

Page 8: Elasticsearchで作る形態素解析サーバ

第10回elasticsearch勉強会

日本語だと品詞とかも取得して、

いろいろと調整したいな…

8

Page 9: Elasticsearchで作る形態素解析サーバ

第10回elasticsearch勉強会

Analyze APIで取得できない…

9

Page 10: Elasticsearchで作る形態素解析サーバ

第10回elasticsearch勉強会

!?

10

Page 11: Elasticsearchで作る形態素解析サーバ

第10回elasticsearch勉強会

そういえば、Extend _analyze APIならいろいろと取得できたな…

11

Page 12: Elasticsearchで作る形態素解析サーバ

第10回elasticsearch勉強会

Extend _analyze APIhttps://github.com/johtani/elasticsearch-extended-analyze

12

■ @johtaniさんが提供するプラグイン

■ RESTなAPIでAnalyze情報を細かく取得できる$ curl -XGET 'localhost:9200/_extended_analyze?analyzer=standard' -d 'this is a test'{ "custom_analyzer" : false, "analyzer" : { "standard" : [ { "token" : "this", "start_offset" : 0, "end_offset" : 4, "type" : "<ALPHANUM>", "position" : 1, "extended_attributes" : { "org.apache.lucene.analysis.tokenattributes.PositionLengthAttribute" : { "positionLength" : 1 },

Page 13: Elasticsearchで作る形態素解析サーバ

第10回elasticsearch勉強会

おー、attributeもいろいろ取れて

便利、便利ー

13

Page 14: Elasticsearchで作る形態素解析サーバ

第10回elasticsearch勉強会

…がしかし、ガンガン呼び出すと

遅い…

パフォーマンス問題が…

14

Page 15: Elasticsearchで作る形態素解析サーバ

第10回elasticsearch勉強会

Extend _analyze APIは詳細なAnalyze状況を確認するものだからそもそも用途が間違っている…

15

Page 16: Elasticsearchで作る形態素解析サーバ

第10回elasticsearch勉強会

!?

16

Page 17: Elasticsearchで作る形態素解析サーバ

第10回elasticsearch勉強会

作るしかない!

17

Page 18: Elasticsearchで作る形態素解析サーバ

第10回elasticsearch勉強会

要件「最小限の情報だけ返す」

「複数のAnalyzeを1回で実行」

18

Page 19: Elasticsearchで作る形態素解析サーバ

第10回elasticsearch勉強会

Analyze APIプラグインhttps://github.com/codelibs/elasticsearch-analyze-api

19

■ RESTでAnalyzeするElasticsearchプラグイン

■ 指定されたattribute情報だけを返却する

■ 1リクエストで複数のAnalzyeを実行することが可能

■ Analyze用のインデックスを作成して利用curl -XPOST "localhost:9200/_analyze_api" -d'{ "{target_name1}":{ "index":"{index_name}", "analyzer":"{analyzer_name}" "text":"{target_text1}" }, "{target_name2}":{ "index":"{index_name}", "analyzer":"{analyzer_name}" "text":"{target_text2}" }, … }'

Page 20: Elasticsearchで作る形態素解析サーバ

第10回elasticsearch勉強会

Analyze APIプラグインの例https://github.com/codelibs/elasticsearch-analyze-api

20

$ curl -XPOST “localhost:9200/_analyze_api?start_offset=true" -d '{“text1”:{“index”:”.analyzer”,”analyzer”:”standard”,”text”:”This is a Pen...”},...}’{ “text1”: [ { “term”: ”this”, “start_offset”: 0 }, { “term”: ”is”, “start_offset”: 5 },... “text2”: [ { “term”: ”that” } ...

Page 21: Elasticsearchで作る形態素解析サーバ

第10回elasticsearch勉強会

まとめ

21

Page 22: Elasticsearchで作る形態素解析サーバ

第10回elasticsearch勉強会

■ Analyze APIプラグインを作成■ Analyze(形態素解析)サーバを構築することが可能

➜ 構築が容易(プラグインをインストールするだけ)■ Analyzerの構成をElasticsearchの機能で管理(

➜ 設定の変更もREST APIで可能■ 指定したAttributeだけを取得可能■ 複数のテキストを1つのリクエストで複数のAnalyzeが

可能

まとめ

22