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.

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 then Hunspell.Hunspell_create_key(affix_path,dic_path,key)
        else        Hunspell.Hunspell_create(affix_path,dic_path)
        end

  @ptr = FFI::AutoPointer.new(ptr,Hunspell.method(:Hunspell_destroy))
end

Class Method Details

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

Opens a Hunspell dictionary.

Yields:

  • (dict)

    The given block will be passed the Hunspell dictionary.

Yield Parameters:

Raises:

  • (ArgumentError)

    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(ArgumentError,"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.



117
118
119
# File 'lib/ffi/hunspell/dictionary.rb', line 117

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

#add_affix(word, example) ⇒ Object

Deprecated.

Please use #add_with_affix instead.



141
142
143
# File 'lib/ffi/hunspell/dictionary.rb', line 141

def add_affix(word,example)
  add_with_affix(word,example)
end

#add_dic(dic_path) ⇒ Object

Load an extra dictionary file. The extra dictionaries use the affix file of the allocated Hunspell object.

Maximal number of extra dictionaries is limited in the source code (20)

Raises:

  • (ArgumentError)

    The extra .dic file did not exist.

Since:

  • 0.6.0



159
160
161
162
163
164
165
# File 'lib/ffi/hunspell/dictionary.rb', line 159

def add_dic(dic_path)
  unless File.file?(dic_path)
    raise(ArgumentError,"invalid extra dictionary path #{dic_path.inspect}")
  end

  Hunspell.Hunspell_add_dic(self,dic_path)
end

#add_with_affix(word, example) ⇒ Object

Adds a word to the dictionary with affix flags.

Since:

  • 0.4.0



134
135
136
# File 'lib/ffi/hunspell/dictionary.rb', line 134

def add_with_affix(word,example)
  Hunspell.Hunspell_add_with_affix(self,word.to_s,example.to_s)
end

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

Checks if the word is validate.



188
189
190
# File 'lib/ffi/hunspell/dictionary.rb', line 188

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

#closenil

Closes the dictionary.



251
252
253
254
255
256
257
258
# File 'lib/ffi/hunspell/dictionary.rb', line 251

def close
  if @ptr
    @ptr.free
    @ptr = nil
  end

  return nil
end

#closed?Boolean

Determines if the dictionary is closed.



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

def closed?
  @ptr.nil?
end

#encodingEncoding

The encoding of the dictionary file.



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

def encoding
  @encoding ||= Encoding.const_get(
    Hunspell.Hunspell_get_dic_encoding(self).gsub('-','_')
  )
end

#force_encoding(string) ⇒ String (protected)

Encodes a String into the dictionary's encoding.



281
282
283
# File 'lib/ffi/hunspell/dictionary.rb', line 281

def force_encoding(string)
  string.force_encoding(encoding)
end

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

Removes a word from the dictionary.



173
174
175
# File 'lib/ffi/hunspell/dictionary.rb', line 173

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

#stem(word) ⇒ Array<String>

Finds the stems of a word.



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/ffi/hunspell/dictionary.rb', line 203

def stem(word)
  stems = []

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

    if count > 0
      stems = ptr.get_array_of_string(0,count)

      Hunspell.Hunspell_free_list(self,output,count)
    end
  end

  return stems.map { |word| force_encoding(word) }
end

#suggest(word) ⇒ Array<String>

Suggests alternate spellings of a word.



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/ffi/hunspell/dictionary.rb', line 229

def suggest(word)
  suggestions = []

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

    if count > 0
      suggestions = ptr.get_array_of_string(0,count)

      Hunspell.Hunspell_free_list(self,output,count)
    end
  end

  return suggestions.map { |word| force_encoding(word) }
end

#to_ptrFFI::Pointer

Converts the dictionary to a pointer.



266
267
268
# File 'lib/ffi/hunspell/dictionary.rb', line 266

def to_ptr
  @ptr
end