Class: FFI::Hunspell::Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi/hunspell/dictionary.rb

Overview

Represents a dictionary for a specific language.

Constant Summary collapse

AFF_EXT =

The affix file extension

'aff'
DIC_EXT =

The dictionary file extension

'dic'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(affix_path, dic_path, key = nil) ⇒ Dictionary

Creates a new dictionary.

Parameters:

  • affix_path (String)

    The path to the .aff file.

  • dict_path (String)

    The path to the .dic file.

  • key (String) (defaults to: nil)

    The optional key for encrypted dictionary files.

Raises:

  • (RuntimeError)

    Either the .aff or .dic files did not exist.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ffi/hunspell/dictionary.rb', line 31

def initialize(affix_path,dic_path,key=nil)
  unless File.file?(affix_path)
    raise("invalid affix path #{affix_path.inspect}")
  end

  unless File.file?(dic_path)
    raise("invalid dic path #{dic_path.inspect}")
  end

  @ptr = if key
           Hunspell.Hunspell_create_key(affix_path,dic_path,key)
         else
           Hunspell.Hunspell_create(affix_path,dic_path)
         end
end

Class Method Details

.open(name) {|dict| ... } ⇒ Dictionary

Opens a Hunspell dictionary.

Parameters:

  • name (Symbol, String)

    The name of the dictionary to open.

Yields:

  • (dict)

    The given block will be passed the Hunspell dictionary.

Yield Parameters:

Returns:

  • (Dictionary)

    If no block is given, the open dictionary will be returned.

Raises:

  • (RuntimeError)

    The dictionary files could not be found in any of the directories.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ffi/hunspell/dictionary.rb', line 65

def self.open(name)
  name = name.to_s

  Hunspell.directories.each do |dir|
    affix_path = File.join(dir,"#{name}.#{AFF_EXT}")
    dic_path = File.join(dir,"#{name}.#{DIC_EXT}")

    if (File.file?(affix_path) && File.file?(dic_path))
      dict = self.new(affix_path,dic_path)

      if block_given?
        yield dict

        dict.close
        return nil
      else
        return dict
      end
    end
  end

  raise("unable to find the dictionary #{name.dump} in any of the directories")
end

Instance Method Details

#add(word) ⇒ Object Also known as: <<

Adds a word to the dictionary.

Parameters:

  • word (String)

    The word to add to the dictionary.



115
116
117
# File 'lib/ffi/hunspell/dictionary.rb', line 115

def add(word)
  Hunspell.Hunspell_add(self,word.to_s)
end

#add_affix(word, example) ⇒ Object



119
120
121
# File 'lib/ffi/hunspell/dictionary.rb', line 119

def add_affix(word,example)
  Hunspell.Hunspell_add_affix(self,word.to_s,example.to_s)
end

#check?(word) ⇒ Boolean Also known as: valid?

Checks if the word is validate.

Parameters:

  • word (String)

    The word in question.

Returns:

  • (Boolean)

    Specifies whether the word is valid.



146
147
148
# File 'lib/ffi/hunspell/dictionary.rb', line 146

def check?(word)
  Hunspell.Hunspell_spell(self,word.to_s) != 0
end

#closenil

Closes the dictionary.

Returns:

  • (nil)


201
202
203
204
205
206
# File 'lib/ffi/hunspell/dictionary.rb', line 201

def close
  Hunspell.Hunspell_destroy(self)

  @ptr = nil
  return nil
end

#closed?Boolean

Determines if the dictionary is closed.

Returns:

  • (Boolean)

    Specifies whether the dictionary was closed.



95
96
97
# File 'lib/ffi/hunspell/dictionary.rb', line 95

def closed?
  @ptr.nil?
end

#encodingString

The encoding of the dictionary file.

Returns:

  • (String)

    The encoding of the dictionary file.



105
106
107
# File 'lib/ffi/hunspell/dictionary.rb', line 105

def encoding
  Hunspell.Hunspell_get_dic_encoding(self)
end

#remove(word) ⇒ Object Also known as: delete

Removes a word from the dictionary.

Parameters:

  • word (String)

    The word to remove.



131
132
133
# File 'lib/ffi/hunspell/dictionary.rb', line 131

def remove(word)
  Hunspell.Hunspell_remove(self,word.to_s)
end

#stem(word) ⇒ Array<String>

Finds the stems of a word.

Parameters:

  • word (String)

    The word in question.

Returns:

  • (Array<String>)

    The stems of the word.



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ffi/hunspell/dictionary.rb', line 161

def stem(word)
  stems = []

  FFI::MemoryPointer.new(:pointer) do |output|
    count = Hunspell.Hunspell_stem(self,output,word.to_s)
    ptr = output.get_pointer(0)

    stems = ptr.get_array_of_string(0,count)
  end

  return stems
end

#suggest(word) ⇒ Array<String>

Suggests alternate spellings of a word.

Parameters:

  • word (String)

    The word in question.

Returns:

  • (Array<String>)

    The suggestions for the word.



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/ffi/hunspell/dictionary.rb', line 183

def suggest(word)
  suggestions = []

  FFI::MemoryPointer.new(:pointer) do |output|
    count = Hunspell.Hunspell_suggest(self,output,word.to_s)
    ptr = output.get_pointer(0)

    suggestions = ptr.get_array_of_string(0,count)
  end

  return suggestions
end

#to_ptrFFI::Pointer

Converts the dictionary to a pointer.

Returns:

  • (FFI::Pointer)

    The pointer for the dictionary.



214
215
216
# File 'lib/ffi/hunspell/dictionary.rb', line 214

def to_ptr
  @ptr
end