2009年9月21日月曜日

[Rails][WebAPI] ActiveResourceでYahoo!API (3)

ルビ振り校正支援日本語係り受け解析キーフレーズ抽出 のクライアントを追加。

ルビ振り


モデル(ActiveResource)のコード。
# app/models/furigana/word.rb

class Furigana::Word < ActiveResource::Base
class Format
def extension() '' end
def mime_type() 'application/xml' end
def encode(hash, options = {}) hash.to_xml(options) end
def decode(xml)
data = Hash.from_xml(xml)['ResultSet']['Result']['WordList']['Word']
data = [ data ] if data.is_a?(Hash)
data.each{ |word|
word['Furigana'] = nil if !word.key?('Furigana')
word['Roman'] = nil if !word.key?('Roman')
}
end
end

self.site = 'http://jlp.yahooapis.jp'
self.format = Format.new

class << self
def collection_path(prefix_options = {}, query_options = nil)
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
"/FuriganaService/V1/furigana#{query_string(query_options)}"
end
end
end


Furigana::Word.find(:all,:params=>params)

(params はリクエストパラメータのハッシュ)
で、レスポンスXMLの /ResultSet/Result/Word 要素をActiveResourceに変換したオブジェクトのリストが取得できる。

校正支援


モデルのコード。
# app/models/kousei/result.rb

class Kousei::Result < ActiveResource::Base
class Format
def extension() '' end
def mime_type() 'application/xml' end
def encode(hash, options = {}) hash.to_xml(options) end
def decode(xml)
data = Hash.from_xml(xml)['ResultSet']['Result']
data = [ data ] if data.is_a?(Hash)
end
end

self.site = 'http://jlp.yahooapis.jp'
self.format = Format.new

class << self
def collection_path(prefix_options = {}, query_options = nil)
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
"/KouseiService/V1/kousei#{query_string(query_options)}"
end
end
end


Kousei::Result.find(:all,:params=>params)

で、レスポンスXMLの /ResultSet/Result 要素をActiveResourceに変換したオブジェクトのリストが取得できる。

日本語係り受け解析


モデルのコード。
# app/models/da_service/chunk.rb

class DaService::Chunk < ActiveResource::Base
class Format
def extension() '' end
def mime_type() 'application/xml' end
def encode(hash, options = {}) hash.to_xml(options) end
def decode(xml)
data = Hash.from_xml(xml)['ResultSet']['Result']['ChunkList']['Chunk']
data = [ data ] if data.is_a?(Hash)
data.each{ |chunk|
if chunk['MorphemList']['Morphem'].is_a?(Hash)
morph = chunk['MorphemList']['Morphem']
chunk['MorphemList']['Morphem'] = [ morph ]
end
}
end
end

self.site = 'http://jlp.yahooapis.jp'
self.format = Format.new

class << self
def collection_path(prefix_options = {}, query_options = nil)
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
"/DAService/V1/parse#{query_string(query_options)}"
end
end
end


DaService::Chunk.find(:all,:params=>params)

で、レスポンスXMLの /ResultSet/Result/ChunkList/chunk 要素をActiveResourceに変換したオブジェクトのリストが取得できる。

キーフレーズ抽出


モデルのコード。

class Keyphrase::Result < ActiveResource::Base

class Format
def extension() '' end
def mime_type() 'application/xml' end
def encode(hash, options = {}) hash.to_xml(options) end
def decode(xml)
data = Hash.from_xml(xml)['ResultSet']['Result']
data = [ data ] if data.is_a?(Hash)
end
end

self.site = 'http://jlp.yahooapis.jp'
self.format = Format.new

class << self
def collection_path(prefix_options = {}, query_options = nil)
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
"/KeyphraseService/V1/extract#{query_string(query_options)}"
end
end
end


Keyphrase::Result.find(:all, :params=>params)

で、レスポンスXMLの /ResultSet/Result 要素をActiveResourceに変換したオブジェクトのリストが取得できる。


画像・動画コンテンツが全盛期の中、テキスト解析というと地味に見えがちなのだけれど、これだけの高度なテキスト解析プログラムがHTTP経由で使えるってすごい。
# ChaSenをローカルに入れるとか、しなくなるのかなぁ。

0 件のコメント:

コメントを投稿