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



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

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.



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

def filepath
  @filepath
end

Instance Method Details

#deprecated_typeObject



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

alias_method :deprecated_type, :type

#inspectString

Overrides Object#inspect.

Returns:

  • (String)

    encoded object id, dictionary filename, and charset

See Also:



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

def inspect
  self.to_s
end

#is_sysdic?Boolean

Returns true if this is a system dictionary.

Returns:

  • (Boolean)


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

def is_sysdic?
  self.type == SYS_DIC
end

#is_unkdic?Boolean

Returns true if this is a unknown dictionary type.

Returns:

  • (Boolean)


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

def is_unkdic?
  self.type == UNK_DIC
end

#is_usrdic?Boolean

Returns true if this is a user dictionary.

Returns:

  • (Boolean)


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

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



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

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



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

def type
  self[:type]
end