Class: OpenCalais::Response
- Inherits:
-
Object
- Object
- OpenCalais::Response
- Includes:
- ActiveSupport::Inflector
- Defined in:
- lib/open_calais/response.rb
Constant Summary collapse
- ALLOWED_OPTIONS =
[ :ignore_literal_match ].freeze
Instance Attribute Summary collapse
-
#entities ⇒ Object
Returns the value of attribute entities.
-
#language ⇒ Object
Returns the value of attribute language.
-
#locations ⇒ Object
Returns the value of attribute locations.
-
#raw ⇒ Object
Returns the value of attribute raw.
-
#relations ⇒ Object
Returns the value of attribute relations.
-
#tags ⇒ Object
Returns the value of attribute tags.
-
#topics ⇒ Object
Returns the value of attribute topics.
Instance Method Summary collapse
- #humanize_topic(topic) ⇒ Object
- #importance_to_score(imp) ⇒ Object
-
#initialize(response, options = {}) ⇒ Response
constructor
A new instance of Response.
- #merge_default_options(opts = {}) ⇒ Object
- #parse(response, options = {}) ⇒ Object
- #parse_entity(k, v, options) ⇒ Object
- #parse_relation(k, v, _options) ⇒ Object
- #set_location_info(item, v) ⇒ Object
Constructor Details
#initialize(response, options = {}) ⇒ Response
Returns a new instance of Response.
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/open_calais/response.rb', line 15 def initialize(response, ={}) @options = () @raw = response @language = 'English' @topics = [] @tags = [] @entities = [] @relations = [] @locations = [] parse(response, @options) end |
Instance Attribute Details
#entities ⇒ Object
Returns the value of attribute entities.
13 14 15 |
# File 'lib/open_calais/response.rb', line 13 def entities @entities end |
#language ⇒ Object
Returns the value of attribute language.
13 14 15 |
# File 'lib/open_calais/response.rb', line 13 def language @language end |
#locations ⇒ Object
Returns the value of attribute locations.
13 14 15 |
# File 'lib/open_calais/response.rb', line 13 def locations @locations end |
#raw ⇒ Object
Returns the value of attribute raw.
13 14 15 |
# File 'lib/open_calais/response.rb', line 13 def raw @raw end |
#relations ⇒ Object
Returns the value of attribute relations.
13 14 15 |
# File 'lib/open_calais/response.rb', line 13 def relations @relations end |
#tags ⇒ Object
Returns the value of attribute tags.
13 14 15 |
# File 'lib/open_calais/response.rb', line 13 def @tags end |
#topics ⇒ Object
Returns the value of attribute topics.
13 14 15 |
# File 'lib/open_calais/response.rb', line 13 def topics @topics end |
Instance Method Details
#humanize_topic(topic) ⇒ Object
37 38 39 |
# File 'lib/open_calais/response.rb', line 37 def humanize_topic(topic) transliterate(topic.gsub('_', ' & ').titleize) end |
#importance_to_score(imp) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/open_calais/response.rb', line 41 def importance_to_score(imp) case imp.to_i when 1 then 0.9 when 2 then 0.7 else 0.5 end end |
#merge_default_options(opts = {}) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/open_calais/response.rb', line 29 def (opts={}) defaults = { :ignore_literal_match => true } = defaults.merge(opts) .select{ |k,v| ALLOWED_OPTIONS.include? k.to_sym } end |
#parse(response, options = {}) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/open_calais/response.rb', line 49 def parse(response, ={}) r = response.body @language = r.doc..language rescue nil @language = nil if @language == 'InputTextTooShort' r.each do |k,v| case v._typeGroup when 'topics' self.topics << { :name => humanize_topic(v.name), :score => v.score.to_f, :original => v.name } when 'socialTag' self. << { :name => v.name.gsub('_', ' and ').downcase, :score => importance_to_score(v.importance) } when 'entities' parse_entity(k, v, ) when 'relations' parse_relation(k, v, ) end end # remove social tags which are in the topics list already topic_names = self.topics.collect { |topic| topic[:name].downcase } self..delete_if { |tag| topic_names.include?(tag[:name]) } end |
#parse_entity(k, v, options) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/open_calais/response.rb', line 79 def parse_entity(k, v, ) if v.name.nil? v.name = v.instances.first[:exact] end item = { :guid => k, :name => v.name, :type => transliterate(v._type).titleize, :score => v.relevance } instances = Array(v.instances) if [:ignore_literal_match] instances = instances.select { |i| i.exact.downcase != item[:name].downcase } end item[:matches] = instances unless instances.empty? if OpenCalais::GEO_TYPES.include?(v._type) item = set_location_info(item, v) self.locations << item else self.entities << item end end |
#parse_relation(k, v, _options) ⇒ Object
117 118 119 120 121 |
# File 'lib/open_calais/response.rb', line 117 def parse_relation(k, v, ) item = v.reject { |key,val| key[0] == '_' || key == 'instances' } || {} item[:type] = transliterate(v._type).titleize self.relations << item end |
#set_location_info(item, v) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/open_calais/response.rb', line 105 def set_location_info(item, v) return item if v.resolutions.nil? || v.resolutions.empty? r = v.resolutions.first item[:name] = r.shortname || r.name item[:latitude] = r.latitude item[:longitude] = r.longitude item[:country] = r.containedbycountry if r.containedbycountry item[:state] = r.containedbystate if r.containedbystate item end |