Sha256: 47a219f732869eb4a825982eb898d32e744a43abbaa7cd751f97a0e12f14ae04

Contents?: true

Size: 1.94 KB

Versions: 5

Compression:

Stored size: 1.94 KB

Contents

require 'opener/opinion_detectors/base'
require 'open3'
require 'nokogiri'
require 'optparse'

require_relative 'opinion_detector/version'
require_relative 'opinion_detector/cli'

module Opener
  ##
  # Primary opinion detector class that delegates work to the various opinion
  # detector kernels.
  #
  # @!attribute [r] options
  #  @return [Hash]
  #
  class OpinionDetector
    attr_reader :options

    ##
    # Hash containing the default options to use.
    #
    # @return [Hash]
    #
    DEFAULT_OPTIONS = {
      :args => []
    }.freeze

    ##
    # @param [Hash] options
    #
    # @option options [Array] :args Collection of arbitrary arguments to pass
    #  to the individual tokenizer commands.
    #
    def initialize(options = {})
      @options = DEFAULT_OPTIONS.merge(options)
    end

    ##
    # Processes the input and returns an array containing the output of STDOUT,
    # STDERR and an object containing process information.
    #
    # @param [String] input
    # @return [Array]
    #
    def run(input)
      language = language_from_kaf(input)

      unless valid_language?(language)
        raise ArgumentError, "The specified language (#{language}) is invalid"
      end

      kernel = language_constant(language).new(options)
      return kernel.run(input)
    end

    protected

    ##
    # Extracts the language from a KAF document.
    #
    # @param [String] input
    # @return [String]
    #
    def language_from_kaf(input)
      reader = Nokogiri::XML::Reader(input)

      return reader.read.lang
    end

    ##
    # @param [String] language
    # @return [Class]
    #
    def language_constant(language)
      Opener::OpinionDetectors.const_get(language.upcase)
    end

    ##
    # @return [TrueClass|FalseClass]
    #
    def valid_language?(language)
      if language
        return Opener::OpinionDetectors.const_defined?(language.upcase)
      else
        return false
      end
    end
  end # OpinionDetector
end # Opener

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
opener-opinion-detector-1.1.3 lib/opener/opinion_detector.rb
opener-opinion-detector-1.1.2 lib/opener/opinion_detector.rb
opener-opinion-detector-1.1.1 lib/opener/opinion_detector.rb
opener-opinion-detector-1.1.0 lib/opener/opinion_detector.rb
opener-opinion-detector-1.0.3 lib/opener/opinion_detector.rb