tagme4r 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/tagme.rb +50 -0
- metadata +56 -0
data/lib/tagme.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'nokogiri'
|
4
|
+
require 'open-uri'
|
5
|
+
|
6
|
+
require 'result'
|
7
|
+
|
8
|
+
class Tagme
|
9
|
+
# API to TAGME API
|
10
|
+
#
|
11
|
+
# Example:
|
12
|
+
# >> Tagme.new('[API Key]').tag('nano fibers')
|
13
|
+
# => [ #<Result:...>, ... ]
|
14
|
+
#
|
15
|
+
# Arguments:
|
16
|
+
# api_key: (String)
|
17
|
+
|
18
|
+
API_PATH = 'http://tagme.di.unipi.it/api'
|
19
|
+
|
20
|
+
attr_accessor :api_key, :lang, :referer
|
21
|
+
|
22
|
+
def initialize(api_key, lang='en', referer='')
|
23
|
+
@api_key = api_key
|
24
|
+
@lang = lang
|
25
|
+
@referer = referer
|
26
|
+
end
|
27
|
+
|
28
|
+
def tag(string)
|
29
|
+
params = "?text=#{CGI.escape(string)}&key=#{@api_key}&lang=#{@lang}"
|
30
|
+
response = open(API_PATH + params, { 'Referer' => @referer })
|
31
|
+
return nil if response.class.superclass == Net::HTTPServerError
|
32
|
+
doc = Nokogiri::XML(response)
|
33
|
+
doc.xpath('//annotation/spot').zip(
|
34
|
+
doc.xpath('//annotation/title'),
|
35
|
+
doc.xpath('//annotation/id'),
|
36
|
+
doc.xpath('//annotation/rho')
|
37
|
+
).inject([]) do |results, annotation|
|
38
|
+
results << Result.new({
|
39
|
+
:spot => {
|
40
|
+
:pos => annotation[0].attribute('pos').content.to_i,
|
41
|
+
:len => annotation[0].attribute('len').content.to_i
|
42
|
+
},
|
43
|
+
:title => annotation[1].content,
|
44
|
+
:wikipedia_id => annotation[2].content.to_i,
|
45
|
+
:rho => annotation[3].content.gsub(',', '.').to_f
|
46
|
+
})
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tagme4r
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Peter Lubell-Doughtie
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-05 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: nokogiri
|
16
|
+
requirement: &69539360 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *69539360
|
25
|
+
description: API for TAGME
|
26
|
+
email: peter@helioid.com
|
27
|
+
executables: []
|
28
|
+
extensions: []
|
29
|
+
extra_rdoc_files: []
|
30
|
+
files:
|
31
|
+
- lib/tagme.rb
|
32
|
+
homepage: http://www.helioid.com/
|
33
|
+
licenses: []
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.13
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: API for TAGME
|
56
|
+
test_files: []
|