Class: Opener::PolarityTagger
- Inherits:
-
Object
- Object
- Opener::PolarityTagger
- 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
Constant Summary collapse
- VERSION =
'2.4.1'
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
- #options ⇒ Hash readonly
Instance Method Summary collapse
-
#command ⇒ String
Returns a String containing the command to use for executing the kernel.
-
#initialize(options = {}) ⇒ PolarityTagger
constructor
A new instance of PolarityTagger.
- #lexicon_path ⇒ Object
-
#run(input) ⇒ Array
Processes the input and returns an Array containing the output of STDOUT, STDERR and an object containing process information.
Constructor Details
#initialize(options = {}) ⇒ PolarityTagger
Returns a new instance of PolarityTagger.
22 23 24 25 |
# File 'lib/opener/polarity_tagger.rb', line 22 def initialize( = {}) @args = .delete(:args) || [] @options = end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
14 15 16 |
# File 'lib/opener/polarity_tagger.rb', line 14 def args @args end |
#options ⇒ Hash (readonly)
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( = {}) @args = .delete(:args) || [] @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 = [: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., 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.('../../../core', __FILE__) end ## # @return [String] # def kernel return File.join(core_dir, 'poltagger-basic-multi.py') end end |
Instance Method Details
#command ⇒ String
Returns a String containing the command to use for executing the kernel.
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_path ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/opener/polarity_tagger.rb', line 36 def lexicon_path if path = [: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.
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., self.class).add end end |