class Ve::Provider::MecabIpadic

Constants

BIT_STOP

Public Class Methods

new(config = {}) click to toggle source
# File lib/providers/mecab_ipadic.rb, line 11
def initialize(config = {})
  # TODO: Make config handling better
  @config = {:app => 'mecab',
             :path => '',
             :flags => ''}.merge(config)

  @config[:app] = `which #{@config[:app]}`.chomp

  start!
end

Public Instance Methods

parse(text, options = {}) click to toggle source

Talks to the app and returns a parse object

# File lib/providers/mecab_ipadic.rb, line 29
def parse(text, options = {})
  start! if @stdin.nil? # Restart if the provider crashed

  @stdin.puts "#{text} #{BIT_STOP}"
  output = []

  while line = @stdout.readline.force_encoding('UTF-8')
    if line =~ /#{BIT_STOP}/x
      output << @stdout.readline # Catch the EOS
      break
    end
    output << line
  end

  Ve::Parse::MecabIpadic.new(text, output)
end
works?() click to toggle source
# File lib/providers/mecab_ipadic.rb, line 22
def works?
  (["だっ\t助動詞,*,*,*,特殊・ダ,連用タ接続,だ,ダッ,ダッ",
    "た\t助動詞,*,*,*,特殊・タ,基本形,た,タ,タ",
    "EOS"] == parse('だった').tokens.collect { |t| t[:raw] } )
end

Private Instance Methods

start!() click to toggle source

TODO: Use Process.spawn/kill for process control?

# File lib/providers/mecab_ipadic.rb, line 49
def start!
  @stdin, @stdout, @stderr = Open3.popen3("#{@config[:app]} #{@config[:flags]}")
  @stdin.set_encoding('UTF-8')
  @stdout.set_encoding('UTF-8')
rescue Errno::ENOENT => e
  # The parser couldn't be started. Probably not installed on this system
end