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 =
'1.1.0'

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.



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

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

Instance Attribute Details

#optionsHash (readonly)

Returns:

  • (Hash)


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
# File 'lib/opener/kaf_naf_parser.rb', line 14

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
    return "python -E #{kernel} #{options[:args].join(' ')} #{conversion}"
  end

  def conversion
    "--#{options[:conversion].gsub(/-/,'')}"
  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)
    return Open3.capture3(command, :stdin_data => input)
  end

  protected

  ##
  # @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)


42
43
44
# File 'lib/opener/kaf_naf_parser.rb', line 42

def command
  return "python -E #{kernel} #{options[:args].join(' ')} #{conversion}"
end

#conversionObject



46
47
48
# File 'lib/opener/kaf_naf_parser.rb', line 46

def conversion
  "--#{options[:conversion].gsub(/-/,'')}"
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)


57
58
59
# File 'lib/opener/kaf_naf_parser.rb', line 57

def run(input)
  return Open3.capture3(command, :stdin_data => input)
end