Class: Tagme
- Inherits:
-
Object
- Object
- Tagme
- Defined in:
- lib/tagme.rb
Constant Summary collapse
- API_PATH =
API to TAGME API
Example:
>> Tagme.new('[API Key]').tag('nano fibers') => [ #<Result:...>, ... ]Arguments:
api_key: (String) 'http://tagme.di.unipi.it/api'
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#lang ⇒ Object
Returns the value of attribute lang.
-
#referer ⇒ Object
Returns the value of attribute referer.
Instance Method Summary collapse
-
#initialize(api_key, lang = 'en', referer = '') ⇒ Tagme
constructor
A new instance of Tagme.
- #tag(string) ⇒ Object
Constructor Details
#initialize(api_key, lang = 'en', referer = '') ⇒ Tagme
Returns a new instance of Tagme.
22 23 24 25 26 |
# File 'lib/tagme.rb', line 22 def initialize(api_key, lang='en', referer='') @api_key = api_key @lang = lang @referer = referer end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
20 21 22 |
# File 'lib/tagme.rb', line 20 def api_key @api_key end |
#lang ⇒ Object
Returns the value of attribute lang.
20 21 22 |
# File 'lib/tagme.rb', line 20 def lang @lang end |
#referer ⇒ Object
Returns the value of attribute referer.
20 21 22 |
# File 'lib/tagme.rb', line 20 def referer @referer end |
Instance Method Details
#tag(string) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/tagme.rb', line 28 def tag(string) params = "?text=#{CGI.escape(string)}&key=#{@api_key}&lang=#{@lang}" response = open(API_PATH + params, { 'Referer' => @referer }) return nil if response.class.superclass == Net::HTTPServerError doc = Nokogiri::XML(response) doc.xpath('//annotation/spot').zip( doc.xpath('//annotation/title'), doc.xpath('//annotation/id'), doc.xpath('//annotation/rho') ).inject([]) do |results, annotation| results << Result.new({ :spot => { :pos => annotation[0].attribute('pos').content.to_i, :len => annotation[0].attribute('len').content.to_i }, :title => annotation[1].content, :wikipedia_id => annotation[2].content.to_i, :rho => annotation[3].content.gsub(',', '.').to_f }) end end |