openamplify 0.2.3 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +3 -0
- data/Gemfile +4 -0
- data/History +4 -0
- data/README.rdoc +61 -46
- data/Rakefile +64 -54
- data/TODO +1 -0
- data/bin/openamplify +49 -0
- data/lib/openamplify.rb +13 -158
- data/lib/openamplify/analysis/context.rb +67 -0
- data/lib/openamplify/client.rb +82 -0
- data/lib/openamplify/configuration.rb +54 -0
- data/lib/openamplify/connection.rb +28 -0
- data/lib/openamplify/error.rb +20 -0
- data/lib/openamplify/request.rb +35 -0
- data/lib/openamplify/response/raise_client_error.rb +28 -0
- data/lib/openamplify/response/raise_server_error.rb +23 -0
- data/lib/openamplify/version.rb +3 -0
- data/openamplify.gemspec +19 -49
- data/test/fixtures/input.txt +2 -0
- data/test/helper.rb +14 -0
- data/test/openamplify/analysis_test.rb +102 -0
- data/test/openamplify/client_test.rb +67 -0
- data/test/openamplify/configuration_test.rb +38 -0
- data/test/openamplify/error_test.rb +10 -0
- metadata +98 -73
- data/VERSION.yml +0 -5
- data/lib/openamplify/validations.rb +0 -38
- data/test/test_openamplify.rb +0 -98
data/.gitignore
CHANGED
data/Gemfile
ADDED
data/History
CHANGED
data/README.rdoc
CHANGED
@@ -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://
|
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
|
-
===
|
23
|
+
=== Configure
|
20
24
|
|
21
|
-
|
25
|
+
OpenAmplify.configure do |config|
|
26
|
+
config.api_key = 'YOUR_API_KEY'
|
22
27
|
|
23
|
-
|
24
|
-
|
28
|
+
# default :get
|
29
|
+
config.method = :post
|
25
30
|
|
26
|
-
|
27
|
-
|
31
|
+
# default :xml
|
32
|
+
config.output_format = :json
|
28
33
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
-
|
37
|
-
puts response['Topics']
|
43
|
+
client = OpenAmplify::Client.new(:method => :get)
|
38
44
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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
|
-
|
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(
|
65
|
+
doc = Nokogiri::XML(analysis.to_xml)
|
52
66
|
|
53
67
|
# or you want a JSON
|
54
|
-
json = JSON.parse(
|
68
|
+
json = JSON.parse(analysis.to_json)
|
55
69
|
|
56
70
|
# you should really try the pretty formats
|
57
|
-
puts
|
71
|
+
puts analysis.to_pretty
|
58
72
|
# or
|
59
|
-
puts
|
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
|
-
|
76
|
+
== Command-line
|
67
77
|
|
68
|
-
|
78
|
+
OpenAmplify gem comes with a command-line tool.
|
69
79
|
|
70
|
-
|
71
|
-
client.analysis = 'topics'
|
80
|
+
openamplify --api-key=APIKEY --format=json --analysis=topics "sample input text"
|
72
81
|
|
73
|
-
|
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
|
-
|
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
|
-
|
88
|
+
The following methods are deprecated and will be removed in 0.3.1
|
80
89
|
|
81
|
-
client
|
90
|
+
client.analyze_text('input')
|
91
|
+
client.base_url = 'http://domain.dev'
|
82
92
|
|
83
|
-
|
84
|
-
|
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
|
-
|
96
|
+
# Treating the result as hash
|
97
|
+
response.each do |k, v|
|
98
|
+
pp k
|
99
|
+
pp v
|
100
|
+
end
|
91
101
|
|
92
|
-
|
102
|
+
# 'response' works like a Hash
|
103
|
+
puts response['Topics']
|
93
104
|
|
94
|
-
|
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 '
|
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
|
4
|
+
Rake::TestTask.new do |test|
|
27
5
|
test.libs << 'lib' << 'test'
|
28
6
|
test.ruby_opts << "-rubygems"
|
29
|
-
test.pattern = 'test
|
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
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
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/bin/openamplify
ADDED
@@ -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
|
+
|
data/lib/openamplify.rb
CHANGED
@@ -1,168 +1,23 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
3
|
-
require 'json'
|
1
|
+
require 'openamplify/configuration'
|
2
|
+
require 'openamplify/client'
|
4
3
|
|
5
4
|
module OpenAmplify
|
6
|
-
|
5
|
+
extend Configuration
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
11
|
+
# Delegate to OpenAmplify::Client
|
156
12
|
|
157
|
-
|
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
|
160
|
-
|
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'
|