Class: Opener::Coreferences::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/opener/coreferences/base.rb,
lib/opener/coreferences/base/version.rb

Overview

Coreference class for various languages such as English and Spanish.

Constant Summary collapse

DEFAULT_LANGUAGE =

Returns the default language to use.

Returns:

  • (String)
'en'.freeze
VERSION =
'2.1.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.

Parameters:

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

Options Hash (options):

  • :args (Array)

    The commandline arguments to pass to the underlying Java code.



32
33
34
35
36
# File 'lib/opener/coreferences/base.rb', line 32

def initialize(options = {})

  @args    = options.delete(:args) || []
  @options = options
end

Instance Attribute Details

#argsArray (readonly)

Returns:

  • (Array)


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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/opener/coreferences/base.rb', line 16

class Base
  attr_reader :args, :options

  ##
  # Returns the default language to use.
  #
  # @return [String]
  #
  DEFAULT_LANGUAGE = 'en'.freeze

  ##
  # @param [Hash] options
  #
  # @option options [Array] :args The commandline arguments to pass to the
  #  underlying Java code.
  #
  def initialize(options = {})

    @args    = options.delete(:args) || []
    @options = options
  end

  ##
  # Returns a String containing the command used to execute the kernel.
  #
  # @return [String]
  #
  def command
    return "#{adjust_python_path} python -E -m #{kernel} #{args.join(' ')}"
  end

  ##
  # Runs the command and returns the output of STDOUT, STDERR and the
  # process information.
  #
  # @param [String] input The input to process.
  # @return [Array]
  #
  def run(input)
    @args << ["--language #{language(input)}"]
    Dir.chdir(core_dir) do
      capture(input)
    end
  end

  ##
  # Runs the command and takes care of error handling/aborting based on the
  # output.
  #
  # @see #run
  #
  def run!(input)
    stdout, stderr, process = run(input)

    if process.success?
      puts stdout

      STDERR.puts(stderr) unless stderr.empty?
    else
      abort stderr
    end
  end

  protected
  ##
  # @return [String]
  #
  def adjust_python_path
    site_packages =  File.join(core_dir, 'site-packages')
    "env PYTHONPATH=#{site_packages}:$PYTHONPATH"
  end

  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 'corefgraph.process.file'
  end

  ##
  # @return the language from the KAF
  #
  def language(input)
    document = Nokogiri::XML(input)
    language = document.at('KAF').attr('xml:lang')
    return language
  end
end

#optionsHash (readonly)

Returns:

  • (Hash)


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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/opener/coreferences/base.rb', line 16

class Base
  attr_reader :args, :options

  ##
  # Returns the default language to use.
  #
  # @return [String]
  #
  DEFAULT_LANGUAGE = 'en'.freeze

  ##
  # @param [Hash] options
  #
  # @option options [Array] :args The commandline arguments to pass to the
  #  underlying Java code.
  #
  def initialize(options = {})

    @args    = options.delete(:args) || []
    @options = options
  end

  ##
  # Returns a String containing the command used to execute the kernel.
  #
  # @return [String]
  #
  def command
    return "#{adjust_python_path} python -E -m #{kernel} #{args.join(' ')}"
  end

  ##
  # Runs the command and returns the output of STDOUT, STDERR and the
  # process information.
  #
  # @param [String] input The input to process.
  # @return [Array]
  #
  def run(input)
    @args << ["--language #{language(input)}"]
    Dir.chdir(core_dir) do
      capture(input)
    end
  end

  ##
  # Runs the command and takes care of error handling/aborting based on the
  # output.
  #
  # @see #run
  #
  def run!(input)
    stdout, stderr, process = run(input)

    if process.success?
      puts stdout

      STDERR.puts(stderr) unless stderr.empty?
    else
      abort stderr
    end
  end

  protected
  ##
  # @return [String]
  #
  def adjust_python_path
    site_packages =  File.join(core_dir, 'site-packages')
    "env PYTHONPATH=#{site_packages}:$PYTHONPATH"
  end

  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 'corefgraph.process.file'
  end

  ##
  # @return the language from the KAF
  #
  def language(input)
    document = Nokogiri::XML(input)
    language = document.at('KAF').attr('xml:lang')
    return language
  end
end

Instance Method Details

#commandString

Returns a String containing the command used to execute the kernel.

Returns:

  • (String)


43
44
45
# File 'lib/opener/coreferences/base.rb', line 43

def command
  return "#{adjust_python_path} python -E -m #{kernel} #{args.join(' ')}"
end

#run(input) ⇒ Array

Runs the command and returns the output of STDOUT, STDERR and the process information.

Parameters:

  • input (String)

    The input to process.

Returns:

  • (Array)


54
55
56
57
58
59
# File 'lib/opener/coreferences/base.rb', line 54

def run(input)
  @args << ["--language #{language(input)}"]
  Dir.chdir(core_dir) do
    capture(input)
  end
end

#run!(input) ⇒ Object

Runs the command and takes care of error handling/aborting based on the output.

See Also:



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/opener/coreferences/base.rb', line 67

def run!(input)
  stdout, stderr, process = run(input)

  if process.success?
    puts stdout

    STDERR.puts(stderr) unless stderr.empty?
  else
    abort stderr
  end
end