Jurafsky has asked for the wisdom of the Perl Monks concerning the following question:
I've created a little spellchecker.
This script works in this way:
After reading each line of the text, realizes some corrections
thanks to a comparison between a dictionary and the text itself.
When it finds a word that doesn't exist in the dictionary, it corrects the words
(giving one or more suggestions) and pushes it into an array.
Here there's my problem:
I would like to give to the user the possibility to choose the correct word among the words suggested. Something like this:
We found the word "wlak" in your text which isn't correct.
The suggested possibilities are:
type the number associated to the word or 0 if you can't find the correct word.
Then I would like to replace the correct word on the original text (creating a new .txt).
How can I do this?
https://www.dropbox.com/s/t9fc2dk5mqbsb20/texte.txtuse diagnostics; use warnings; my ($file_dictionary, $word, $line, $line1, $alph, $elt, $w, $transpos +ition, $letter1, $letter2); my (@word, @altered_word, @filedictionary, @filetext, @dictionary, @ad +dition, @replacement, @transposition, @removal); $file_dictionary = "lexique.txt"; $file_text = "texte.txt"; #I create an array for the dictionary open (L, "<", $file_dictionary); while (defined( $line1 = <L>)) { chomp($line1); @filedictionary = split (/\s/, $line1); push (@dictionary, @filedictionary); } #I create an array for the text open (T, "<", $file_text); while (defined( $line = <T>)) { chomp($line); @filetext = split (/(\s|\pP)/, $line); for ($i = 0; $i < @filetext; $i++) { if (!grep(/^$filetext[$i]$/, @dictionary)) { push (@word, $filetext[$i]); } } } #then I create an array for each word foreach $w(@word) { @altered_word = split (//, $w); #I create an array for the dictionary open (L, "<", $file_dictionary); while (defined( $line1 = <L>)) { chomp($line1); @filedictionary = split (/\s/, $line1); push (@dictionary, @filedictionary); } #first operation --> "palrer" will be "parler" for (my $i=0; $i < $#altered_word ; $i++) { @transposition = @altered_word; $letter1 = $transposition[$i]; $letter2 = $transposition[$i+1]; $transposition[$i] = $letter2; $transposition[$i+1] = $letter1; $transposition = join "", @transposition; if (grep(/^$transposition$/, @dictionary)) { print "post transposition : $transposition\n"; } } foreach $elt (0 .. $#altered_word) { #second operation --> parller will be parler @removal = @altered_word; splice(@removal, $elt, 1); $removal = join "", @removal; if (grep(/^$removal$/, @dictionary)) { print "post enlevement : $removal\n"; } #third operation --> parer will be parler foreach $alph('a' .. 'z') { @addition = @altered_word; splice(@addition, $elt, 0, $alph); $addition = join "", @addition; if (grep(/^$addition$/, @dictionary)) { print "post addition : $addition\n"; } #last operation : mancer will be manger @replacement = @altered_word; splice(@replacement, $elt, 1, $alph); $replacement = join "", @replacement; if (grep(/^$replacement$/, @dictionary)) { print "post replacement : $replacement\n"; } } } }
this is the text
https://www.dropbox.com/s/717rczou0mkrp0s/lexique.txt"
this is the French Dictionary
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: problem spellchecker
by Anonymous Monk on Nov 18, 2012 at 12:13 UTC | |
Re: problem spellchecker
by linuxkid (Sexton) on Nov 19, 2012 at 15:42 UTC | |
by Anonymous Monk on Nov 19, 2012 at 16:29 UTC | |
by linuxkid (Sexton) on Nov 20, 2012 at 01:44 UTC | |
by Anonymous Monk on Nov 20, 2012 at 07:30 UTC | |
by linuxkid (Sexton) on Nov 20, 2012 at 16:46 UTC | |
| |
Re: problem spellchecker
by Anonymous Monk on Nov 30, 2012 at 10:30 UTC | |
by Anonymous Monk on Nov 30, 2012 at 12:08 UTC | |
by Anonymous Monk on Dec 01, 2012 at 13:05 UTC | |
by Anonymous Monk on Nov 30, 2012 at 11:52 UTC |
Back to
Seekers of Perl Wisdom