LoginSignup
1
0

More than 3 years have passed since last update.

日本語形態素解析エンジン「kuromoji」を使ってみた

Posted at

日本語形態素解析エンジン「kuromoji」を使ってみた

ElasticSearchに、日本語形態素解析エンジン「kuromoji」のプラグインをインストールしたため、せっかくなので少しだけ遊んでみた。

実行環境

実行するOS環境は以下である。

$ cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)

ElasticSearchのプラグインインストール状況は以下である。

$ curl -XGET localhost:9200/_cat/plugins?v
name          component         version
elasticsearch analysis-icu      7.9.1
elasticsearch analysis-kuromoji 7.9.1

kuromojiを使用しないで分析する ※ElasticSearchのデフォルト分析を使用する

ElasticSearchのデフォルト分析APIに、「吾輩は猫である」の文字列をリクエストするため、以下のようなjsonファイルを用意した。

$ cat standard.json 
{
  "analyzer" : "standard",
  "text" : "吾輩は猫である"
}'

以下コマンドを実行して、リクエスト結果を確認した。なお、コマンド後半のpython3の実行は、jsonを整形するためのコマンドである。

$ curl -XGET 'localhost:9200/_analyze' -H 'Content-Type: application/json' -d @standard.json | python3 -c 'import sys,json;print(json.dumps(json.loads(sys.stdin.read()),indent=4,ensure_ascii=False))'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   651  100   588  100    63  11094   1188 --:--:-- --:--:-- --:--:-- 12519
{
    "tokens": [
        {
            "token": "吾",
            "start_offset": 0,
            "end_offset": 1,
            "type": "<IDEOGRAPHIC>",
            "position": 0
        },
        {
            "token": "輩",
            "start_offset": 1,
            "end_offset": 2,
            "type": "<IDEOGRAPHIC>",
            "position": 1
        },
        {
            "token": "は",
            "start_offset": 2,
            "end_offset": 3,
            "type": "<HIRAGANA>",
            "position": 2
        },
        {
            "token": "猫",
            "start_offset": 3,
            "end_offset": 4,
            "type": "<IDEOGRAPHIC>",
            "position": 3
        },
        {
            "token": "で",
            "start_offset": 4,
            "end_offset": 5,
            "type": "<HIRAGANA>",
            "position": 4
        },
        {
            "token": "あ",
            "start_offset": 5,
            "end_offset": 6,
            "type": "<HIRAGANA>",
            "position": 5
        },
        {
            "token": "る",
            "start_offset": 6,
            "end_offset": 7,
            "type": "<HIRAGANA>",
            "position": 6
        }
    ]
}

kuromojiを使用して分析する

ElasticSearchのkuromojiプラグインに、「吾輩は猫である」の文字列をリクエストするため、以下のようなjsonファイルを用意した。

$ cat kuromoji.json 
{
  "analyzer" : "kuromoji",
  "text" : "吾輩は猫である"
}'

以下コマンドを実行して、リクエスト結果を確認した。なお、コマンド後半のpython3の実行は、jsonを整形するためのコマンドである。

$ curl -XGET 'localhost:9200/_analyze' -H 'Content-Type: application/json' -d @kuromoji.json
 | python3 -c 'import sys,json;print(json.dumps(json.loads(sys.stdin.read()),indent=4,ensure_ascii=False))'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   228  100   165  100    63   1187    453 --:--:-- --:--:-- --:--:--  1640
{
    "tokens": [
        {
            "token": "吾輩",
            "start_offset": 0,
            "end_offset": 2,
            "type": "word",
            "position": 0
        },
        {
            "token": "猫",
            "start_offset": 3,
            "end_offset": 4,
            "type": "word",
            "position": 2
        }
    ]
}
1
0
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
1
0