LoginSignup
0
0

More than 5 years have passed since last update.

Groongaのサジェスト機能を手軽に試す

Posted at

前回「そもそも間違わないようにしてあげる」という部分が試せなかったので、今回はサンプルを使って実現してみた。
なお、今回取り扱う内容はここにドキュメントされている。

前提

  • OSX Yosemite
  • Ruby 2.0以上がインストール済み
  • groongaはインストール済み(homebrew)

手順

1. データベースの準備

まずはデータベースをを作る(今回は/tmp/db/suggest_db)

groonga -n /tmp/db/suggest_db quit

groonga-suggest-create-dataset db/suggest_db dictionary

2. データの投入

今回は、groongaのソースコードに同梱されているツールを使って、辞書を用意した。
以下のツール(edict-import.sh)でデータをダウンロードして、先ほど作った/tmp/db/suggest_dbに投入している。

データ投入
wget http://packages.groonga.org/source/groonga/groonga-6.0.5.tar.gz
tar zxvf //groonga-6.0.5.tar.gz
cd /groonga-6.0.5/examples/dictionary/edict/
./edict-import.sh /tmp/db/suggest_db

注意: OSXで動かしたい場合は、 ./edict-import.shの中のzcatgzcatに書き換える必要あり (2016.07.01時点)

うまくいくと、以下のように224760件(2016.07.01時点)のデータが投入された事が分かる。

データの投入を確認
groonga db/suggest_db select item_dictionary

[[0,1467373551.1142,0.00647878646850586],[[[224760],[["_id","UInt32"],["_key","ShortText"],["boost","Int32"],["buzz","Int32"],["co","UInt32"],["edict_desc","ShortText"],["freq","Int32"],["freq2","Int32"],["kana","kana"],["last","Time"]],[1," ???",0,0,0,"EDICT, EDICT_SUB(P), EDICT2 Japanese-English Electronic Dictionary Files/Copyright Electronic Dictionary Research & Development Group - 2014/Created: 2016-06-30/",0,0,[" ???"],0.0],[22,"(w",0,0,0,"[わらい] (n) (abbr) (sl) LOL/haha/",0,0,["ワライ"],0.0],[23,"+α",0,0,0,"[プラスアルファ] (n) a little more than usual (wasei: plus alpha)/bribe money added to a regular fee/and then some/",0,0,["プラスアルファ"],0.0],[40,"090金融",0,0,0,"[ゼロキューゼロきんゆう] (n) \"090\" financing/loan sharks operating from a cellphone (i.e. without a fixed phone number)/",0,0,["ゼロキューゼロキンユウ"],0.0],[41,"1",0,0,0,"[いち] (num,pref) (1) one/(adj-no,suf) (2) best/(adj-no) (3) first/foremost/(n) (4) beginning/start/(5) bottom string (on a shamisen, etc.)/",0,0,["イチ"],0.0],[42,"1.5次会",0,0,0,"[いってんごじかい] (n) informal wedding ceremony/",0,0,["イッテンゴジカイ"],0.0],[43,"1000円",0,0,0,"[せんえん] (n) 1000 yen/(P)/",0,0,["センエン"],0.0],[44,"100円ショップ",0,0,0,"[ひゃくえんショップ] (n) hundred-yen store/(P)/",0,0,["ヒャクエンショップ"],0.0],[46,"100円均一",0,0,0,"[ひゃくえんきんいち] (n) (abbr) hundred-yen store/100 yen store/",0,0,["ヒャクエンキンイチ"],0.0],[47,"100円均一ショップ",0,0,0,"[ひゃくえんきんいちショップ] (n) hundred-yen store/100 yen store/",0,0,["ヒャクエンキンイチショップ"],0.0]]]]

3. 実際に試してみる

以下のコマンドを実行して、groongaをhttpサーバモードで起動する。

groonga --document-root groonga-6.0.5/examples/dictionary/html --protocol http -s /tmp/db/suggest_db

その上で、http://localhost:10041にアクセスするとサジェストが試せる。
なお、実際にgroongaサーバにクエリを投げているのはgroonga-6.0.5/examples/dictionary/html/js/dictionary.jsなので、どんなクエリを投げているかはこのファイルの65行目付近を参照の事。

おまけ

  • 上記のhttpサーバモードのgroongaをプロダクション環境でそのまま使っていいの?

    • 以下の問題点が解決できるならOK
      • groongaのhttpサーバモードはシャットダウンなどのコマンドもURLで受け付けてしまうため、不要なコマンドをきちんとアクセス出来ないようにする必要がある。
        • /d/suggestと、監視用の/d/status以外は止めてよい。
      • サジェスト機能でユーザーの入力を学習させたい場合は、ユーザーのアクセスログの収集と解析が必要だが、groongaのhttpサーバモードではログを取得できない。
        • 取得したい場合は、自前のアプリを前段に置いてgroongaへのアクセスを中継する。もしくは、groonga-suggest-httpdというツールがあるので、そっちを使う。
          • groonga-suggest-httpdは現在のhomebrew版のgroongaでは動かない :sob:
  • 上記の話題の流れの中で、groongaをサービスとして動かせないと運用が大変ではと言ったら、ちゃんとパッケージがある事を教えていただいた。

    • 詳細はこちら
    • こちらはNginxベースなので特定のURL以外を止めたりするのやりやすそう。
0
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
0
0