yahoo_content_analysis 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,6 +7,9 @@ require 'yahoo_content_analysis/connection'
7
7
  require 'yahoo_content_analysis/response'
8
8
  require 'yahoo_content_analysis/client'
9
9
 
10
+ require 'active_support/all'
11
+ require 'stringex'
12
+
10
13
  module YahooContentAnalysis
11
14
  extend Configuration
12
15
  end
@@ -24,17 +24,18 @@ module YahooContentAnalysis
24
24
  end
25
25
 
26
26
  def setup(options={})
27
- options = YahooContentAnalysis.options.merge(options)
28
- self.current_options = options
27
+ opts = Hash[options.map{ |k, v| [k.to_sym, v] }]
28
+ opts = YahooContentAnalysis.options.merge(opts)
29
+ self.current_options = opts
29
30
  Configuration.keys.each do |key|
30
- send("#{key}=", options[key])
31
+ send("#{key}=", opts[key])
31
32
  end
32
33
  end
33
34
 
34
35
  def analyze(content, opts={})
35
36
  raise 'Specify a value for the content' unless content
36
37
  response = connection.post do |request|
37
- request.params = options(content)
38
+ request.body = options(content)
38
39
  end
39
40
  YahooContentAnalysis::Response.new(response)
40
41
  end
@@ -18,28 +18,46 @@ module YahooContentAnalysis
18
18
  parse(response)
19
19
  end
20
20
 
21
+ def humanize_topic(topic)
22
+ topic.titleize.remove_formatting
23
+ end
24
+
21
25
  def parse(response)
22
- r = response.body['query']['results'] || {}
26
+ r = response.body['query']['results'] || {}
23
27
  @language = get_language(r['lang'])
24
28
 
25
- @topics = Array((r['yctCategories'] || {})['yctCategory']).collect do |cat|
26
- {:name => cat['content'], :score => cat['score']}
29
+ yahoo_categories = (r['yctCategories'] || {})['yctCategory'] || []
30
+ yahoo_categories = [yahoo_categories] unless yahoo_categories.is_a?(Array)
31
+ @topics = yahoo_categories.collect do |cat|
32
+ {:name => humanize_topic(cat['content']), :score => cat['score'].to_f, :original=>cat['content']}
27
33
  end
28
34
 
29
- @entities = Array((r['entities'] || {})['entity']).collect do |ent|
30
- type = extract_type(ent['types'])
35
+ yahoo_entities = (r['entities'] || {})['entity'] || []
36
+ yahoo_entities = [yahoo_entities] unless yahoo_entities.is_a?(Array)
37
+ entities_hash = yahoo_entities.inject({}) do |hash, ent|
38
+ name = ent['text']['content']
39
+ if hash.has_key?(name)
40
+ existing = hash[name]
41
+ existing[:score] = [existing[:score], ent['score'].to_f].max
42
+ else
43
+ type = extract_type(ent['types'])
31
44
 
32
- entity = {:name => ent['text']['content'], :score => ent['score']}
33
- entity[:type] = type if type
34
- entity[:wikipedia_url] = ent['wiki_url'] if ent['wiki_url']
45
+ entity = {:name => ent['text']['content'], :score => ent['score'].to_f}
46
+ entity[:type] = type if type
47
+ entity[:wikipedia_url] = ent['wiki_url'] if ent['wiki_url']
35
48
 
36
- ## these aren't showing up in any results, so not worrying about them
37
- # if cat['related_entities'] && cat['related_entities']['wikipedia']
38
- # end
49
+ ## these aren't showing up in any results, so not worrying about them
50
+ # if cat['related_entities'] && cat['related_entities']['wikipedia']
51
+ # end
39
52
 
40
- entity
53
+ hash[name] = entity
54
+ end
55
+
56
+ hash
41
57
  end
42
58
 
59
+ @entities = entities_hash.values
60
+
43
61
  end
44
62
 
45
63
  def get_language(lang)
@@ -51,7 +69,7 @@ module YahooContentAnalysis
51
69
  def extract_type(h)
52
70
  return nil unless (h && h['type'])
53
71
  type = h['type'].is_a?(Array) ? h['type'].first : h['type']
54
- (type['content'] || '').sub(/^\/(.*)/, '')
72
+ (type['content'] || '').split('/')[1].remove_formatting.titleize
55
73
  end
56
74
 
57
75
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module YahooContentAnalysis
4
- VERSION = "0.0.1"
4
+ VERSION = "0.1.0"
5
5
  end
@@ -24,6 +24,8 @@ Gem::Specification.new do |gem|
24
24
  gem.add_runtime_dependency('hashie', '>= 0.4.0')
25
25
  gem.add_runtime_dependency('simple_oauth')
26
26
  gem.add_runtime_dependency('language_list')
27
+ gem.add_runtime_dependency('activesupport')
28
+ gem.add_runtime_dependency('stringex')
27
29
 
28
30
  gem.add_development_dependency('rake')
29
31
  gem.add_development_dependency('minitest')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yahoo_content_analysis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-09 00:00:00.000000000 Z
12
+ date: 2013-04-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -135,6 +135,38 @@ dependencies:
135
135
  - - ! '>='
136
136
  - !ruby/object:Gem::Version
137
137
  version: '0'
138
+ - !ruby/object:Gem::Dependency
139
+ name: activesupport
140
+ requirement: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ! '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ none: false
150
+ requirements:
151
+ - - ! '>='
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ - !ruby/object:Gem::Dependency
155
+ name: stringex
156
+ requirement: !ruby/object:Gem::Requirement
157
+ none: false
158
+ requirements:
159
+ - - ! '>='
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ type: :runtime
163
+ prerelease: false
164
+ version_requirements: !ruby/object:Gem::Requirement
165
+ none: false
166
+ requirements:
167
+ - - ! '>='
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
138
170
  - !ruby/object:Gem::Dependency
139
171
  name: rake
140
172
  requirement: !ruby/object:Gem::Requirement
@@ -201,7 +233,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
201
233
  version: '0'
202
234
  segments:
203
235
  - 0
204
- hash: 2699003970576411257
236
+ hash: 750957635226304825
205
237
  required_rubygems_version: !ruby/object:Gem::Requirement
206
238
  none: false
207
239
  requirements:
@@ -210,7 +242,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
210
242
  version: '0'
211
243
  segments:
212
244
  - 0
213
- hash: 2699003970576411257
245
+ hash: 750957635226304825
214
246
  requirements: []
215
247
  rubyforge_project:
216
248
  rubygems_version: 1.8.23