Class: OpenCalais::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/open_calais/response.rb', line 8

def initialize(response)
  @raw  = response

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

  parse(response)
end

Instance Attribute Details

#entitiesObject

Returns the value of attribute entities.



6
7
8
# File 'lib/open_calais/response.rb', line 6

def entities
  @entities
end

#languageObject

Returns the value of attribute language.



6
7
8
# File 'lib/open_calais/response.rb', line 6

def language
  @language
end

#locationsObject

Returns the value of attribute locations.



6
7
8
# File 'lib/open_calais/response.rb', line 6

def locations
  @locations
end

#rawObject

Returns the value of attribute raw.



6
7
8
# File 'lib/open_calais/response.rb', line 6

def raw
  @raw
end

#relationsObject

Returns the value of attribute relations.



6
7
8
# File 'lib/open_calais/response.rb', line 6

def relations
  @relations
end

#tagsObject

Returns the value of attribute tags.



6
7
8
# File 'lib/open_calais/response.rb', line 6

def tags
  @tags
end

#topicsObject

Returns the value of attribute topics.



6
7
8
# File 'lib/open_calais/response.rb', line 6

def topics
  @topics
end

Instance Method Details

#humanize_topic(topic) ⇒ Object



21
22
23
# File 'lib/open_calais/response.rb', line 21

def humanize_topic(topic)
  topic.gsub('_', ' & ').titleize.remove_formatting
end

#importance_to_score(imp) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/open_calais/response.rb', line 25

def importance_to_score(imp)
  case imp
  when 1 then 0.9
  when 2 then 0.7
  else 0.5
  end
end

#parse(response) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
# File 'lib/open_calais/response.rb', line 33

def parse(response)
  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'
      item = {:guid => k, :name => v.name, :type => v._type.remove_formatting.titleize, :score => 1.0}

      instances = Array(v.instances).select{|i| i.exact.downcase != item[:name].downcase }
      item[:matches] = instances if instances && instances.size > 0

      if OpenCalais::GEO_TYPES.include?(v._type)
        if (v.resolutions && v.resolutions.size > 0)
          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
        end
        self.locations << item
      else
        self.entities << item
      end
    when 'relations'
      item = v.reject{|k,v| k[0] == '_' || k == 'instances'} || {}
      item[:type] = v._type.remove_formatting.titleize
      self.relations << item
    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