Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Item Description: Plug ispell easily into an application
Review Synopsis: Lightweight CPAN module for spell checking
(module written by JDPORTER)
This module simply encapsulates the Unix utility ispell, a text file spell checker that was first written by Ralph E. Gorin in 1971.
.
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"; } }
As a matter of fact, you will find that the above example probably will not work on your box without this line.$Lingua::Ispell::path = '/usr/bin/ispell';
#!/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"; } } }
|
---|