class Opener::OpinionDetector

Primary opinion detector class that delegates work to the various opinion detector kernels.

@!attribute [r] options

@return [Hash]

Constants

VERSION

Attributes

options[R]

Public Class Methods

new(options = {}) click to toggle source

@param [Hash] options

@see [Opener::OpinionDetectors::Base#initialize]

# File lib/opener/opinion_detector.rb, line 27
def initialize(options = {})
  @options = options
end

Public Instance Methods

language_from_kaf(input) click to toggle source

Extracts the language from a KAF document.

@param [String] input @return [String]

# File lib/opener/opinion_detector.rb, line 56
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

Processes the input KAF document and returns a new KAF document containing the results.

@param [String] input @return [String]

# File lib/opener/opinion_detector.rb, line 38
def run(input)
  language = language_from_kaf(input)

  unless valid_language?(language)
    raise Core::UnsupportedLanguageError, language
  end

  kernel = language_constant(language).new(kernel_options)

  return kernel.run(input)
end

Private Instance Methods

kernel_options() click to toggle source

Returns the options to use for the kernel.

@return [Hash]

# File lib/opener/opinion_detector.rb, line 70
def kernel_options
  return options.merge(:resource_path => models_path)
end
language_constant(language) click to toggle source

@param [String] language @return [Class]

# File lib/opener/opinion_detector.rb, line 88
def language_constant(language)
  Opener::OpinionDetectors.const_get(language.upcase)
end
models_path() click to toggle source

Returns the path to the models to use.

@return [String]

# File lib/opener/opinion_detector.rb, line 79
def models_path
  return options[:resource_path] || ENV['RESOURCE_PATH'] ||
    ENV['OPINION_DETECTOR_MODELS_PATH']
end
valid_language?(language) click to toggle source

@return [TrueClass|FalseClass]

# File lib/opener/opinion_detector.rb, line 95
def valid_language?(language)
  if language
    return Opener::OpinionDetectors.const_defined?(language.upcase)
  else
    return false
  end
end