ffi-aspell 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gems +5 -0
- data/.gitignore +2 -0
- data/.rvmrc +1 -0
- data/.yardopts +9 -0
- data/LICENSE +19 -0
- data/README.md +64 -0
- data/Rakefile +3 -0
- data/ffi-aspell.gemspec +21 -0
- data/lib/ffi/aspell/speller.rb +214 -3
- data/lib/ffi/aspell/version.rb +1 -1
- data/lib/ffi/aspell.rb +210 -7
- data/pkg/.gitkeep +0 -0
- data/spec/ffi/aspell/speller.rb +98 -0
- data/spec/ffi/aspell/suggestions.rb +45 -0
- data/spec/fixtures/personal.en.pws +3 -0
- data/spec/fixtures/personal.nl.pws +3 -0
- data/spec/helper.rb +7 -0
- data/task/build.rake +15 -0
- data/task/memory.rake +50 -0
- data/task/test.rake +4 -0
- metadata +40 -7
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use --create 1.9.3@ffi-aspell
|
data/.yardopts
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2012, Yorick Peterse
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# FFI::Aspell
|
2
|
+
|
3
|
+
FFI::Aspell is an FFI binding for the Aspell library. It was mainly written as
|
4
|
+
[Raspell][raspell], a C binding for Aspell, is buggy and no longer maintained by
|
5
|
+
the main author as of April 2012.
|
6
|
+
|
7
|
+
## Requirements
|
8
|
+
|
9
|
+
* FFI: `gem install ffi`
|
10
|
+
* Aspell (`sudo pacman -S aspell` if you're on Arch Linux)
|
11
|
+
|
12
|
+
## Usage
|
13
|
+
|
14
|
+
Install the gem:
|
15
|
+
|
16
|
+
$ gem install ffi-aspell
|
17
|
+
|
18
|
+
Load it:
|
19
|
+
|
20
|
+
require 'ffi/aspell'
|
21
|
+
|
22
|
+
The primary class is `FFI::Aspell::Speller`, this class can be used to check for
|
23
|
+
spelling errors and the like:
|
24
|
+
|
25
|
+
speller = FFI::Aspell::Speller.new('en_US')
|
26
|
+
|
27
|
+
if speller.correct?('cookie')
|
28
|
+
puts 'The word "cookie" is correct'
|
29
|
+
else
|
30
|
+
puts 'The word "cookie" is incorrect'
|
31
|
+
end
|
32
|
+
|
33
|
+
For more information see the YARD documentation.
|
34
|
+
|
35
|
+
## Hacking & Contributing
|
36
|
+
|
37
|
+
1. Make sure that Aspell and the English and Dutch dictionaries for it are
|
38
|
+
installed as well. On Arch Linux this can be done by running `sudo pacman -S
|
39
|
+
aspell aspell-en aspell-nl`.
|
40
|
+
2. Import the gems using RVM: `rvm gemset import .gems`.
|
41
|
+
3. Run the tests to see if everything is working: `rake test`
|
42
|
+
4. Hack away!
|
43
|
+
|
44
|
+
## Coding Standards
|
45
|
+
|
46
|
+
* FFI functions go in FFI::Aspell
|
47
|
+
* Attached function names should resemble the C function names as much as
|
48
|
+
possible.
|
49
|
+
* No more than 80 characters per line of code.
|
50
|
+
* Document your code, pull requests with big changes but without documentation
|
51
|
+
will be rejected.
|
52
|
+
* Git commits should be signed off, this can be done by running `git commit
|
53
|
+
--sign`. Commits that are not signed off will be rejected.
|
54
|
+
* Follow the Git commit standards are described here:
|
55
|
+
<http://ramaze.net/documentation/file.contributing.html#Commit_Messages>.
|
56
|
+
* Test your code! Pull requests without tests will not be accepted.
|
57
|
+
|
58
|
+
## License
|
59
|
+
|
60
|
+
The code in this repository is licensed under the MIT license. A copy of this
|
61
|
+
license can be found in the file "LICENSE" in the root directory of this
|
62
|
+
repository.
|
63
|
+
|
64
|
+
[raspell]: https://github.com/evan/raspell
|
data/Rakefile
ADDED
data/ffi-aspell.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require File.expand_path('../lib/ffi/aspell/version', __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'ffi-aspell'
|
5
|
+
s.version = FFI::Aspell::VERSION
|
6
|
+
s.date = '2012-04-26'
|
7
|
+
s.authors = ['Yorick Peterse']
|
8
|
+
s.email = 'yorickpeterse@gmail.com'
|
9
|
+
s.summary = 'FFI bindings for Aspell'
|
10
|
+
s.homepage = 'https://github.com/YorickPeterse/ffi-aspell'
|
11
|
+
s.description = s.summary
|
12
|
+
s.files = `git ls-files`.split("\n")
|
13
|
+
s.has_rdoc = 'yard'
|
14
|
+
|
15
|
+
s.add_dependency('ffi', ['>= 1.0.11'])
|
16
|
+
|
17
|
+
s.add_development_dependency('rake', ['>= 0.9.2.2'])
|
18
|
+
s.add_development_dependency('yard',['>= 0.7.5'])
|
19
|
+
s.add_development_dependency('redcarpet', ['>= 2.1.1'])
|
20
|
+
s.add_development_dependency('bacon', ['>= 1.1.0'])
|
21
|
+
end
|
data/lib/ffi/aspell/speller.rb
CHANGED
@@ -4,9 +4,90 @@ module FFI
|
|
4
4
|
# The Speller class is used for spell checking individual words as well as
|
5
5
|
# generating a list of suggestions.
|
6
6
|
#
|
7
|
+
# ## Usage
|
8
|
+
#
|
9
|
+
# First you'll have to create a new instance:
|
10
|
+
#
|
11
|
+
# speller = FFI::Aspell::Speller.new
|
12
|
+
#
|
13
|
+
# When creating a new instance you can specify the language as well as a set
|
14
|
+
# of arbitrary Aspell options (e.g. the personal wordlist file). The
|
15
|
+
# language can be set in the first parameter, other options are set as a
|
16
|
+
# hash in the second parameter:
|
17
|
+
#
|
18
|
+
# speller = FFI::Aspell::Speller.new('nl', :personal => 'aspell.nl.pws')
|
19
|
+
#
|
20
|
+
# Unlike Raspell the keys of the hash used for additional options can be
|
21
|
+
# both strings and symbols.
|
22
|
+
#
|
23
|
+
# Once an instance has been created you can change the options, check the
|
24
|
+
# spelling of a word or retrieve a list of suggestions.
|
25
|
+
#
|
26
|
+
# ### Options
|
27
|
+
#
|
28
|
+
# There are four methods for dealing with Aspell options:
|
29
|
+
#
|
30
|
+
# * {FFI::Aspell::Speller#set}
|
31
|
+
# * {FFI::Aspell::Speller#get}
|
32
|
+
# * {FFI::Aspell::Speller#get_default}
|
33
|
+
# * {FFI::Aspell::Speller#reset}
|
34
|
+
#
|
35
|
+
# There are also two extra methods which can be used to set the suggestion
|
36
|
+
# mode, both these methods are simply shortcuts and use the `#set()` method
|
37
|
+
# for actually setting the values:
|
38
|
+
#
|
39
|
+
# speller.suggestion_mode = 'fast'
|
40
|
+
#
|
41
|
+
# if speller.suggestion_mode == 'fast'
|
42
|
+
# # ...
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# Setting an option:
|
46
|
+
#
|
47
|
+
# speller.set('lang', 'en_US')
|
48
|
+
#
|
49
|
+
# Retrieving an option:
|
50
|
+
#
|
51
|
+
# speller.get('lang')
|
52
|
+
#
|
53
|
+
# Resetting an option:
|
54
|
+
#
|
55
|
+
# speller.reset('lang')
|
56
|
+
#
|
57
|
+
# ### Checking a Word
|
58
|
+
#
|
59
|
+
# Checking the spelling of a word is done using
|
60
|
+
# {FFI::Aspell::Speller#correct?}. This method takes a string containing the
|
61
|
+
# word to verify and returns `true` if the word is spelled correctly and
|
62
|
+
# `false` otherwise:
|
63
|
+
#
|
64
|
+
# speller.correct?('cookie') # => true
|
65
|
+
# speller.correct?('cookei') # => false
|
66
|
+
#
|
67
|
+
# ### Suggestions
|
68
|
+
#
|
69
|
+
# Suggestions can be generated using {FFI::Aspell::Speller.suggestions}.
|
70
|
+
# This method returns an array containing all the possible suggestions based
|
71
|
+
# on the suggestion mode that is being used:
|
72
|
+
#
|
73
|
+
# speller.suggestions('cookei') # => ["cookie", ...]
|
74
|
+
#
|
75
|
+
# For more information see the documentation of the individual methods in
|
76
|
+
# this class.
|
77
|
+
#
|
7
78
|
# @since 13-04-2012
|
79
|
+
# @todo Currently this class creates a new speller object every time
|
80
|
+
# {FFI::Aspell::Speller#correct?} and similar methods are called. I'm not
|
81
|
+
# entirely sure if this is needed, if not it should be modified.
|
8
82
|
#
|
9
83
|
class Speller
|
84
|
+
##
|
85
|
+
# Array containing the possible suggestion modes to use.
|
86
|
+
#
|
87
|
+
# @since 18-04-2012
|
88
|
+
#
|
89
|
+
SUGGESTION_MODES = ['ultra', 'fast', 'normal', 'bad-spellers']
|
90
|
+
|
10
91
|
##
|
11
92
|
# Creates a new instance of the class, sets the language as well as the
|
12
93
|
# options specified in the `options` hash.
|
@@ -16,8 +97,12 @@ module FFI
|
|
16
97
|
# @param [Hash] options A hash containing extra configuration options,
|
17
98
|
# such as the "personal" option to set.
|
18
99
|
#
|
19
|
-
def initialize(language, options = {})
|
100
|
+
def initialize(language = nil, options = {})
|
101
|
+
@config = Aspell.config_new
|
20
102
|
|
103
|
+
options['lang'] = language if language
|
104
|
+
|
105
|
+
options.each { |k, v| set(k, v) }
|
21
106
|
end
|
22
107
|
|
23
108
|
##
|
@@ -28,7 +113,64 @@ module FFI
|
|
28
113
|
# @return [TrueClass|FalseClass]
|
29
114
|
#
|
30
115
|
def correct?(word)
|
116
|
+
unless word.is_a?(String)
|
117
|
+
raise(TypeError, "Expected String but got #{word.class} instead")
|
118
|
+
end
|
119
|
+
|
120
|
+
speller = Aspell.speller_new(@config)
|
121
|
+
correct = Aspell.speller_check(speller, word.to_s, word.length)
|
122
|
+
|
123
|
+
Aspell.speller_delete(speller)
|
31
124
|
|
125
|
+
return correct
|
126
|
+
end
|
127
|
+
|
128
|
+
##
|
129
|
+
# Returns an array containing words suggested as an alternative to the
|
130
|
+
# specified word.
|
131
|
+
#
|
132
|
+
# @since 13-04-2012
|
133
|
+
# @param [String] word The word for which to generate a suggestion list.
|
134
|
+
# @return [Array]
|
135
|
+
#
|
136
|
+
def suggestions(word)
|
137
|
+
unless word.is_a?(String)
|
138
|
+
raise(TypeError, "Expected String but got #{word.class} instead")
|
139
|
+
end
|
140
|
+
|
141
|
+
speller = Aspell.speller_new(@config)
|
142
|
+
list = Aspell.speller_suggest(speller, word, word.length)
|
143
|
+
suggestions = []
|
144
|
+
elements = Aspell.word_list_elements(list)
|
145
|
+
|
146
|
+
while word = Aspell.string_enumeration_next(elements)
|
147
|
+
suggestions << word
|
148
|
+
end
|
149
|
+
|
150
|
+
Aspell.string_enumeration_delete(elements)
|
151
|
+
Aspell.speller_delete(speller)
|
152
|
+
|
153
|
+
return suggestions
|
154
|
+
end
|
155
|
+
|
156
|
+
##
|
157
|
+
# Sets the suggestion mode for {FFI::Aspell::Speller#suggestions}.
|
158
|
+
#
|
159
|
+
# @since 13-04-2012
|
160
|
+
# @param [String] mode The suggestion mode to use.
|
161
|
+
#
|
162
|
+
def suggestion_mode=(mode)
|
163
|
+
set('sug-mode', mode)
|
164
|
+
end
|
165
|
+
|
166
|
+
##
|
167
|
+
# Returns the suggestion mode that's currently used.
|
168
|
+
#
|
169
|
+
# @since 13-04-2012
|
170
|
+
# @return [String]
|
171
|
+
#
|
172
|
+
def suggestion_mode
|
173
|
+
return get('sug-mode')
|
32
174
|
end
|
33
175
|
|
34
176
|
##
|
@@ -38,23 +180,92 @@ module FFI
|
|
38
180
|
# @param [#to_s] key The configuration key to set.
|
39
181
|
# @param [#to_s] value The value of the configuration key.
|
40
182
|
# @raise [FFI::Aspell::ConfigError] Raised when the configuration value
|
41
|
-
# could not be set.
|
183
|
+
# could not be set or when an incorrect suggestion mode was given.
|
42
184
|
#
|
43
185
|
def set(key, value)
|
186
|
+
unless key.respond_to?(:to_s)
|
187
|
+
raise(TypeError, 'Configuration keys should respond to #to_s()')
|
188
|
+
end
|
189
|
+
|
190
|
+
unless value.respond_to?(:to_s)
|
191
|
+
raise(TypeError, 'Configuration values should respond to #to_s()')
|
192
|
+
end
|
193
|
+
|
194
|
+
if key == 'sug-mode' and !SUGGESTION_MODES.include?(value)
|
195
|
+
raise(ConfigError, "The suggestion mode #{value} is invalid")
|
196
|
+
end
|
44
197
|
|
198
|
+
unless Aspell.config_replace(@config, key.to_s, value.to_s)
|
199
|
+
raise(ConfigError, "Failed to set the configuration item #{key}")
|
200
|
+
end
|
45
201
|
end
|
46
202
|
|
47
203
|
##
|
48
204
|
# Retrieves the value of the specified configuration item.
|
49
205
|
#
|
50
206
|
# @since 13-04-2012
|
51
|
-
# @param [
|
207
|
+
# @param [#to_s] key The configuration key to retrieve.
|
52
208
|
# @return [String]
|
53
209
|
# @raise [FFI::Aspell::ConfigError] Raised when the configuration item
|
54
210
|
# does not exist.
|
55
211
|
#
|
56
212
|
def get(key)
|
213
|
+
unless key.respond_to?(:to_s)
|
214
|
+
raise(TypeError, 'Configuration keys should respond to #to_s()')
|
215
|
+
end
|
216
|
+
|
217
|
+
value = Aspell.config_retrieve(@config, key.to_s)
|
218
|
+
|
219
|
+
if value
|
220
|
+
return value
|
221
|
+
else
|
222
|
+
raise(ConfigError, "The configuration item #{key} does not exist")
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
##
|
227
|
+
# Retrieves the default value for the given configuration key.
|
228
|
+
#
|
229
|
+
# @since 13-04-2012
|
230
|
+
# @param [#to_s] key The name of the configuration key.
|
231
|
+
# @return [String]
|
232
|
+
# @raise [FFI::Aspell::ConfigError] Raised when the configuration item
|
233
|
+
# does not exist.
|
234
|
+
#
|
235
|
+
def get_default(key)
|
236
|
+
unless key.respond_to?(:to_s)
|
237
|
+
raise(TypeError, 'Configuration keys should respond to #to_s()')
|
238
|
+
end
|
239
|
+
|
240
|
+
value = Aspell.config_retrieve_default(@config, key.to_s)
|
241
|
+
|
242
|
+
if value
|
243
|
+
return value
|
244
|
+
else
|
245
|
+
raise(ConfigError, "The configuration item #{key} does not exist")
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
##
|
250
|
+
# Resets a configuration item to its default value.
|
251
|
+
#
|
252
|
+
# @since 13-04-2012
|
253
|
+
# @param [#to_s] key The name of the configuration item to reset.
|
254
|
+
# @raise [FFI::Aspell::ConfigError] Raised when the configuration item
|
255
|
+
# could not be reset.
|
256
|
+
#
|
257
|
+
def reset(key)
|
258
|
+
unless key.respond_to?(:to_s)
|
259
|
+
raise(TypeError, 'Configuration keys should respond to #to_s()')
|
260
|
+
end
|
57
261
|
|
262
|
+
unless Aspell.config_remove(@config, key.to_s)
|
263
|
+
raise(
|
264
|
+
ConfigError,
|
265
|
+
"The configuration item #{key} could not be reset, most likely " \
|
266
|
+
"it doesn't exist"
|
267
|
+
)
|
268
|
+
end
|
58
269
|
end
|
59
270
|
end # Speller
|
60
271
|
end # Aspell
|
data/lib/ffi/aspell/version.rb
CHANGED
data/lib/ffi/aspell.rb
CHANGED
@@ -1,13 +1,22 @@
|
|
1
1
|
require 'ffi'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
require 'aspell/
|
6
|
-
require 'aspell/speller'
|
3
|
+
require File.expand_path('../aspell/error', __FILE__)
|
4
|
+
require File.expand_path('../aspell/speller', __FILE__)
|
5
|
+
require File.expand_path('../aspell/version', __FILE__)
|
7
6
|
|
8
7
|
module FFI
|
9
8
|
##
|
10
|
-
# FFI::Aspell is an FFI binding for the Aspell spell checking library.
|
9
|
+
# FFI::Aspell is an FFI binding for the Aspell spell checking library. Basic
|
10
|
+
# usage is as following:
|
11
|
+
#
|
12
|
+
# require 'ffi/aspell'
|
13
|
+
#
|
14
|
+
# speller = FFI::Aspell::Speller.new
|
15
|
+
#
|
16
|
+
# speller.correct?('cookie') # => true
|
17
|
+
# speller.correct?('cookei') # => false
|
18
|
+
#
|
19
|
+
# For more information see {FFI::Aspell::Speller}.
|
11
20
|
#
|
12
21
|
# @since 13-04-2012
|
13
22
|
#
|
@@ -15,41 +24,235 @@ module FFI
|
|
15
24
|
extend FFI::Library
|
16
25
|
ffi_lib 'aspell'
|
17
26
|
|
18
|
-
|
27
|
+
##
|
28
|
+
# Creates a pointer for a configuration struct.
|
29
|
+
#
|
30
|
+
# @since 24-04-2012
|
31
|
+
# @method config_new
|
32
|
+
# @scope class
|
33
|
+
# @return [FFI::Pointer]
|
34
|
+
#
|
19
35
|
attach_function 'config_new',
|
20
36
|
'new_aspell_config',
|
21
37
|
[],
|
22
38
|
:pointer
|
23
39
|
|
40
|
+
##
|
41
|
+
# Retrieves the value of a given configuration item. The value is returned
|
42
|
+
# as a string or nil upon failure.
|
43
|
+
#
|
44
|
+
# @example
|
45
|
+
# config = FFI::Aspell.config_new
|
46
|
+
# value = FFI::Aspell.config_retrieve(config, 'lang')
|
47
|
+
#
|
48
|
+
# puts value # => "en_US"
|
49
|
+
#
|
50
|
+
# @since 24-04-2012
|
51
|
+
# @method config_retrieve(config, key)
|
52
|
+
# @scope class
|
53
|
+
# @param [FFI::Pointer] config A pointer to a configuration struct.
|
54
|
+
# @param [String] key The name of the configuration item to retrieve.
|
55
|
+
# @return [String]
|
56
|
+
#
|
24
57
|
attach_function 'config_retrieve',
|
25
58
|
'aspell_config_retrieve',
|
26
59
|
[:pointer, :string],
|
27
60
|
:string
|
28
61
|
|
29
|
-
|
62
|
+
##
|
63
|
+
# Retrieves the default value of a configuration item.
|
64
|
+
#
|
65
|
+
# @since 24-04-2012
|
66
|
+
# @method config_retrieve_default(config, key)
|
67
|
+
# @scope class
|
68
|
+
# @see FFI::Aspell.config_retrieve
|
69
|
+
#
|
70
|
+
attach_function 'config_retrieve_default',
|
30
71
|
'aspell_config_get_default',
|
31
72
|
[:pointer, :string],
|
32
73
|
:string
|
33
74
|
|
75
|
+
##
|
76
|
+
# Sets the new value of the specified configuration item.
|
77
|
+
#
|
78
|
+
# @example
|
79
|
+
# config = FFI::Aspell.config_new
|
80
|
+
#
|
81
|
+
# FFI::Aspell.config_replace(config, 'lang', 'nl')
|
82
|
+
#
|
83
|
+
# @since 24-04-2012
|
84
|
+
# @method config_replace(config, key, value)
|
85
|
+
# @scope class
|
86
|
+
# @param [FFI::Pointer] config Pointer to the configuration struct.
|
87
|
+
# @param [String] key The name of the configuration item to set.
|
88
|
+
# @param [String] value The new value of the configuration item.
|
89
|
+
# @return [TrueClass|FalseClass]
|
90
|
+
#
|
34
91
|
attach_function 'config_replace',
|
35
92
|
'aspell_config_replace',
|
36
93
|
[:pointer, :string, :string],
|
37
94
|
:bool
|
38
95
|
|
96
|
+
##
|
97
|
+
# Sets the value of the specified configuration item back to its default
|
98
|
+
# value.
|
99
|
+
#
|
100
|
+
# @example
|
101
|
+
# config = FFI::Aspell.config_new
|
102
|
+
#
|
103
|
+
# FFI::Aspell.config_replace(config, 'lang', 'nl')
|
104
|
+
# FFI::Aspell.config_remove(config, 'lang')
|
105
|
+
#
|
106
|
+
# @since 24-04-2012
|
107
|
+
# @method config_remove(config, key)
|
108
|
+
# @scope class
|
109
|
+
# @param [FFI::Pointer] config Pointer to the configuration struct.
|
110
|
+
# @param [String] key The name of the configuration item to reset.
|
111
|
+
# @return [TrueClass|FalseClass]
|
112
|
+
#
|
39
113
|
attach_function 'config_remove',
|
40
114
|
'aspell_config_remove',
|
41
115
|
[:pointer, :string],
|
42
116
|
:bool
|
43
117
|
|
44
118
|
# Spell checking related functions.
|
119
|
+
|
120
|
+
##
|
121
|
+
# Creates a pointer to a speller struct.
|
122
|
+
#
|
123
|
+
# @example
|
124
|
+
# config = FFI::Aspell.config_new
|
125
|
+
# speller = FFI::Aspell.speller_new(config)
|
126
|
+
#
|
127
|
+
# @since 24-04-2012
|
128
|
+
# @method speller_new(config)
|
129
|
+
# @scope class
|
130
|
+
# @param [FFI::Pointer] config The configuration struct to use for the
|
131
|
+
# speller.
|
132
|
+
# @return [FFI::Pointer]
|
133
|
+
#
|
45
134
|
attach_function 'speller_new',
|
46
135
|
'new_aspell_speller',
|
47
136
|
[:pointer],
|
48
137
|
:pointer
|
49
138
|
|
139
|
+
##
|
140
|
+
# Removes a speller pointer and frees the memory associated with said
|
141
|
+
# pointer.
|
142
|
+
#
|
143
|
+
# @since 24-04-2012
|
144
|
+
# @method speller_delete(speller)
|
145
|
+
# @scope class
|
146
|
+
# @param [FFI::Pointer] speller The pointer to remove.
|
147
|
+
#
|
148
|
+
attach_function 'speller_delete',
|
149
|
+
'delete_aspell_speller',
|
150
|
+
[:pointer],
|
151
|
+
:void
|
152
|
+
|
153
|
+
##
|
154
|
+
# Checks if a given word is spelled correctly or not. If the word is valid
|
155
|
+
# `true` will be returned, `false` otherwise.
|
156
|
+
#
|
157
|
+
# @example
|
158
|
+
# config = FFI::Aspell.config_new
|
159
|
+
# speller = FFI::Aspell.speller_new(config)
|
160
|
+
# word = 'cookie'
|
161
|
+
# valid = FFI::Aspell.speller_check(speller, word, word.length)
|
162
|
+
#
|
163
|
+
# if valid
|
164
|
+
# puts 'The word "cookie" is valid'
|
165
|
+
# else
|
166
|
+
# puts 'The word "cookie" is invalid'
|
167
|
+
# end
|
168
|
+
#
|
169
|
+
# @since 24-04-2012
|
170
|
+
# @method speller_check(speller, word, length)
|
171
|
+
# @scope class
|
172
|
+
# @param [FFI::Pointer] speller Pointer to a speller struct to use.
|
173
|
+
# @param [String] word The word to check.
|
174
|
+
# @param [Fixnum] length The length of the word.
|
175
|
+
# @return [TrueClass|FalseClass]
|
176
|
+
#
|
50
177
|
attach_function 'speller_check',
|
51
178
|
'aspell_speller_check',
|
52
179
|
[:pointer, :string, :int],
|
53
180
|
:bool
|
181
|
+
|
182
|
+
# Functions for dealing with suggestions.
|
183
|
+
|
184
|
+
##
|
185
|
+
# Returns a pointer that can be used to retrieve a list of suggestions for a
|
186
|
+
# given word.
|
187
|
+
#
|
188
|
+
# @since 24-04-2012
|
189
|
+
# @method speller_suggest(speller, word, length)
|
190
|
+
# @see FFI::Aspell.speller_check
|
191
|
+
# @return [FFI::Pointer]
|
192
|
+
#
|
193
|
+
attach_function 'speller_suggest',
|
194
|
+
'aspell_speller_suggest',
|
195
|
+
[:pointer, :string, :int],
|
196
|
+
:pointer
|
197
|
+
|
198
|
+
##
|
199
|
+
# Returns a pointer to a list which can be used by
|
200
|
+
# {FFI::Aspell.string_enumeration_next} to retrieve all the suggested words.
|
201
|
+
#
|
202
|
+
# @since 24-04-2012
|
203
|
+
# @method word_list_elements(suggestions)
|
204
|
+
# @scope class
|
205
|
+
# @param [FFI::Pointer] suggestions A pointer with suggestions as returned
|
206
|
+
# by {FFI::Aspell.speller_suggest}
|
207
|
+
# @return [FFI::Pointer]
|
208
|
+
#
|
209
|
+
attach_function 'word_list_elements',
|
210
|
+
'aspell_word_list_elements',
|
211
|
+
[:pointer],
|
212
|
+
:pointer
|
213
|
+
|
214
|
+
##
|
215
|
+
# Removes the pointer returned by {FFI::Aspell.word_list_elements} and frees
|
216
|
+
# the associated memory.
|
217
|
+
#
|
218
|
+
# @since 24-04-2012
|
219
|
+
# @method string_enumeration_delete(elements)
|
220
|
+
# @scope class
|
221
|
+
# @param [FFI::Pointer] elements A pointer for a list of elements as
|
222
|
+
# returned by {FFI::Aspell.word_list_elements}.
|
223
|
+
#
|
224
|
+
attach_function 'string_enumeration_delete',
|
225
|
+
'delete_aspell_string_enumeration',
|
226
|
+
[:pointer],
|
227
|
+
:void
|
228
|
+
|
229
|
+
##
|
230
|
+
# Retrieves the next item in the list of suggestions.
|
231
|
+
#
|
232
|
+
# @example
|
233
|
+
# speller = FFI::Aspell.speller_new(FFI::Aspell.config_new)
|
234
|
+
# word = 'cookie'
|
235
|
+
# list = FFI::Aspell.speller_suggest(speller, word, word.length)
|
236
|
+
# elements = FFI::Aspell.word_list_elements(list)
|
237
|
+
# words = []
|
238
|
+
#
|
239
|
+
# while word = FFI::Aspell.string_enumeration_next(elements)
|
240
|
+
# words << word
|
241
|
+
# end
|
242
|
+
#
|
243
|
+
# FFI::Aspell.string_enumeration_delete(elements)
|
244
|
+
# FFI::Aspell.speller_delete(speller)
|
245
|
+
#
|
246
|
+
# @since 24-04-2012
|
247
|
+
# @method string_enumeration_next(elements)
|
248
|
+
# @scope class
|
249
|
+
# @param [FFI::Pointer] elements Pointer to a list of elements as returned
|
250
|
+
# by {FFI::Aspell.word_list_elements}.
|
251
|
+
# @return [String|NilClass]
|
252
|
+
#
|
253
|
+
attach_function 'string_enumeration_next',
|
254
|
+
'aspell_string_enumeration_next',
|
255
|
+
[:pointer],
|
256
|
+
:string
|
54
257
|
end # Aspell
|
55
258
|
end # FFI
|
data/pkg/.gitkeep
ADDED
File without changes
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require File.expand_path('../../../helper', __FILE__)
|
2
|
+
|
3
|
+
describe 'FFI::Aspell::Speller' do
|
4
|
+
it 'Set the language in the constructor' do
|
5
|
+
FFI::Aspell::Speller.new.get('lang').should == 'en_US'
|
6
|
+
FFI::Aspell::Speller.new('en').get('lang').should == 'en'
|
7
|
+
FFI::Aspell::Speller.new('nl').get('lang').should == 'nl'
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'Set options in the constructor' do
|
11
|
+
FFI::Aspell::Speller.new.get('personal').should == '.aspell.en_US.pws'
|
12
|
+
|
13
|
+
FFI::Aspell::Speller.new('en', :personal => 'foo') \
|
14
|
+
.get(:personal).should == 'foo'
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'Raise when setting a non existing option' do
|
18
|
+
should.raise(FFI::Aspell::ConfigError) do
|
19
|
+
FFI::Aspell::Speller.new.set('foo', 'bar')
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'Raise when retrieving a non existing option' do
|
24
|
+
should.raise(FFI::Aspell::ConfigError) do
|
25
|
+
FFI::Aspell::Speller.new.get('foo')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'Retrieve the default value of an option' do
|
30
|
+
FFI::Aspell::Speller.new.get_default('personal') \
|
31
|
+
.should == '.aspell.en_US.pws'
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'Reset an option to its default value' do
|
35
|
+
speller = FFI::Aspell::Speller.new
|
36
|
+
|
37
|
+
speller.set('personal', 'foo')
|
38
|
+
|
39
|
+
speller.get('personal').should == 'foo'
|
40
|
+
|
41
|
+
speller.reset('personal')
|
42
|
+
|
43
|
+
speller.get('personal').should == '.aspell.en_US.pws'
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'Validate various English words' do
|
47
|
+
speller = FFI::Aspell::Speller.new('en')
|
48
|
+
|
49
|
+
speller.correct?('cookie').should == true
|
50
|
+
speller.correct?('werld').should == false
|
51
|
+
speller.correct?('house').should == true
|
52
|
+
speller.correct?('huis').should == false
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'Validate various Dutch words' do
|
56
|
+
speller = FFI::Aspell::Speller.new('nl')
|
57
|
+
|
58
|
+
speller.correct?('koekje').should == true
|
59
|
+
speller.correct?('werld').should == false
|
60
|
+
speller.correct?('huis').should == true
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'Change the language of an existing speller object' do
|
64
|
+
speller = FFI::Aspell::Speller.new
|
65
|
+
|
66
|
+
speller.correct?('house').should == true
|
67
|
+
speller.correct?('huis').should == false
|
68
|
+
|
69
|
+
speller.set('lang', 'nl')
|
70
|
+
|
71
|
+
speller.correct?('house').should == true
|
72
|
+
speller.correct?('huis').should == true
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'Use an English personal word list' do
|
76
|
+
speller = FFI::Aspell::Speller.new('en')
|
77
|
+
|
78
|
+
speller.correct?('github').should == false
|
79
|
+
speller.correct?('nodoc').should == false
|
80
|
+
|
81
|
+
speller.set(:personal, File.join(FIXTURES, 'personal.en.pws'))
|
82
|
+
|
83
|
+
speller.correct?('github').should == true
|
84
|
+
speller.correct?('nodoc').should == true
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'Use a Dutch personal word list' do
|
88
|
+
speller = FFI::Aspell::Speller.new('nl')
|
89
|
+
|
90
|
+
speller.correct?('github').should == false
|
91
|
+
speller.correct?('nodoc').should == false
|
92
|
+
|
93
|
+
speller.set(:personal, File.join(FIXTURES, 'personal.nl.pws'))
|
94
|
+
|
95
|
+
speller.correct?('github').should == true
|
96
|
+
speller.correct?('nodoc').should == true
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path('../../../helper', __FILE__)
|
2
|
+
|
3
|
+
describe 'FFI::Aspell::Speller#suggestions' do
|
4
|
+
it 'Return a list of word suggestions using the default mode' do
|
5
|
+
speller = FFI::Aspell::Speller.new
|
6
|
+
suggestions = speller.suggestions('cookei')
|
7
|
+
|
8
|
+
suggestions.include?('coke').should == true
|
9
|
+
suggestions.include?('cookie').should == true
|
10
|
+
suggestions.include?('cooked').should == true
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'Return a list of word suggestions using the "bad-spellers" mode' do
|
14
|
+
speller = FFI::Aspell::Speller.new
|
15
|
+
|
16
|
+
# Get the amount of suggestions for the normal mode. The "bad-spellers" mode
|
17
|
+
# should result in a lot more possible suggestions.
|
18
|
+
normal_length = speller.suggestions('cookei').length
|
19
|
+
|
20
|
+
speller.suggestion_mode = 'bad-spellers'
|
21
|
+
suggestions = speller.suggestions('cookei')
|
22
|
+
|
23
|
+
suggestions.include?('coke').should == true
|
24
|
+
suggestions.include?('cookie').should == true
|
25
|
+
suggestions.include?('cooked').should == true
|
26
|
+
|
27
|
+
suggestions.length.should > normal_length
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'Raise an error when an invalid suggestion mode is used' do
|
31
|
+
speller = FFI::Aspell::Speller.new
|
32
|
+
|
33
|
+
should.raise(FFI::Aspell::ConfigError) do
|
34
|
+
speller.suggestion_mode = 'does-not-exist'
|
35
|
+
end
|
36
|
+
|
37
|
+
speller.suggestion_mode.should == 'normal'
|
38
|
+
|
39
|
+
should.not.raise(FFI::Aspell::ConfigError) do
|
40
|
+
speller.suggestion_mode = 'ultra'
|
41
|
+
end
|
42
|
+
|
43
|
+
speller.suggestion_mode.should == 'ultra'
|
44
|
+
end
|
45
|
+
end
|
data/spec/helper.rb
ADDED
data/task/build.rake
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
namespace :build do
|
2
|
+
desc 'Builds a new Gem'
|
3
|
+
task :gem do
|
4
|
+
root = File.expand_path('../../', __FILE__)
|
5
|
+
gemspec = Gem::Specification.load(File.join(root, 'ffi-aspell.gemspec'))
|
6
|
+
name = "#{gemspec.name}-#{gemspec.version.version}.gem"
|
7
|
+
path = File.join(root, name)
|
8
|
+
pkg = File.join(root, 'pkg', name)
|
9
|
+
|
10
|
+
# Build and install the gem
|
11
|
+
sh('gem', 'build', File.join(root, 'ffi-aspell.gemspec'))
|
12
|
+
sh('mv' , path, pkg)
|
13
|
+
sh('gem', 'install', pkg)
|
14
|
+
end
|
15
|
+
end
|
data/task/memory.rake
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# Cheap way of benchmarking the memory usage of various parts of the FFI
|
2
|
+
# binding.
|
3
|
+
def benchmark_block(amount = 10000)
|
4
|
+
require File.expand_path('../../lib/ffi/aspell', __FILE__)
|
5
|
+
|
6
|
+
start_mem = `ps -o rss= #{Process.pid}`.to_f
|
7
|
+
|
8
|
+
amount.times { yield }
|
9
|
+
|
10
|
+
mem = ((`ps -o rss= #{Process.pid}`.to_f - start_mem) / 1024).round(2)
|
11
|
+
|
12
|
+
puts "Memory increase in Megabytes: #{mem} MB"
|
13
|
+
end
|
14
|
+
|
15
|
+
namespace :memory do
|
16
|
+
memory = proc { `ps -o rss= #{Process.pid}`.to_i }
|
17
|
+
|
18
|
+
desc 'Show memory usage of Aspell.speller_new'
|
19
|
+
task :speller, [:amount] do |task, args|
|
20
|
+
args.with_defaults(:amount => 10000)
|
21
|
+
|
22
|
+
benchmark_block(args.amount) do
|
23
|
+
speller = FFI::Aspell.speller_new(FFI::Aspell.config_new)
|
24
|
+
|
25
|
+
FFI::Aspell.speller_delete(speller)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
desc 'Show memory usage of Speller#correct?'
|
30
|
+
task :correct, [:amount] do |task, args|
|
31
|
+
args.with_defaults(:amount => 10000)
|
32
|
+
|
33
|
+
benchmark_block(args.amount) do
|
34
|
+
speller = FFI::Aspell::Speller.new
|
35
|
+
|
36
|
+
speller.correct?('cookie')
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
desc 'Show memory usage of Speller#suggestions'
|
41
|
+
task :suggestions, [:amount] do |task, args|
|
42
|
+
args.with_defaults(:amount => 10000)
|
43
|
+
|
44
|
+
benchmark_block(args.amount) do
|
45
|
+
speller = FFI::Aspell::Speller.new
|
46
|
+
|
47
|
+
speller.suggestions('cookei')
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/task/test.rake
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ffi-aspell
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-04-
|
12
|
+
date: 2012-04-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: ffi
|
@@ -27,6 +27,22 @@ dependencies:
|
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: 1.0.11
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.9.2.2
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.9.2.2
|
30
46
|
- !ruby/object:Gem::Dependency
|
31
47
|
name: yard
|
32
48
|
requirement: !ruby/object:Gem::Requirement
|
@@ -81,10 +97,27 @@ executables: []
|
|
81
97
|
extensions: []
|
82
98
|
extra_rdoc_files: []
|
83
99
|
files:
|
84
|
-
-
|
85
|
-
-
|
86
|
-
-
|
87
|
-
-
|
100
|
+
- .gems
|
101
|
+
- .gitignore
|
102
|
+
- .rvmrc
|
103
|
+
- .yardopts
|
104
|
+
- LICENSE
|
105
|
+
- README.md
|
106
|
+
- Rakefile
|
107
|
+
- ffi-aspell.gemspec
|
108
|
+
- lib/ffi/aspell.rb
|
109
|
+
- lib/ffi/aspell/error.rb
|
110
|
+
- lib/ffi/aspell/speller.rb
|
111
|
+
- lib/ffi/aspell/version.rb
|
112
|
+
- pkg/.gitkeep
|
113
|
+
- spec/ffi/aspell/speller.rb
|
114
|
+
- spec/ffi/aspell/suggestions.rb
|
115
|
+
- spec/fixtures/personal.en.pws
|
116
|
+
- spec/fixtures/personal.nl.pws
|
117
|
+
- spec/helper.rb
|
118
|
+
- task/build.rake
|
119
|
+
- task/memory.rake
|
120
|
+
- task/test.rake
|
88
121
|
homepage: https://github.com/YorickPeterse/ffi-aspell
|
89
122
|
licenses: []
|
90
123
|
post_install_message:
|
@@ -105,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
105
138
|
version: '0'
|
106
139
|
requirements: []
|
107
140
|
rubyforge_project:
|
108
|
-
rubygems_version: 1.8.
|
141
|
+
rubygems_version: 1.8.23
|
109
142
|
signing_key:
|
110
143
|
specification_version: 3
|
111
144
|
summary: FFI bindings for Aspell
|