Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

(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.


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"; } } }

In reply to Lingua::Ispell by jeffa

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2023-06-05 07:24 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (23 votes). Check out past polls.