openamplify 0.1.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/History +0 -0
- data/LICENSE +20 -0
- data/README.rdoc +75 -0
- data/Rakefile +53 -0
- data/VERSION.yml +5 -0
- data/lib/openamplify.rb +121 -0
- metadata +86 -0
data/History
ADDED
File without changes
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Greg Moreno
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
= OpenAmplify
|
2
|
+
|
3
|
+
The OpenAmplify API reads text you supply and returns linguistic data
|
4
|
+
explaining and classifying the content. What you do with that analysis is,
|
5
|
+
in the fine tradition of APIs and mashups, up to you. Some possibilities
|
6
|
+
might include pairing ads with articles, creating rich tag-clouds, or
|
7
|
+
monitoring the tone of forum threads.
|
8
|
+
|
9
|
+
== Helpful links
|
10
|
+
|
11
|
+
* <b>Overview:</b> http://community.openamplify.com/blogs/quickstart/pages/overview.aspx
|
12
|
+
|
13
|
+
== Usage
|
14
|
+
|
15
|
+
=== Show the result from OpenAmplify as a Hash
|
16
|
+
|
17
|
+
require 'openamplify'
|
18
|
+
|
19
|
+
API_KEY = "register to get a key"
|
20
|
+
client = OpenAmplify::Client.new(:api_key => API_KEY)
|
21
|
+
|
22
|
+
text = "After getting the MX1000 laser mouse and the Z-5500 speakers i fell in love with logitech"
|
23
|
+
response = client.analyze_text(text)
|
24
|
+
|
25
|
+
# List all the keys and values returned by OpenAmplify
|
26
|
+
response.each do |k, v|
|
27
|
+
pp k
|
28
|
+
pp v
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
# 'response' works like a Hash
|
33
|
+
puts response['Topics']
|
34
|
+
|
35
|
+
=== Output Format
|
36
|
+
|
37
|
+
In case you need a different format, OpenAmplify supports XML, JSON, RDF, CSV.
|
38
|
+
It can also return the result as a fancy HTML page.
|
39
|
+
|
40
|
+
# assuming you use Nokogiri
|
41
|
+
doc = Nokogiri::XML(response.to_xml)
|
42
|
+
|
43
|
+
# or you want a JSON
|
44
|
+
json = JSON.parse(response.to_json)
|
45
|
+
|
46
|
+
# you should really try the pretty formats
|
47
|
+
puts response.to_pretty
|
48
|
+
# or
|
49
|
+
puts response.to_signals
|
50
|
+
|
51
|
+
=== Analysis options
|
52
|
+
|
53
|
+
By default, OpenAmplify returns a number of 'signals' about your text.
|
54
|
+
You can limit the result by setting the 'analysis' option.
|
55
|
+
|
56
|
+
The different options and explanations are available at http://community.openamplify.com/blogs/quickstart/pages/overview.aspx
|
57
|
+
|
58
|
+
client = OpenAmplify::Client.new(:api_key => API_KEY, :analysis => 'topics')
|
59
|
+
|
60
|
+
# or if you have a client instance already
|
61
|
+
client.analysis = 'topics'
|
62
|
+
|
63
|
+
response = client.analyze_text(text)
|
64
|
+
response['Topics'] # => should be another big Hash of key-value pairs
|
65
|
+
response['Demographics'] # => nil
|
66
|
+
|
67
|
+
=== Request URL
|
68
|
+
|
69
|
+
In case you are wondering what the request URL looks like:
|
70
|
+
|
71
|
+
response.request_url
|
72
|
+
|
73
|
+
If someday, OpenAmplify decides to change their API URL:
|
74
|
+
|
75
|
+
client.base_url = 'http://newurl'
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
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
|
+
gem.add_dependency 'json'
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/test_*.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "openamplify #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION.yml
ADDED
data/lib/openamplify.rb
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module OpenAmplify
|
6
|
+
API_URL = "http://portaltnx20.openamplify.com/AmplifyWeb_v20/AmplifyThis"
|
7
|
+
|
8
|
+
class Client
|
9
|
+
def initialize(options)
|
10
|
+
@options = { :base_url => API_URL }
|
11
|
+
@options.merge!(OpenAmplify.symbolize_keys(options))
|
12
|
+
end
|
13
|
+
|
14
|
+
def analyze_text(text)
|
15
|
+
Response.new(:base_url => @options[:base_url], :query => query.merge(:inputText => text))
|
16
|
+
end
|
17
|
+
|
18
|
+
%w(api_key analysis base_url).each do |attr|
|
19
|
+
class_eval <<-EOS
|
20
|
+
def #{attr}
|
21
|
+
@options[:#{attr}]
|
22
|
+
end
|
23
|
+
|
24
|
+
def #{attr}=(v)
|
25
|
+
@options[:#{attr}] = v
|
26
|
+
end
|
27
|
+
EOS
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def query
|
33
|
+
q = { :apiKey => @options[:api_key] }
|
34
|
+
q.merge!(:analysis => @options[:analysis]) if @options[:analysis]
|
35
|
+
q
|
36
|
+
end
|
37
|
+
|
38
|
+
end # OpenAmplify::Client
|
39
|
+
|
40
|
+
# Contains the response from OpenAmplify
|
41
|
+
class Response
|
42
|
+
include Enumerable
|
43
|
+
|
44
|
+
def initialize(options)
|
45
|
+
@options = options
|
46
|
+
end
|
47
|
+
|
48
|
+
def request_url
|
49
|
+
@request_url ||= compose_url(@options[:base_url], @options[:query])
|
50
|
+
end
|
51
|
+
|
52
|
+
def response
|
53
|
+
@response ||= get_response
|
54
|
+
end
|
55
|
+
|
56
|
+
# Make this class behave like a Hash
|
57
|
+
# TODO: Add more methods
|
58
|
+
|
59
|
+
def each
|
60
|
+
response.each do |k, v|
|
61
|
+
yield(k, v)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
['has_key?', '==', '[]', 'fetch', 'empty?', 'include?', 'inspect',
|
66
|
+
'key?', 'keys', 'length', 'member?', 'merge', 'merge!'
|
67
|
+
].each do |method|
|
68
|
+
class_eval <<-EOS
|
69
|
+
def #{method}(*args)
|
70
|
+
response.send('#{method}', *args)
|
71
|
+
end
|
72
|
+
EOS
|
73
|
+
end
|
74
|
+
## Hash methods end here
|
75
|
+
|
76
|
+
# Support the different formats. Note this would entail another request
|
77
|
+
# to openamplify
|
78
|
+
%w(xml json rdf csv oas signals pretty).each do |format|
|
79
|
+
class_eval <<-EOS
|
80
|
+
def to_#{format}
|
81
|
+
get('#{format}')
|
82
|
+
end
|
83
|
+
EOS
|
84
|
+
end
|
85
|
+
|
86
|
+
private
|
87
|
+
|
88
|
+
def compose_url(path, params)
|
89
|
+
path + '?' + URI.escape(params.collect{ |k, v| "#{k}=#{v}" }.join('&'))
|
90
|
+
end
|
91
|
+
|
92
|
+
def get_response
|
93
|
+
response = get('json')
|
94
|
+
result = JSON.parse(response)
|
95
|
+
|
96
|
+
if analysis = @options[:query][:analysis]
|
97
|
+
name = analysis.sub(/./){ |s| s.upcase }
|
98
|
+
result["ns1:#{name}Response"]["#{name}Return"]
|
99
|
+
else
|
100
|
+
result['ns1:AmplifyResponse']['AmplifyReturn']
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def get(format)
|
105
|
+
params = @options[:query]
|
106
|
+
url = compose_url(@options[:base_url], params.merge(:outputFormat => format))
|
107
|
+
Net::HTTP.get_response(URI.parse(url)).body
|
108
|
+
end
|
109
|
+
|
110
|
+
end # OpenAmplify::Response
|
111
|
+
|
112
|
+
private
|
113
|
+
|
114
|
+
def self.symbolize_keys(hash)
|
115
|
+
hash.inject({}) do |options, (key, value)|
|
116
|
+
options[(key.to_sym rescue key) || key] = value
|
117
|
+
options
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
end # module OpenAmplify
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: openamplify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Greg Moreno
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-06-05 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: json
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
description: The OpenAmplify API reads text you supply and returns linguistic data explaining and classifying the content.
|
36
|
+
email: rubyoncloud@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- History
|
46
|
+
- LICENSE
|
47
|
+
- README.rdoc
|
48
|
+
- Rakefile
|
49
|
+
- VERSION.yml
|
50
|
+
- lib/openamplify.rb
|
51
|
+
has_rdoc: true
|
52
|
+
homepage: http://github.com/gregmoreno/openamplify
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options:
|
57
|
+
- --charset=UTF-8
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
hash: 3
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
requirements: []
|
79
|
+
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 1.3.7
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: Wrapper for the OpenAmplify API
|
85
|
+
test_files: []
|
86
|
+
|