MeCab REST WebサービスAPIサンプル

概要

日本語形態素解析器 MeCab の REST Web Service を使用したサンプルです。
以下のページで公開されているAPIを使用させていただいています。
http://rikima.dip.jp:8081/rikima/zope/mecab-rest-w...

Ruby on Railsで作成しています。

サンプルページ

文章を入力してボタンをクリックすると解析結果が下に表示されます。
http://sample.sakura-computer.co.jp/mecab

参考文献・リンク


MeCab REST Web Service
Rails API ドキュメント
逆引きRuby

サンプル作成手順

Ruby on Railsをインストールした環境にてサンプルを作成します。
各アプリケーションのバージョンは以下の通りです。
Ruby 1.8.6
Rails 1.2.3

アプリケーションの作成

% rails samples

コントローラ・ビューの作成

% cd samples
samples% ruby script/generate controller mecab index
上記コマンド実行後、samples/app/controllers/mecab_controller.rb、samples/app/views/mecab/index.rhtml、samples/app/views/layouts/mecab.rhtmlが作成されるのでそれを編集します。

ソースコード

samples/app/controllers/mecab_controller.rb

# $Id: mecab_controller.rb 35 2008-03-01 12:33:22Z tabi $
require "cgi"
require "open-uri"
require "rexml/document"
require "ruby-debug"

class MecabController < ApplicationController
  MECABQ = "http://rikima.dip.jp/xfy/mecab/mecab_server"

  attr_reader :morphs
  attr_accessor :sentence

  def index
    @sentence = session[:sentence]
    @morphs = session[:morphs] ||= []
  end

  def mecab
    @sentence = params[:sentence]
    if @sentence && @sentence.length > 0
      @morphs = get_morphs(@sentence)
    else
      @morphs = []
    end
    session[:sentence] = @sentence
    session[:morphs] = @morphs
    redirect_to :action => :index
  end

  private
  def get_morphs(sentence)
    morphs = []
    mquery = MECABQ + "?sentence=" + CGI.escape(sentence)
    mxml = REXML::Document.new(open(mquery))

    # 単語解析結果のループ
    mxml.elements.each("mecab:result/mecab:morph") do |morph|

      # 結果がある場合だけ処理
      surface = morph.elements["mecab:surface"].text
      if surface && surface != ""
        morphs << {
          :surface => surface,
          :dpos1 => (morph.elements["mecab:feature/mecab:dpos1"] ? morph.elements["mecab:feature/mecab:dpos1"].text : ""),
          :dpos2 => (morph.elements["mecab:feature/mecab:dpos2"] ? morph.elements["mecab:feature/mecab:dpos2"].text : ""),
          :dpos3 => (morph.elements["mecab:feature/mecab:dpos3"] ? morph.elements["mecab:feature/mecab:dpos3"].text : ""),
          :base => (morph.elements["mecab:feature/mecab:base"] ? morph.elements["mecab:feature/mecab:base"].text : ""),
          :reading => (morph.elements["mecab:feature/mecab:reading"] ? morph.elements["mecab:feature/mecab:reading"].text : ""),
          :pronounciation => (morph.elements["mecab:feature/mecab:pronounciation"] ? morph.elements["mecab:feature/mecab:pronounciation"].text : "")
        }
      end
    end
    morphs
  end
end

samples/app/views/mecab/index.rhtml

<%# $Id: index.rhtml 30 2008-03-01 11:58:42Z tabi $ %>
<h1>MeCab REST Web Service Sample</h1>
<h3>解析したい文章を入力してボタンをクリック</h3>
<% form_tag :action =>:mecab do %>
  <%= text_field_tag :sentence, @sentence, {"size" => 50} %>
  <%= submit_tag "解析する" %>
<% end %>
<h3>解析結果</h3>
<table cellpadding="5" cellspacing="1" border="1">
  <tr>
    <td>surface</td><td>dpos1</td><td>dpos2</td><td>dpos3</td><td>base</td><td>reading</td><td>pronounciation</td>
  </tr>
  <% for morph in @morphs %>
    <tr>
      <td><%= h(morph[:surface]) %></td>
      <td><%= h(morph[:dpos1]) %></td>
      <td><%= h(morph[:dpos2]) %></td>
      <td><%= h(morph[:dpos3]) %></td>
      <td><%= h(morph[:base]) %></td>
      <td><%= h(morph[:reading]) %></td>
      <td><%= h(morph[:pronounciation]) %></td>
    </tr>
  <% end %>
</table>

samples/app/views/layouts/mecab.rhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>MeCab REST Webサービスサンプル</title>
  </head>
  <body>
    <%= yield :layout %>
    <h5>Powered by <a href="http://rikima.dip.jp:8081/rikima/zope/mecab-rest-web-service" target="_blank">MeCab REST Web Service</a></h5>
    <h5><a href="http://wiki.livedoor.jp/y_tabira/d/FrontPage" target="_blank">WebサービスAPIサンプルWiki</a></h5>
    <h6>$Id: mecab.rhtml 36 2008-03-01 12:37:29Z tabi $</h>
  </body>
</html>

railsサーバの起動

以下のコマンドを実行します。
samples% ruby script/server
2008年03月02日(日) 02:52:09 Modified by y_tabira




スマートフォン版で見る