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

new(affix_path,dic_path,key=nil) click to toggle source

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
open(name) { |dict| ... } click to toggle source

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

<<(word)

@deprecated Please use {#add_with_affix} instead.

Alias for: add
add(word) click to toggle source

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
Also aliased as: <<
add_affix(word,example) click to toggle source
# File lib/ffi/hunspell/dictionary.rb, line 139
def add_affix(word,example)
  add_with_affix(word,example)
end
add_dic(dic_path) click to toggle source

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)

@param [String] dic_path

The path to the extra `.dic` file.

@raise [RuntimeError]

The extra `.dic` file did not exist.
# File lib/ffi/hunspell/dictionary.rb, line 156
def add_dic(dic_path)
  unless File.file?(dic_path)
    raise("invalid extra dictionary path #{dic_path.inspect}")
  end

  Hunspell.Hunspell_add_dic(self,dic_path)
end
add_with_affix(word,example) click to toggle source

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
check?(word) click to toggle source

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 185
def check?(word)
  Hunspell.Hunspell_spell(self,word.to_s) != 0
end
Also aliased as: valid?
close() click to toggle source

Closes the dictionary.

@return [nil]

# File lib/ffi/hunspell/dictionary.rb, line 244
def close
  Hunspell.Hunspell_destroy(self)

  @ptr = nil
  return nil
end
closed?() click to toggle source

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
delete(word)
Alias for: remove
encoding() click to toggle source

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
remove(word) click to toggle source

Removes a word from the dictionary.

@param [#to_s] word

The word to remove.
# File lib/ffi/hunspell/dictionary.rb, line 170
def remove(word)
  Hunspell.Hunspell_remove(self,word.to_s)
end
Also aliased as: delete
stem(word) click to toggle source

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 200
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
suggest(word) click to toggle source

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 224
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
to_ptr() click to toggle source

Converts the dictionary to a pointer.

@return [FFI::Pointer]

The pointer for the dictionary.
# File lib/ffi/hunspell/dictionary.rb, line 257
def to_ptr
  @ptr
end
valid?(word)
Alias for: check?

Protected Instance Methods

force_encoding(string) click to toggle source

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 272
def force_encoding(string)
  string.force_encoding(encoding)
end