Class: Ve::Provider::MecabIpadic

Inherits:
Ve::Provider show all
Defined in:
lib/providers/mecab_ipadic.rb

Constant Summary collapse

BIT_STOP =
'VeEnd'

Instance Method Summary collapse

Methods inherited from Ve::Provider

#provides

Constructor Details

#initialize(config = {}) ⇒ MecabIpadic

Returns a new instance of MecabIpadic.



11
12
13
14
15
16
17
18
19
20
# 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

Instance Method Details

#parse(text, options = {}) ⇒ Object

Talks to the app and returns a parse object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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)
rescue => e
  # TODO: No good to catch all errors like this
  # I need a backtrace when something unexpected fails
  Ve::Parse::MecabIpadic.new(text, [])
end

#works?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
# File 'lib/providers/mecab_ipadic.rb', line 22

def works?
  (["だっ\t助動詞,*,*,*,特殊・ダ,連用タ接続,だ,ダッ,ダッ",
    "た\t助動詞,*,*,*,特殊・タ,基本形,た,タ,タ",
    "EOS"] == parse('だった').tokens.collect { |t| t[:raw] } )
end