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
-
#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 ⇒ String
The encoding of the dictionary file.
-
#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 44 45 |
# 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 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.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/ffi/hunspell/dictionary.rb', line 65 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("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
119 120 121 |
# File 'lib/ffi/hunspell/dictionary.rb', line 119 def add_affix(word,example) Hunspell.Hunspell_add_affix(self,word.to_s,example.to_s) end |
#check?(word) ⇒ Boolean Also known as: valid?
Checks if the word is validate.
146 147 148 |
# File 'lib/ffi/hunspell/dictionary.rb', line 146 def check?(word) Hunspell.Hunspell_spell(self,word.to_s) != 0 end |
#close ⇒ nil
Closes the dictionary.
201 202 203 204 205 206 |
# File 'lib/ffi/hunspell/dictionary.rb', line 201 def close Hunspell.Hunspell_destroy(self) @ptr = nil return nil end |
#closed? ⇒ Boolean
Determines if the dictionary is closed.
95 96 97 |
# File 'lib/ffi/hunspell/dictionary.rb', line 95 def closed? @ptr.nil? end |
#encoding ⇒ String
The encoding of the dictionary file.
105 106 107 |
# File 'lib/ffi/hunspell/dictionary.rb', line 105 def encoding Hunspell.Hunspell_get_dic_encoding(self) end |
#remove(word) ⇒ Object Also known as: delete
Removes a word from the dictionary.
131 132 133 |
# File 'lib/ffi/hunspell/dictionary.rb', line 131 def remove(word) Hunspell.Hunspell_remove(self,word.to_s) end |
#stem(word) ⇒ Array<String>
Finds the stems of a word.
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/ffi/hunspell/dictionary.rb', line 161 def stem(word) stems = [] FFI::MemoryPointer.new(:pointer) do |output| count = Hunspell.Hunspell_stem(self,output,word.to_s) ptr = output.get_pointer(0) stems = ptr.get_array_of_string(0,count) end return stems end |
#suggest(word) ⇒ Array<String>
Suggests alternate spellings of a word.
183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/ffi/hunspell/dictionary.rb', line 183 def suggest(word) suggestions = [] FFI::MemoryPointer.new(:pointer) do |output| count = Hunspell.Hunspell_suggest(self,output,word.to_s) ptr = output.get_pointer(0) suggestions = ptr.get_array_of_string(0,count) end return suggestions end |
#to_ptr ⇒ FFI::Pointer
Converts the dictionary to a pointer.
214 215 216 |
# File 'lib/ffi/hunspell/dictionary.rb', line 214 def to_ptr @ptr end |