1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
package Text::Aspell;
# $Id: Aspell.pm,v 1.7 2002/08/29 20:28:00 moseley Exp $
require DynaLoader;
use vars qw/ @ISA $VERSION /;
@ISA = 'DynaLoader';
$VERSION = '0.04';
bootstrap Text::Aspell $VERSION;
# Preloaded methods go here.
1;
__END__
=head1 NAME
Text::Aspell - Perl interface to the GNU Aspell library
=head1 SYNOPSIS
use Text::Aspell;
my $speller = Text::Aspell->new;
die unless $speller;
# Set some options
$speller->set_option('lang','en_US');
$speller->set_option('sug-mode','fast');
# check a word
print $speller->check( $word )
? "$word found\n"
: "$word not found!\n";
# lookup up words
my @suggestions = $speller->suggest( $misspelled );
# lookup config options
my $language = $speller->get_option('lang');
print $speller->errstr unless defined $language;
# fetch a config item that is a list
my @sgml_extensions = $speller->get_option_as_list('sgml-extension');
# fetch the configuration keys and their default settings
my $options = $speller->fetch_option_keys;
# or dump config settings to STDOUT
$speller->print_config || $speller->errstr;
# What dictionaries are installed as simple strings
my @dicts = $speller->list_dictionaries;
# or as an array of hashes
@dicts = $speller->dictionary_info;
print Data::Dumper::Dumper( \@dicts );
Here's an example how to create and use your own word list
Create a dictionary:
$ aspell --lang=en create master ./dictionary.local < space_separated_word_list
Then in your code:
use Text::Aspell;
my $speller = Text::Aspell->new;
die unless $speller;
$speller->set_option('master','./dictionary.local');
# check a word
print $speller->check( $word )
? "$word found\n"
: "$word not found!\n";
=head1 DESCRIPTION
This module provides a Perl interface to the GNU Aspell library.
The GNU Aspell library provides access to system spelling libraries, including a
spell checker.
This module is to meet the need of looking up many
words, one at a time, in a single session.
This is a Perl xs interface which should provide good performance compared
to forking the aspell program for every word.
For example, a tiny run of about 400 word lookups resulted in:
aspell-fast: 18.44 seconds
pspell-fast: 0.63 seconds
Where aspell-fast was forking (about 4 words per fork) and pspell-fast was using this
module[1] ("fast" refers to the spelling mode used).
The GNU C interface is described at:
http://savannah.gnu.org/download/aspell/manual/user/6_Writing.html
It's worth looking over the way config and speller (manager) objects are created when using
the Aspell C API as some of that is hidden in the Text::Aspell module. In this discussion
the term "speller object" means the internal Aspell object, not the value returned from
Text::Aspell->new.
For example, with Text::Aspell you do not have to explicitly create a speller object.
The speller (manager) object is created automatically the first time you call suggest() or
check().
Note also that once the speller object is created some (all?) config options cannot be
changed. For example, setting configuration options such as "lang" are what determine what
dictionary Aspell will use. Once the speller object is created that dictionary will be
used. I.e. setting "lang" after the speller object is created will have no effect.
[1] Actually, running Text::Pspell which was the predecessor to this module.
=head1 DEPENDENCIES
You must have installed GNU Aspell version 0.50.1 or higher on your system.
Download from:
Aspell: http://aspell.net
=head1 METHODS
The following methods are available:
=over 4
=item $speller = Text::Aspell->new;
Creates a new speller object. New does not take any parameters (future version
may allow options set by passing in a hash reference of options and value pairs).
Returns C<undef> if the object could not be created, which is unlikely.
Internally, new() creates an object to store Aspell structures (AspellConfig,
AspellSpeller, and a space for an error string and then calls new_aspell_config();
=item $speller->set_option($option_name, $value);
Sets the configuration option C<$option_name> to the value of C<$value>.
Returns C<undef> on error, and the error message can be printed with $speller->errstr.
You should set configuration options before calling the $speller->create_speller
method. See the GNU Aspell documentation for the available configuration settings
and how (and when) they may be used.
=item $speller->remove_option($option_name);
Removes (sets to the default value) the configuration option specified by C<$option_name>.
Returns C<undef> on error, and the error message can be printed with $speller->errstr.
You may only set configuration options before calling the $speller->create_speller
method.
=item $string = $speller->get_option($option_name);
Returns the current setting for the given configuration option. The values are strings.
For configuration options that are lists used the C<get_option_as_list()> method.
Returns C<undef> on error, and the error message can be printed with $speller->errstr.
Note that this may return different results depending on if it's called before or after
$speller->create_speller is called.
=item @list = $speller->get_option_as_list($option_name);
Returns an array of list items for the given option. Use this method to fetch configuration
values that are of type I<list>.
Returns C<undef> on error, and the error message can be printed with $speller->errstr.
Note that this may return different results depending on if it's called before or after
$speller->create_speller is called.
=item $options = $speller->fetch_option_keys;
Returns a hash of hashes. The keys are the possible configuration options
and the values is a hash with keys of:
desc : A short description of the option
default : The default value for this option
type : The data type of option (see aspell.h)
=item $speller->print_config;
Prints the current configuration to STDOUT. Useful for debugging.
Note that this will return different results depending on if it's called before or after
$speller->create_speller is called.
=item $speller->errstr;
Returns the error string from the last error. Check the previous call for an C<undef> return
value before calling this method
=item $errnum = $speller->errnum;
Returns the error number from the last error. Some errors may only set the
error string ($speller->errstr) on errors, so it's best to check use the errstr method
over this method.
=item $found = $speller->check($word);
Checks if a word is found in the dictionary. Returns true if the word is found
in the dictionary, false but defined if the word is not in the dictionary.
Returns C<undef> on error, and the error message can be printed with $speller->errstr.
This calls $speller->create_speller if the speller has not been created by an
explicit call to $speller->create_speller.
=item @suggestions = $speller->suggest($word)
Returns an array of word suggestions for the specified word. The words are returned
with the best guesses at the start of the list.
=item $speller->create_speller;
This method is normally not called by your program.
It is called automatically the first time $speller->check() or
$speller->suggest() is called to create a spelling "speller".
You might want to call this when your program first starts up to make the first
access a bit faster, or if you need to read back configuration settings before
looking up words.
The creation of the speller builds a configuration
profile in the speller structure. Results from calling print_config() and get_option() will
change after calling create_speller(). In general, it's best to read config settings back
after calling create_speller() or after calling spell() or suggest().
Returns C<undef> on error, and the error message can be printed with $speller->errstr.
=item $speller->add_to_session($word)
=item $speller->add_to_personal($word)
Adds a word to the session or personal word lists.
Words added will be offered as suggestions.
=item $speller->store_replacement($word, $replacement);
This method can be used to instruct the speller which word you used as a replacement
for a misspelled word. This allows the speller to offer up the replacement next time
the word is misspelled. See section 6.3 of the GNU Aspell documentation for a better description.
=item $speller->save_all_word_lists;
Writes any pending word lists to disk.
=item $speller->clear_session;
Clears the current session word list.
=item @dicts = $speller->list_dictionaries;
This returns an array of installed dictionary files. Each is a single string
formatted as:
[name]:[code]:[jargon]:[size]:[module]
Name and code will often be the same, but
name is the complete name of the dictionary which can be used to directly
select a dictionary, and code is the language/region code only.
=item $array_ref = $speller->$speller->dictionary_info;
Like the C<list_dictionaries()> method, this method returns an array of
hash references. For example, an entry for a dictionary might have the
following hash reference:
{
'module' => 'default',
'code' => 'en_US',
'size' => 60,
'jargon' => 'w-accents',
'name' => 'en_US-w-accents'
},
Not all hash keys will be available for every dictionary
(e.g. the dictionary may not have a "jargon" key).
=back
=head1 Upgrading from Text::Pspell
Text::Aspell works with GNU Aspell and is a replacement for the
module Text::Pspell. Text::Pspell is no longer supported.
Upgrading should be a simple process. Only one method name has changed:
C<create_manager> is now called C<create_speller>.
Code designed to use the old Text::Pspell module may not even call the
C<create_manager> method so this may not be an issue.
The C<language_tag> configuration setting is now called C<lang>.
Diffs for code that uses Text::Pspell might look like:
- use Text::Pspell;
+ use Text::Aspell;
- $speller = Text::Pspell->new;
+ $speller = Text::Aspell->new;
- $speller->create_manager || die "Failed to create speller: " . $speller->errstr;
+ $speller->create_speller || die "Failed to create speller: " . $speller->errstr;
If you used a custom dictionary installed in non-standard location and indexed the dictionary with
Aspell/Pspell .pwli files you will need to change how you access your dictionary (e.g.
by setting the "master" configuration setting with the path to the dictionary).
See the GNU Aspell documentation for details.
=head1 BUGS
Probably.
=head1 COPYRIGHT
This library is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.
=head1 AUTHOR
Bill Moseley moseley@hank.org.
This module is based on a perl module written by Doru Theodor Petrescu <pdoru@kappa.ro>.
Aspell is written and maintained by Kevin Atkinson.
Please see:
http://aspell.net
=cut
|