geneviedube

Untitled

Sep 2nd, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.86 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.             //count the number of words
  61.             words++;
  62.  
  63.             // append character to word
  64.             c = word[letter_position];
  65.  
  66.             //if letter is capital, make it lower to make sure no sensitive case to be count misspelling by error because of capital
  67.             c = tolower(word[i]);      //int letter_position = tolower(word[i]);
  68.  
  69.             //word[letter_position] = c //match the caracter of the text to the pointer in dictionary
  70.             //letter_position++   // move to the next letter position// find the position of the letter
  71.  
  72.             // go to next letter and check if it doesnt exist to declare mispelling at the wrong letter encounter
  73.             if (pointer->children[letter_position] == NULL)
  74.             {
  75.                 return false;
  76.             }
  77.  
  78.             else // if not NULL, then move to the next node
  79.             {
  80.                 pointer = pointer->children[letter_position];
  81.             }
  82.  
  83.             //Only return true if the pointer goes until the end of the word
  84.             if (pointer->is_word)
  85.             return true;
  86.  
  87.         }
  88.         else
  89.         return false;
  90.  
  91.  
  92.     }
  93. return false;
  94.  
  95. }
  96.  
  97.  
  98.  
  99. /**
  100.  * Loads dictionary into memory. Returns true if successful else false.
  101.  */
  102. bool load(const char *dictionary)
  103. {
  104.     //no need to consider the case senstive
  105.  
  106.     //create memory space for the first node (lenght depend of the word)
  107.     root = malloc(sizeof(node));
  108.  
  109.     // note lenght min for a word is 2 letters (+1) and the max is 45 ( the longest word)
  110.     // open the dictionnary file to read his content
  111.     FILE *fp = fopen(dictionary, "r");
  112.     if (fp == NULL)
  113.     {
  114.         printf("Could not open %s.\n", dictionary);
  115.         return false;
  116.     }
  117.  
  118.     //keep pointer at the first node, the root
  119.     node *pointer = root;
  120.  
  121.    //get dictionaryfile
  122.     for (int c = fgetc(fp); c!= EOF; c = fgetc(fp))
  123.     {
  124.         if (isalpha(c) || (c== '\'' && letter_position > 0))
  125.         {
  126.         // check if next node exist if not NULL
  127.             if (pointer->children[letter_position] == NULL)
  128.             {
  129.                 //create memory for the next node
  130.                 node *new = malloc(sizeof(node));
  131.                 pointer->children[letter_position] = new;
  132.  
  133.  
  134.                 //move to the next node
  135.                 pointer = pointer->children[letter_position];
  136.             }
  137.  
  138.             //if newline means end of word in dictionary reached
  139.             else if (pointer->is_word)
  140.             return true;
  141.             //bring back pointer to root
  142.             pointer = root;
  143.         }
  144.  
  145.     }
  146.  
  147. fclose(fp);
  148. return true;
  149. }
  150.  
  151.  
  152. /**
  153.  * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  154.  */
  155. unsigned int size(void)
  156. {
  157.     // if dictionary is loaded, return number of words
  158.     if (words > 0)
  159.     return words;
  160.  
  161.     // if dictionnary hasn't been loaded, return 0
  162.     else
  163.     return 0;
  164. }
  165.  
  166. /**
  167.  * Unloads dictionary from memory. Returns true if successful else false.
  168.  */
  169. bool unload(void)
  170. {
  171.        //FILE *fp = NULL;
  172.  
  173.     //go through node's children  // make sure to free the childrens and come back to the parent
  174.     for (int letter_position = 0; letter_position < (LENGTH + 1); letter_position++)
  175.     {
  176.         //if children is a pointer so a parent to his children, check also that children)
  177.         if (root->children[letter_position] != NULL)
  178.         {
  179.             freeNode(root->children[letter_position]);
  180.             return false;
  181.         }
  182.         else
  183.         return true;
  184.     }
  185.  
  186. return true;
  187. }
  188.  
  189. void freeNode(node *pointer)
  190. {
  191.     //go through node's children  // make sure to free the childrens and come back to the parent
  192.     for (int letter_position = 0; letter_position < (LENGTH + 1); letter_position++)
  193.     {
  194.         //if children is a pointer so a parent to his children, check also that children)
  195.         if (pointer->children[letter_position] != NULL)
  196.         {
  197.             freeNode(pointer->children[letter_position]);
  198.         }
  199.  
  200.         // when all children are null, it time to free node
  201.         if (root == NULL)
  202.         {
  203.             free(pointer);
  204.         }
  205.     }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment