1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| use diagnostics;
use warnings;
my ($file_dictionary, $word, $line, $line1, $alph, $elt, $w, $transposition, $letter1, $letter2);
my (@word, @altered_word, @filedictionary, @filetext, @dictionary, @addition, @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";
}
}
}
} |
Partager