Class: OpenCalais::Response

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Inflector
Defined in:
lib/open_calais/response.rb

Constant Summary collapse

ALLOWED_OPTIONS =
[
  :ignore_literal_match
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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={})
  @options = merge_default_options(options)
  @raw  = response

  @language = 'English'
  @topics    = []
  @tags      = []
  @entities  = []
  @relations = []
  @locations = []

  parse(response, @options)
end

Instance Attribute Details

#entitiesObject

Returns the value of attribute entities.



13
14
15
# File 'lib/open_calais/response.rb', line 13

def entities
  @entities
end

#languageObject

Returns the value of attribute language.



13
14
15
# File 'lib/open_calais/response.rb', line 13

def language
  @language
end

#locationsObject

Returns the value of attribute locations.



13
14
15
# File 'lib/open_calais/response.rb', line 13

def locations
  @locations
end

#rawObject

Returns the value of attribute raw.



13
14
15
# File 'lib/open_calais/response.rb', line 13

def raw
  @raw
end

#relationsObject

Returns the value of attribute relations.



13
14
15
# File 'lib/open_calais/response.rb', line 13

def relations
  @relations
end

#tagsObject

Returns the value of attribute tags.



13
14
15
# File 'lib/open_calais/response.rb', line 13

def tags
  @tags
end

#topicsObject

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 merge_default_options(opts={})
  defaults = {
    :ignore_literal_match => true
  }
  options = defaults.merge(opts)
  options.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, options={})
  r = response.body
  @language = r.doc.meta.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.tags << {
        :name => v.name.gsub('_', ' and ').downcase,
        :score => importance_to_score(v.importance)
      }
    when 'entities'
      parse_entity(k, v, options)
    when 'relations'
      parse_relation(k, v, options)
    end
  end

  # remove social tags which are in the topics list already
  topic_names = self.topics.collect { |topic| topic[:name].downcase }
  self.tags.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, options)
  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 options[: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, _options)
  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