Created
August 15, 2014 18:44
-
-
Save rummelonp/13f8189f973baf9559cc to your computer and use it in GitHub Desktop.
雑な MeCab の Ruby バインディング
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Negitoro | |
extend FFI::Library | |
ffi_lib 'mecab' | |
attach_function :mecab_new2, [:string], :pointer | |
attach_function :mecab_sparse_tostr, [:pointer, :string], :string | |
attach_function :mecab_destroy, [:pointer], :void | |
def self.clean_proc(tagger) | |
Proc.new { mecab_destroy tagger } | |
end | |
def initialize(option = "") | |
@tagger = mecab_new2 option | |
ObjectSpace.define_finalizer self, self.class.clean_proc(@tagger) | |
end | |
def parse(str) | |
raise ArgumentError.new 'String to parse cannot be nil' if str.nil? | |
mecab_sparse_tostr(@tagger, str) | |
.force_encoding(Encoding.default_external) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment