openamplify 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -5,3 +5,6 @@ tmp/
5
5
  log/
6
6
  pkg/
7
7
  *.swp
8
+ *.gem
9
+ .bundle
10
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # See openamplify.gemspec for the dependencies
4
+ gemspec
data/History CHANGED
@@ -1,3 +1,7 @@
1
+ 0.3.0 - TBA
2
+ * Supports OpenAmplify v3.0 by default
3
+ * Major code revamp
4
+ * Added command-line tool 'openamplify'
1
5
  0.2.2 - Jun 14, 2010
2
6
  * refactored the code
3
7
  0.2.1 - Jun 13, 2010
@@ -6,9 +6,13 @@ in the fine tradition of APIs and mashups, up to you. Some possibilities
6
6
  might include pairing ads with articles, creating rich tag-clouds, or
7
7
  monitoring the tone of forum threads.
8
8
 
9
+ === Supported version
10
+
11
+ 2.1
12
+
9
13
  == Helpful links
10
14
 
11
- * <b>Overview:</b> http://community.openamplify.com/blogs/quickstart/pages/overview.aspx
15
+ * <b>Overview:</b> http://openamplify.com/quickstart
12
16
 
13
17
  == Install
14
18
 
@@ -16,82 +20,93 @@ monitoring the tone of forum threads.
16
20
 
17
21
  == Usage
18
22
 
19
- === Show the result from OpenAmplify as a Hash
23
+ === Configure
20
24
 
21
- require 'openamplify'
25
+ OpenAmplify.configure do |config|
26
+ config.api_key = 'YOUR_API_KEY'
22
27
 
23
- API_KEY = "register to get a key"
24
- client = OpenAmplify::Client.new(:api_key => API_KEY)
28
+ # default :get
29
+ config.method = :post
25
30
 
26
- text = "After getting the MX1000 laser mouse and the Z-5500 speakers i fell in love with logitech"
27
- response = client.analyze_text(text)
31
+ # default :xml
32
+ config.output_format = :json
28
33
 
29
- # List all the keys and values returned by OpenAmplify
30
- response.each do |k, v|
31
- pp k
32
- pp v
34
+ # default :all
35
+ config.analysis = :topics
36
+
37
+ # default :standard
38
+ config.scoring = :todo
33
39
  end
34
40
 
41
+ Default settings can be overriden by the client
35
42
 
36
- # 'response' works like a Hash
37
- puts response['Topics']
43
+ client = OpenAmplify::Client.new(:method => :get)
38
44
 
39
- # or use the shortcuts
40
- response.top_topics
41
- response.proper_nouns
42
- response.locations
43
- response.domains
45
+ Or when you start 'amplifying'.
46
+
47
+ result = client.amplify('input', :output_format => :rdf)
48
+
49
+ === Usage
50
+
51
+ client = OpenAmplify::Client.new
44
52
 
45
- === Output Format
53
+ text = "After getting the MX1000 laser mouse and the Z-5500 speakers i fell in love with logitech"
54
+ analysis = client.amplify(text)
55
+ puts analysis # default as xml
56
+
57
+ It knows if the input is a URL and uses the right webservice param (ie. sourceurl)
58
+
59
+ client.amplify('http://theonion.com')
46
60
 
47
61
  In case you need a different format, OpenAmplify supports XML, JSON, RDF, CSV.
48
62
  It can also return the result as a fancy HTML page.
49
63
 
50
64
  # assuming you use Nokogiri
51
- doc = Nokogiri::XML(response.to_xml)
65
+ doc = Nokogiri::XML(analysis.to_xml)
52
66
 
53
67
  # or you want a JSON
54
- json = JSON.parse(response.to_json)
68
+ json = JSON.parse(analysis.to_json)
55
69
 
56
70
  # you should really try the pretty formats
57
- puts response.to_pretty
71
+ puts analysis.to_pretty
58
72
  # or
59
- puts response.to_signals
60
-
61
- === Analysis options
73
+ puts analysis.to_signals
62
74
 
63
- By default, OpenAmplify returns a number of 'signals' about your text.
64
- You can limit the result by setting the 'analysis' option.
65
75
 
66
- The different options and explanations are available at http://community.openamplify.com/blogs/quickstart/pages/overview.aspx
76
+ == Command-line
67
77
 
68
- client = OpenAmplify::Client.new(:api_key => API_KEY, :analysis => 'topics')
78
+ OpenAmplify gem comes with a command-line tool.
69
79
 
70
- # or if you have a client instance already
71
- client.analysis = 'topics'
80
+ openamplify --api-key=APIKEY --format=json --analysis=topics "sample input text"
72
81
 
73
- response = client.analyze_text(text)
74
- response['Topics'] # => should be another big Hash of key-value pairs
75
- response['Demographics'] # => nil
82
+ == Upgrading from 0.2.3 to 0.3.0
76
83
 
77
- === POST method
84
+ Note 0.3.0 is a major revamp of the code. In general, it is better but
85
+ if you encounter a problem, please let me know. Or better, submit a pull
86
+ request.
78
87
 
79
- By default, GET is used. If you need to analyze lots of text, use POST
88
+ The following methods are deprecated and will be removed in 0.3.1
80
89
 
81
- client = OpenAmplify::Client.new(:api_key => API_KEY, :method => :post)
90
+ client.analyze_text('input')
91
+ client.base_url = 'http://domain.dev'
82
92
 
83
- # or
84
- client.method = :post
85
-
86
- === Request URL
87
-
88
- In case you are wondering what the request URL looks like:
93
+ The following methods are no longer supported. I figure it's better to
94
+ let the caller decide how they want to traverse the results.
89
95
 
90
- response.request_url
96
+ # Treating the result as hash
97
+ response.each do |k, v|
98
+ pp k
99
+ pp v
100
+ end
91
101
 
92
- If someday, OpenAmplify decides to change their API URL:
102
+ # 'response' works like a Hash
103
+ puts response['Topics']
93
104
 
94
- client.base_url = 'http://newurl'
105
+ # Nor the shortcuts
106
+ response.top_topics
107
+ response.proper_nouns
108
+ response.locations
109
+ response.domains
95
110
 
96
111
  == Testing
97
112
  rake test OPEN_AMPLIFY_KEY=YOUR_KEY
data/Rakefile CHANGED
@@ -1,61 +1,71 @@
1
- require 'rubygems'
2
- require 'rake'
3
-
4
- begin
5
- require 'jeweler'
6
- Jeweler::Tasks.new do |gem|
7
- gem.name = "openamplify"
8
- gem.summary = "Wrapper for the OpenAmplify API"
9
- gem.description = "The OpenAmplify API reads text you supply and returns linguistic data explaining and classifying the content."
10
- gem.email = "rubyoncloud@gmail.com"
11
- gem.homepage = "http://github.com/gregmoreno/openamplify"
12
- gem.authors = ["Greg Moreno"]
13
-
14
- gem.add_dependency "json", "~>1.2"
15
-
16
- gem.add_development_dependency "shoulda", "~> 2.11"
17
-
18
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
19
- end
20
- Jeweler::GemcutterTasks.new
21
- rescue LoadError
22
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
23
- end
1
+ require 'bundler/gem_tasks'
24
2
 
25
3
  require 'rake/testtask'
26
- Rake::TestTask.new(:test) do |test|
4
+ Rake::TestTask.new do |test|
27
5
  test.libs << 'lib' << 'test'
28
6
  test.ruby_opts << "-rubygems"
29
- test.pattern = 'test/**/test_*.rb'
7
+ test.pattern = 'test/**/*_test.rb'
30
8
  test.verbose = true
31
9
  end
32
- # Note to run a single test do:
33
- # ruby -Ilib test/test_foo.rb
34
-
35
-
36
- begin
37
- require 'rcov/rcovtask'
38
- Rcov::RcovTask.new do |test|
39
- test.libs << 'test'
40
- test.pattern = 'test/**/test_*.rb'
41
- test.verbose = true
42
- end
43
- rescue LoadError
44
- task :rcov do
45
- abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
46
- end
47
- end
48
-
49
- task :test => :check_dependencies
50
10
 
51
- task :default => :test
52
-
53
- require 'rake/rdoctask'
54
- Rake::RDocTask.new do |rdoc|
55
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
56
-
57
- rdoc.rdoc_dir = 'rdoc'
58
- rdoc.title = "openamplify #{version}"
59
- rdoc.rdoc_files.include('README*')
60
- rdoc.rdoc_files.include('lib/**/*.rb')
61
- end
11
+ #require 'rubygems'
12
+ #require 'rake'
13
+ #
14
+ #begin
15
+ # require 'jeweler'
16
+ # Jeweler::Tasks.new do |gem|
17
+ # gem.name = "openamplify"
18
+ # gem.summary = "Wrapper for the OpenAmplify API"
19
+ # gem.description = "The OpenAmplify API reads text you supply and returns linguistic data explaining and classifying the content."
20
+ # gem.email = "rubyoncloud@gmail.com"
21
+ # gem.homepage = "http://github.com/gregmoreno/openamplify"
22
+ # gem.authors = ["Greg Moreno"]
23
+ #
24
+ # gem.add_dependency "json", "~>1.2"
25
+ #
26
+ # gem.add_development_dependency "shoulda", "~> 2.11"
27
+ #
28
+ # # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
29
+ # end
30
+ # Jeweler::GemcutterTasks.new
31
+ #rescue LoadError
32
+ # puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
33
+ #end
34
+ #
35
+ #require 'rake/testtask'
36
+ #Rake::TestTask.new(:test) do |test|
37
+ # test.libs << 'lib' << 'test'
38
+ # test.ruby_opts << "-rubygems"
39
+ # test.pattern = 'test/**/test_*.rb'
40
+ # test.verbose = true
41
+ #end
42
+ ## Note to run a single test do:
43
+ ## ruby -Ilib test/test_foo.rb
44
+ #
45
+ #
46
+ #begin
47
+ # require 'rcov/rcovtask'
48
+ # Rcov::RcovTask.new do |test|
49
+ # test.libs << 'test'
50
+ # test.pattern = 'test/**/test_*.rb'
51
+ # test.verbose = true
52
+ # end
53
+ #rescue LoadError
54
+ # task :rcov do
55
+ # abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
56
+ # end
57
+ #end
58
+ #
59
+ #task :test => :check_dependencies
60
+ #
61
+ #task :default => :test
62
+ #
63
+ #require 'rake/rdoctask'
64
+ #Rake::RDocTask.new do |rdoc|
65
+ # version = File.exist?('VERSION') ? File.read('VERSION') : ""
66
+ #
67
+ # rdoc.rdoc_dir = 'rdoc'
68
+ # rdoc.title = "openamplify #{version}"
69
+ # rdoc.rdoc_files.include('README*')
70
+ # rdoc.rdoc_files.include('lib/**/*.rb')
71
+ #end
data/TODO ADDED
@@ -0,0 +1 @@
1
+ * Support RequestID, Cache
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'awesome_print'
5
+
6
+ $:.unshift(File.join(File.dirname(__FILE__), "/../lib"))
7
+ require 'openamplify'
8
+
9
+ opts = {
10
+ :output_format => :json
11
+ }
12
+
13
+ OptionParser.new do |o|
14
+ o.banner = "USAGE: #{$0} [options] input"
15
+
16
+ o.on('--api-key [APIKEY]', 'API key') do |f|
17
+ opts[:api_key] = f
18
+ end
19
+
20
+ o.on('-f', '--format [FORMAT]', 'Output format: json (default), xml, rdf, rdfa, csv signals, pretty, oas dart') do |f|
21
+ opts[:output_format] = f.downcase.to_sym
22
+ end
23
+
24
+ o.on('--analysis [TYPE]', 'Type of signals request: all (default), toptopics, topics, actions, topicintentions, demographics, styles') do |f|
25
+ opts[:analysis] = f.downcase.to_sym
26
+ end
27
+
28
+ # TODO: Add more options
29
+
30
+ end.parse!
31
+
32
+ client = OpenAmplify::Client.new(opts)
33
+ input = ARGV.first
34
+ response = client.amplify(input)
35
+
36
+ ap response
37
+
38
+ case opts[:output_format]
39
+ when :json
40
+ require 'json'
41
+ puts JSON.pretty_generate JSON.load(response)
42
+ when :xml, :rdf, :rdfa
43
+ require 'rexml/document'
44
+ doc = REXML::Document.new(response)
45
+ doc.write($stdout, 2)
46
+ else
47
+ puts response
48
+ end
49
+
@@ -1,168 +1,23 @@
1
- require 'net/http'
2
- require 'uri'
3
- require 'json'
1
+ require 'openamplify/configuration'
2
+ require 'openamplify/client'
4
3
 
5
4
  module OpenAmplify
6
- API_URL = "http://portaltnx20.openamplify.com/AmplifyWeb_v20/AmplifyThis"
5
+ extend Configuration
7
6
 
8
- class Client
9
- def initialize(options={})
10
- @options = { :api_url => API_URL, :method => :get }
11
- @options.merge!(OpenAmplify.symbolize_keys(options))
12
- end
13
-
14
- def analyze_text(text)
15
- OpenAmplify.validate_client!(self)
16
- Response.new(self, :query => query.merge(:inputText => text), :method => @options[:method])
17
- end
18
-
19
- %w(api_key analysis api_url method).each do |attr|
20
- class_eval <<-EOS
21
- def #{attr}
22
- @options[:#{attr}]
23
- end
24
-
25
- def #{attr}=(v)
26
- @options[:#{attr}] = v
27
- end
28
- EOS
29
- end
30
-
31
- def fetch(params, method)
32
- raise OpenAmplify::NotSupported unless [:get, :post].include?(method.to_sym)
33
- Client::send(method, self.api_url, params)
34
- end
35
-
36
- def self.compose_url(path, params)
37
- path + '?' + URI.escape(params.collect{ |k,v| "#{k}=#{v}" }.join('&'))
38
- end
39
-
40
-
41
- private
42
-
43
-
44
- def query
45
- q = { :apiKey => @options[:api_key] }
46
- q.merge!(:analysis => @options[:analysis]) if @options[:analysis]
47
- q
48
- end
49
-
50
- def self.get(path, params)
51
- uri = URI.parse(compose_url(path, params))
52
- response = Net::HTTP.get_response(uri)
53
- OpenAmplify.validate_response!(response)
54
- response.body
55
- end
56
-
57
- def self.post(path, params)
58
- uri = URI::parse(path)
59
- response = Net::HTTP.post_form(uri, params)
60
- OpenAmplify.validate_response!(response)
61
- response.body
62
- end
63
-
64
- end # OpenAmplify::Client
65
-
66
-
67
- # Contains the response from OpenAmplify
68
- class Response
69
- include Enumerable
70
-
71
- def initialize(client, options)
72
- @client = client
73
- @options = options
74
- end
75
-
76
- def request_url
77
- @request_url ||= Client.compose_url(@client.api_url, @options[:query])
78
- end
79
-
80
- def reload
81
- response
82
- end
83
-
84
- def each
85
- response.each do |k, v|
86
- yield(k, v)
87
- end
88
- end
89
-
90
- def method_missing(name, *args, &block)
91
- response.send(name, *args, &block)
92
- end
93
-
94
- # Support the different formats. Note this would entail another request
95
- # to openamplify
96
- %w(xml json rdf csv oas signals pretty).each do |format|
97
- class_eval <<-EOS
98
- def to_#{format}
99
- fetch_as_format(:#{format})
100
- end
101
- EOS
102
- end
103
-
104
- def top_topics
105
- items = response && response['Topics']['TopTopics']
106
- end
107
-
108
- def proper_nouns
109
- items = response && response['Topics']['ProperNouns']
110
-
111
- return items if items.is_a?(Array)
112
-
113
- # I'm not sure if this is the default behavior if
114
- # only a single item is found, or if it is a bug
115
- # TODO: check other helpers as well
116
- if items.is_a?(Hash)
117
- return [ items['TopicResult'] ]
118
- end
119
- end
120
-
121
- def locations
122
- response && response['Topics']['Locations']
123
- end
124
-
125
- def domains
126
- response && response['Topics']['Domains']
127
- end
128
-
129
- def styles
130
- response && response['Styles']
131
- end
132
-
133
- private
134
-
135
- def response
136
- @response ||= fetch_response
137
- end
138
-
139
- def fetch_response
140
- response = fetch_as_format(:json)
141
- result = JSON.parse(response)
142
-
143
- if analysis = @options[:query][:analysis]
144
- name = analysis.sub(/./){ |s| s.upcase }
145
- result["ns1:#{name}Response"]["#{name}Return"]
146
- else
147
- result['ns1:AmplifyResponse']['AmplifyReturn']
148
- end
149
- end
150
-
151
- def fetch_as_format(format)
152
- @client.fetch(@options[:query].merge(:outputFormat => format), @options[:method])
153
- end
7
+ def new(options={})
8
+ Client.new(options)
9
+ end
154
10
 
155
- end # OpenAmplify::Response
11
+ # Delegate to OpenAmplify::Client
156
12
 
157
- private
13
+ def method_missing(method, *args, &block)
14
+ return super unless new.respond_to?(method)
15
+ new.send(method, *args, &block)
16
+ end
158
17
 
159
- def self.symbolize_keys(hash)
160
- hash.inject({}) do |options, (key, value)|
161
- options[(key.to_sym rescue key) || key] = value
162
- options
163
- end
18
+ def respond_to?(method, include_private = false)
19
+ new.respond_to?(method, include_private) || super(method, include_private)
164
20
  end
165
21
 
166
22
  end # module OpenAmplify
167
23
 
168
- require 'openamplify/validations'