alchemy-api-rb 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/.gitignore +19 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +80 -0
- data/Rakefile +11 -0
- data/alchemy-api-rb.gemspec +24 -0
- data/lib/alchemy-api.rb +47 -0
- data/lib/alchemy-api/author_extraction.rb +21 -0
- data/lib/alchemy-api/base.rb +78 -0
- data/lib/alchemy-api/concept_tagging.rb +16 -0
- data/lib/alchemy-api/config.rb +34 -0
- data/lib/alchemy-api/entity_extraction.rb +15 -0
- data/lib/alchemy-api/keyword_extraction.rb +15 -0
- data/lib/alchemy-api/language_detection.rb +17 -0
- data/lib/alchemy-api/relation_extraction.rb +16 -0
- data/lib/alchemy-api/sentiment_analysis.rb +16 -0
- data/lib/alchemy-api/text_categorization.rb +21 -0
- data/lib/alchemy-api/text_extraction.rb +19 -0
- data/lib/alchemy-api/version.rb +3 -0
- data/spec/alchemy_api_spec.rb +70 -0
- data/spec/author_extraction_spec.rb +43 -0
- data/spec/base_spec.rb +20 -0
- data/spec/concept_tagging_spec.rb +44 -0
- data/spec/entity_extraction_spec.rb +44 -0
- data/spec/keyword_extraction_spec.rb +50 -0
- data/spec/language_detection_spec.rb +43 -0
- data/spec/relation_extraction_spec.rb +44 -0
- data/spec/sentiment_analysis_spec.rb +43 -0
- data/spec/spec_helper.rb +13 -0
- data/spec/text_categorization_spec.rb +43 -0
- data/spec/text_extraction_spec.rb +32 -0
- data/spec/vcr_cassettes/author_basic_html_json_search.yml +46 -0
- data/spec/vcr_cassettes/author_basic_url_json_search.yml +47 -0
- data/spec/vcr_cassettes/category_basic_html_json_search.yml +43 -0
- data/spec/vcr_cassettes/category_basic_text_json_search.yml +46 -0
- data/spec/vcr_cassettes/category_basic_url_json_search.yml +46 -0
- data/spec/vcr_cassettes/concept_basic_html_json_search.yml +54 -0
- data/spec/vcr_cassettes/concept_basic_text_json_search.yml +55 -0
- data/spec/vcr_cassettes/concept_basic_url_json_search.yml +75 -0
- data/spec/vcr_cassettes/entity_basic_html_json_search.yml +61 -0
- data/spec/vcr_cassettes/entity_basic_text_json_search.yml +60 -0
- data/spec/vcr_cassettes/entity_basic_url_json_search.yml +71 -0
- data/spec/vcr_cassettes/keyword_basic_html_json_search.yml +48 -0
- data/spec/vcr_cassettes/keyword_basic_text_json_search.yml +48 -0
- data/spec/vcr_cassettes/keyword_basic_url_json_search.yml +67 -0
- data/spec/vcr_cassettes/language_basic_html_json_search.yml +49 -0
- data/spec/vcr_cassettes/language_basic_text_json_search.yml +49 -0
- data/spec/vcr_cassettes/language_basic_url_json_search.yml +49 -0
- data/spec/vcr_cassettes/relation_basic_html_json_search.yml +53 -0
- data/spec/vcr_cassettes/relation_basic_text_json_search.yml +53 -0
- data/spec/vcr_cassettes/relation_basic_url_json_search.yml +58 -0
- data/spec/vcr_cassettes/sentiment_basic_html_json_search.yml +48 -0
- data/spec/vcr_cassettes/sentiment_basic_text_json_search.yml +48 -0
- data/spec/vcr_cassettes/sentiment_basic_url_json_search.yml +48 -0
- data/spec/vcr_cassettes/text_basic_html_json_search.yml +47 -0
- data/spec/vcr_cassettes/text_basic_url_json_search.yml +47 -0
- metadata +196 -0
@@ -0,0 +1,21 @@
|
|
1
|
+
module AlchemyAPI
|
2
|
+
class TextCategorization < Base
|
3
|
+
Config.add_mode :text_categorization, self
|
4
|
+
|
5
|
+
def web_method
|
6
|
+
"#{method_prefix}GetCategory"
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def supported_search_types
|
12
|
+
# TODO: fix html
|
13
|
+
[:text, :url]
|
14
|
+
end
|
15
|
+
|
16
|
+
def indexer
|
17
|
+
nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module AlchemyAPI
|
2
|
+
class TextExtraction < Base
|
3
|
+
Config.add_mode :text_extraction, self
|
4
|
+
|
5
|
+
def web_method
|
6
|
+
"#{method_prefix}GetText"
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def supported_search_types
|
12
|
+
[:url, :html]
|
13
|
+
end
|
14
|
+
|
15
|
+
def indexer
|
16
|
+
"text"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
2
|
+
|
3
|
+
describe AlchemyAPI do
|
4
|
+
subject() { AlchemyAPI }
|
5
|
+
|
6
|
+
it "knows the alchemy api url" do
|
7
|
+
AlchemyAPI::BASE_URL.must_be :==, "http://access.alchemyapi.com/calls/"
|
8
|
+
end
|
9
|
+
|
10
|
+
it "allows you to set the key directly" do
|
11
|
+
key = "xxxxxxxx"
|
12
|
+
AlchemyAPI.key = key
|
13
|
+
|
14
|
+
AlchemyAPI.key.must_be :==, key
|
15
|
+
end
|
16
|
+
|
17
|
+
describe AlchemyAPI::Config do
|
18
|
+
describe ".add_mode" do
|
19
|
+
it "allows classes to register themselves" do
|
20
|
+
class Foo
|
21
|
+
AlchemyAPI::Config.add_mode :foo, self
|
22
|
+
end
|
23
|
+
|
24
|
+
AlchemyAPI::Config.modes[:foo].must_be :==, Foo
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe ".output_mode" do
|
29
|
+
after do
|
30
|
+
AlchemyAPI.configure do |config|
|
31
|
+
config.output_mode = :json
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
it "allows output mode to be set" do
|
36
|
+
AlchemyAPI.configure do |config|
|
37
|
+
config.output_mode = :xml
|
38
|
+
end
|
39
|
+
|
40
|
+
AlchemyAPI.config.output_mode.must_be :==, :xml
|
41
|
+
end
|
42
|
+
|
43
|
+
it "errors on invalid output mode" do
|
44
|
+
-> {
|
45
|
+
AlchemyAPI.configure do |config|
|
46
|
+
config.output_mode = :xls
|
47
|
+
end
|
48
|
+
}.must_raise AlchemyAPI::InvalidOutputMode
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe ".search" do
|
54
|
+
it "needs an api key" do
|
55
|
+
AlchemyAPI::Config.apikey = nil
|
56
|
+
|
57
|
+
-> {
|
58
|
+
AlchemyAPI.search(:keyword_extraction, :text => "foo")
|
59
|
+
}.must_raise AlchemyAPI::InvalidAPIKey
|
60
|
+
end
|
61
|
+
|
62
|
+
it "needs a valid mode" do
|
63
|
+
AlchemyAPI::Config.apikey = "xxxxxxxxxxx"
|
64
|
+
|
65
|
+
-> {
|
66
|
+
AlchemyAPI.search(:foo, :text => "hello")
|
67
|
+
}.must_raise AlchemyAPI::InvalidSearchMode
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
2
|
+
|
3
|
+
describe AlchemyAPI, "author_extraction" do
|
4
|
+
before do
|
5
|
+
AlchemyAPI::Config.apikey = API_KEY
|
6
|
+
end
|
7
|
+
|
8
|
+
subject() { AlchemyAPI::AuthorExtraction.new }
|
9
|
+
|
10
|
+
describe "#search" do
|
11
|
+
{
|
12
|
+
:html => "<html><body>Google is a large, by Joe Smith</body></html>",
|
13
|
+
:url => "http://blog.alchemyapi.com/"
|
14
|
+
}.each do |type,value|
|
15
|
+
[:json].each do |output_mode|
|
16
|
+
before do
|
17
|
+
AlchemyAPI::Config.output_mode = output_mode
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#{type} search with #{output_mode} results" do
|
21
|
+
it "returns an array of results" do
|
22
|
+
VCR.use_cassette("author_basic_#{type}_#{output_mode}_search") do
|
23
|
+
result = subject.search(type => value)
|
24
|
+
|
25
|
+
result.must_be_instance_of Hash
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it "includes the keyword text and relavence" do
|
30
|
+
VCR.use_cassette("author_basic_#{type}_#{output_mode}_search") do
|
31
|
+
result = subject.search(type => value)
|
32
|
+
|
33
|
+
result["author"].wont_be_nil
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
|
data/spec/base_spec.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
2
|
+
|
3
|
+
describe AlchemyAPI::Base do
|
4
|
+
subject() { AlchemyAPI::Base.new }
|
5
|
+
|
6
|
+
describe "#connection" do
|
7
|
+
it "creates a connection" do
|
8
|
+
subject.send(:connection).wont_be_nil
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "#merge_options" do
|
13
|
+
it "combines default options with user provided" do
|
14
|
+
opts = { extra: "extra" }
|
15
|
+
expected = { outputMode: :json }.merge(opts)
|
16
|
+
|
17
|
+
subject.merged_options(opts).must_be :==, expected
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
2
|
+
|
3
|
+
describe AlchemyAPI, "concept_extraction" do
|
4
|
+
before do
|
5
|
+
AlchemyAPI::Config.apikey = API_KEY
|
6
|
+
end
|
7
|
+
|
8
|
+
subject() { AlchemyAPI::ConceptTagging.new }
|
9
|
+
|
10
|
+
describe "#search" do
|
11
|
+
{
|
12
|
+
:text => "Google is a large company",
|
13
|
+
:html => "<html><body>Google is a large company</body></html>",
|
14
|
+
:url => "http://www.alchemy.com"
|
15
|
+
}.each do |type,value|
|
16
|
+
[:json].each do |output_mode|
|
17
|
+
before do
|
18
|
+
AlchemyAPI::Config.output_mode = output_mode
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#{type} search with #{output_mode} results" do
|
22
|
+
it "returns an array of results" do
|
23
|
+
VCR.use_cassette("concept_basic_#{type}_#{output_mode}_search") do
|
24
|
+
result = subject.search(type => value)
|
25
|
+
|
26
|
+
result.must_be_instance_of Array
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "includes the keyword text and relavence" do
|
31
|
+
VCR.use_cassette("concept_basic_#{type}_#{output_mode}_search") do
|
32
|
+
result = subject.search(type => value)[0]
|
33
|
+
|
34
|
+
result["text"].wont_be_nil
|
35
|
+
result["relevance"].wont_be_nil
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
2
|
+
|
3
|
+
describe AlchemyAPI, "entity_extraction" do
|
4
|
+
before do
|
5
|
+
AlchemyAPI::Config.apikey = API_KEY
|
6
|
+
end
|
7
|
+
|
8
|
+
subject() { AlchemyAPI::EntityExtraction.new }
|
9
|
+
|
10
|
+
describe "#search" do
|
11
|
+
{
|
12
|
+
:text => "Google is a large company",
|
13
|
+
:html => "<html><body>Google is a large company</body></html>",
|
14
|
+
:url => "http://www.google.com"
|
15
|
+
}.each do |type,value|
|
16
|
+
[:json].each do |output_mode|
|
17
|
+
before do
|
18
|
+
AlchemyAPI::Config.output_mode = output_mode
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#{type} search with #{output_mode} results" do
|
22
|
+
it "returns an array of results" do
|
23
|
+
VCR.use_cassette("entity_basic_#{type}_#{output_mode}_search") do
|
24
|
+
result = subject.search(type => value)
|
25
|
+
|
26
|
+
result.must_be_instance_of Array
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "includes the keyword text and relavence" do
|
31
|
+
VCR.use_cassette("entity_basic_#{type}_#{output_mode}_search") do
|
32
|
+
result = subject.search(type => value)[0]
|
33
|
+
|
34
|
+
result["text"].wont_be_nil
|
35
|
+
result["type"].wont_be_nil
|
36
|
+
result["count"].wont_be_nil
|
37
|
+
result["relevance"].wont_be_nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
2
|
+
|
3
|
+
describe AlchemyAPI, "keyword_extraction" do
|
4
|
+
before do
|
5
|
+
AlchemyAPI::Config.apikey = API_KEY
|
6
|
+
end
|
7
|
+
|
8
|
+
subject() { AlchemyAPI::KeywordExtraction.new }
|
9
|
+
|
10
|
+
# WTF?!?
|
11
|
+
# let(:searches) do
|
12
|
+
# {
|
13
|
+
# :text => "lorem ipsum",
|
14
|
+
# :html => "<html><body>foo bar</body></html>"
|
15
|
+
# }
|
16
|
+
# end
|
17
|
+
|
18
|
+
describe "#search" do
|
19
|
+
{
|
20
|
+
:text => "lorem ipsum",
|
21
|
+
:html => "<html><body>foo bar</body></html>",
|
22
|
+
:url => "http://www.google.com"
|
23
|
+
}.each do |type,value|
|
24
|
+
[:json].each do |output_mode|
|
25
|
+
before do
|
26
|
+
AlchemyAPI::Config.output_mode = output_mode
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#{type} search with #{output_mode} results" do
|
30
|
+
it "returns an array of results" do
|
31
|
+
VCR.use_cassette("keyword_basic_#{type}_#{output_mode}_search") do
|
32
|
+
result = subject.search(type => value)
|
33
|
+
|
34
|
+
result.must_be_instance_of Array
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
it "includes the keyword text and relavence" do
|
39
|
+
VCR.use_cassette("keyword_basic_#{type}_#{output_mode}_search") do
|
40
|
+
result = subject.search(type => value)[0]
|
41
|
+
|
42
|
+
result["text"].wont_be_nil
|
43
|
+
result["relevance"].wont_be_nil
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
2
|
+
|
3
|
+
describe AlchemyAPI, "language_detection" do
|
4
|
+
before do
|
5
|
+
AlchemyAPI::Config.apikey = API_KEY
|
6
|
+
end
|
7
|
+
|
8
|
+
subject() { AlchemyAPI::LanguageDetection.new }
|
9
|
+
|
10
|
+
describe "#search" do
|
11
|
+
{
|
12
|
+
:text => "Lorem ipsum dolor sit amet.",
|
13
|
+
:html => "<html><body>Lorem ipsum dolor sit amet.</body></html>",
|
14
|
+
:url => "http://www.alchemyapi.com/"
|
15
|
+
}.each do |type,value|
|
16
|
+
[:json].each do |output_mode|
|
17
|
+
before do
|
18
|
+
AlchemyAPI::Config.output_mode = output_mode
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#{type} search with #{output_mode} results" do
|
22
|
+
it "returns an array of results" do
|
23
|
+
VCR.use_cassette("language_basic_#{type}_#{output_mode}_search") do
|
24
|
+
result = subject.search(type => value)
|
25
|
+
|
26
|
+
result.must_be_instance_of Hash
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "includes the keyword text and relavence" do
|
31
|
+
VCR.use_cassette("language_basic_#{type}_#{output_mode}_search") do
|
32
|
+
result = subject.search(type => value)
|
33
|
+
|
34
|
+
result["language"].wont_be_nil
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
2
|
+
|
3
|
+
describe AlchemyAPI, "relation_extraction" do
|
4
|
+
before do
|
5
|
+
AlchemyAPI::Config.apikey = API_KEY
|
6
|
+
end
|
7
|
+
|
8
|
+
subject() { AlchemyAPI::RelationExtraction.new }
|
9
|
+
|
10
|
+
describe "#search" do
|
11
|
+
{
|
12
|
+
:text => "Google is a large company",
|
13
|
+
:html => "<html><body>Google is a large company</body></html>",
|
14
|
+
:url => "http://www.alchemy.com"
|
15
|
+
}.each do |type,value|
|
16
|
+
[:json].each do |output_mode|
|
17
|
+
before do
|
18
|
+
AlchemyAPI::Config.output_mode = output_mode
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#{type} search with #{output_mode} results" do
|
22
|
+
it "returns an array of results" do
|
23
|
+
VCR.use_cassette("relation_basic_#{type}_#{output_mode}_search") do
|
24
|
+
result = subject.search(type => value)
|
25
|
+
|
26
|
+
result.must_be_instance_of Array
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "includes the keyword text and relavence" do
|
31
|
+
VCR.use_cassette("relation_basic_#{type}_#{output_mode}_search") do
|
32
|
+
result = subject.search(type => value)[0]
|
33
|
+
|
34
|
+
result["subject"].wont_be_nil
|
35
|
+
result["action"].wont_be_nil
|
36
|
+
result["object"].wont_be_nil
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
2
|
+
|
3
|
+
describe AlchemyAPI, "sentiment_analysis" do
|
4
|
+
before do
|
5
|
+
AlchemyAPI::Config.apikey = API_KEY
|
6
|
+
end
|
7
|
+
|
8
|
+
subject() { AlchemyAPI::SentimentAnalysis.new }
|
9
|
+
|
10
|
+
describe "#search" do
|
11
|
+
{
|
12
|
+
:text => "Alchemy is cool",
|
13
|
+
:html => "<html><body>Alchemy is cool</body></html>",
|
14
|
+
:url => "http://www.alchemyapi.com"
|
15
|
+
}.each do |type,value|
|
16
|
+
[:json].each do |output_mode|
|
17
|
+
before do
|
18
|
+
AlchemyAPI::Config.output_mode = output_mode
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#{type} search with #{output_mode} results" do
|
22
|
+
it "returns an array of results" do
|
23
|
+
VCR.use_cassette("sentiment_basic_#{type}_#{output_mode}_search") do
|
24
|
+
result = subject.search(type => value)
|
25
|
+
|
26
|
+
result.must_be_instance_of Hash
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "includes the keyword text and relavence" do
|
31
|
+
VCR.use_cassette("sentiment_basic_#{type}_#{output_mode}_search") do
|
32
|
+
result = subject.search(type => value)
|
33
|
+
|
34
|
+
result["type"].wont_be_nil
|
35
|
+
result["score"].wont_be_nil
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|