Skip to content

Commit

Permalink
initial check-in -- still added specs to test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffrey Damick committed Oct 2, 2008
1 parent 2f6f31d commit e8945c9
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/yahoo_service_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:hostname: http://search.yahooapis.com
:endpoint: ContentAnalysisService/V1/termExtraction
12 changes: 12 additions & 0 deletions lib/yahoo-term-extractor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@


require 'rubygems'

require 'yaml'
require 'net/http'

gem 'json'
require 'json'

require 'yahoo/config'
require 'yahoo/term_extractor'
33 changes: 33 additions & 0 deletions lib/yahoo/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

module Yahoo
module Config
def app_id()
config[:app_id]
end

def output_format()
config[:output_format] || :json
end

def endpoint_uri()
if @endpoint_uri.nil?
raise "Configuration Error - missing hostname" if config[:hostname].nil?
raise "Configuration Error - missing endpoint" if config[:endpoint].nil?
@endpoint_uri = URI.parse("#{config[:hostname]}/#{config[:endpoint]}".gsub(/^\:\/\//, '/'))
end
@endpoint_uri
end

private
def config()
@default_config ||= YAML.load_file(File.join(File.dirname(__FILE__), '..', '..', 'config', 'yahoo_service_config.yml'))
# try to load from RAILS_ROOT
if @config.nil?
config = YAML.load_file(File.join(RAILS_ROOT, 'config', 'yahoo_service_config.yml'))
@config = @default_config.merge(config)
end
@config
end

end
end
32 changes: 32 additions & 0 deletions lib/yahoo/term_extractor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'logger'

module Yahoo
class RemoteServerError < RuntimeError; end
class ConfigurationError < RuntimeError; end

class TermExtractor
extend Yahoo::Config

# Extracts terms from content provided. Provide an optional query to provide context for your terms.
def self.find_terms(content, query = nil)
debug { "post_form(#{endpoint_uri()}, #{query_hash(content, query).inspect})" }
response = Net::HTTP.post_form(endpoint_uri(), query_hash(content, query))
raise Yahoo::RemoteServerError unless response.code == "200"
results = JSON.parse(response.body)['ResultSet']['Result'] rescue []
end

private

def self.query_hash(content, query)
raise Yahoo::ConfigurationError if app_id().nil?
{ 'appid' => app_id(),
'output' => output_format().to_s,
'context' => content,
'query' => query }
end

def self.debug()
Logger.new(STDOUT).debug(yield) if ENV['DEBUG']
end
end
end
20 changes: 20 additions & 0 deletions test/live_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
$:.push File.join(File.dirname(__FILE__), '..', 'lib')

require 'rubygems'
require 'test/unit'
require 'yahoo-term-extractor'


RAILS_ROOT = File.dirname(__FILE__) unless defined?(RAILS_ROOT)

class LiveTest < Test::Unit::TestCase
def test_get_terms
# results = Yahoo::TermExtractor.get_terms(
text = 'Diabetes treatment plans consist of a healthy diet, exercise, medications and sleep'
#text = "i testd this morning and it was positive but than it disappeared in 20 mins. Is it still positive? and than i testd again later this mornin and it waas very very faint and it wasnt pink it was ghost like but it appeared before the 5 mins. Am i pregnant?"
results = Yahoo::TermExtractor.find_terms(text)
require 'pp'
pp results
assert_not_nil results
end
end

0 comments on commit e8945c9

Please sign in to comment.