class Opener::Ned

Ruby wrapper around the Java based NED tool that’s powered by DBpedia.

@!attribute [r] options

@return [Hash]

Constants

DEFAULT_OPTIONS

Hash containing the default options to use.

@return [Hash]

LANGUAGE_ENDPOINTS

The DBpedia endpoints for every language code.

@return [Hash]

VERSION

Attributes

options[R]

Public Class Methods

new(options = {}) click to toggle source

@param [Hash] options

@option options [Array] :args Arbitrary arguments to pass to the

underlying kernel.

@option options [TrueClass|FalseClass] :logging When set to ‘true`

logging is enabled. This is disabled by default.

@option options [TrueClass|FalseClass] :enable_time When set to ‘true`

the output will include timestamps.
# File lib/opener/ned.rb, line 64
def initialize(options = {})
  @options = DEFAULT_OPTIONS.merge(options)
end

Public Instance Methods

language_from_kaf(input) click to toggle source

Returns the language from the KAF document.

@param [String] input The input KAF document. @return [String]

# File lib/opener/ned.rb, line 103
def language_from_kaf(input)
  document = Nokogiri::XML(input)
  language = document.xpath('KAF/@xml:lang')[0]

  return language ? language.to_s : nil
end
run(input) click to toggle source

Performs NED on the given input document. The return value is the resulting KAF document.

@param [String] input The input KAF document. @return [String]

# File lib/opener/ned.rb, line 75
def run(input)
  if !input or input.strip.empty?
    raise ArgumentError, 'No input specified'
  end

  language = language_from_kaf(input)

  unless LANGUAGE_ENDPOINTS[language]
    raise Core::UnsupportedLanguageError, language
  end

  endpoint  = uri_for_language(language)
  input_io  = StringIO.new(input)
  reader    = Java::java.io.InputStreamReader.new(input_io.to_inputstream)
  document  = Java::ixa.kaflib.KAFDocument.create_from_stream(reader)
  annotator = new_annotator

  annotator.disambiguateNEsToKAF(document, endpoint)

  return document.to_string
end

Private Instance Methods

new_annotator() click to toggle source

Creates and configures a new Annotate class.

@return [Java::ehu.ned.Annotate]

# File lib/opener/ned.rb, line 117
def new_annotator
  annotator = Java::ehu.ned.Annotate.new

  unless options[:logging]
    annotator.disableLogging
  end

  unless options[:enable_time]
    annotator.disableTimestamp
  end

  return annotator
end
uri_for_language(language) click to toggle source

Returns the endpoint URL for the given language.

@param [String] language @return [String]

# File lib/opener/ned.rb, line 137
def uri_for_language(language)
  return LANGUAGE_ENDPOINTS[language] + "/rest/disambiguate"
end