Skip to content

Instantly share code, notes, and snippets.

@ohnishiakira
Created February 12, 2014 03:49
Show Gist options
  • Save ohnishiakira/8949780 to your computer and use it in GitHub Desktop.
Save ohnishiakira/8949780 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# coding: utf-8
require "forwardable"
require "csv"
require "MeCab"
class Mecab
def self.wakati
new "-O wakati"
end
def initialize(output)
@mecab = MeCab::Tagger.new output
end
def parse(str)
n = @mecab.parseToNode str
begin
yield Node.new n
end while n = n.next
end
class Node
extend Forwardable
def initialize(node)
@node = node
end
%i(
surface length rlength id rcAttr lcAttr
posid char_type stat isbest alpha beta
prob wcost cost
).each{|m|
def_delegator :@node, m
}
def feature
Hash[%w(品詞 品詞細分類1 品詞細分類2 品詞細分類3 活用形 活用型 原形 読み 発音).zip(CSV.parse_line(@node.feature))]
end
end
end
if $0 == __FILE__
require "pp"
Mecab.wakati.parse("月が綺麗ですね"){|n|
puts [
n.feature["品詞"],
n.surface,
n.cost
].join("\t")
# p n.surface if n.feature["品詞"] == "名詞"
# p %i(
# surface length rlength id rcAttr lcAttr
# posid char_type stat isbest alpha beta
# prob wcost cost
# ).map{|m|
# n.send(m)
# }
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment