Skip to content

Instantly share code, notes, and snippets.

@tyabe
Created March 6, 2012 16:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyabe/1987380 to your computer and use it in GitHub Desktop.
Save tyabe/1987380 to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
# coding: utf-8
require 'sinatra/base'
require 'active_resource'
require 'haml'
class MAService < ActiveResource::Base
self.site = 'http://jlp.yahooapis.jp'
DEFAULT_PARAMS = {
appid: 'your_appid'
}
class << self
def find_by_sentence(text, params={})
params[:sentence] = text
params = params.merge(DEFAULT_PARAMS)
self.find(:one, from: '/MAService/V1/parse', params: params)
end
def zenrize(text)
result = find_by_sentence(text).word_list
return "" unless result
word = result.word
if word.is_a?(Array)
word.inject(""){ |w,t| w << (t.pos == '動詞' ? "全裸で#{t.surface}" : t.surface) }
else
word.surface
end
end
end
class Format
include ActiveResource::Formats::XmlFormat
def decode(xml)
data = Hash.from_xml(xml)["ResultSet"]["ma_result"]
end
end
self.format = Format.new
end
class ZenraApp < Sinatra::Base
enable :inline_templates
get '/' do
haml :index
end
post '/' do
@result = MAService.zenrize(params[:text]).to_s
haml :result
end
end
ZenraApp.run!
__END__
@@ layout
!!! 5
%html
%head
%title MvcZenra
%body
%h1 zenrize
= yield
@@ index
= haml :form
@@ result
%p
%b&= @result
= haml :form
@@ form
%form{ action: "/", method: "post" }
%textarea{ name: "text", rows: "3", cols: "60" }
%br
%input{ type: "submit", value: "zenrize" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment