Advertisement
geneviedube

Untitled

Sep 2nd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.70 KB | None | 0 0
  1. /**
  2.  * Implements a dictionary's functionality.
  3.  */
  4.  
  5. #include <stdbool.h>
  6.  
  7. #include "dictionary.h"
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13.  
  14.  
  15.  
  16. //no need to include mispelling counting because it s integrated into speller
  17. //define the data structure of the dictionnary => defining the node structure (trie)
  18. typedef struct node
  19. {
  20.     bool is_word;                //char *word[LENGTH + 1];     // for the max lenght which is 45 + the one for the "\0"
  21.     struct node *children[27];  //array space of 27 for the 26 letters + apostrophes
  22. }
  23. node;
  24.  
  25. node *root;
  26.  
  27.  
  28.  
  29. int letter_position = 0;  // position of letter in tries
  30. int words = 0; // number of word counting
  31. int word = 0;  // word
  32. unsigned int c = 0;
  33.  
  34.  
  35. // free function for recusion
  36. void freeNode(node *pointer);
  37.  
  38. /**
  39.  * Returns true if word is in dictionary else false. Word not in disctionnary = misspelled
  40.  */
  41. bool check(const char *word)
  42. {
  43.      // sign for apostrophe is backshash '\''
  44.      // check if dictionary is loaded
  45.     if (root == NULL)
  46.     {
  47.         printf("root is NULL");
  48.         return false;
  49.     }
  50.  
  51.     //set pointer to root, first node,
  52.     node *pointer = root;        //if one letter is not good, than the word is not in the dictionnary but it s a word => result is misspelled
  53.  
  54.     // iterate every letters of the word until end of file making sur that max lenght is 27 // ide tells me to replace "\0" by strncmp
  55.     for (int i = 0; i < word[LENGTH + 1]; i++)  //(int letter_position = 0; word[letter] != EOF; letter++)
  56.     {
  57.         //allowed only alphabetical characters and apostrophes
  58.         if (isalpha(c) || (c == '\'' && letter_position > 0)) //letter position > 0 means a word more than 1 letter
  59.         {
  60.             //if letter is capital, make it lower to make sure no sensitive case to be count misspelling by error because of capital
  61.             int letter_position = tolower(word[i]);
  62.  
  63.             //word[letter_position] = c //match the caracter of the text to the pointer in dictionary
  64.             //letter_position++   // move to the next letter position// find the position of the letter
  65.  
  66.             // go to next letter and check if it doesnt exist to declare mispelling at the wrong letter encounter
  67.             if (pointer->children[letter_position] == NULL)
  68.             {
  69.                 return false;
  70.             }
  71.  
  72.             else // if not NULL, then move to the next node
  73.             {
  74.                 pointer = pointer->children[letter_position];
  75.             }
  76.  
  77.             //Only return true if the pointer goes until the end of the word
  78.             if (pointer->is_word)
  79.             return true;
  80.             words++;
  81.         }
  82.         else
  83.         return false;
  84.  
  85.  
  86.     }
  87. return false;
  88.  
  89. }
  90.  
  91.  
  92.  
  93. /**
  94.  * Loads dictionary into memory. Returns true if successful else false.
  95.  */
  96. bool load(const char *dictionary)
  97. {
  98.     //no need to consider the case senstive
  99.  
  100.     //create memory space for the first node (lenght depend of the word)
  101.     root = malloc(sizeof(node));
  102.  
  103.     // note lenght min for a word is 2 letters (+1) and the max is 45 ( the longest word)
  104.     // open the dictionnary file to read his content
  105.     FILE *fp = fopen(dictionary, "r");
  106.     if (fp == NULL)
  107.     {
  108.         printf("Could not open %s.\n", dictionary);
  109.         return false;
  110.     }
  111.  
  112.     //keep pointer at the first node, the root
  113.     node *pointer = root;
  114.  
  115.    //get dictionaryfile
  116.     for (int c = fgetc(fp); c!= EOF; c = fgetc(fp))
  117.     {
  118.         if (isalpha(c) || (c== '\'' && letter_position > 0))
  119.         {
  120.         // check if next node exist if not NULL
  121.             if (pointer->children[letter_position] == NULL)
  122.             {
  123.                 //create memory for the next node
  124.                 node *new = malloc(sizeof(node));
  125.                 pointer->children[letter_position] = new;
  126.  
  127.  
  128.                 //move to the next node
  129.                 pointer = pointer->children[letter_position];
  130.             }
  131.  
  132.             //if newline means end of word in dictionary reached
  133.             else if (pointer->is_word)
  134.             return true;
  135.             //bring back pointer to root
  136.             pointer = root;
  137.         }
  138.  
  139.     }
  140.  
  141. fclose(fp);
  142. return true;
  143. }
  144.  
  145.  
  146. /**
  147.  * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  148.  */
  149. unsigned int size(void)
  150. {
  151.     // if dictionary is loaded, return number of words
  152.     if (words > 0)
  153.     return words;
  154.  
  155.     // if dictionnary hasn't been loaded, return 0
  156.     else
  157.     return 0;
  158. }
  159.  
  160. /**
  161.  * Unloads dictionary from memory. Returns true if successful else false.
  162.  */
  163. bool unload(void)
  164. {
  165.        //FILE *fp = NULL;
  166.  
  167.     //go through node's children  // make sure to free the childrens and come back to the parent
  168.     for (int letter_position = 0; letter_position < (LENGTH + 1); letter_position++)
  169.     {
  170.         //if children is a pointer so a parent to his children, check also that children)
  171.         if (root->children[letter_position] != NULL)
  172.         {
  173.             freeNode(root->children[letter_position]);
  174.             return false;
  175.         }
  176.         else
  177.         return true;
  178.     }
  179.  
  180. return true;
  181. }
  182.  
  183. void freeNode(node *pointer)
  184. {
  185.     //go through node's children  // make sure to free the childrens and come back to the parent
  186.     for (int letter_position = 0; letter_position < (LENGTH + 1); letter_position++)
  187.     {
  188.         //if children is a pointer so a parent to his children, check also that children)
  189.         if (pointer->children[letter_position] != NULL)
  190.         {
  191.             freeNode(pointer->children[letter_position]);
  192.         }
  193.  
  194.         // when all children are null, it time to free node
  195.         if (root == NULL)
  196.         {
  197.             free(pointer);
  198.         }
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement