Advertisement
geneviedube

Untitled

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