Ruby class to fetch suggested keywords
Sun, Aug 3, 2008
Vote on HN
I have recently
ditched Perl and fell for Ruby.
Here is a Ruby class which gets suggested keywords from yahoo API and presents it in a comma separated list. This might be useful for people who use Ruby to upload/edit multiple posts and need to tag them automagically.
This might be awfully simple for the hardcore guys, but for a noob like me, had I come across this example earlier it would have saved me an hour or so. Here it goes...
The Class :-
# A ruby class to fetch suggested keywords using Yahoo's term Extraction API
require 'rubygems'
require 'xmlsimple'
require 'net/http'
class Tagger
def content
@content
end
def content=(content)
@content = content
end
def appid
@appid
end
def appid=(appid)
@appid = appid
end
def initialize
end
def fetch
url = URI.parse('http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction')
post_args = {
'appid' => appid,
'context' => content,
'output' => "xml"
}
resp1, xml_data = Net::HTTP.post_form(url, post_args)
keywords = ""
data = XmlSimple.xml_in(xml_data)
data['Result'].each do |item|
keywords = keywords + item + ", "
end
return keywords
end
end
Download the class
here
Sample usage :
require 'tagger'
tagg = Tagger.new
tagg.appid = 'GO_GET_YOUR_OWN!'
tagg.content = "The content of the post....."
puts tagg.fetch
The appid is available from the
Yahoo Developer Network
Limitations : Maximum 5000 queries per day per IP.