module Natto::Binding

Module `Binding` encapsulates methods and behavior which are made available via `FFI` bindings to MeCab.

Constants

MECAB_PATH

Public Class Methods

find_library() click to toggle source

Returns the absolute pathname to the MeCab library based on the runtime environment. @return [String] absolute pathname to the MeCab library @raise [LoadError] if the library cannot be located

# File lib/natto/binding.rb, line 22
def self.find_library
  if ENV[MECAB_PATH]
    File.absolute_path(ENV[MECAB_PATH])
  else
    host_os = RbConfig::CONFIG['host_os']

    if host_os =~ /mswin|mingw/i
      require 'win32/registry'
      begin
        base = nil
        Win32::Registry::HKEY_CURRENT_USER.open('Software\MeCab') do |r|
          base = r['mecabrc'].split('etc').first
        end
        lib = File.join(base, 'bin/libmecab.dll')
        File.absolute_path(lib)
      rescue
        raise LoadError, "Please set #{MECAB_PATH} to the full path to libmecab.dll"
      end
    else
      require 'open3'
      if host_os =~ /darwin/i
        ext = 'dylib'
      else
        ext = 'so'
      end

      begin
        base, lib = nil, nil
        cmd = 'mecab-config --libs'
        Open3.popen3(cmd) do |stdin,stdout,stderr|
          toks = stdout.read.split
          base = toks[0][2..-1]
          lib  = toks[1][2..-1]
        end
        File.absolute_path(File.join(base, "lib#{lib}.#{ext}"))
      rescue
        raise LoadError, "Please set #{MECAB_PATH} to the full path to libmecab.#{ext}"
      end
    end
  end
end
included(base) click to toggle source

@private

# File lib/natto/binding.rb, line 14
def self.included(base)
  base.extend(ClassMethods)
end