Class: LibKKC::Core

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

Instance Method Summary collapse

Instance Method Details

#convert(sentence, boundary: nil) ⇒ Object

第1候補を文字列で返す

convert(String) #=> String



9
10
11
# File 'lib/libkkc_ruby/core.rb', line 9

def convert(sentence, boundary: nil)
  convert_n_best(sentence, boundary: boundary).first
end

#convert_n_best(sentence, n_best: 1, boundary: nil) ⇒ Object

第n候補までを文字列の配列で返す

convert_n_best(String, Integer) #=> [String, …]



19
20
21
22
23
24
# File 'lib/libkkc_ruby/core.rb', line 19

def convert_n_best(sentence, n_best: 1, boundary: nil)
  segments = segmentalize_n_best(sentence, n_best: n_best, boundary: boundary)
  segments.map do |segment|
    segment.map(&:output).join
  end
end

#segmentalize(sentence, boundary: nil) ⇒ Object

第1候補をSegmentの配列で返す

segmentalize(String) #=> [Segment, …]



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

def segmentalize(sentence, boundary: nil)
  segmentalize_n_best(sentence, boundary: boundary).first
end

#segmentalize_n_best(sentence, n_best: 1, boundary: nil) ⇒ Object

第n候補までをSegmentの配列の配列で返す

segmentalize_n_best(String, Integer) #=> [[Segment, …], …]



42
43
44
45
46
47
48
49
50
# File 'lib/libkkc_ruby/core.rb', line 42

def segmentalize_n_best(sentence, n_best: 1, boundary: nil)
  return [] if sentence.empty?
  command = "echo '#{sentence} #{n_best} #{boundary}' | kkc"

  run_command(command) do |kkc_io|
    kkc_stdout = kkc_io.read
    extract_segments(kkc_stdout)
  end
end