Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Spell Check Logic

by arashi (Priest)
on Jul 31, 2002 at 19:12 UTC ( #186563=perlquestion: print w/ replies, xml ) Need Help??
arashi has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

The problem: We need to have a web form for content management have access to a spell checker.

What we can currently do: We have a word list of about 175,000 words that we can compare to, however we lack the ability to offer suggestions for misspelled words.

What we can not do: Unfortunately we can't get ISpell working on our server (see Ispell path for W2K) otherwise we would be using that.

What we're looking for: Any good resources/help on the logic behind spell checking with the ability to suggest words. If you have any ideas we would appreciate it.

Thank you in advance for your help.
Arashi

I'm sure Edison turned himself a lot of colors before he invented the lightbulb. - H.S.

edited: Wed Jul 31 20:44:36 2002 by jeffa - replaced <a href=""> with [id://]

Comment on Spell Check Logic
Re: Spell Check Logic
by dree (Monsignor) on Jul 31, 2002 at 20:14 UTC
    First of all you have to build the set of suggetions.

    For this purpose, you have to avoid string distance as Hamming, Text::Levenshtein or Text::WagnerFischer (but you can use them later)
    Instead you can use a phonetic algorithm like Text::Soundex (that you should find along with Perl), Text::Metaphone and Text::DoubleMetaphone.
    They transform a word in a phonetic code that you can use to retrive suggested words:

    hello->(phonetic alg.)->h2l3
    hullo->(phonetic alg.)->h2l3 (note: same code for phonetic similarity)
    harry->(phonetic alg.)->h549

    So you can made an hash on disk (e.g. with DB_File) with phonetic codes as keys and as values the words that have the same phonetic code:

    {h213}->hello,hullo
    {h549}->harry

    and so on.

    After the hash (that you have built before running the spell checker), you parse the text; you have to:

    1) isolate the word
    2) calculate the phonetic code of the word.
    3) retrive the suggestions for this word (i.e. words that have the same phonetic code)
    4) see if the word is in the set of suggestions
    4a) if yes, parse another word (i.e. the word is in my corpus)
    4b) if not, prompt the user THIS set of suggetions
    4ba) now you can use the distance string algorithms to sort the suggested words (e.g. with Levensthein).

    To retrive the words you can do for example:
    my $sentence="worda wordb, wordc"; my @sentence = split(//, $sentence); my $current_word; foreach my $s (0..$#sentence) { if ($sentence[$s] =~ /($\W|_|\d)/) { # process current word $current_word='' ; } else { $current_word.= $sentence[$s]; } }
      you could speed this up a bit by maintaining a list of the words themselves in a separate hash, and checking that before you calculate the phonetic code (step 2). that way, you only have to calculate the code for words that are not in the list. of course, that only helps if most words are spelled correctly :)
        You are right! :)

        But an hash (DB_File) with 2/3 millions words+phonetic codes is around 150+Mb.
        So to gain some speed-up you have to twice the database: one that has keys as phonetic codes and the second that has keys as correct words.
        And this is not always a good thing.

        But in this particulary case, with only 100.000+ words, your suggestion is ok :)
Re: Spell Check Logic
by hossman (Prior) on Aug 01, 2002 at 00:01 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://186563]
Approved by Mission
Front-paged by gmax
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (15)
As of 2014-11-21 16:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (114 votes), past polls