-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial check-in -- still added specs to test
- Loading branch information
Jeffrey Damick
committed
Oct 2, 2008
1 parent
2f6f31d
commit e8945c9
Showing
5 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
:hostname: http://search.yahooapis.com | ||
:endpoint: ContentAnalysisService/V1/termExtraction |
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
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' |
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
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 |
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
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 |
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
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 |