1Aspell(3) User Contributed Perl Documentation Aspell(3)
2
3
4
6 Text::Aspell - Perl interface to the GNU Aspell library
7
9 use Text::Aspell;
10 my $speller = Text::Aspell->new;
11
12 die unless $speller;
13
14 # Set some options
15 $speller->set_option('lang','en_US');
16 $speller->set_option('sug-mode','fast');
17
18 # check a word
19 print $speller->check( $word )
20 ? "$word found\n"
21 : "$word not found!\n";
22
23 # lookup up words
24 my @suggestions = $speller->suggest( $misspelled );
25
26 # lookup config options
27 my $language = $speller->get_option('lang');
28 print $speller->errstr unless defined $language;
29
30 # fetch a config item that is a list
31 my @sgml_extensions = $speller->get_option_as_list('sgml-extension');
32
33 # fetch the configuration keys and their default settings
34 my $options = $speller->fetch_option_keys;
35
36 # or dump config settings to STDOUT
37 $speller->print_config ⎪⎪ $speller->errstr;
38
39 # What dictionaries are installed as simple strings
40 my @dicts = $speller->list_dictionaries;
41
42 # or as an array of hashes
43 @dicts = $speller->dictionary_info;
44 print Data::Dumper::Dumper( \@dicts );
45
46 Here's an example how to create and use your own word list
47
48 Create a dictionary:
49
50 $ aspell --lang=en create master ./dictionary.local < space_separated_word_list
51
52 Then in your code:
53
54 use Text::Aspell;
55 my $speller = Text::Aspell->new;
56 die unless $speller;
57 $speller->set_option('master','./dictionary.local');
58 # check a word
59 print $speller->check( $word )
60 ? "$word found\n"
61 : "$word not found!\n";
62
64 This module provides a Perl interface to the GNU Aspell library. This
65 module is to meet the need of looking up many words, one at a time, in
66 a single session, such as spell-checking a document in memory.
67
68 The GNU C interface is described at:
69
70 http://aspell.net/man-html/Through-the-C-API.html#Through-the-C-API
71
72 It's worth looking over the way config and speller (manager) objects
73 are created when using the Aspell C API as some of that is hidden in
74 the Text::Aspell module.
75
76 For example, with Text::Aspell you do not have to explicitly create a
77 speller object. The speller (manager) object is created automatically
78 the first time you call suggest() or check().
79
80 Note also that once the speller object is created some (all?) config
81 options cannot be changed. For example, setting configuration options
82 such as "lang" are what determine what dictionary Aspell will use.
83 Once the speller object is created that dictionary will be used. I.e.
84 setting "lang" after the speller object is created will have no effect.
85
87 You MUST have installed GNU Aspell library version 0.50.1 or higher on
88 your system before installing this Text::Aspell Perl module. If
89 installing Aspell using your operating system's package management sys‐
90 tem, you may need to install the Aspell development package (for exam‐
91 ple, on Debian libaspell-dev).
92
93 Aspell can source can be downloaded from:
94
95 http://aspell.net
96
97 There have been a number of bug reports because people failed to
98 install aspell before installing this module. This is an interface to
99 the aspell library installed on your system, not a replacement for
100 aspell.
101
102 You must also have the English dictionary installed when running the
103 module's test suite.
104
105 Also, please see the README and Changes files. README may have spe‐
106 cific information about your platform.
107
109 The following methods are available:
110
111 $speller = Text::Aspell->new;
112 Creates a new speller object. New does not take any parameters
113 (future version may allow options set by passing in a hash refer‐
114 ence of options and value pairs). Returns "undef" if the object
115 could not be created, which is unlikely.
116
117 Internally, new() creates an object to store Aspell structures
118 (AspellConfig, AspellSpeller, and a space for an error string and
119 then calls new_aspell_config();
120
121 $speller->set_option($option_name, $value);
122 Sets the configuration option $option_name to the value of $value.
123 Returns "undef" on error, and the error message can be printed with
124 $speller->errstr.
125
126 You should set configuration options before calling the $spell‐
127 er->create_speller method. See the GNU Aspell documentation for
128 the available configuration settings and how (and when) they may be
129 used.
130
131 $speller->remove_option($option_name);
132 Removes (sets to the default value) the configuration option speci‐
133 fied by $option_name. Returns "undef" on error, and the error mes‐
134 sage can be printed with $speller->errstr. You may only set con‐
135 figuration options before calling the $speller->create_speller
136 method.
137
138 $string = $speller->get_option($option_name);
139 Returns the current setting for the given configuration option.
140 The values are strings. For configuration options that are lists
141 used the "get_option_as_list()" method.
142
143 Returns "undef" on error, and the error message can be printed with
144 $speller->errstr.
145
146 Note that this may return different results depending on if it's
147 called before or after $speller->create_speller is called.
148
149 @list = $speller->get_option_as_list($option_name);
150 Returns an array of list items for the given option. Use this
151 method to fetch configuration values that are of type list.
152
153 Returns "undef" on error, and the error message can be printed with
154 $speller->errstr.
155
156 Note that this may return different results depending on if it's
157 called before or after $speller->create_speller is called.
158
159 $options = $speller->fetch_option_keys;
160 Returns a hash of hashes. The keys are the possible configuration
161 options and the values is a hash with keys of:
162
163 desc : A short description of the option
164 default : The default value for this option
165 type : The data type of option (see aspell.h)
166
167 $speller->print_config;
168 Prints the current configuration to STDOUT. Useful for debugging.
169 Note that this will return different results depending on if it's
170 called before or after $speller->create_speller is called.
171
172 $speller->errstr;
173 Returns the error string from the last error. Check the previous
174 call for an "undef" return value before calling this method
175
176 $errnum = $speller->errnum;
177 Returns the error number from the last error. Some errors may only
178 set the error string ($speller->errstr) on errors, so it's best to
179 check use the errstr method over this method.
180
181 This method is deprecated.
182
183 $found = $speller->check($word);
184 Checks if a word is found in the dictionary. Returns true if the
185 word is found in the dictionary, false but defined if the word is
186 not in the dictionary. Returns "undef" on error, and the error
187 message can be printed with $speller->errstr.
188
189 This calls $speller->create_speller if the speller has not been
190 created by an explicit call to $speller->create_speller.
191
192 @suggestions = $speller->suggest($word)
193 Returns an array of word suggestions for the specified word. The
194 words are returned with the best guesses at the start of the list.
195
196 $speller->create_speller;
197 This method is normally not called by your program. It is called
198 automatically the first time $speller->check() or $speller->sug‐
199 gest() is called to create a spelling "speller".
200
201 You might want to call this when your program first starts up to
202 make the first access a bit faster, or if you need to read back
203 configuration settings before looking up words.
204
205 The creation of the speller builds a configuration profile in the
206 speller structure. Results from calling print_config() and
207 get_option() will change after calling create_speller(). In gen‐
208 eral, it's best to read config settings back after calling cre‐
209 ate_speller() or after calling spell() or suggest(). Returns
210 "undef" on error, and the error message can be printed with $spell‐
211 er->errstr.
212
213 $speller->add_to_session($word)
214 $speller->add_to_personal($word)
215 Adds a word to the session or personal word lists. Words added
216 will be offered as suggestions.
217
218 $speller->store_replacement($word, $replacement);
219 This method can be used to instruct the speller which word you used
220 as a replacement for a misspelled word. This allows the speller to
221 offer up the replacement next time the word is misspelled. See
222 section 6.3 of the GNU Aspell documentation for a better descrip‐
223 tion.
224
225 (July 2005 note: best to ignore any return value for now)
226
227 $speller->save_all_word_lists;
228 Writes any pending word lists to disk.
229
230 $speller->clear_session;
231 Clears the current session word list.
232
233 @dicts = $speller->list_dictionaries;
234 This returns an array of installed dictionary files. Each is a
235 single string formatted as:
236
237 [name]:[code]:[jargon]:[size]:[module]
238
239 Name and code will often be the same, but name is the complete name
240 of the dictionary which can be used to directly select a dictio‐
241 nary, and code is the language/region code only.
242
243 $array_ref = $speller->$speller->dictionary_info;
244 Like the "list_dictionaries()" method, this method returns an array
245 of hash references. For example, an entry for a dictionary might
246 have the following hash reference:
247
248 {
249 'module' => 'default',
250 'code' => 'en_US',
251 'size' => 60,
252 'jargon' => 'w-accents',
253 'name' => 'en_US-w-accents'
254 },
255
256 Not all hash keys will be available for every dictionary (e.g. the
257 dictionary may not have a "jargon" key).
258
260 Text::Aspell works with GNU Aspell and is a replacement for the module
261 Text::Pspell. Text::Pspell is no longer supported.
262
263 Upgrading should be a simple process. Only one method name has
264 changed: "create_manager" is now called "create_speller". Code
265 designed to use the old Text::Pspell module may not even call the "cre‐
266 ate_manager" method so this may not be an issue.
267
268 The "language_tag" configuration setting is now called "lang".
269
270 Diffs for code that uses Text::Pspell might look like:
271
272 - use Text::Pspell;
273 + use Text::Aspell;
274
275 - $speller = Text::Pspell->new;
276 + $speller = Text::Aspell->new;
277
278 - $speller->create_manager ⎪⎪ die "Failed to create speller: " . $speller->errstr;
279 + $speller->create_speller ⎪⎪ die "Failed to create speller: " . $speller->errstr;
280
281 If you used a custom dictionary installed in non-standard location and
282 indexed the dictionary with Aspell/Pspell .pwli files you will need to
283 change how you access your dictionary (e.g. by setting the "master"
284 configuration setting with the path to the dictionary). See the GNU
285 Aspell documentation for details.
286
288 Probably.
289
291 This library is free software; you can redistribute it and/or modify it
292 under the same terms as Perl itself.
293
295 Bill Moseley moseley@hank.org.
296
297 This module is based on a perl module written by Doru Theodor Petrescu
298 <pdoru@kappa.ro>.
299
300 Aspell is written and maintained by Kevin Atkinson.
301
302 Please see:
303
304 http://aspell.net
305
306
307
308perl v5.8.8 2007-09-20 Aspell(3)