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 = )) { 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 = )) { 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 of 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? Type the number or type 'exit'.\n"; $user = ; chomp ($user); if ("$user" eq 'exit') { print "Next word.\n"; } else { $line2 =~ s/$word/$correct[$user]/; } } } print T2 "$line2 \n"; } close(T); close(T2);