Commit ffa95c23f45b04b01e90a92e603d3a06a129187b

Authored by grouille
1 parent 0e62ceb1

Version finale sans commentaire avec correcteur orthographique opérationnel

Showing 5 changed files with 254 additions and 92 deletions   Show diff stats
1   -aspirateur chien. chienne, banane.banane
2   -Doucement, Etrange. Odorat; odorant;
3   -L'ile, L'ocean; aaee''ee
4   -chiens
5   -chienne
6   -chie
7   -Doucement.
  1 +aspirateur. Doucement, bien;
  2 +chien
  3 +chien. Doucament.
  4 +chien
... ...
main 0 → 100755
No preview for this file type
... ... @@ -11,6 +11,7 @@ int main(int argc, char *argv[])
11 11 init_pgrm();
12 12 Node tree = NULL;
13 13 int error = 0;
  14 + int correct = 0;
14 15 FILE* fp_lib;
15 16 FILE* fp_txt;
16 17  
... ... @@ -21,7 +22,7 @@ int main(int argc, char *argv[])
21 22 }
22 23  
23 24 fp_lib = fopen(argv[argc-2], "r");
24   - fp_txt = fopen(argv[argc-1], "r");
  25 + fp_txt = fopen(argv[argc-1], "r+");
25 26  
26 27 if(fp_lib==NULL || fp_txt==NULL)
27 28 {
... ... @@ -31,17 +32,18 @@ int main(int argc, char *argv[])
31 32  
32 33 init_tree(&tree);
33 34 read_lib(fp_lib, &tree);
34   - read_txt(fp_txt, &tree, &error, fp_txt);
  35 + read_txt(fp_txt, &tree, &error, &correct);
35 36  
36   - // printf("%p\n", tree);
37   -
38   - //print_first(tree);
39   - //printf("\n");
40   - //print_tree(tree, 0);
  37 + // Si vous souhaitez imprimer l'arbre indexé complet :
  38 + //char word[MAX] = "";
  39 + //print_tree(tree, 0, word);
41 40  
42 41 if(error<2) printf("Dans le texte %s, %d mot n'est pas dans le dictionnaire %s.\n", argv[argc-1], error, argv[argc-2]);
43 42 else printf("Dans le texte %s, %d mots ne sont pas dans le dictionnaire %s.\n", argv[argc-1], error, argv[argc-2]);
44 43  
  44 + if(correct<2) printf("%d a été corrigé.\n", correct);
  45 + else printf("%d ont été corrigés.\n", correct);
  46 +
45 47 free_tree(&tree);
46 48 fclose(fp_lib);
47 49 fclose(fp_txt);
... ...
... ... @@ -27,7 +27,7 @@ void init_tree(Node* Tree)
27 27 if(is_empty_tree(*Tree))
28 28 {
29 29 *Tree = malloc(sizeof(node));
30   - (*Tree)->letter = '?'; // caractère choisi arbitrairement
  30 + (*Tree)->letter = 7; //
31 31 (* Tree)->endWord = false;
32 32 for(int i=0; i<NB_CARAC; i++)
33 33 (*Tree)->next[i] = NULL; // initialisation du tableau 'next' à un tableau de pointeurs NULL
... ... @@ -71,9 +71,10 @@ void add_in_tree(Node Tree, char word[])
71 71 {
72 72 new->next[i]=NULL;
73 73 }
  74 + new->endWord = false;
74 75 Tree2->next[ind] = new; // on fait pointé le tableau du caractère précédent vers cette cellule (vers ce caractère)
75   - /*if(!(Tree2->endWord)) // si le caractère n'est pas un caractère de fin, on le met à 'false' -> UTILITE ?
76   - Tree2->endWord = false;*/
  76 + if(!(Tree2->endWord)) // si le caractère n'est pas un caractère de fin, on le met à 'false' -> UTILITE ?
  77 + Tree2->endWord = false;
77 78 Tree2=Tree2->next[ind]; // on se place au niveau du caractère suivant dans l'arbre indexé
78 79 /*printf("%c %d\t", letter, ind);
79 80 printf("okB %d\n", j);*/
... ... @@ -102,7 +103,7 @@ char max_index(char word[])
102 103 return index;
103 104 }
104 105  
105   -void scan_word(Node Tree, char word[], int* error, FILE* fp_txt) // si un mot démarre juste après un caractère de fin, la fonction ne lit pas les mots séparément
  106 +void scan_word(Node Tree, char word[], int* error, FILE* fp_txt, int* correct) // si un mot démarre juste après un caractère de fin, la fonction ne lit pas les mots séparément
106 107 {
107 108 bool endWord = false;
108 109 bool stop = false;
... ... @@ -124,7 +125,7 @@ void scan_word(Node Tree, char word[], int* error, FILE* fp_txt) // si un mot dÃ
124 125 }
125 126 else
126 127 {
127   - add_error(error, word, ind, Tree, fp_txt);
  128 + add_error(error, word, ind, Tree2, fp_txt, correct);
128 129 //printf("%d\n", ind);
129 130 ind = max_index(word);
130 131 //printf("%d\n", ind);
... ... @@ -134,28 +135,134 @@ void scan_word(Node Tree, char word[], int* error, FILE* fp_txt) // si un mot dÃ
134 135 }
135 136 if(!endWord && !stop)
136 137 {
137   - add_error(error, word, ind, Tree, fp_txt);
  138 + add_error(error, word, ind, Tree2, fp_txt, correct);
138 139 }
139 140 }
140 141  
141   -void add_error(int* error, char word[], int index, Node Tree, FILE* fp_txt)
  142 +void add_error(int* error, char word[], int index, Node Tree, FILE* fp_txt, int* correct)
142 143 {
143 144 (*error)++;
144 145 if(index==0)
145 146 {
146   - printf("Le mot %s ne correspond à aucun mot du dictionnaire.\n\n", word);
  147 + printf("Le mot %s ne correspond à aucun mot du dictionnaire.\n", word);
147 148 return;
148 149 }
149 150 else if(index<strlen(word))
150   - printf("Il y a une erreur dans le mot '%s', au caractère %c d'incide %d.\n\n", word, word[index], index);
  151 + printf("Il y a une erreur dans le mot '%s', au caractère %c d'incide %d.\n", word, word[index], index);
151 152 else
152   - printf("Il manque au moins une lettre au mot '%s'.\n\n", word);
153   - make_correction(word, index, Tree, fp_txt);
  153 + printf("Il manque au moins une lettre au mot '%s'.\n", word);
  154 + make_correction(word, index, Tree, fp_txt, correct);
154 155 }
155 156  
156   -void make_correction(char word[], int index, Node Tree, FILE* fp_txt)
  157 +void make_correction(char word[], int index, Node Tree, FILE* fp_txt, int* correct) // gestion des caractères de fin
157 158 {
158   - return;
  159 + correction liste = NULL;
  160 + init_correction(&liste);
  161 + char end;
  162 + bool testEnd;
  163 + testEnd = detect_end_caract(&end, word);
  164 + int longueur = strlen(word);
  165 + Node Tree2 = Tree;
  166 + word[index-1]='\0';
  167 + printf("\n");
  168 + char words[MAX] = "";
  169 + make_tree_correct(Tree2, 0, words, word, liste);
  170 + choice_word(liste, fp_txt, end, longueur, correct, testEnd);
  171 + free(liste); // VALGRIND ???
  172 +}
  173 +
  174 +bool detect_end_caract(char* end, char word[])
  175 +{
  176 + if(is_end_caract(word[strlen(word)-1]))
  177 + {
  178 + *end = word[strlen(word)-1];
  179 + return true;
  180 + }
  181 + return false;
  182 +}
  183 +
  184 +void choice_word(correction liste, FILE* fp_txt, char end, int longueur, int* correct, bool testEnd)
  185 +{
  186 + int choix;
  187 + printf("Voici les mots possibles pour corriger ce mot :\n");
  188 + for(int i=0; i<liste->dernier+1; i++)
  189 + printf("%d. %s\n", i+1, liste->mots[i]);
  190 + printf("\n");
  191 + choix = -1;
  192 + printf("Quel mot voulez-vous sélectionner ? (Entrez %d pour ne pas corriger le mot)\n", (liste->dernier)+2);
  193 + while(choix<1 || choix>liste->dernier+2)
  194 + {
  195 + scanf("%d", &choix);
  196 + }
  197 + if(choix==(liste->dernier)+2)
  198 + {
  199 + printf("\n");
  200 + return;
  201 + }
  202 + (*correct)++;
  203 + char word[MAX];
  204 + strcpy(word, liste->mots[choix-1]);
  205 + printf("Le mot %s a été sélectionné.\n\n", word);
  206 + if(testEnd) add_caract(word, end);
  207 + correct_word(word, fp_txt, longueur);
  208 +}
  209 +
  210 +void correct_word(char word[], FILE* fp_txt, int longueur)
  211 +{
  212 + FILE* fp_tamp;
  213 + fp_tamp = fopen("tampon.txt", "w+");
  214 + if(fp_tamp==NULL)
  215 + {
  216 + printf("Erreur lors de l'ouverture du fichier tampon.\n");
  217 + return;
  218 + }
  219 + int position = ftell(fp_txt)-longueur;
  220 + rewind(fp_txt);
  221 + char tempo;
  222 + while(ftell(fp_tamp)<position)
  223 + {
  224 + tempo = fgetc(fp_txt);
  225 + if(tempo==EOF) break;
  226 + if(tempo=='\n');
  227 + fputc(tempo, fp_tamp);
  228 + }
  229 + fprintf(fp_tamp, "%s", word);
  230 + long newPosition = ftell(fp_tamp);
  231 + fseek(fp_txt, ftell(fp_txt)+longueur, SEEK_SET);
  232 + while(1)
  233 + {
  234 + tempo = fgetc(fp_txt);
  235 + if(tempo==EOF) break;
  236 + if(tempo=='\n');
  237 + fputc(tempo, fp_tamp);
  238 + }
  239 + rewind(fp_txt);
  240 + rewind(fp_tamp);
  241 + char temp[MAX_READ];
  242 + while(1)
  243 + {
  244 + if(fgets(temp, MAX_READ, fp_tamp)==NULL)
  245 + break;
  246 + fputs(temp, fp_txt);
  247 + }
  248 + fseek(fp_txt, newPosition, SEEK_SET);
  249 + fclose(fp_tamp);
  250 + remove("tampon.txt");
  251 +}
  252 +
  253 +void init_correction(correction* liste)
  254 +{
  255 + if((*liste)!=NULL)
  256 + {
  257 + printf("Déjà initialisé.\n");
  258 + return;
  259 + }
  260 + *liste = malloc(sizeof(struct liste));
  261 + (*liste)->dernier = -1;
  262 + for(int i=0; i<NB_MOT_CORRECTION; i++)
  263 + {
  264 + strcpy((*liste)->mots[i], "");
  265 + }
159 266 }
160 267  
161 268 void init_pgrm(void)
... ... @@ -174,7 +281,7 @@ bool no_accent(char word[])
174 281 return true;
175 282 }
176 283  
177   -void read_txt(FILE* fp, Node* Tree, int* error, FILE* fp_txt)
  284 +void read_txt(FILE* fp, Node* Tree, int* error, int* correct)
178 285 {
179 286 char word[MAX];
180 287 while(1)
... ... @@ -182,7 +289,7 @@ void read_txt(FILE* fp, Node* Tree, int* error, FILE* fp_txt)
182 289 if(fscanf(fp, "%s", word)!=1)
183 290 break;
184 291 if(no_accent(word)) // !!! MOTS AVEC ACCENTS NON PRIS EN COMPTE
185   - scan_word(*Tree, word, error, fp_txt);
  292 + scan_word(*Tree, word, error, fp, correct);
186 293 }
187 294 if(feof(fp)) printf("Fin de la lecture du fichier à scanner.\n\n");
188 295 if(ferror(fp)) printf("Erreur lors de la lecture du fichier à scanner.\n\n");
... ... @@ -204,54 +311,83 @@ void read_lib(FILE* fp, Node* Tree)
204 311 if(ferror(fp)) printf("Erreur lors de la lecture de la bibliothèque.\n\n");
205 312 }
206 313  
  314 +void print_tree(Node Tree, int index, char word[])
  315 +{
  316 + if(is_empty_tree(Tree))
  317 + return;
  318 + add_caract(word, Tree->letter);
  319 + if(is_leaf(Tree))
  320 + {
  321 + printf("%s\n", word);
  322 + supp_caract(word);
  323 + return;
  324 + }
  325 + if(Tree->endWord)
  326 + printf("%s\n", word);
  327 + while(index < NB_CARAC)
  328 + {
  329 + if(!is_empty_tree(Tree->next[index]))
  330 + {
  331 + print_tree(Tree->next[index], 0, word);
  332 + }
  333 + index++;
  334 + }
  335 + supp_caract(word);
  336 + return;
  337 +}
207 338  
208   -void print_tree(Node Tree, int index)
  339 +void make_tree_correct(Node Tree, int index, char word[], char start[], correction liste)
209 340 {
210   - if(is_empty_tree(Tree))
211   - return;
212   - Node cpTree = NULL;
213   - cpTree = Tree;
214   - if(cpTree->next[index]==NULL && index<26)
215   - {
216   - print_tree(cpTree, index+1);
217   - }
218   - else if(index == 26)
219   - {
220   - return;
221   - }
222   - else
223   - {
224   - printf("%c\n", (cpTree->next[index])->letter);
225   - if((cpTree->next[index])->endWord)
226   - {
227   - printf("fin\n");
228   - //print_tree(cpTree, 0);
229   - return;
230   - }
231   - print_tree(cpTree->next[index], 0);
232   - }
  341 + if(is_empty_tree(Tree))
  342 + return;
  343 + add_caract(word, Tree->letter);
  344 + if(is_leaf(Tree))
  345 + {
  346 + //printf("%s%s\n", start, word);
  347 + add_in_liste(liste, start, word);
  348 + supp_caract(word);
  349 + return;
  350 + }
  351 + if(Tree->endWord)
  352 + {
  353 + //printf("%s%s\n", start, word);
  354 + add_in_liste(liste, start, word);
  355 + }
  356 + while(index < NB_CARAC)
  357 + {
  358 + if(!is_empty_tree(Tree->next[index]))
  359 + {
  360 + make_tree_correct(Tree->next[index], 0, word, start, liste);
  361 + }
  362 + index++;
  363 + }
  364 + supp_caract(word);
  365 + return;
233 366 }
234 367  
235   -int find_index(Node tree)
  368 +void add_in_liste(correction liste, char start[], char word[])
236 369 {
237   - Node cpTree = tree;
238   - int index = 0;
239   - while(cpTree->next[index]==NULL && index < NB_CARAC)
240   - index++;
241   - return index;
  370 + char temp[MAX];
  371 + strcpy(temp, start);
  372 + strcat(temp, word);
  373 + if(liste->dernier<NB_MOT_CORRECTION-1)
  374 + {
  375 + (liste->dernier)++;
  376 + strcpy(liste->mots[liste->dernier], temp);
  377 + }
242 378 }
243 379  
244   -void print_first(Node Tree)
  380 +void add_caract(char word[], char caract)
245 381 {
246   - Node cpTree = Tree;
247   - int index = 0;
248   - while(!is_leaf(cpTree) || !(cpTree->endWord))
249   - {
250   - index = find_index(cpTree);
251   - printf("%c\n", (cpTree->next[index])->letter);
252   - if(cpTree->next[index]->endWord) printf("fin\n");
253   - cpTree=cpTree->next[index];
254   - }
  382 + char str[2];
  383 + str[0] = caract;
  384 + str[1] = '\0';
  385 + strcat(word, str);
  386 +}
  387 +
  388 +void supp_caract(char word[])
  389 +{
  390 + word[strlen(word)-1]='\0';
255 391 }
256 392  
257 393 void free_tree(Node* Tree)
... ...
... ... @@ -11,6 +11,8 @@
11 11 #include <string.h>
12 12 #define MAX 30 // taille maximale d'une chaîne lue dans un fichier
13 13 #define NB_CARAC 27 // nombre de caractères différents pouvant être identifiés -> 89 avec accentués
  14 +#define NB_MOT_CORRECTION 15 // nombre de mots pour la correction
  15 +#define MAX_READ 1000
14 16  
15 17 // Déclaration de la structure 'trie' ou 'arbre indexé', ainsi que des pointeurs associés
16 18 typedef struct node* Node;
... ... @@ -21,57 +23,82 @@ typedef struct node {
21 23 bool endWord;
22 24 }node;
23 25  
  26 +// Déclaration de la structure contenant les mots possible pour la correction
  27 +typedef struct liste* correction;
  28 +
  29 +typedef struct liste {
  30 + char mots[NB_MOT_CORRECTION][MAX];
  31 + int dernier;
  32 +}liste;
  33 +
24 34 // Fonction permettant de savoir si la structure est vide
25   -bool is_empty_tree(Node Tree);
  35 +bool is_empty_tree(Node);
26 36  
27 37 // Fonction permettant de savoir si le tableau 'next' est un tableau de pointeurs NULL
28   -bool is_leaf(Node Tree);
  38 +bool is_leaf(Node);
29 39  
30 40 // Initialisation de la structure accueillant le dictionnaire
31   -void init_tree(Node* Tree);
  41 +void init_tree(Node*);
32 42  
33 43 // Détermine l'indice de rangement dans le tableau 'next' du caractère 'letter'
34   -int find_caract_indice(char letter); // Ne fonctionne pas pour les caractères accentués
35   -
  44 +int find_caract_indice(char); // Ne fonctionne pas pour les caractères accentués
36 45  
37 46 // Fonction d'ajout d'un mot 'word' dans la structure 'tree' de type 'arbre indexé'
38   -void add_in_tree(Node Tree, char word[]);
  47 +void add_in_tree(Node, char*);
39 48  
40 49 // Fonction qui détermine si le caractère est un caractère de fin de mot (espace, ',', ';', '.', etc..)
41   -bool is_end_caract(char letter);
  50 +bool is_end_caract(char);
42 51  
43 52 // Renvoi l'indice maximum du mot 'word'
44   -char max_index(char word[]);
  53 +char max_index(char*);
45 54  
46 55 // Détermine si le mot 'word' est présent dans l'arbre indexé
47   -void scan_word(Node Tree, char word[], int* error, FILE*);
  56 +void scan_word(Node, char*, int*, FILE*, int*);
48 57  
49 58 // Retourne 'true' si le mot 'word' est non accentué, 'false' sinon
50   -bool no_accent(char word[]);
  59 +bool no_accent(char*);
51 60  
52 61 // Transmet les mots du texte à analyser à 'scan_word'
53   -void read_txt(FILE* fp, Node* Tree, int* error, FILE*);
  62 +void read_txt(FILE*, Node*, int*, int*);
54 63  
55 64 // Transmet les mots de la biliothèque à 'add_in_tree'
56   -void read_lib(FILE* fp, Node* Tree);
57   -
58   -// A SUPPRIMER
59   -void print_tree(Node Tree, int index);
  65 +void read_lib(FILE*, Node*);
60 66  
61   -// A SUPPRIMER
62   -int find_index(Node tree);
63   -
64   -// A SUPPRIMER
65   -void print_first(Node Tree);
  67 +// Affiche l'arbre indexé
  68 +void print_tree(Node, int, char*);
66 69  
67 70 // Libère l'espace mémoire associé à l'arbre indexé
68   -void free_tree(Node* Tree);
  71 +void free_tree(Node*);
69 72  
70 73 // Ajoute une erreur
71   -void add_error(int*, char*, int, Node, FILE*);
  74 +void add_error(int*, char*, int, Node, FILE*, int*);
72 75  
73 76 // Corrige les erreurs
74   -void make_correction(char *, int, Node, FILE*);
  77 +void make_correction(char *, int, Node, FILE*, int*);
75 78  
76 79 // Initialise le programme
77 80 void init_pgrm(void);
  81 +
  82 +// Ajoute un caractère à une chaine de caractères
  83 +void add_caract(char*, char);
  84 +
  85 +// Supprime le dernier caractère d'une chaine
  86 +void supp_caract(char*);
  87 +
  88 +// Initialise le tableau contenant les mots de correction
  89 +void init_correction(correction*);
  90 +
  91 +// Imprime les mots possibles pour la correction
  92 +void make_tree_correct(Node, int, char*, char*, correction);
  93 +
  94 +// Ajoute un mot dans une liste contigûe
  95 +void add_in_liste(correction, char*, char*);
  96 +
  97 +// Choix du mot corrigé
  98 +void choice_word(correction, FILE*, char, int, int*, bool);
  99 +
  100 +// Détecte les caractères spéciaux
  101 +bool detect_end_caract(char*, char*);
  102 +
  103 +// Corrige le mot
  104 +void correct_word(char*, FILE*, int);
... ...