16
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHPでElasticSearch x kuromojiの形態素解析を行うサンプル

Last updated at Posted at 2016-02-17

#概要
ElasticSearchとkuromojiプラグインの導入方法はこちらをご参照ください。

要は、curlコマンドのコレをphpでやりたい。

$ curl -XGET "localhost:9200/_analyze?analyzer=kuromoji&pretty" -d "お寿司食べたい"
{
  "tokens" : [ {
    "token" : "寿司",
    "start_offset" : 1,
    "end_offset" : 3,
    "type" : "word",
    "position" : 2
  }, {
    "token" : "食べる",
    "start_offset" : 3,
    "end_offset" : 5,
    "type" : "word",
    "position" : 3
  } ]
}

#サンプルコード

だいぶ適当ですが。
ご参考までに。

analyze_word_by_elasticsearch.php
<?php

/**
 * ElasticSearchとのやり取りをするクラス
 */
class ElasticSearchService
{
    /** 接続先のElasticSearchのURL & ポート */
    const BASE_URL = 'localhost';
    const PORT     = '9200';

    /**
     * リクエスト用のオプションを設定
     */
    protected function create_options($url, $body)
    {
        return array(
            CURLOPT_URL            => $url,
            CURLOPT_POST           => true,
            CURLOPT_POSTFIELDS     => $body,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HEADER         => true,
        );
    }

    /**
     * リクエストURLを生成
     */
    protected function create_request_url()
    {
        return self::BASE_URL.':'.self::PORT.'/_analyze?'.http_build_query(array(
            'analyzer' => 'kuromoji', // 形態素解析にkuromojiプラグインを利用する
            'pretty'   => true,
        ));
    }

    /**
     * リクエストを実行
     */
    protected function request($options)
    {
        $ch = curl_init();
        curl_setopt_array($ch, $options);
        $result      = curl_exec($ch);
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
        $header      = substr($result, 0, $header_size);
        $result      = substr($result, $header_size);
        curl_close($ch);

        return array(
            'Header' => $header,
            'Result' => $result,
        );
    }

    /**
     * 指定した文字列を形態素解析し、結果を返却する
     */
    public function analyze_word($text)
    {
        return $this->request($this->create_options($this->create_request_url(), $text));
    }
}

// 引数チェック
if ($argc < 2) {
    exit('引数に解析対象の文字列を指定してください');
}
$text = $argv[1];
$service = new ElasticSearchService();
// 実行 & 結果出力
print_r($service->analyze_word($text));

#実行

$ php analyze_word_by_elasticsearch.php お寿司食べたい
Array
(
    [Header] => HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 257


    [Result] => {
  "tokens" : [ {
    "token" : "寿司",
    "start_offset" : 1,
    "end_offset" : 3,
    "type" : "word",
    "position" : 2
  }, {
    "token" : "食べる",
    "start_offset" : 3,
    "end_offset" : 5,
    "type" : "word",
    "position" : 3
  } ]
}

)

一応、gistにもあげときました。

16
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?