class FFI::Hunspell::Dictionary
Represents a dictionary for a specific language.
Constants
- AFF_EXT
The affix file extension
- DIC_EXT
The dictionary file extension
Public Class Methods
Creates a new dictionary.
@param [String] affix_path
The path to the `.aff` file.
@param [String] dic_path
The path to the `.dic` file.
@param [String] key
The optional key for encrypted dictionary files.
@raise [RuntimeError]
Either the `.aff` or `.dic` files did not exist.
# 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 end
Opens a Hunspell
dictionary.
@param [Symbol, String] name
The name of the dictionary to open.
@yield [dict]
The given block will be passed the Hunspell dictionary.
@yieldparam [Dictionary] dict
The opened dictionary.
@return [Dictionary]
If no block is given, the open dictionary will be returned.
@raise [ArgumentError]
The dictionary files could not be found in any of the directories.
# File lib/ffi/hunspell/dictionary.rb, line 63 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
Public Instance Methods
Adds a word to the dictionary.
@param [#to_s] word
The word to add to the dictionary.
# File lib/ffi/hunspell/dictionary.rb, line 115 def add(word) Hunspell.Hunspell_add(self,word.to_s) end
@deprecated Please use {#add_with_affix} instead.
# File lib/ffi/hunspell/dictionary.rb, line 137 def add_affix(word,example) add_with_affix(word,example) end
Adds a word to the dictionary with affix flags.
@param [#to_s] word
The word to add to the dictionary.
@param [#to_s] example
Affix flags.
@since 0.4.0
# File lib/ffi/hunspell/dictionary.rb, line 130 def add_with_affix(word,example) Hunspell.Hunspell_add_with_affix(self,word.to_s,example.to_s) end
Checks if the word is validate.
@param [#to_s] word
The word in question.
@return [Boolean]
Specifies whether the word is valid.
# File lib/ffi/hunspell/dictionary.rb, line 164 def check?(word) Hunspell.Hunspell_spell(self,word.to_s) != 0 end
Closes the dictionary.
@return [nil]
# File lib/ffi/hunspell/dictionary.rb, line 223 def close Hunspell.Hunspell_destroy(self) @ptr = nil return nil end
Determines if the dictionary is closed.
@return [Boolean]
Specifies whether the dictionary was closed.
# File lib/ffi/hunspell/dictionary.rb, line 93 def closed? @ptr.nil? end
The encoding of the dictionary file.
@return [Encoding]
The encoding of the dictionary file.
# File lib/ffi/hunspell/dictionary.rb, line 103 def encoding @encoding ||= Encoding.const_get( Hunspell.Hunspell_get_dic_encoding(self).gsub('-','_') ) end
Removes a word from the dictionary.
@param [#to_s] word
The word to remove.
# File lib/ffi/hunspell/dictionary.rb, line 149 def remove(word) Hunspell.Hunspell_remove(self,word.to_s) end
Finds the stems of a word.
@param [#to_s] word
The word in question.
@return [Array<String>]
The stems of the word.
# File lib/ffi/hunspell/dictionary.rb, line 179 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) end end return stems.map { |word| force_encoding(word) } end
Suggests alternate spellings of a word.
@param [#to_s] word
The word in question.
@return [Array<String>]
The suggestions for the word.
# File lib/ffi/hunspell/dictionary.rb, line 203 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) end end return suggestions.map { |word| force_encoding(word) } end
Converts the dictionary to a pointer.
@return [FFI::Pointer]
The pointer for the dictionary.
# File lib/ffi/hunspell/dictionary.rb, line 236 def to_ptr @ptr end
Protected Instance Methods
Encodes a String into the dictionary's encoding.
@param [String] string
The unencoded String.
@return [String]
The encoded String.
# File lib/ffi/hunspell/dictionary.rb, line 251 def force_encoding(string) string.force_encoding(encoding) end