Class: Opener::KafNafParser
- Inherits:
-
Object
- Object
- Opener::KafNafParser
- Defined in:
- lib/opener/kaf_naf_parser.rb,
lib/opener/kaf_naf_parser/cli.rb,
lib/opener/kaf_naf_parser/server.rb,
lib/opener/kaf_naf_parser/version.rb
Overview
Ruby wrapper around the Python based KafNafParser.
Defined Under Namespace
Constant Summary collapse
- DEFAULT_OPTIONS =
Hash containing the default options to use.
{ :args => [], :conversion => 'to-kaf' }.freeze
- VERSION =
'2.0.2'
Instance Attribute Summary collapse
- #options ⇒ Hash readonly
Instance Method Summary collapse
-
#command ⇒ String
Returns a String containing the command to use for executing the kernel.
- #conversion ⇒ String
-
#initialize(options = {}) ⇒ KafNafParser
constructor
A new instance of KafNafParser.
-
#run(input) ⇒ String
Processes a given input KAF/NAF document and returns a new document in the opposite format.
Constructor Details
#initialize(options = {}) ⇒ KafNafParser
Returns a new instance of KafNafParser.
34 35 36 |
# File 'lib/opener/kaf_naf_parser.rb', line 34 def initialize( = {}) @options = DEFAULT_OPTIONS.merge() end |
Instance Attribute Details
#options ⇒ Hash (readonly)
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 |
# File 'lib/opener/kaf_naf_parser.rb', line 15 class KafNafParser attr_reader :options ## # Hash containing the default options to use. # # @return [Hash] # DEFAULT_OPTIONS = { :args => [], :conversion => 'to-kaf' }.freeze ## # @param [Hash] options # # @option options [Array] :args Collection of arbitrary arguments to pass # to the underlying kernel. # def initialize( = {}) @options = DEFAULT_OPTIONS.merge() end ## # Returns a String containing the command to use for executing the kernel. # # @return [String] # def command args = [:args].join(' ') return "python -E #{kernel} #{args} #{conversion}" end ## # @return [String] # def conversion "--#{[:conversion].gsub(/-/,'')}" end ## # Processes a given input KAF/NAF document and returns a new document in the # opposite format. # # @param [String] input # @return [String] # def run(input) stdout, stderr, process = capture(input) raise stderr unless process.success? return stdout end protected ## # 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, 'kaf-naf-parser.py') end end |
Instance Method Details
#command ⇒ String
Returns a String containing the command to use for executing the kernel.
43 44 45 46 47 |
# File 'lib/opener/kaf_naf_parser.rb', line 43 def command args = [:args].join(' ') return "python -E #{kernel} #{args} #{conversion}" end |
#conversion ⇒ String
52 53 54 |
# File 'lib/opener/kaf_naf_parser.rb', line 52 def conversion "--#{[:conversion].gsub(/-/,'')}" end |
#run(input) ⇒ String
Processes a given input KAF/NAF document and returns a new document in the opposite format.
63 64 65 66 67 68 69 |
# File 'lib/opener/kaf_naf_parser.rb', line 63 def run(input) stdout, stderr, process = capture(input) raise stderr unless process.success? return stdout end |