Skip to content

Instantly share code, notes, and snippets.

@rummelonp
Created August 15, 2014 18:44
Show Gist options
  • Save rummelonp/13f8189f973baf9559cc to your computer and use it in GitHub Desktop.
Save rummelonp/13f8189f973baf9559cc to your computer and use it in GitHub Desktop.
雑な MeCab の Ruby バインディング
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