PERL Word Suggestion function 11 Lines of Code

sub {
  use strict;
  use CGI qw(:standard);
  my $keyword = shift;
  my $args = "aspell -a";
  my $returntext = `echo $keyword | $args`;
  my @suggestions = split("\n", $returntext);
  my @suggestion_list = split(":", $suggestions[1]);
  my @individual_keywords = split(", ", $suggestion_list[1]);
  return $suggestion_list[1];
}#end sub

 

 

Here ya go. This was killin me for the longest. We have a client who has a search page where they want to do auto suggestions for misspelled words similar to how Google Spell does it. Well, apache has a built in module that you can use (Aspell). The snipped above is all you need to get the most relevant spelling for a misspelled word (according to apache).

You can add into this function database calls to check a list of keywords that you may have stored and only return those (which is what I'm going to do with it).

I thought this was too creative to be true but it's possible.

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
Share

 

 

Blogs

LinzSloan.Net
Modeling profile of Lindsay Sloan... a Diva

My Two Cents
Lori Oswald's Blog
Lori Oswald's Blog

AdamNutting.Com
Learn some more about the NetNutt

Jay Adkins Online Blog
Father, Husband... GEEK

Patrick Thurmond's Blog
Thoughs, rants and influential creations

Business Links

New Millennium Tecknology
Custom Website Design & Development

Teck Interactive Media
Online marketing and advertising

Other:

Senior Year Basketball Stats
(Just for the records so we can remember)

Donovan Thompson - Park University Alumni 2006

June 2010 Posts

Clearing All Favicons in Firefox
LDAP Integration w/Drupal 5.x

March 2010 Posts

More Grief About CMSs

December 2009 Posts

DVD Codec Pack
PERL Word Suggestion function 11 Lines of Code
Ajax/JQuery File Upload
Ajax Star Rating Script