Class: FFI::Hunspell::Dictionary
- Inherits:
-
Object
- Object
- FFI::Hunspell::Dictionary
- 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
-
.open(name) {|dict| ... } ⇒ Dictionary
Opens a Hunspell dictionary.
Instance Method Summary collapse
-
#add(word) ⇒ Object
(also: #<<)
Adds a word to the dictionary.
-
#add_affix(word, example) ⇒ Object
deprecated
Deprecated.
Please use #add_with_affix instead.
-
#add_with_affix(word, example) ⇒ Object
Adds a word to the dictionary with affix flags.
-
#check?(word) ⇒ Boolean
(also: #valid?)
Checks if the word is validate.
-
#close ⇒ nil
Closes the dictionary.
-
#closed? ⇒ Boolean
Determines if the dictionary is closed.
-
#encoding ⇒ Encoding
The encoding of the dictionary file.
-
#force_encoding(string) ⇒ String
protected
Encodes a String into the dictionary's encoding.
-
#initialize(affix_path, dic_path, key = nil) ⇒ Dictionary
constructor
Creates a new dictionary.
-
#remove(word) ⇒ Object
(also: #delete)
Removes a word from the dictionary.
-
#stem(word) ⇒ Array<String>
Finds the stems of a word.
-
#suggest(word) ⇒ Array<String>
Suggests alternate spellings of a word.
-
#to_ptr ⇒ FFI::Pointer
Converts the dictionary to a pointer.
Constructor Details
#initialize(affix_path, dic_path, key = nil) ⇒ Dictionary
Creates a new dictionary.
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.
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.
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
Please use #add_with_affix instead.
137 138 139 |
# File 'lib/ffi/hunspell/dictionary.rb', line 137 def add_affix(word,example) add_with_affix(word,example) end |
#add_with_affix(word, example) ⇒ Object
Adds a word to the dictionary with affix flags.
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.
164 165 166 |
# File 'lib/ffi/hunspell/dictionary.rb', line 164 def check?(word) Hunspell.Hunspell_spell(self,word.to_s) != 0 end |
#close ⇒ nil
Closes the dictionary.
223 224 225 226 227 228 |
# File 'lib/ffi/hunspell/dictionary.rb', line 223 def close Hunspell.Hunspell_destroy(self) @ptr = nil return nil end |
#closed? ⇒ Boolean
Determines if the dictionary is closed.
93 94 95 |
# File 'lib/ffi/hunspell/dictionary.rb', line 93 def closed? @ptr.nil? end |
#encoding ⇒ 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.
251 252 253 |
# File 'lib/ffi/hunspell/dictionary.rb', line 251 def force_encoding(string) string.force_encoding(encoding) end |
#remove(word) ⇒ Object Also known as: delete
Removes a word from the dictionary.
149 150 151 |
# File 'lib/ffi/hunspell/dictionary.rb', line 149 def remove(word) Hunspell.Hunspell_remove(self,word.to_s) end |
#stem(word) ⇒ Array<String>
Finds the stems of a word.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# 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 |
#suggest(word) ⇒ Array<String>
Suggests alternate spellings of a word.
203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# 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 |
#to_ptr ⇒ FFI::Pointer
Converts the dictionary to a pointer.
236 237 238 |
# File 'lib/ffi/hunspell/dictionary.rb', line 236 def to_ptr @ptr end |