Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses


by jeffa (Bishop)
on Sep 13, 2000 at 08:22 UTC ( #32245=modulereview: print w/replies, xml ) Need Help??

Item Description: Plug ispell easily into an application

Review Synopsis: Lightweight CPAN module for spell checking

(module written by John Porter)

This module simply encapsulates the Unix utility ispell, a text file spell checker that was first written by Ralph E. Gorin in 1971.


The Good

The great thing about Lingua::Ispell is the ability to incorporate a fully functional spell checker into an application, great for finding spelling suggestions on any CGI search engine. You can specify a dictionary file for the module to use, on my system it defaulted to /usr/dict/words. You can also add words to the dictionary file and save it, all via the module's functions.

To use Lingua::Ispell, you simply use the module, and pass the text you wish to spell checker to the spellcheck function. This function will break the text up into words and return a list of hashes containing at least two keys, term and type. Term is the word being checked and type is one of six types the module uses to identify different possibilities. Out of these six types, I found only 2 of them to be usefull: miss and none. If the type is none, then the term is not found in the dictionary file and no corrections were found. If the type is miss, then an additional array that contains spelling corrections for that term is made available, named misses.

Example from the perldoc:

use Lingua::Ispell qw(spellcheck); for my $r ( spellcheck( "ys we ave no banans" ) ) { if ( $r->{'type'} eq 'miss' ) { print "'$r->{'term'}' was not found in the dictionary;\n"; print "Near misses: @{$r->{'misses'}}\n"; } elsif ( $r->{'type'} eq 'none' ) { print "No match for term '$r->{'term'}'\n"; } }

The Bad

You have to specify where the ispell executable is located - not really bad, but it does affect the portability of you application - so keep this in mind when you have to move your app to another computer.
$Lingua::Ispell::path = '/usr/bin/ispell';
As a matter of fact, you will find that the above example probably will not work on your box without this line.

The Not So Ugly Code

Here is an example of Lingua::Ispell in action - a CGI form that allows the user to enter some text for spell checking. Remember to specify the location of ispell on your box.
#!/usr/bin/perl use strict; $|++; use Lingua::Ispell qw(:all); use CGI qw(standard); # override the default path - Your Mileage May Vary $Lingua::Ispell::path = '/usr/bin/ispell'; my $cgi = new CGI; print $cgi->header; if (my $query = $cgi->param('query')) { &print_form; &print_corrections($query); } else { &print_form; } #thanks to Randall for this slick dereferencing trick sub print_form { print <<_FORM_; <H1 align=center>Spell Checker</H1> <HR align=center> @{[$cgi->startform('POST',$cgi->script_name)]} <P> Enter Text To Spell Check: @{[$cgi->textfield('query')]} @{[$cgi->submit('Go')]} @{[$cgi->endform]} <P> <HR> _FORM_ } sub print_corrections($) { my $query = shift; print "Results for <i>'$query'</i> :<p>"; for my $result (spellcheck($query)) { my $term = "<font color=red>$result->{'term'}</font>"; if ($result->{'type'} eq 'miss') { print "'$term' was not found in the dictionary,<br>\n"; print "<u>Near misses</u>: "; print join(',', @{$result->{'misses'}}), "<p>\n"; } elsif ($result->{'type'} eq 'none') { print "No match for term '$term'.<p>\n"; } } }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: modulereview [id://32245]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2017-09-24 23:20 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (275 votes). Check out past polls.