Skip to content

Instantly share code, notes, and snippets.

@zonoise
Created October 29, 2010 04:39
Show Gist options
  • Save zonoise/652929 to your computer and use it in GitHub Desktop.
Save zonoise/652929 to your computer and use it in GitHub Desktop.
yahoo形態素解析とキーフレーズAPIを触ってみた。
#!/usr/bin/ruby
require 'uri'
require 'net/http'
require 'rexml/document'
require 'yaml'
require 'pp'
Net::HTTP.version_1_2
class YahooApi
Host='jlp.yahooapis.jp'
Port='80'
Parse='/MAService/V1/parse' #形態素解析APIパス
Phrase='/KeyphraseService/V1/extract' #キーフレーズ解析APIパス
def get_key
path='../config/yahooapi.yml'
config = YAML::load(File.open(path)) #ファイルからAPIキーを読み込み
return config["app_id"]
end
#形態素解析
#param は解析対象の文字列
def parse(param)
query_hash={'appid' => "#{get_key}",
'sentence' =>URI.encode(param) ,
'results' => 'uniq', #ma は冗長かも
'filter' => "9", #名詞のみ
'response' =>'surface'} #名詞のみで、活用ないので、表面だけでよいか
query=construct_query(query_hash)
#p "query_string #{query}"
http = Net::HTTP.start(Host,Port)
res = http.post(Parse,query)
words = Array.new
doc= REXML::Document.new res.body
doc.elements.each("/ResultSet/uniq_result/word_list/word"){|w|
words << w.elements['surface'].text
}
return words #return string array
end
#キーフレーズAPI
def key_phrase(param)
begin
query_hash={'appid' => get_key,
'sentence' =>URI.encode(param),
'output' => 'xml' }
query=construct_query(query_hash)
http = Net::HTTP.start(Host,Port)
res = http.post(Phrase,query)
words = Array.new
doc= REXML::Document.new res.body
doc.elements.each("/ResultSet/Result"){|r|
words <<r.elements['Keyphrase'].text
}
return words #配列で返す
rescue
raise 'error occured while yahoo keyphrase api'
end
end
#ハッシュからクエリー文字列組み立て
def construct_query(param ={})
param.map{|key,value|
"#{URI.encode(key)}=#{value}"
}.join("&")
end
end
y = YahooApi.new
puts y.parse("我輩は猫である")
puts y.key_phrase("我輩は猫である")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment