Created
April 26, 2011 05:47
-
-
Save kshimo69/941847 to your computer and use it in GitHub Desktop.
yahoo keyphrase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
require 'logger' | |
require 'optparse' | |
require 'net/http' | |
require 'uri' | |
require 'nokogiri' | |
$log = Logger.new(STDOUT) | |
$log.formatter = Logger::Formatter.new | |
$log.progname = File.basename(__FILE__) | |
# $log.level = Logger::WARN | |
$log.level = Logger::INFO | |
opt = OptionParser.new | |
opt.on('-d') { $log.level = Logger::DEBUG } | |
opt.parse!(ARGV) | |
$log.info("START!") | |
class YahooKeyword | |
YAHOO_URL = 'http://jlp.yahooapis.jp/KeyphraseService/V1/extract' | |
APPID = 'hoge' | |
def initialize(output='xml') | |
@uri = URI.parse(YAHOO_URL) | |
@output = output | |
end | |
def get(sentence) | |
if sentence == '' | |
$log.debug("No data found in argument.") | |
return nil | |
else | |
$log.debug("request to Yahoo!") | |
# Net::HTTP.start(@uri.host, @uri.port) {|http| | |
proxy = URI.parse(ENV['http_proxy']) | |
proxy_host = proxy.host | |
proxy_port = proxy.port | |
proxy_user, proxy_passwd = proxy.userinfo.split(/:/) | |
Net::HTTP::Proxy(proxy_host, proxy_port, proxy_user, proxy_passwd).start(@uri.host, @uri.port) {|http| | |
request = Net::HTTP::Post.new(@uri.path) | |
request["user-agent"] = "Ruby/#{RUBY_VERSION} NetHTTP" | |
request.set_form_data({:appid => APPID, | |
:sentence => sentence, | |
:output => @output}, "&") | |
@response = http.request(request) | |
} | |
$log.debug("#{@response.code}: #{@response.message}") | |
return nil if @response.code != '200' | |
parsed_data = parse_response | |
$log.debug(parsed_data) | |
return parsed_data | |
end | |
end | |
private | |
def parse_response() | |
ret = [] | |
docs = Nokogiri::XML(@response.body) | |
docs.at("ResultSet").children.search("Result").each {|doc| | |
doc.children.each {|d| | |
ret << d.text if d.element? | |
} | |
} | |
return Hash[*ret] | |
end | |
end | |
if $0 == __FILE__ | |
yahoo = YahooKeyword.new() | |
yahoo.get("") | |
data = "今日はおいしいラーメンを食べました。明日も食べに行きたいです。昨日食べたカレーもうまかったっす。" | |
yahoo.get(data) | |
end | |
$log.info("END!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment