Class: Opener::PolarityTagger::External
- Inherits:
-
Object
- Object
- Opener::PolarityTagger::External
- Defined in:
- lib/opener/polarity_tagger/external.rb
Overview
Ruby wrapper around the Python based polarity tagger.
Instance Attribute Summary collapse
- #args ⇒ Array readonly
- #options ⇒ Hash readonly
Instance Method Summary collapse
-
#command ⇒ String
Returns a String containing the command to use for executing the kernel.
-
#initialize(options = {}) ⇒ External
constructor
A new instance of External.
- #lexicon_path ⇒ String
-
#run(input, params) ⇒ Array
Processes the input and returns an Array containing the output of STDOUT, STDERR and an object containing process information.
Constructor Details
#initialize(options = {}) ⇒ External
Returns a new instance of External.
24 25 26 27 |
# File 'lib/opener/polarity_tagger/external.rb', line 24 def initialize = {} @args = .delete(:args) || [] @options = end |
Instance Attribute Details
#args ⇒ Array (readonly)
12 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 100 101 102 103 |
# File 'lib/opener/polarity_tagger/external.rb', line 12 class External attr_reader :options, :args ## # @param [Hash] options # # @option options [Array] :args Collection of arbitrary arguments to pass # to the underlying kernel. # # @option options [String] :resource_path Path to the lexicons to use. # 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 #{kernel} #{lexicon_path} #{args.join(" ")}" end ## # @return [String] # def lexicon_path path = [:resource_path] || ENV['RESOURCE_PATH'] || ENV['POLARITY_LEXICON_PATH'] return path ? "--lexicon-path #{path}" : nil 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, params stdout, stderr, process = capture(input) raise stderr unless process.success? puts stderr if ENV['DEBUG'] return stdout 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 File. '../../../../core', __FILE__ end ## # @return [String] # def kernel File.join core_dir, 'poltagger-basic-multi.py' end end |
#options ⇒ Hash (readonly)
12 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 100 101 102 103 |
# File 'lib/opener/polarity_tagger/external.rb', line 12 class External attr_reader :options, :args ## # @param [Hash] options # # @option options [Array] :args Collection of arbitrary arguments to pass # to the underlying kernel. # # @option options [String] :resource_path Path to the lexicons to use. # 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 #{kernel} #{lexicon_path} #{args.join(" ")}" end ## # @return [String] # def lexicon_path path = [:resource_path] || ENV['RESOURCE_PATH'] || ENV['POLARITY_LEXICON_PATH'] return path ? "--lexicon-path #{path}" : nil 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, params stdout, stderr, process = capture(input) raise stderr unless process.success? puts stderr if ENV['DEBUG'] return stdout 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 File. '../../../../core', __FILE__ end ## # @return [String] # def kernel 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.
34 35 36 |
# File 'lib/opener/polarity_tagger/external.rb', line 34 def command return "#{adjust_python_path} python -E #{kernel} #{lexicon_path} #{args.join(" ")}" end |
#lexicon_path ⇒ String
41 42 43 44 45 46 |
# File 'lib/opener/polarity_tagger/external.rb', line 41 def lexicon_path path = [:resource_path] || ENV['RESOURCE_PATH'] || ENV['POLARITY_LEXICON_PATH'] return path ? "--lexicon-path #{path}" : nil end |
#run(input, params) ⇒ Array
Processes the input and returns an Array containing the output of STDOUT, STDERR and an object containing process information.
55 56 57 58 59 60 61 62 |
# File 'lib/opener/polarity_tagger/external.rb', line 55 def run input, params stdout, stderr, process = capture(input) raise stderr unless process.success? puts stderr if ENV['DEBUG'] return stdout end |