From 293e356ce9231c2ecb41fd692003dbef3f2934fb Mon Sep 17 00:00:00 2001 From: grouille Date: Tue, 23 Apr 2019 10:02:54 +0200 Subject: [PATCH] Version quasi définitive sans la fonction free --- doc.txt | 6 +++++- lib2.txt | 9 ++++++++- texte.txt | 19 +++++++++++++++++++ tree.c | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------- 4 files changed, 132 insertions(+), 51 deletions(-) create mode 100644 texte.txt diff --git a/doc.txt b/doc.txt index 0a453f7..36f4373 100644 --- a/doc.txt +++ b/doc.txt @@ -1 +1,5 @@ -spirateur, chevre, chien, chat;. chienne;? banc. +aspirateur chien. chienne, banane.banane +Doucement, Etrange. Odorat; odorant; +L'ile, L'ocean; aaee''ee +chiens +chienne diff --git a/lib2.txt b/lib2.txt index 8eb9891..3365936 100644 --- a/lib2.txt +++ b/lib2.txt @@ -8,4 +8,11 @@ aspirateur chaussette chien chienne -chiens \ No newline at end of file +chiens +Doucement +Etrange +Odorat +odorant +L'ile +L'ocean +aaee''ee \ No newline at end of file diff --git a/texte.txt b/texte.txt new file mode 100644 index 0000000..b36daf1 --- /dev/null +++ b/texte.txt @@ -0,0 +1,19 @@ +The ant and the cicada + + + + In the old days, ants and cicadas were friends. They were very different. The ants were hardworking, but the cicadas were lazy. + + In the summer, the ant families were very busy. They knew that in the winter they would have to stay in their anthill. They wanted to have enough food for the whole winter. + + While the ants worked hard, the cicadas didn't do anything. They sang and danced all day. When they were hungry, they could fly to the farm and get something to eat. + + One day the cicadas were singing and dancing. They saw a long line of ants bringing food to their anthill. The cicadas said, Stop, my silly friends. It's a very nice day. Come and dance with us. The ants said, Don't you know about winter? If you don't work now, you'll have trouble later. + + But the cicadas said, We have strong wings. We can fly anywhere we want. Stupid ants! And they continued to sing and dance. + + In the winter, it rained or snowed all the time, and it was very cold. In the anthill, there was singing and dancing. But the cicadas had nothing to eat. They asked the ants for some food. The ants said, We thought you could fly anywhere. Now who is stupid and silly? + + The cicadas cried and said that their wings were wet from the rain. The ants said, We're sorry, but now it's to late. If we help you, there won't be enough food for us. Sorry, very sorry. And the ants closed their door. + + The next day, when the ants opened their door, all the cicadas were dead! That's why we can hear cicadas sing in the summer, but in the winter they are silent. diff --git a/tree.c b/tree.c index 951d93d..dbdf2eb 100644 --- a/tree.c +++ b/tree.c @@ -1,10 +1,17 @@ +// -------------------------------------------------------- +// Projet IMA3 2019 - Lecture d'une bibliothèque +// Décompte du nombre de fautes d'orthographe dans un texte +// Normand Quentin & Rouillé Guillaume +// -------------------------------------------------------- + +// Initialisation des variables et inclusion des bibliothèques #include #include #include -#define MAX 30 -#define NB_CARAC 26 -#define nb_car 5 +#define MAX 30 // taille maximale d'une chaîne lue dans un fichier +#define NB_CARAC 27 // nombre de caractères différents pouvant être identifiés -> 89 avec accentués +// Déclaration de la structure 'trie' ou 'arbre indexé', ainsi que des pointeurs associés typedef struct node* Node; typedef struct node { @@ -13,11 +20,13 @@ typedef struct node { bool endWord; }node; +// 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; iletter = '?'; + (*Tree)->letter = '?'; // caractère choisi arbitrairement (* Tree)->endWord = false; for(int i=0; inext[i] = NULL; + (*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; + 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') + while(word[j] != '\0') // on parcourt tout le mot 'word' { - if(is_leaf(Tree2)) - printf("empty\t"); + /*if(is_leaf(Tree2)) // a retirer + printf("empty\t");*/ char letter = word[j]; - if(Tree2->next[letter-'a']!=NULL) - { - Tree2 = Tree2->next[letter-'a']; - printf("%c %d\t", letter, letter-'a'); - printf("okA %d\n", j); - } - else - { - Node new = NULL; - new = malloc(sizeof(node)); - new->letter = letter; - for(int i=0; i<26; i++) - { - new->next[i]=NULL; - } - Tree2->next[letter-'a'] = new; - if(!(Tree2->endWord)) - Tree2->endWord = false; - Tree2=Tree2->next[letter-'a']; - printf("%c %d\t", letter, letter-'a'); - printf("okB %d\n", 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; inext[i]=NULL; + } + 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') - Tree2->endWord = true; + 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"); + //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<=47)||(letter>=58 && letter<=64)||(letter>=123 && letter<=126)||(letter==128)) 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; @@ -93,29 +118,32 @@ char max_index(char word[]) void scan_word(Node Tree, char word[], int* error) // 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; + 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]; - if(Tree2->next[letter-'a']!=NULL) + indice = find_caract_indice(letter); + + if(Tree2 != NULL && Tree2->next[indice]!=NULL) { ind++; - Tree2 = Tree2->next[letter-'a']; + Tree2 = Tree2->next[indice]; endWord = Tree2->endWord; - if(endWord) printf("end :: %s ::\n", word); + /*if(endWord) printf("end :: %s ::\n", word);*/ } else { printf("mot : %s erreur :%c %d \n", word, word[ind], word[ind]); (*error)++; - printf("%d\n", ind); + //printf("%d\n", ind); ind = max_index(word); - printf("%d\n", ind); + //printf("%d\n", ind); stop = true; } } @@ -126,6 +154,14 @@ void scan_word(Node Tree, char word[], int* error) // si un mot démarre juste a } } +bool no_accent(char word[]) +{ + int index_max = max_index(word); + for(int i=0; inext[index])->letter); if((cpTree->next[index])->endWord) - { - printf("fin\n"); - //print_tree(cpTree, 0); - return; - } + { + printf("fin\n"); + //print_tree(cpTree, 0); + return; + } print_tree(cpTree->next[index], 0); } } @@ -199,6 +236,19 @@ void print_first(Node Tree) } } +/*void free_tree(Node Tree) +{ + int index=0; + while(indexnext[index]!=NULL) + free_tree(Tree->next[index]); + else + index++; + } + free(Tree); + }*/ + int main(int argc, char *argv[]) { Node tree = NULL; @@ -214,11 +264,12 @@ int main(int argc, char *argv[]) // printf("%p\n", tree); - print_first(tree); + //print_first(tree); //printf("\n"); //print_tree(tree, 0); printf("erreurs : %d\n", error); + //free_tree(tree); return 0; } -- libgit2 0.21.2