Class: JumanppRuby::Juman

Inherits:
Object
  • Object
show all
Defined in:
lib/jumanpp_ruby.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(beam: nil, force_single_path: false, partial: false) ⇒ Juman

Returns a new instance of Juman.



9
10
11
12
13
14
15
# File 'lib/jumanpp_ruby.rb', line 9

def initialize(beam: nil, force_single_path: false, partial: false)
  argv = ['jumanpp']
  argv << '--beam' << beam if beam
  argv << '--force-single-path' if force_single_path
  argv << '--partial' if partial
  @pipe = IO.popen(argv, 'r+')
end

Class Method Details

.helpObject



41
42
43
44
# File 'lib/jumanpp_ruby.rb', line 41

def self.help
  o, = Open3.capture2('jumanpp -h')
  o.strip
end

.versionObject



36
37
38
39
# File 'lib/jumanpp_ruby.rb', line 36

def self.version
  o, = Open3.capture2('jumanpp -v')
  o.strip
end

Instance Method Details

#closeObject



32
33
34
# File 'lib/jumanpp_ruby.rb', line 32

def close
  @pipe.close
end

#parse(sentents) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jumanpp_ruby.rb', line 17

def parse(sentents)
  @pipe.puts(zh_convert(sentents))
  responses = []
  while sentent = @pipe.gets
    a = sentent.scan(/(?:\\ |[^\s"]|"[^"]*")+/)
    a.each {|i| i.sub!(/\A"(.*)"\z/, '\\1') }
    responses << a
    break if sentent =~ /EOS\n/
  end
  if block_given?
    responses.each { |word| yield word }
  else responses.map { |word| word.first }
  end
end