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.



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

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.



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

def entities
  @entities
end

#languageObject

Returns the value of attribute language.



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

def language
  @language
end

#locationsObject

Returns the value of attribute locations.



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

def locations
  @locations
end

#rawObject

Returns the value of attribute raw.



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

def raw
  @raw
end

#relationsObject

Returns the value of attribute relations.



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

def relations
  @relations
end

#tagsObject

Returns the value of attribute tags.



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

def tags
  @tags
end

#topicsObject

Returns the value of attribute topics.



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

def topics
  @topics
end

Instance Method Details

#extract_type(h) ⇒ Object



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

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



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

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

#humanize_topic(topic) ⇒ Object



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

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

#parse(response = raw) ⇒ Object



25
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
# File 'lib/yahoo_content_analysis/response.rb', line 25

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

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

  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