Class: Opener::KafNafParser

Inherits:
Object
  • Object
show all
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

Classes: CLI, Server

Constant Summary collapse

DEFAULT_OPTIONS =

Hash containing the default options to use.

Returns:

  • (Hash)
{
  :args       => [],
  :conversion => 'to-kaf'
}.freeze
VERSION =
'2.0.2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ KafNafParser

Returns a new instance of KafNafParser.

Parameters:

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

Options Hash (options):

  • :args (Array)

    Collection of arbitrary arguments to pass to the underlying kernel.



34
35
36
# File 'lib/opener/kaf_naf_parser.rb', line 34

def initialize(options = {})
  @options = DEFAULT_OPTIONS.merge(options)
end

Instance Attribute Details

#optionsHash (readonly)

Returns:

  • (Hash)


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 = {})
    @options = DEFAULT_OPTIONS.merge(options)
  end

  ##
  # Returns a String containing the command to use for executing the kernel.
  #
  # @return [String]
  #
  def command
    args = options[:args].join(' ')

    return "python -E #{kernel} #{args} #{conversion}"
  end

  ##
  # @return [String]
  #
  def conversion
    "--#{options[: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.expand_path('../../../core', __FILE__)
  end

  ##
  # @return [String]
  #
  def kernel
    return File.join(core_dir, 'kaf-naf-parser.py')
  end
end

Instance Method Details

#commandString

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

Returns:

  • (String)


43
44
45
46
47
# File 'lib/opener/kaf_naf_parser.rb', line 43

def command
  args = options[:args].join(' ')

  return "python -E #{kernel} #{args} #{conversion}"
end

#conversionString

Returns:

  • (String)


52
53
54
# File 'lib/opener/kaf_naf_parser.rb', line 52

def conversion
  "--#{options[:conversion].gsub(/-/,'')}"
end

#run(input) ⇒ String

Processes a given input KAF/NAF document and returns a new document in the opposite format.

Parameters:

  • input (String)

Returns:

  • (String)


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