Class: Opener::OpinionDetectors::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/opener/opinion_detectors/base.rb,
lib/opener/opinion_detectors/base/version.rb

Overview

The base Opinion detector that supports English and Dutch.

Direct Known Subclasses

DE, EN, ES, FR, IT, NL

Constant Summary collapse

DEFAULT_OPTIONS =

Hash containing the default options to use.

Returns:

  • (Hash)
{
  :domain => "hotel"
}.freeze
VERSION =
"2.2.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :args (Array)

    Extra commandline arguments to use.



43
44
45
46
# File 'lib/opener/opinion_detectors/base.rb', line 43

def initialize(options = {})
  @args    = options.delete(:args) || []
  @options = DEFAULT_OPTIONS.merge(options)
end

Instance Attribute Details

#argsArray (readonly)

Returns:

  • (Array)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/opener/opinion_detectors/base.rb', line 26

class Base
  attr_reader :args, :options

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

  ##
  # @param [Hash] options
  #
  # @option options [Array] :args Extra commandline arguments to use.
  #
  def initialize(options = {})
    @args    = options.delete(:args) || []
    @options = DEFAULT_OPTIONS.merge(options)
  end

  ##
  # Builds the command used to execute the kernel.
  #
  # @param [Array] args Commandline arguments passed to the command.
  #
  def command(config_file)
    return "#{adjust_python_path} python -OO #{kernel} -m #{config_file} #{args.join(' ')}"
  end

  ##
  # Runs the command and returns the output of STDOUT, STDERR and the
  # process information.
  #
  # @param [String] input The input to tag.
  # @return [String]
  #
  def run(input)
    language = language(input)
    conf     = ConfigurationCreator.new(
      language,
      options[:domain],
      options[:resource_path]
    )

    stdout, stderr, process = capture(conf.config_file_path, input)

    conf.close_config

    raise stderr unless process.success?

    return stdout
  end

  protected

  ##
  # @return [String]
  #
  def adjust_python_path
    site_packages =  File.join(core_dir, 'site-packages/pre_install')

    return "env PYTHONPATH=#{site_packages}:$PYTHONPATH"
  end

  ##
  # capture3 method doesn't work properly with Jruby, so this is a
  # workaround
  #
  def capture(config_file, input)
    return Open3.popen3(*command(config_file).split(" ")) {|i, o, e, t|
      out_reader = Thread.new { o.read }
      err_reader = Thread.new { e.read }
      i.write input
      i.close
      [out_reader.value, err_reader.value, t.value]
    }
  end

  ##
  # @return [String]
  #
  def core_dir
    return File.expand_path('../../../../core', __FILE__)
  end

  ##
  # @return [String]
  #
  def kernel
    return File.join(core_dir, 'python-scripts/classify_kaf_naf_file.py')
  end

  ##
  # @return the language from the KAF
  #
  def language(input)
    document = Nokogiri::XML(input)
    language = document.at('KAF').attr('xml:lang')

    return language
  end
end

#optionsHash (readonly)

Returns:

  • (Hash)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/opener/opinion_detectors/base.rb', line 26

class Base
  attr_reader :args, :options

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

  ##
  # @param [Hash] options
  #
  # @option options [Array] :args Extra commandline arguments to use.
  #
  def initialize(options = {})
    @args    = options.delete(:args) || []
    @options = DEFAULT_OPTIONS.merge(options)
  end

  ##
  # Builds the command used to execute the kernel.
  #
  # @param [Array] args Commandline arguments passed to the command.
  #
  def command(config_file)
    return "#{adjust_python_path} python -OO #{kernel} -m #{config_file} #{args.join(' ')}"
  end

  ##
  # Runs the command and returns the output of STDOUT, STDERR and the
  # process information.
  #
  # @param [String] input The input to tag.
  # @return [String]
  #
  def run(input)
    language = language(input)
    conf     = ConfigurationCreator.new(
      language,
      options[:domain],
      options[:resource_path]
    )

    stdout, stderr, process = capture(conf.config_file_path, input)

    conf.close_config

    raise stderr unless process.success?

    return stdout
  end

  protected

  ##
  # @return [String]
  #
  def adjust_python_path
    site_packages =  File.join(core_dir, 'site-packages/pre_install')

    return "env PYTHONPATH=#{site_packages}:$PYTHONPATH"
  end

  ##
  # capture3 method doesn't work properly with Jruby, so this is a
  # workaround
  #
  def capture(config_file, input)
    return Open3.popen3(*command(config_file).split(" ")) {|i, o, e, t|
      out_reader = Thread.new { o.read }
      err_reader = Thread.new { e.read }
      i.write input
      i.close
      [out_reader.value, err_reader.value, t.value]
    }
  end

  ##
  # @return [String]
  #
  def core_dir
    return File.expand_path('../../../../core', __FILE__)
  end

  ##
  # @return [String]
  #
  def kernel
    return File.join(core_dir, 'python-scripts/classify_kaf_naf_file.py')
  end

  ##
  # @return the language from the KAF
  #
  def language(input)
    document = Nokogiri::XML(input)
    language = document.at('KAF').attr('xml:lang')

    return language
  end
end

Instance Method Details

#command(config_file) ⇒ Object

Builds the command used to execute the kernel.

Parameters:

  • args (Array)

    Commandline arguments passed to the command.



53
54
55
# File 'lib/opener/opinion_detectors/base.rb', line 53

def command(config_file)
  return "#{adjust_python_path} python -OO #{kernel} -m #{config_file} #{args.join(' ')}"
end

#run(input) ⇒ String

Runs the command and returns the output of STDOUT, STDERR and the process information.

Parameters:

  • input (String)

    The input to tag.

Returns:

  • (String)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/opener/opinion_detectors/base.rb', line 64

def run(input)
  language = language(input)
  conf     = ConfigurationCreator.new(
    language,
    options[:domain],
    options[:resource_path]
  )

  stdout, stderr, process = capture(conf.config_file_path, input)

  conf.close_config

  raise stderr unless process.success?

  return stdout
end