auto_tagging 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.rdebugrc +3 -0
- data/.rspec +4 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.rdoc +58 -0
- data/Rakefile +2 -0
- data/auto_tagging.gemspec +20 -0
- data/lib/auto_tagging.rb +13 -0
- data/lib/auto_tagging/alchemy.rb +33 -0
- data/lib/auto_tagging/config.rb +46 -0
- data/lib/auto_tagging/error.rb +27 -0
- data/lib/auto_tagging/open_calais.rb +38 -0
- data/lib/auto_tagging/search_param.rb +23 -0
- data/lib/auto_tagging/string_ext.rb +8 -0
- data/lib/auto_tagging/version.rb +3 -0
- data/lib/auto_tagging/yahoo.rb +68 -0
- data/spec/fixtures/content.yml +13 -0
- data/spec/lib/auto_tagging/alchemy_spec.rb +64 -0
- data/spec/lib/auto_tagging/open_calais_spec.rb +53 -0
- data/spec/lib/auto_tagging/search_param_spec.rb +55 -0
- data/spec/lib/auto_tagging/yahoo_spec.rb +51 -0
- data/spec/lib/auto_tagging_spec.rb +79 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/support/content.rb +10 -0
- metadata +129 -0
data/.gitignore
ADDED
data/.rdebugrc
ADDED
data/.rspec
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.3-p194@auto-tagging --create
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 KhoiD + TienN
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
= AutoTagging
|
2
|
+
|
3
|
+
A Ruby wrapper library for multiple auto tagging services such as Open Calais, Yahoo Content Analysis and Alchemy.
|
4
|
+
Learn more about these services at:
|
5
|
+
|
6
|
+
http://www.alchemyapi.com/
|
7
|
+
|
8
|
+
http://www.opencalais.com/
|
9
|
+
|
10
|
+
http://developer.yahoo.com/contentanalysis/
|
11
|
+
|
12
|
+
Just by specifying a paragraph or an url , meaningful tags/keywords will return in form of an array
|
13
|
+
|
14
|
+
== Examples :
|
15
|
+
|
16
|
+
AutoTagging.get_tags(url: "www.rubygems.org")
|
17
|
+
results : ["gem hosting service", "available gems", "Ruby community", "contributor", "API", "RubyGems.org", "site", "changes", "information"]
|
18
|
+
|
19
|
+
AutoTagging.get_tags("Tens of thousands of Rails applications are already live. People are using Rails in the tiniest part-time operations to the biggest companies.")
|
20
|
+
results : ["Rails applications", "biggest companies", "Tens", "thousands", "People", "Software" "Web application frameworks", "Computing", "Web 2.0", "Ruby on Rails", "Labor"]
|
21
|
+
|
22
|
+
|
23
|
+
== Installation
|
24
|
+
gem install 'auto_tagging'
|
25
|
+
|
26
|
+
or add to Gemfile:
|
27
|
+
gem 'auto_tagging'
|
28
|
+
|
29
|
+
== Usage
|
30
|
+
Configure:
|
31
|
+
Specify one or many services you want to use, for example:
|
32
|
+
AutoTagging.services = [{:alchemy => "alchemy_api_key"}]
|
33
|
+
or
|
34
|
+
AutoTagging.services = [{:open_calais => "open_calais_api_key}]
|
35
|
+
or
|
36
|
+
AutoTagging.services = ["yahoo"]
|
37
|
+
or
|
38
|
+
AutoTagging.services = [{:open_calais => "open_calais_api_key}, {:alchemy => "alchemy_api_key"}, "yahoo"]
|
39
|
+
|
40
|
+
Note: The result will be the combination of the results return from these services calls but it will slow down the run time
|
41
|
+
|
42
|
+
Then invoke
|
43
|
+
AutoTagging.get_tags("Put content here")
|
44
|
+
|
45
|
+
You can give url as parameter , please note that this feature only available for Yahoo Content Analysis service and Alchemy
|
46
|
+
AutoTagging.get_tags({:url => "rubygems.org"})
|
47
|
+
|
48
|
+
== Contributing
|
49
|
+
|
50
|
+
1. Fork it
|
51
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
52
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
53
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
54
|
+
5. Create new Pull Request
|
55
|
+
|
56
|
+
== Copyright
|
57
|
+
|
58
|
+
Copyright (c) 2013 East Agile.
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/auto_tagging/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Eastagile"]
|
6
|
+
gem.email = ["@eastagile.com"]
|
7
|
+
gem.description = %q{A Ruby wrapper library for multiple auto tagging services such as Open Calais, Yahoo, Alchemy.}
|
8
|
+
gem.summary = %q{A Ruby wrapper library for multiple auto tagging services such as Open Calais, Yahoo, Alchemy.}
|
9
|
+
gem.homepage = "http://www.eastagile.com"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "auto_tagging"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = AutoTagging::VERSION
|
17
|
+
gem.add_development_dependency 'rspec'
|
18
|
+
gem.add_development_dependency 'debugger'
|
19
|
+
gem.add_dependency 'alchemy-api-rb'
|
20
|
+
end
|
data/lib/auto_tagging.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "auto_tagging/version"
|
2
|
+
require "auto_tagging/config"
|
3
|
+
require "auto_tagging/error"
|
4
|
+
|
5
|
+
module AutoTagging
|
6
|
+
extend AutoTagging::Config
|
7
|
+
|
8
|
+
def self.get_tags(content = '')
|
9
|
+
[] if content.to_s.empty?
|
10
|
+
raise AutoTagging::Errors::NoServiceConfigurationError if self.mains.to_a.empty?
|
11
|
+
self.mains.map { | main | main.get_tags(content) }.flatten.compact.uniq
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'alchemy_api'
|
2
|
+
require 'auto_tagging/search_param'
|
3
|
+
|
4
|
+
module AutoTagging
|
5
|
+
class Alchemy
|
6
|
+
class << self
|
7
|
+
attr_accessor :api_key
|
8
|
+
|
9
|
+
def api_key=(api_key)
|
10
|
+
AlchemyAPI.key = api_key
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def get_tags(opts)
|
15
|
+
tags = AlchemyAPI.search(:keyword_extraction, src_options(opts)) || []
|
16
|
+
tags.map { |tag| tag["text"] }
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def src_options(opts)
|
22
|
+
AutoTagging::SearchParam.url_search?(opts) ? url(opts) : text(opts)
|
23
|
+
end
|
24
|
+
|
25
|
+
def url(opts)
|
26
|
+
{ :url => AutoTagging::SearchParam.to_valid_url(opts[:url]) }
|
27
|
+
end
|
28
|
+
|
29
|
+
def text(opts)
|
30
|
+
{:text => opts}
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require "auto_tagging/open_calais"
|
2
|
+
require "auto_tagging/alchemy"
|
3
|
+
require "auto_tagging/yahoo"
|
4
|
+
require "auto_tagging/error"
|
5
|
+
require "auto_tagging/string_ext"
|
6
|
+
|
7
|
+
module AutoTagging
|
8
|
+
include Errors
|
9
|
+
|
10
|
+
module Config
|
11
|
+
include AutoTagging::StringExt
|
12
|
+
|
13
|
+
attr_accessor :services, :mains
|
14
|
+
|
15
|
+
def services=(services)
|
16
|
+
reset_mains
|
17
|
+
services.each { |service| add_service(service) }
|
18
|
+
end
|
19
|
+
|
20
|
+
def reset_mains
|
21
|
+
self.mains = []
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def add_service(service)
|
27
|
+
klass = const(service)
|
28
|
+
klass.api_key = api_key(service) if klass.respond_to?("api_key=")
|
29
|
+
( self.mains ||= [] ) << klass.new
|
30
|
+
end
|
31
|
+
|
32
|
+
def api_key(service)
|
33
|
+
service.values[0].to_s if service.instance_of? Hash
|
34
|
+
end
|
35
|
+
|
36
|
+
def service_name(service)
|
37
|
+
( service.instance_of?(Hash) ? service.keys[0] : service ).to_s
|
38
|
+
end
|
39
|
+
|
40
|
+
def const(service)
|
41
|
+
AutoTagging::const_get(camelize(service_name(service)))
|
42
|
+
rescue NameError
|
43
|
+
raise AutoTagging::Errors::InvalidServiceError
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module AutoTagging
|
2
|
+
module Errors
|
3
|
+
class InvalidServiceError < StandardError
|
4
|
+
def initialize(msg = "Please specify a valid tagging service")
|
5
|
+
super(msg)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class NilContentError < StandardError
|
10
|
+
def initialize(msg = "Content is required")
|
11
|
+
super(msg)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class NoServiceConfigurationError < StandardError
|
16
|
+
def initialize(msg = "Please configure at lease 1 tagging service")
|
17
|
+
super(msg)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class InvalidSearchError < StandardError
|
22
|
+
def initialize(msg = "Invalid search options")
|
23
|
+
super(msg)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require "net/http"
|
2
|
+
require "json"
|
3
|
+
|
4
|
+
module AutoTagging
|
5
|
+
class OpenCalais
|
6
|
+
API_SITE_URL = 'api.opencalais.com'
|
7
|
+
API_PAGE_URL = '/tag/rs/enrich'
|
8
|
+
|
9
|
+
class << self
|
10
|
+
attr_accessor :api_key
|
11
|
+
end
|
12
|
+
|
13
|
+
def get_tags(content)
|
14
|
+
res = nil
|
15
|
+
Net::HTTP.start(API_SITE_URL) do |http|
|
16
|
+
req = Net::HTTP::Post.new(API_PAGE_URL)
|
17
|
+
req.initialize_http_header(options)
|
18
|
+
req.body = content.to_s
|
19
|
+
res = http.request(req)
|
20
|
+
end
|
21
|
+
|
22
|
+
tags = JSON.parse(res.body).values.map { |value| value["name"] }.compact if res && res.code == "200"
|
23
|
+
tags || []
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def options
|
29
|
+
@options ||= {
|
30
|
+
'x-calais-licenseID' => OpenCalais.api_key,
|
31
|
+
'content-type' => 'text/raw',
|
32
|
+
'outputFormat' => 'Application/JSON',
|
33
|
+
'calculateRelevanceScore' => 'false',
|
34
|
+
'enableMetadataType' => 'SocialTags'
|
35
|
+
}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'auto_tagging/error'
|
2
|
+
|
3
|
+
module AutoTagging
|
4
|
+
module SearchParam
|
5
|
+
class << self
|
6
|
+
def url_search?(opts)
|
7
|
+
src = false
|
8
|
+
if opts.instance_of? Hash
|
9
|
+
if opts.keys.size == 1 && opts.keys[0].to_s.downcase == "url"
|
10
|
+
src = true
|
11
|
+
else
|
12
|
+
raise AutoTagging::Errors::InvalidSearchError
|
13
|
+
end
|
14
|
+
end
|
15
|
+
src
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_valid_url(url)
|
19
|
+
( /http:\/\/|https:\/\// =~ url ) == 0 ? url : "http://#{url}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'auto_tagging/search_param'
|
2
|
+
|
3
|
+
module AutoTagging
|
4
|
+
class Yahoo
|
5
|
+
|
6
|
+
API_SITE_URL = 'http://query.yahooapis.com/v1/public/yql'
|
7
|
+
|
8
|
+
def get_tags(content)
|
9
|
+
res = service_request(content)
|
10
|
+
json_res = JSON.parse(res.body)
|
11
|
+
tags = parse_response(res) if (json_res["query"]["count"] > 0)
|
12
|
+
tags || []
|
13
|
+
rescue NoMethodError
|
14
|
+
[]
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def service_request(content)
|
20
|
+
Net::HTTP.post_form(uri, options(content))
|
21
|
+
end
|
22
|
+
|
23
|
+
def parse_response(res)
|
24
|
+
results = JSON.parse(res.body)["query"]["results"]
|
25
|
+
categories(results).concat entities(results)
|
26
|
+
end
|
27
|
+
|
28
|
+
def categories(results)
|
29
|
+
( [] << results["yctCategories"]["yctCategory"] ).flatten.map { |c| c["content"] }
|
30
|
+
rescue
|
31
|
+
[]
|
32
|
+
end
|
33
|
+
|
34
|
+
def entities(results)
|
35
|
+
( [] << results["entities"]["entity"] ).flatten.map { |e| e["text"]["content"] }
|
36
|
+
rescue
|
37
|
+
[]
|
38
|
+
end
|
39
|
+
|
40
|
+
def query(opts)
|
41
|
+
%{ SELECT * FROM contentanalysis.analyze WHERE #{src_options(opts)} }
|
42
|
+
end
|
43
|
+
|
44
|
+
def src_options(opts)
|
45
|
+
AutoTagging::SearchParam.url_search?(opts) ? url(opts) : text(opts)
|
46
|
+
end
|
47
|
+
|
48
|
+
def url(opts)
|
49
|
+
%{ url = "#{AutoTagging::SearchParam.to_valid_url(opts.values[0])}" }
|
50
|
+
end
|
51
|
+
|
52
|
+
def text(opts)
|
53
|
+
%{ text = "#{opts.gsub!('"', '\"')}" }
|
54
|
+
end
|
55
|
+
|
56
|
+
def uri
|
57
|
+
URI(API_SITE_URL)
|
58
|
+
end
|
59
|
+
|
60
|
+
def options(content)
|
61
|
+
{
|
62
|
+
'q' => query(content),
|
63
|
+
'format' => 'json',
|
64
|
+
'max' => '50'
|
65
|
+
}
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
empty_content: ""
|
2
|
+
short_content: "This is too short"
|
3
|
+
long_content: "In the UK, the HPA has recorded eight cases of oseltamivir-resistant H1N1pdm09 in
|
4
|
+
the community setting.
|
5
|
+
The HPA's head of flu surveillance Dr Richard Pebody said: \"While the frequency
|
6
|
+
of oseltamivir resistance in community settings has increased slightly since the 2009-10
|
7
|
+
pandemic from 1-2% in the 2012/13 flu season, rates of detection remain low.\"
|
8
|
+
Swine flu (H1N1) infected a fifth of people during the first year of the pandemic in 2009, data suggest.
|
9
|
+
It is thought the virus killed 200,000 people globally.
|
10
|
+
Although the pandemic has been declared by officials as over, the virus is still circulating.
|
11
|
+
During the pandemic, the H1N1 virus crowded out other influenza viruses to become the dominant virus. This is no longer the case. Many countries are reporting a mix of influenza viruses."
|
12
|
+
|
13
|
+
complex_content: "Soccer !@#$%^&*())_+ []{}||\";',./<>? Manchester United vs Chelsea , Real Madrid, Barcelona, Football match"
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe "AutoTagging::Alchemy" do
|
4
|
+
let(:main) { AutoTagging::Alchemy.new }
|
5
|
+
before(:each) { AutoTagging::Alchemy.api_key = key }
|
6
|
+
|
7
|
+
describe "#get_tags" do
|
8
|
+
context "invalid key" do
|
9
|
+
let(:key) { 'invalid_key' }
|
10
|
+
|
11
|
+
it "should return an empty array" do
|
12
|
+
main.get_tags(empty_content).should == []
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context "valid key" do
|
17
|
+
let(:key) { "e0d4cf69ba90640704a9f831b69b6ec7531142fe" }
|
18
|
+
|
19
|
+
context "with no content" do
|
20
|
+
it "should return an empty array" do
|
21
|
+
main.get_tags(empty_content).should == []
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "with content" do
|
26
|
+
context "url" do
|
27
|
+
context "invalid url" do
|
28
|
+
let(:opts) { {:url => "invalid url"} }
|
29
|
+
it "should return an empty array" do
|
30
|
+
main.get_tags(opts).should == []
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "valid url" do
|
35
|
+
let(:opts) { {:url => "www.bbc.co.uk"} }
|
36
|
+
it "should return an empty array" do
|
37
|
+
main.get_tags(opts).should_not be_empty
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "content" do
|
43
|
+
context "short content" do
|
44
|
+
it "should return an empty array" do
|
45
|
+
main.get_tags(short_content).should == []
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context "valid content" do
|
50
|
+
it "should return an array" do
|
51
|
+
main.get_tags(long_content).should_not be_empty
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context "complex content" do
|
56
|
+
it "should return an array" do
|
57
|
+
main.get_tags(complex_content).should_not be_empty
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe "AutoTagging::OpenCalais" do
|
4
|
+
describe "#get_tags" do
|
5
|
+
before(:each) { AutoTagging::OpenCalais.api_key = key }
|
6
|
+
let(:main) { AutoTagging::OpenCalais.new }
|
7
|
+
|
8
|
+
context "invalid key" do
|
9
|
+
let(:key) { 'invalid_key' }
|
10
|
+
|
11
|
+
it "should return an empty array" do
|
12
|
+
main.get_tags(long_content).should == []
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context "valid key" do
|
17
|
+
let(:key) { "vcpghu34sh4exhrgx6nvetfg" }
|
18
|
+
|
19
|
+
context "with no content" do
|
20
|
+
it "should return an empty array" do
|
21
|
+
main.get_tags(empty_content).should == []
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "with content" do
|
26
|
+
context "url" do
|
27
|
+
let(:opts) { {:url => "http://www.heroku.com" } }
|
28
|
+
it "should not raise error" do
|
29
|
+
expect { main.get_tags(opts) }.to_not raise_error
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context "short content" do
|
34
|
+
it "should return an empty array" do
|
35
|
+
main.get_tags(short_content).should == []
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "valid content" do
|
40
|
+
it "should return an array" do
|
41
|
+
main.get_tags(long_content).should_not be_empty
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "complex content" do
|
46
|
+
it "should return an array" do
|
47
|
+
main.get_tags(complex_content).should_not be_empty
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe "AutoTagging::SearchParam" do
|
4
|
+
describe "#url_search?" do
|
5
|
+
let(:key) { "e0d4cf69ba90640704a9f831b69b6ec7531142fe" }
|
6
|
+
|
7
|
+
context "invalid search options" do
|
8
|
+
let(:opts) { {:website => "http://www.yahoo.com"} }
|
9
|
+
|
10
|
+
it "should raise AutoTagging::Errors::InvalidSearchError" do
|
11
|
+
expect do
|
12
|
+
AutoTagging::SearchParam.url_search?(opts)
|
13
|
+
end.to raise_error(AutoTagging::Errors::InvalidSearchError)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "valid search options" do
|
18
|
+
context "url search" do
|
19
|
+
let(:opts) { {:url => "http://www.yahoo.com"} }
|
20
|
+
|
21
|
+
it "should return true" do
|
22
|
+
AutoTagging::SearchParam.url_search?(opts).should be_true
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "text search" do
|
27
|
+
let(:opts) { "just a normal text search" }
|
28
|
+
|
29
|
+
it "should return false" do
|
30
|
+
AutoTagging::SearchParam.url_search?(opts).should be_false
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "to_valid_url" do
|
37
|
+
context "with http/https prefix" do
|
38
|
+
let(:urls) { ["http://www.db.com", "https://www.db.com"] }
|
39
|
+
|
40
|
+
it "should not adjust url" do
|
41
|
+
urls.each do |url|
|
42
|
+
AutoTagging::SearchParam.to_valid_url(url).should == url
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "without http/https prefix" do
|
48
|
+
let(:url) { "www.db.com" }
|
49
|
+
|
50
|
+
it "should add 'http://' prefix " do
|
51
|
+
AutoTagging::SearchParam.to_valid_url(url).should == "http://#{url}"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe "AutoTagging::Yahoo" do
|
4
|
+
describe "#get_tags" do
|
5
|
+
let(:main) { AutoTagging::Yahoo.new }
|
6
|
+
|
7
|
+
context "yahoo service return an invalid response" do
|
8
|
+
before(:each) { main.stub(:service_request).and_return("") }
|
9
|
+
it "should return empty array" do
|
10
|
+
main.get_tags(long_content).should == []
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "empty content" do
|
15
|
+
it "should return empty array" do
|
16
|
+
main.get_tags(empty_content).should == []
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "not empty content" do
|
21
|
+
it "should return an array" do
|
22
|
+
main.get_tags(long_content).should_not be_empty
|
23
|
+
end
|
24
|
+
|
25
|
+
context "complex content" do
|
26
|
+
it "should return an array" do
|
27
|
+
main.get_tags(complex_content).should_not be_empty
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "url search" do
|
32
|
+
context "invalid url" do
|
33
|
+
let(:opts) { {:url => "not a valid url"} }
|
34
|
+
|
35
|
+
it "should return an empty array" do
|
36
|
+
main.get_tags(opts).should == []
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "valid url" do
|
41
|
+
let(:opts) { {:url => "http://www.bbc.co.uk/"} }
|
42
|
+
|
43
|
+
it "should return an array" do
|
44
|
+
main.get_tags(opts).should_not be_empty
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe "AutoTagging" do
|
4
|
+
describe "services=" do
|
5
|
+
let(:services) { ["yahoo", {"open_calais" => "key"}] }
|
6
|
+
it "should call add_service for each service" do
|
7
|
+
services.each {|service| AutoTagging.should_receive(:add_service).with(service)}
|
8
|
+
AutoTagging.services = services
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "#const" do
|
13
|
+
let(:service) { {"open_calais" => "jqk145" } }
|
14
|
+
|
15
|
+
before(:each) { AutoTagging.should_receive(:service_name).with(service).and_return(service_name) }
|
16
|
+
|
17
|
+
context "valid service" do
|
18
|
+
let(:service_name) { "open_calais" }
|
19
|
+
|
20
|
+
it "should return valid class" do
|
21
|
+
AutoTagging.send(:const, service).should == AutoTagging::OpenCalais
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "invalid service name" do
|
26
|
+
let(:service_name) { "google" }
|
27
|
+
|
28
|
+
it "should raise AutoTagging::Errors::InvalidServiceError" do
|
29
|
+
expect do
|
30
|
+
AutoTagging.send(:const, service)
|
31
|
+
end.to raise_error(AutoTagging::Errors::InvalidServiceError)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "#add_service" do
|
37
|
+
let(:service) { "yahoo" }
|
38
|
+
let(:klass) { double(:const, :new => "") }
|
39
|
+
before(:each) { AutoTagging.stub(:const).and_return(klass) }
|
40
|
+
|
41
|
+
it "should invoke const on given service" do
|
42
|
+
AutoTagging.should_receive(:const).with(service)
|
43
|
+
AutoTagging.send(:add_service,service)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should add obj to mains" do
|
47
|
+
expect do
|
48
|
+
AutoTagging.send(:add_service,service)
|
49
|
+
end.to change(AutoTagging.mains,:size).by(1)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "#get_tags" do
|
54
|
+
context "without main objs" do
|
55
|
+
before(:each) { AutoTagging.reset_mains }
|
56
|
+
it "should raise AutoTagging::Errors::NoServiceConfigurationError" do
|
57
|
+
expect do
|
58
|
+
AutoTagging.get_tags(short_content)
|
59
|
+
end.to raise_error(AutoTagging::Errors::NoServiceConfigurationError)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context "with main objs" do
|
64
|
+
let(:yahoo_main) { AutoTagging::Yahoo.new }
|
65
|
+
let(:alchemy_main) { AutoTagging::Alchemy.new }
|
66
|
+
let(:open_calais_main) { AutoTagging::OpenCalais.new }
|
67
|
+
let(:mains) { [yahoo_main, alchemy_main, open_calais_main] }
|
68
|
+
|
69
|
+
before(:each) do
|
70
|
+
AutoTagging.stub(:mains).and_return(mains)
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should invoke get_tags for each main obj" do
|
74
|
+
mains.each { |main| main.should_receive(:get_tags).with(long_content) }
|
75
|
+
AutoTagging.get_tags(long_content)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'auto_tagging'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.include FixtureContent
|
12
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
FIXTURE_CONTENTS = YAML::load( File.open(File.join(File.dirname(__FILE__), '../fixtures/content.yml')) )
|
2
|
+
shared_context 'load fixture contents' do
|
3
|
+
FIXTURE_CONTENTS.each { |key, value| let(key) { value } }
|
4
|
+
end
|
5
|
+
|
6
|
+
module FixtureContent
|
7
|
+
def self.included(scope)
|
8
|
+
scope.include_context "load fixture contents"
|
9
|
+
end
|
10
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: auto_tagging
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Eastagile
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: debugger
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: alchemy-api-rb
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: A Ruby wrapper library for multiple auto tagging services such as Open
|
63
|
+
Calais, Yahoo, Alchemy.
|
64
|
+
email:
|
65
|
+
- ! '@eastagile.com'
|
66
|
+
executables: []
|
67
|
+
extensions: []
|
68
|
+
extra_rdoc_files: []
|
69
|
+
files:
|
70
|
+
- .gitignore
|
71
|
+
- .rdebugrc
|
72
|
+
- .rspec
|
73
|
+
- .rvmrc
|
74
|
+
- Gemfile
|
75
|
+
- LICENSE
|
76
|
+
- README.rdoc
|
77
|
+
- Rakefile
|
78
|
+
- auto_tagging.gemspec
|
79
|
+
- lib/auto_tagging.rb
|
80
|
+
- lib/auto_tagging/alchemy.rb
|
81
|
+
- lib/auto_tagging/config.rb
|
82
|
+
- lib/auto_tagging/error.rb
|
83
|
+
- lib/auto_tagging/open_calais.rb
|
84
|
+
- lib/auto_tagging/search_param.rb
|
85
|
+
- lib/auto_tagging/string_ext.rb
|
86
|
+
- lib/auto_tagging/version.rb
|
87
|
+
- lib/auto_tagging/yahoo.rb
|
88
|
+
- spec/fixtures/content.yml
|
89
|
+
- spec/lib/auto_tagging/alchemy_spec.rb
|
90
|
+
- spec/lib/auto_tagging/open_calais_spec.rb
|
91
|
+
- spec/lib/auto_tagging/search_param_spec.rb
|
92
|
+
- spec/lib/auto_tagging/yahoo_spec.rb
|
93
|
+
- spec/lib/auto_tagging_spec.rb
|
94
|
+
- spec/spec_helper.rb
|
95
|
+
- spec/support/content.rb
|
96
|
+
homepage: http://www.eastagile.com
|
97
|
+
licenses: []
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 1.8.24
|
117
|
+
signing_key:
|
118
|
+
specification_version: 3
|
119
|
+
summary: A Ruby wrapper library for multiple auto tagging services such as Open Calais,
|
120
|
+
Yahoo, Alchemy.
|
121
|
+
test_files:
|
122
|
+
- spec/fixtures/content.yml
|
123
|
+
- spec/lib/auto_tagging/alchemy_spec.rb
|
124
|
+
- spec/lib/auto_tagging/open_calais_spec.rb
|
125
|
+
- spec/lib/auto_tagging/search_param_spec.rb
|
126
|
+
- spec/lib/auto_tagging/yahoo_spec.rb
|
127
|
+
- spec/lib/auto_tagging_spec.rb
|
128
|
+
- spec/spec_helper.rb
|
129
|
+
- spec/support/content.rb
|