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.

  • dic_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
# 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

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:

  • (ArgumentError)

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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

Instance Method Details

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

Adds a word to the dictionary.

Parameters:

  • word (#to_s)

    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



139
140
141
# File 'lib/ffi/hunspell/dictionary.rb', line 139

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)

Parameters:

  • dic_path (String)

    The path to the extra .dic file.

Raises:

  • (RuntimeError)

    The extra .dic file did not exist.



156
157
158
159
160
161
162
# 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) ⇒ Object

Adds a word to the dictionary with affix flags.

Parameters:

  • word (#to_s)

    The word to add to the dictionary.

  • example (#to_s)

    Affix flags.

Since:

  • 0.4.0



130
131
132
# 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) ⇒ Boolean Also known as: valid?

Checks if the word is validate.

Parameters:

  • word (#to_s)

    The word in question.

Returns:

  • (Boolean)

    Specifies whether the word is valid.



185
186
187
# File 'lib/ffi/hunspell/dictionary.rb', line 185

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

#closenil

Closes the dictionary.

Returns:

  • (nil)


244
245
246
247
248
249
# File 'lib/ffi/hunspell/dictionary.rb', line 244

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.



93
94
95
# File 'lib/ffi/hunspell/dictionary.rb', line 93

def closed?
  @ptr.nil?
end

#encodingEncoding

The encoding of the dictionary file.

Returns:

  • (Encoding)

    The encoding of the dictionary file.



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

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.

Parameters:

  • string (String)

    The unencoded String.

Returns:

  • (String)

    The encoded String.



272
273
274
# File 'lib/ffi/hunspell/dictionary.rb', line 272

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

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

Removes a word from the dictionary.

Parameters:

  • word (#to_s)

    The word to remove.



170
171
172
# File 'lib/ffi/hunspell/dictionary.rb', line 170

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

#stem(word) ⇒ Array<String>

Finds the stems of a word.

Parameters:

  • word (#to_s)

    The word in question.

Returns:

  • (Array<String>)

    The stems of the word.



200
201
202
203
204
205
206
207
208
209
210
211
212
213
# 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) ⇒ Array<String>

Suggests alternate spellings of a word.

Parameters:

  • word (#to_s)

    The word in question.

Returns:

  • (Array<String>)

    The suggestions for the word.



224
225
226
227
228
229
230
231
232
233
234
235
236
237
# 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_ptrFFI::Pointer

Converts the dictionary to a pointer.

Returns:

  • (FFI::Pointer)

    The pointer for the dictionary.



257
258
259
# File 'lib/ffi/hunspell/dictionary.rb', line 257

def to_ptr
  @ptr
end