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
-
#add_dic(dic_path) ⇒ Object
Load an extra dictionary file.
-
#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
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)
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.
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.
185 186 187 |
# File 'lib/ffi/hunspell/dictionary.rb', line 185 def check?(word) Hunspell.Hunspell_spell(self,word.to_s) != 0 end |
#close ⇒ nil
Closes the dictionary.
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.
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.
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.
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.
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.
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_ptr ⇒ FFI::Pointer
Converts the dictionary to a pointer.
257 258 259 |
# File 'lib/ffi/hunspell/dictionary.rb', line 257 def to_ptr @ptr end |