alexrabarts-term_extraction 0.1.2 → 0.1.3
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/VERSION.yml +1 -1
- data/lib/term_extraction/yahoo.rb +14 -13
- data/lib/term_extraction/zemanta.rb +16 -15
- data/test/term_extraction_test.rb +14 -6
- data/test/test_helper.rb +11 -2
- metadata +2 -2
data/VERSION.yml
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
require 'nokogiri'
|
2
|
+
require 'addressable/uri'
|
3
|
+
require 'open-uri'
|
2
4
|
|
3
5
|
class TermExtraction
|
4
6
|
class Yahoo < TermExtraction
|
@@ -13,7 +15,17 @@ class TermExtraction
|
|
13
15
|
end
|
14
16
|
|
15
17
|
terms
|
16
|
-
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def uri
|
21
|
+
api_uri = Addressable::URI.parse(gateway)
|
22
|
+
api_uri.query_values = {
|
23
|
+
'appid' => @api_key,
|
24
|
+
'output' => 'xml',
|
25
|
+
'context' => @context
|
26
|
+
}
|
27
|
+
api_uri
|
28
|
+
end
|
17
29
|
|
18
30
|
class << self
|
19
31
|
def canonical_name
|
@@ -30,19 +42,8 @@ class TermExtraction
|
|
30
42
|
'http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction'
|
31
43
|
end
|
32
44
|
|
33
|
-
def url
|
34
|
-
uri = Addressable::URI.parse(gateway)
|
35
|
-
uri.query_values = {
|
36
|
-
# TODO: Change appid to the BMP one
|
37
|
-
'appid' => @api_key,
|
38
|
-
'output' => 'xml',
|
39
|
-
'context' => @context
|
40
|
-
}
|
41
|
-
uri
|
42
|
-
end
|
43
|
-
|
44
45
|
def remote_xml
|
45
|
-
open(
|
46
|
+
open(uri).read
|
46
47
|
end
|
47
48
|
end
|
48
49
|
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'net/http'
|
2
2
|
require 'nokogiri'
|
3
|
+
require 'addressable/uri'
|
3
4
|
|
4
5
|
class TermExtraction
|
5
6
|
class Zemanta < TermExtraction
|
@@ -14,6 +15,20 @@ class TermExtraction
|
|
14
15
|
terms
|
15
16
|
end
|
16
17
|
|
18
|
+
def uri
|
19
|
+
Addressable::URI.parse(gateway)
|
20
|
+
end
|
21
|
+
|
22
|
+
def post_params
|
23
|
+
{
|
24
|
+
'method' =>'zemanta.suggest',
|
25
|
+
'api_key' => @api_key,
|
26
|
+
'return_images' => 0,
|
27
|
+
'text' => @context,
|
28
|
+
'format' => 'xml'
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
17
32
|
class << self
|
18
33
|
def canonical_name
|
19
34
|
'zemanta'
|
@@ -25,22 +40,8 @@ class TermExtraction
|
|
25
40
|
'http://api.zemanta.com/services/rest/0.0/'
|
26
41
|
end
|
27
42
|
|
28
|
-
def url
|
29
|
-
URI.parse(gateway)
|
30
|
-
end
|
31
|
-
|
32
|
-
def post_params
|
33
|
-
{
|
34
|
-
'method' =>'zemanta.suggest',
|
35
|
-
'api_key' => @api_key,
|
36
|
-
'return_images' => 0,
|
37
|
-
'text' => @context,
|
38
|
-
'format' => 'xml'
|
39
|
-
}
|
40
|
-
end
|
41
|
-
|
42
43
|
def remote_xml
|
43
|
-
Net::HTTP.post_form(
|
44
|
+
Net::HTTP.post_form(uri, post_params).body
|
44
45
|
end
|
45
46
|
end
|
46
47
|
end
|
@@ -3,13 +3,13 @@ require File.dirname(__FILE__) + '/test_helper'
|
|
3
3
|
class TermExtractionTest < Test::Unit::TestCase
|
4
4
|
should 'return correct terms from Yahoo!' do
|
5
5
|
yahoo = TermExtraction::Yahoo.new
|
6
|
-
|
6
|
+
fake_uri(:get, yahoo.uri, 'yahoo.xml')
|
7
7
|
assert_equal yahoo.terms, correct_yahoo_terms
|
8
8
|
end
|
9
9
|
|
10
10
|
should 'return correct terms from Zemanta' do
|
11
11
|
zemanta = TermExtraction::Zemanta.new
|
12
|
-
|
12
|
+
fake_uri(:post, zemanta.uri, 'zemanta.xml')
|
13
13
|
assert_equal zemanta.terms, correct_zemanta_terms
|
14
14
|
end
|
15
15
|
|
@@ -29,20 +29,28 @@ class TermExtractionTest < Test::Unit::TestCase
|
|
29
29
|
|
30
30
|
should 'return different response on subsequent calls when different data is returned from Yahoo!' do
|
31
31
|
yahoo = TermExtraction::Yahoo.new
|
32
|
-
|
32
|
+
fake_uri(:get, yahoo.uri, 'yahoo.xml')
|
33
33
|
original_terms = yahoo.terms
|
34
|
-
|
34
|
+
fake_uri(:get, yahoo.uri, 'yahoo2.xml')
|
35
35
|
assert_not_equal original_terms, yahoo.terms
|
36
36
|
end
|
37
37
|
|
38
38
|
should 'return different response on subsequent calls when different data is returned from Zemanta' do
|
39
39
|
zemanta = TermExtraction::Zemanta.new
|
40
|
-
|
40
|
+
fake_uri(:post, zemanta.uri, 'zemanta.xml')
|
41
41
|
original_terms = zemanta.terms
|
42
|
-
|
42
|
+
fake_uri(:post, zemanta.uri, 'zemanta2.xml')
|
43
43
|
assert_not_equal original_terms, zemanta.terms
|
44
44
|
end
|
45
45
|
|
46
|
+
context 'Yahoo!' do
|
47
|
+
should 'be able to handle a context with "%" in it' do
|
48
|
+
yahoo = TermExtraction::Yahoo.new(:context => '%')
|
49
|
+
fake_uri(:get, yahoo.uri, 'yahoo.xml')
|
50
|
+
assert_nothing_thrown{ yahoo.terms }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
46
54
|
private
|
47
55
|
def correct_yahoo_terms
|
48
56
|
['gears of war', 'gears']
|
data/test/test_helper.rb
CHANGED
@@ -2,12 +2,21 @@ require 'rubygems'
|
|
2
2
|
require 'test/unit'
|
3
3
|
require 'shoulda'
|
4
4
|
require 'mocha'
|
5
|
+
require 'fake_web'
|
5
6
|
|
6
7
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
8
|
require 'term_extraction'
|
8
9
|
|
9
10
|
class Test::Unit::TestCase
|
10
|
-
def
|
11
|
-
|
11
|
+
def fake_uri(method, uri, fixture)
|
12
|
+
FakeWeb.clean_registry
|
13
|
+
FakeWeb.allow_net_connect = false
|
14
|
+
response = File.open(File.join(File.dirname(__FILE__), 'fixtures', fixture)).read
|
15
|
+
FakeWeb.register_uri(method, uri.to_s, :string => response)
|
16
|
+
end
|
17
|
+
|
18
|
+
def teardown
|
19
|
+
FakeWeb.allow_net_connect = true
|
20
|
+
FakeWeb.clean_registry
|
12
21
|
end
|
13
22
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: alexrabarts-term_extraction
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- alex
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-03-
|
12
|
+
date: 2009-03-17 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|