Class: Opener::PolarityTagger

Inherits:
Object
  • Object
show all
Defined in:
lib/opener/polarity_tagger.rb,
lib/opener/polarity_tagger/cli.rb,
lib/opener/polarity_tagger/server.rb,
lib/opener/polarity_tagger/version.rb

Overview

Ruby wrapper around the Python based polarity tagger.

Defined Under Namespace

Classes: CLI, Server

Constant Summary collapse

VERSION =
'2.4.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PolarityTagger

Returns a new instance of PolarityTagger.

Parameters:

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

Options Hash (options):

  • :args (Array)

    Collection of arbitrary arguments to pass to the underlying kernel.



22
23
24
25
# File 'lib/opener/polarity_tagger.rb', line 22

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

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



14
15
16
# File 'lib/opener/polarity_tagger.rb', line 14

def args
  @args
end

#optionsHash (readonly)

Returns:

  • (Hash)


13
14
15
16
17
18
19
20
21
22
23
24
25
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
# File 'lib/opener/polarity_tagger.rb', line 13

class PolarityTagger
  attr_reader :options, :args

  ##
  # @param [Hash] options
  #
  # @option options [Array] :args Collection of arbitrary arguments to pass
  #  to the underlying kernel.
  #
  def initialize(options = {})
    @args    = options.delete(:args) || []
    @options = options
  end

  ##
  # Returns a String containing the command to use for executing the kernel.
  #
  # @return [String]
  #
  def command
    return "#{adjust_python_path} python -E -OO #{kernel} #{lexicon_path} #{args.join(" ")}"
  end

  def lexicon_path
    if path = options[:resource_path]
      return "--lexicon-path #{path}"
    elsif path = ENV['POLARITY_LEXICON_PATH']
      return "--lexicon-path #{path}"
    else
      return nil
    end
  end

  ##
  # Processes the input and returns an Array containing the output of STDOUT,
  # STDERR and an object containing process information.
  #
  # @param [String] input The text of which to detect the language.
  # @return [Array]
  #
  def run(input)
    begin
      stdout, stderr, process = capture(input)
      raise stderr unless process.success?
      return stdout
    rescue Exception => error
      return Opener::Core::ErrorLayer.new(input, error.message, self.class).add
    end
  end

  protected
  ##
  # @return [String]
  #
  def adjust_python_path
    site_packages =  File.join(core_dir, 'site-packages')
    "env PYTHONPATH=#{site_packages}:$PYTHONPATH"
  end

  ##
  # capture3 method doesn't work properly with Jruby, so
  # this is a workaround
  #
  def capture(input)
    Open3.popen3(*command.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, 'poltagger-basic-multi.py')
  end
end

Instance Method Details

#commandString

Returns a String containing the command to use for executing the kernel.

Returns:

  • (String)


32
33
34
# File 'lib/opener/polarity_tagger.rb', line 32

def command
  return "#{adjust_python_path} python -E -OO #{kernel} #{lexicon_path} #{args.join(" ")}"
end

#lexicon_pathObject



36
37
38
39
40
41
42
43
44
# File 'lib/opener/polarity_tagger.rb', line 36

def lexicon_path
  if path = options[:resource_path]
    return "--lexicon-path #{path}"
  elsif path = ENV['POLARITY_LEXICON_PATH']
    return "--lexicon-path #{path}"
  else
    return nil
  end
end

#run(input) ⇒ Array

Processes the input and returns an Array containing the output of STDOUT, STDERR and an object containing process information.

Parameters:

  • input (String)

    The text of which to detect the language.

Returns:

  • (Array)


53
54
55
56
57
58
59
60
61
# File 'lib/opener/polarity_tagger.rb', line 53

def run(input)
  begin
    stdout, stderr, process = capture(input)
    raise stderr unless process.success?
    return stdout
  rescue Exception => error
    return Opener::Core::ErrorLayer.new(input, error.message, self.class).add
  end
end