Class: Natto::DictionaryInfo

Inherits:
MeCabStruct show all
Defined in:
lib/natto/struct.rb

Overview

DictionaryInfo is a wrapper for the struct mecab_dictionary_info_t structure holding the MeCab instance's related dictionary information.

Values for the MeCab dictionary attributes may be obtained by using the following Symbols as keys to the layout associative array of FFI::Struct members.

  • :filename - filename of dictionary; on Windows, filename is stored in UTF-8 encoding
  • :charset - character set of the dictionary
  • :size - number of words contained in dictionary
  • :type - dictionary type: 0 (system), 1 (user-defined), 2 (unknown)
  • :lsize - left attributes size
  • :rsize - right attributes size
  • :version - version of this dictionary
  • :next - pointer to next dictionary in list

Usage

MeCab dictionary attributes can be obtained by using their corresponding accessor.

nm = Natto::MeCab.new

sysdic = nm.dicts.first

# display the real path to the mecab lib
puts sysdic.filepath
=> /usr/local/lib/mecab/dic/ipadic/sys.dic

# what charset (encoding) is the system dictionary?
puts sysdic.charset
=> utf8

# is this really the system dictionary?
puts sysdic.is_sysdic?
=> true

Constant Summary collapse

SYS_DIC =

System dictionary.

0
USR_DIC =

User dictionary.

1
UNK_DIC =

Unknown dictionary.

2

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from MeCabStruct

#method_missing

Constructor Details

#initialize(ptr) ⇒ DictionaryInfo

Initializes this dictionary info instance. Sets the DictionaryInfo filepath value.

Parameters:

  • ptr (FFI::Pointer)

    pointer to MeCab dictionary



95
96
97
98
99
# File 'lib/natto/struct.rb', line 95

def initialize(ptr)
  super(ptr)

  @filepath = File.absolute_path(self[:filename])
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Natto::MeCabStruct

Instance Attribute Details

#filepathString (readonly)

Returns Absolute filepath to MeCab dictionary.

Returns:

  • (String)

    Absolute filepath to MeCab dictionary.



60
61
62
# File 'lib/natto/struct.rb', line 60

def filepath
  @filepath
end

Instance Method Details

#deprecated_typeObject



81
# File 'lib/natto/struct.rb', line 81

alias_method :deprecated_type, :type

#inspectString

Overrides Object#inspect.

Returns:

  • (String)

    encoded object id, dictionary filename, and charset

See Also:



120
121
122
# File 'lib/natto/struct.rb', line 120

def inspect
  self.to_s
end

#is_sysdic?Boolean

Returns true if this is a system dictionary.

Returns:

  • (Boolean)


126
127
128
# File 'lib/natto/struct.rb', line 126

def is_sysdic?
  self.type == SYS_DIC
end

#is_unkdic?Boolean

Returns true if this is a unknown dictionary type.

Returns:

  • (Boolean)


138
139
140
# File 'lib/natto/struct.rb', line 138

def is_unkdic?
  self.type == UNK_DIC
end

#is_usrdic?Boolean

Returns true if this is a user dictionary.

Returns:

  • (Boolean)


132
133
134
# File 'lib/natto/struct.rb', line 132

def is_usrdic?
  self.type == USR_DIC
end

#to_sString

Returns human-readable details for this MeCab dictionary. Overrides Object#to_s.

  • encoded object id
  • real file path to this dictionary
  • dictionary charset
  • dictionary type type

Returns:

  • (String)

    encoded object id, file path to dictionary, charset and



110
111
112
113
114
115
# File 'lib/natto/struct.rb', line 110

def to_s
  [ super.chop,
    "@filepath=\"#{@filepath}\",", 
     "charset=#{self.charset},", 
     "type=#{self.type}>" ].join(' ')
end

#typeFixnum

Object#type override defined when both type and class are Object methods. This is a hack to avoid the Object#type deprecation warning thrown up in Ruby 1.8.7 and in JRuby.

Returns:

  • (Fixnum)

    MeCab dictionary type



87
88
89
# File 'lib/natto/struct.rb', line 87

def type
  self[:type]
end