tree.c
10.3 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// --------------------------------------------------------
// Projet IMA3 2019 - Lecture d'une bibliothèque
// Décompte du nombre de fautes d'orthographe dans un texte
// Normand Quentin & Rouillé Guillaume
// --------------------------------------------------------
#include "tree.h"
// Fonction permettant de savoir si la structure est vide
bool is_empty_tree(Node Tree)
{
return(Tree==NULL);
}
// Fonction permettant de savoir si le tableau 'next' est un tableau de pointeurs NULL
bool is_leaf(Node Tree)
{
for(int i=0; i<NB_CARAC; i++)
if(Tree->next[i] != NULL)
return false;
return true;
}
// Initialisation de la structure accueillant le dictionnaire
void init_tree(Node* Tree)
{
if(is_empty_tree(*Tree))
{
*Tree = malloc(sizeof(node));
(*Tree)->letter = 7; //
(* Tree)->endWord = false;
for(int i=0; i<NB_CARAC; i++)
(*Tree)->next[i] = NULL; // initialisation du tableau 'next' à un tableau de pointeurs NULL
}
}
// Détermine l'indice de rangement dans le tableau 'next' du caractère 'letter'
int find_caract_indice(char letter) // Ne fonctionne pas pour les caractères accentués
{
//printf("__%d__\n", letter);
if(letter>=97 && letter<=122) return letter-'a';
if(letter>=65 && letter<=90) return letter-'A';
if(letter == 39) return letter-13; // l'apostrophe est placée en 27ème position
//if(letter>=192) {printf("%d", letter-166); return letter-166;} //-192+26
}
// Fonction d'ajout d'un mot 'word' dans la structure 'tree' de type 'arbre indexé'
void add_in_tree(Node Tree, char word[])
{
int j=0; // indice du caractère dans le mot 'word'
int ind; // indice du caractère dans le tableau 'next'
Node Tree2 = Tree;
while(word[j] != '\0') // on parcourt tout le mot 'word'
{
/*if(is_leaf(Tree2)) // a retirer
printf("empty\t");*/
char letter = word[j];
ind = find_caract_indice(letter);
if(Tree2->next[ind]!=NULL) // si le pointeur du tableau 'next' corresdant au caractère lu n'est pas NULL, on s'insère dans cette 'branche' de l'arbre indexé et on continue avec le caractère suivant
{
Tree2 = Tree2->next[ind];
/* printf("%c %d\t", letter, ind);
printf("okA %d\n", j);*/
}
else // sinon, on ajoute une nouvelle cellule de type 'struct node' et on y insère les informations concernant le caractères
{
Node new = NULL;
new = malloc(sizeof(node));
new->letter = letter; // le caractère
for(int i=0; i<NB_CARAC; i++) // un tableau de pointeurs NULL
{
new->next[i]=NULL;
}
new->endWord = false;
Tree2->next[ind] = new; // on fait pointé le tableau du caractère précédent vers cette cellule (vers ce caractère)
if(!(Tree2->endWord)) // si le caractère n'est pas un caractère de fin, on le met à 'false' -> UTILITE ?
Tree2->endWord = false;
Tree2=Tree2->next[ind]; // on se place au niveau du caractère suivant dans l'arbre indexé
/*printf("%c %d\t", letter, ind);
printf("okB %d\n", j);*/
}
j++;
if(word[j]=='\0') // si le caractère suivant est la fin de la chaîne, on dit que le caractère précédent est un caractère de fin
Tree2->endWord = true;
}
//printf("ok\n");
}
// Fonction qui détermine si le caractère est un caractère de fin de mot (espace, ',', ';', '.', etc..)
bool is_end_caract(char letter)
{
if(letter==0) return true;
if((letter>=32 && letter<=38)||(letter>=40 && letter<=47)||(letter>=58 && letter<=64)||(letter>=123 && letter<=126)||(letter==128)) return true;
return false;
}
// Renvoi l'indice maximum du mot 'word'
char max_index(char word[])
{
int index = 0;
while(!is_end_caract(word[index]))
index++;
return index;
}
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
{
bool endWord = false;
bool stop = false;
int ind = 0;
int indice;
char letter;
Node Tree2 = Tree;
while(!is_end_caract(word[ind]))
{
stop = false;
letter = word[ind];
indice = find_caract_indice(letter);
if(Tree2 != NULL && Tree2->next[indice]!=NULL)
{
ind++;
Tree2 = Tree2->next[indice];
endWord = Tree2->endWord;
}
else
{
add_error(error, word, ind, Tree2, fp_txt, correct);
//printf("%d\n", ind);
ind = max_index(word);
//printf("%d\n", ind);
stop = true;
endWord = false;
}
}
if(!endWord && !stop)
{
add_error(error, word, ind, Tree2, fp_txt, correct);
}
}
void add_error(int* error, char word[], int index, Node Tree, FILE* fp_txt, int* correct)
{
(*error)++;
if(index==0)
{
printf("Le mot '%s' ne correspond à aucun mot du dictionnaire.\n\n", word);
return;
}
else if(index<strlen(word))
printf("Il y a une erreur dans le mot '%s', au caractère %c d'incide %d.\n", word, word[index], index);
else
printf("Il manque au moins une lettre au mot '%s'.\n", word);
make_correction(word, index, Tree, fp_txt, correct);
}
void make_correction(char word[], int index, Node Tree, FILE* fp_txt, int* correct) // gestion des caractères de fin
{
correction liste = NULL;
init_correction(&liste);
char end;
bool testEnd;
testEnd = detect_end_caract(&end, word);
int longueur = strlen(word);
Node Tree2 = Tree;
word[index-1]='\0';
printf("\n");
char words[MAX] = "";
make_tree_correct(Tree2, 0, words, word, liste);
choice_word(liste, fp_txt, end, longueur, correct, testEnd);
free(liste); // VALGRIND ???
}
bool detect_end_caract(char* end, char word[])
{
if(is_end_caract(word[strlen(word)-1]))
{
*end = word[strlen(word)-1];
return true;
}
return false;
}
void choice_word(correction liste, FILE* fp_txt, char end, int longueur, int* correct, bool testEnd)
{
int choix;
printf("Voici les mots possibles pour corriger ce mot :\n");
for(int i=0; i<liste->dernier+1; i++)
printf("%d. %s\n", i+1, liste->mots[i]);
printf("\n");
choix = -1;
printf("Quel mot voulez-vous sélectionner ? (Entrez %d pour ne pas corriger le mot)\n", (liste->dernier)+2);
while(choix<1 || choix>liste->dernier+2)
{
scanf("%d", &choix);
}
if(choix==(liste->dernier)+2)
{
printf("\n");
return;
}
(*correct)++;
char word[MAX];
strcpy(word, liste->mots[choix-1]);
printf("Le mot %s a été sélectionné.\n\n", word);
if(testEnd) add_caract(word, end);
correct_word(word, fp_txt, longueur);
}
void correct_word(char word[], FILE* fp_txt, int longueur)
{
FILE* fp_tamp;
fp_tamp = fopen("tampon.txt", "w+");
if(fp_tamp==NULL)
{
printf("Erreur lors de l'ouverture du fichier tampon.\n");
return;
}
int position = ftell(fp_txt)-longueur;
rewind(fp_txt);
char tempo;
while(ftell(fp_tamp)<position)
{
tempo = fgetc(fp_txt);
if(tempo==EOF) break;
//if(tempo=='\n');
fputc(tempo, fp_tamp);
}
fprintf(fp_tamp, "%s", word);
long newPosition = ftell(fp_tamp);
fseek(fp_txt, ftell(fp_txt)+longueur, SEEK_SET);
while(1)
{
tempo = fgetc(fp_txt);
if(tempo==EOF) break;
//if(tempo=='\n');
fputc(tempo, fp_tamp);
}
rewind(fp_txt);
rewind(fp_tamp);
char temp[MAX_READ];
while(1)
{
if(fgets(temp, MAX_READ, fp_tamp)==NULL)
break;
fputs(temp, fp_txt);
}
fseek(fp_txt, newPosition, SEEK_SET);
fclose(fp_tamp);
remove("tampon.txt");
}
void init_correction(correction* liste)
{
if((*liste)!=NULL)
{
printf("Déjà initialisé.\n");
return;
}
*liste = malloc(sizeof(struct liste));
(*liste)->dernier = -1;
for(int i=0; i<NB_MOT_CORRECTION; i++)
{
strcpy((*liste)->mots[i], "");
}
}
void init_pgrm(void)
{
printf("\n------------------------------------------\n");
printf("Correcteur ortographique.\n");
printf("Normand Quentin & Rouillé Guillaume.\n");
printf("------------------------------------------\n\n");
}
bool no_accent(char word[])
{
int index_max = max_index(word);
for(int i=0; i<index_max; i++)
if(word[i]<0) return false;
return true;
}
void read_txt(FILE* fp, Node* Tree, int* error, int* correct)
{
char word[MAX];
while(1)
{
if(fscanf(fp, "%s", word)!=1)
break;
if(no_accent(word)) // !!! MOTS AVEC ACCENTS NON PRIS EN COMPTE
scan_word(*Tree, word, error, fp, correct);
}
if(feof(fp)) printf("Fin de la lecture du fichier à scanner.\n\n");
if(ferror(fp)) printf("Erreur lors de la lecture du fichier à scanner.\n\n");
}
void read_lib(FILE* fp, Node* Tree)
{
char word[MAX];
while(1)
{
if(fscanf(fp, "%s", word)!=1)
break;
// printf("--%s--\n", word);
//fflush(stdout);
if(no_accent(word))
add_in_tree(*Tree, word);
}
if(feof(fp)) printf("Fin de la lecture de la bibliothèque.\n\n");
if(ferror(fp)) printf("Erreur lors de la lecture de la bibliothèque.\n\n");
}
void print_tree(Node Tree, int index, char word[])
{
if(is_empty_tree(Tree))
return;
add_caract(word, Tree->letter);
if(is_leaf(Tree))
{
printf("%s\n", word);
supp_caract(word);
return;
}
if(Tree->endWord)
printf("%s\n", word);
while(index < NB_CARAC)
{
if(!is_empty_tree(Tree->next[index]))
{
print_tree(Tree->next[index], 0, word);
}
index++;
}
supp_caract(word);
return;
}
void make_tree_correct(Node Tree, int index, char word[], char start[], correction liste)
{
if(is_empty_tree(Tree))
return;
add_caract(word, Tree->letter);
if(is_leaf(Tree))
{
//printf("%s%s\n", start, word);
add_in_liste(liste, start, word);
supp_caract(word);
return;
}
if(Tree->endWord)
{
//printf("%s%s\n", start, word);
add_in_liste(liste, start, word);
}
while(index < NB_CARAC)
{
if(!is_empty_tree(Tree->next[index]))
{
make_tree_correct(Tree->next[index], 0, word, start, liste);
}
index++;
}
supp_caract(word);
return;
}
void add_in_liste(correction liste, char start[], char word[])
{
char temp[MAX];
strcpy(temp, start);
strcat(temp, word);
if(liste->dernier<NB_MOT_CORRECTION-1)
{
(liste->dernier)++;
strcpy(liste->mots[liste->dernier], temp);
}
}
void add_caract(char word[], char caract)
{
char str[2];
str[0] = caract;
str[1] = '\0';
strcat(word, str);
}
void supp_caract(char word[])
{
word[strlen(word)-1]='\0';
}
void free_tree(Node* Tree)
{
if(*Tree!=NULL)
{
for(int i=0; i<NB_CARAC; i++)
{
free_tree(&(*Tree)->next[i]);
}
free(*Tree);
}
}