Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: problem spellchecker

by Anonymous Monk
on Nov 30, 2012 at 10:30 UTC ( #1006451=note: print w/ replies, xml ) Need Help??


in reply to problem spellchecker

The problem is that is an exercice and I can't use subroutines or modules.
Now it works but I've just one problem: the script reads several time each line of the Input text.. Why? How can I resolve this problem?

use diagnostics; use warnings; my ($word, $file_dict, $txt, $line, $line2, $i); my ($first_letter, $second_letter, $letter, $alphabet); my ($user, $exchange, $transposition, $removal, $addition); my (@text, @words, @dictionary, @dict, @single_letters); my (@transposition, @removal, @addiction, @exchange, @correct); $file_dict = "dict.txt"; $txt = "txt.txt"; $new_txt = "output.txt"; #open the dictionary and save it in an array open (D, "<", $file_dict); while(defined($line = <D>)) { chomp($line); @dict = split(/\s/, $line); push (@dictionary, @dict); } close (D); #open the file for output open (T2, ">", $new_txt); #open and save the text open (T, "+<", $txt); while(defined($line2 = <T>)) { chomp($line2); @text = split (/ /, $line2); push (@words, @text); #foreach word of the text, I reset the array of correct words #then I verify if the word is in the dictionary #if it isn't there, I split the word in letters #then I apply the correction that will be saved in @correct array +. foreach $word(@words){ @correct = "exit"; if (!grep(/^$word$/, @dictionary)) { print "Word : '$word' isn't in the dictionary.\n"; @single_letters = split (//, $word); #transposition for (my $i = 0; $i < $#single_letters; $i++) { @transposition = @single_letters; $first_letter = $transposition[$i]; $second_letter = $transposition[$i+1]; $transposition[$i] = $second_letter; $transposition[$i+1] = $first_letter; $transposition = join "", @transposition; if (grep(/^$transposition$/, @dictionary)) { push (@correct, $transposition); } } #removal foreach $lettera ( 0 .. $#single_letters) { @removal = @single_letters; splice (@removal, $lettera, 1); $removal = join "", @removal; if (grep(/^$removal$/, @dictionary)) { push (@correct, $removal); } #addition foreach $alphabet ( 'a' .. 'z') { @addition = @single_letters; splice (@addition, $lettera, 0, $alphabet); + $addition = join "", @addition; if (grep(/^$addition$/, @dictionary)) { push (@correct, $addition); } #exchange @exchange = @single_letters; splice(@exchange, $lettera, 1, $alphabet); $exchange = join "", @exchange; if (grep(/^$exchange$/, @dictionary)) { push (@correct, $exchange); } } } #now I display the solutions and user can choose one o +f them print "These are the correction of word $word\n"; for (my $c = 0; $c < @correct; $c++) { print "$c. : $correct[$c]\n"; } print "Are you interested on one of this solution? Typ +e the number or type 'exit'.\n"; $user = <STDIN>; chomp ($user); if ("$user" eq 'exit') { print "Next word.\n"; } else { $line2 =~ s/$word/$correct[$user]/; } } } print T2 "$line2 \n"; } close(T); close(T2);


Comment on Re: problem spellchecker
Download Code
Re^2: problem spellchecker
by Anonymous Monk on Nov 30, 2012 at 11:52 UTC
    Ok, got it. What do you think about this solution?
    #now I display the solutions and user can choose one o +f them print "These are the correction of word $word\n"; for (my $c = 0; $c < @correct; $c++) { print "$c. : $correct[$c]\n"; } print "Are you interested on one of this solution? Typ +e the number or type 'exit'.\n"; $user = <STDIN>; chomp ($user); if ("$user" eq 'exit') { print "Next word.\n"; } else { $line2 =~ s/$word/$correct[$user]/; $word = $error; @words = grep { $_ ne $error } @words; } } } print T2 "$line2 \n"; }
Re^2: problem spellchecker
by Anonymous Monk on Nov 30, 2012 at 12:08 UTC

    The problem is that is an exercice and I can't use subroutines or modules.

    That sounds unbelievable -- sure modules, if part of the exercise is reinventing this particular wheel, but subroutines? the fundamental unit of reusable code? I'd ask for my money back.

      LoL however.. I got it in this way. I think the problem was the first foreach loop..

      else { $line2 =~ s/$word/$correct[$user]/; $word = $error; @words = grep { $_ ne $error } @words; }

      I made a substitution in line, then I copied $word in another scalar, $error, and then I created again the @words array without $error !

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1006451]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2014-09-18 06:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (108 votes), past polls