Class: YahooContentAnalysis::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



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

def initialize(response)
  @raw  = response

  @language = nil
  @topics    = []
  @tags      = []
  @entities  = []
  @relations = []
  @locations = []

  parse(response) if response
end

Instance Attribute Details

#entitiesObject

Returns the value of attribute entities.



7
8
9
# File 'lib/yahoo_content_analysis/response.rb', line 7

def entities
  @entities
end

#languageObject

Returns the value of attribute language.



7
8
9
# File 'lib/yahoo_content_analysis/response.rb', line 7

def language
  @language
end

#locationsObject

Returns the value of attribute locations.



7
8
9
# File 'lib/yahoo_content_analysis/response.rb', line 7

def locations
  @locations
end

#rawObject

Returns the value of attribute raw.



7
8
9
# File 'lib/yahoo_content_analysis/response.rb', line 7

def raw
  @raw
end

#relationsObject

Returns the value of attribute relations.



7
8
9
# File 'lib/yahoo_content_analysis/response.rb', line 7

def relations
  @relations
end

#tagsObject

Returns the value of attribute tags.



7
8
9
# File 'lib/yahoo_content_analysis/response.rb', line 7

def tags
  @tags
end

#topicsObject

Returns the value of attribute topics.



7
8
9
# File 'lib/yahoo_content_analysis/response.rb', line 7

def topics
  @topics
end

Instance Method Details

#extract_type(h) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/yahoo_content_analysis/response.rb', line 75

def extract_type(h)
  # puts "extract_type: #{h.inspect}"
  return nil unless (h && h['type'])
  type = h['type'].is_a?(Array) ? h['type'].first : h['type']
  content = (type['content'] || '').split('/')
  content = (content[1] || content[0] || '').remove_formatting.titleize
  content.blank? ? nil : content
end

#get_language(lang) ⇒ Object



69
70
71
72
73
# File 'lib/yahoo_content_analysis/response.rb', line 69

def get_language(lang)
  return nil unless lang
  l = LanguageList::LanguageInfo.find(lang.split('-')[0].downcase)
  l.name
end

#humanize_topic(topic) ⇒ Object



22
23
24
# File 'lib/yahoo_content_analysis/response.rb', line 22

def humanize_topic(topic)
  (topic || '').titleize.remove_formatting
end

#parse(response = raw) ⇒ Object



26
27
28
29
30
31
32
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
# File 'lib/yahoo_content_analysis/response.rb', line 26

def parse(response=raw)
  return if (response.nil?)
  return if (!response.respond_to?(:body))
  return if (!response.body['query'] || !response.body['query']['results'])

  @language = get_language(response.body['query']['lang'])
  r = response.body['query']['results']

  yahoo_categories = (r['yctCategories'] || {})['yctCategory'] || []
  yahoo_categories = [yahoo_categories] unless yahoo_categories.is_a?(Array)
  @topics = yahoo_categories.collect do |cat|
    {:name => humanize_topic(cat['content']), :score => cat['score'].to_f, :original=>cat['content']}
  end

  yahoo_entities = (r['entities'] || {})['entity'] || []
  yahoo_entities = [yahoo_entities] unless yahoo_entities.is_a?(Array)
  entities_hash = yahoo_entities.inject({}) do |hash, ent|
    # puts "entity: #{ent.inspect}"
    name = ent['text']['content']
    if hash.has_key?(name)
      existing = hash[name]
      existing[:score] = [existing[:score], ent['score'].to_f].max
    else
      type = extract_type(ent['types'])

      entity = {:name => ent['text']['content'], :score => ent['score'].to_f}
      entity[:type] = type if type
      entity[:wikipedia_url] = ent['wiki_url'] if ent['wiki_url']

      ## these aren't showing up in any results, so not worrying about them
      # if cat['related_entities'] && cat['related_entities']['wikipedia']
      # end

      hash[name] = entity
    end

    hash
  end

  @entities = entities_hash.values

end