geneviedube

Untitled

Sep 4th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.40 KB | None | 0 0
  1. /**
  2.  * Implements a dictionary's functionality.
  3.  */
  4.  
  5. #include <stdbool.h>
  6. #include "dictionary.h"
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <ctype.h>
  11.  
  12.  
  13.  
  14. //no need to include mispelling counting because it s integrated into speller
  15. //define the data structure of the dictionnary => defining the node structure (trie)
  16. typedef struct node
  17. {
  18.     bool is_word;                //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 *temp_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. int count = 0;  //counting caracter
  32.  
  33.  
  34. // free function for recusion
  35. void freeNode(node *pointer);
  36.  
  37. /**
  38.  * Returns true if word is in dictionary else false. Word not in disctionnary = misspelled
  39.  */
  40. bool check(const char *word)
  41. {
  42.     //if one letter is not good, than the word is not in the dictionnary but it s a word => result is misspelled
  43.     // 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
  44.     for (int i = 0; i < strlen(word); i++)  //(int letter_position = 0; word[letter] != EOF; letter++)
  45.     {
  46.         //allowed only alphabetical characters and apostrophes
  47.         if (isalpha(i)) //letter position > 0 means a word more than 1 letter
  48.         {
  49.             //if letter is capital, make it lower to make sure no sensitive case to be count misspelling by error because of capital
  50.             if (isupper(i))
  51.             {
  52.                 letter_position = (i - 65);
  53.                  //is it the first caracter of the text word
  54.                 if (count == 0)
  55.                 {
  56.                     if(root->children[letter_position] == NULL)
  57.                     return false;
  58.  
  59.                     else
  60.                     {
  61.                         temp_pointer = root->children[letter_position];
  62.                     }
  63.                 }
  64.  
  65.                 // caracter is not the first caracter of the letter
  66.                 else if(count > 0)
  67.                 {
  68.                     if (temp_pointer->children[letter_position] == NULL)
  69.                     return false;
  70.  
  71.                     else
  72.                     {
  73.                         //make sure to link all the character of the same word
  74.                         temp_pointer = temp_pointer->children[letter_position];
  75.                     }
  76.  
  77.                 }
  78.  
  79.             }
  80.  
  81.  
  82.             else if (islower(i))
  83.             {
  84.                 letter_position = (i - 97);
  85.                  //is it the first caracter of the text word
  86.                 if (count == 0)
  87.                 {
  88.                     if(root->children[letter_position] == NULL)
  89.                     return false;
  90.  
  91.                     else
  92.                     {
  93.                         //make sure that next word start with the root
  94.                         temp_pointer = root->children[letter_position];
  95.  
  96.                     }
  97.                 }
  98.  
  99.  
  100.                 // caracter is not the first caracter of the letter
  101.                 else if(count > 0)
  102.                 {
  103.                     if (temp_pointer->children[letter_position] == NULL)
  104.                     return false;
  105.  
  106.                     else
  107.                     {
  108.                         //make sure to link all the character of the same word
  109.                         temp_pointer = temp_pointer->children[letter_position];
  110.                     }
  111.  
  112.                 }
  113.             }
  114.  
  115.         }
  116.  
  117.         else if (c == '\'') //the apostrophe
  118.         {
  119.             letter_position = 26;
  120.             if (temp_pointer->children[letter_position] == NULL)
  121.             return false;
  122.  
  123.             else
  124.             {
  125.                 //move to the next nde
  126.                 temp_pointer = temp_pointer->children[letter_position];
  127.             }
  128.         }
  129.  
  130.         //Only return true if the pointer goes until the end of the word
  131.         else if (c == '\0')
  132.         {
  133.             if(temp_pointer->is_word == false)
  134.             return false;
  135.  
  136.             else
  137.             {
  138.                 temp_pointer->is_word = true;
  139.                 return true;
  140.             }
  141.         }
  142.     }
  143. return false;
  144.  
  145. }
  146.  
  147.  
  148.  
  149. /**
  150.  * Loads dictionary into memory. Returns true if successful else false.
  151.  */
  152. bool load(const char *dictionary)
  153. {
  154.     //no need to consider the case senstive
  155.  
  156.     root = malloc(sizeof(node));
  157.     temp_pointer = malloc(sizeof(node));
  158.  
  159.  
  160.     // note lenght min for a word is 2 letters (+1) and the max is 45 ( the longest word)
  161.     // open the dictionnary file to read his content
  162.     FILE *fp = fopen(dictionary, "r");
  163.     if (fp == NULL)
  164.     {
  165.         printf("Could not open %s.\n", dictionary);
  166.         return false;
  167.     }
  168.  
  169.     //get dictionaryfile
  170.     for (int c = fgetc(fp); c!= EOF; c = fgetc(fp))
  171.     {
  172.  
  173.         if (isalpha(c))
  174.         {
  175.             letter_position = (c - 97);
  176.  
  177.             //if the caracter is the first letter
  178.             if (count == 0)
  179.             {
  180.                 if(root->children[letter_position] == NULL)
  181.                 {
  182.                     //allocate memory to see next node
  183.                     (root->children[letter_position]) = malloc(sizeof(node));
  184.                     (root->children[letter_position])->is_word = false;
  185.                 }
  186.                 //make sure that next word start with the root
  187.                 temp_pointer = root->children[letter_position];
  188.                 count++;
  189.                 words++;
  190.             }
  191.  
  192.  
  193.             //caracter is not the firts
  194.  
  195.             else if (count > 0)
  196.             {
  197.                 letter_position = (c - 97);
  198.                 if (temp_pointer->children[letter_position] == NULL)
  199.                 {
  200.                     //move to the next node
  201.                     temp_pointer->children[letter_position] = malloc(sizeof(node));
  202.                     temp_pointer->children[letter_position]->is_word = false;
  203.                 }
  204.                 //make sure to link all the character of the same word
  205.                 temp_pointer = temp_pointer->children[letter_position];
  206.                 count++;
  207.             }
  208.  
  209.  
  210.         }
  211.  
  212.         else if(c == '\'')
  213.         {
  214.             letter_position = 26;
  215.             if (temp_pointer->children[letter_position] == NULL)
  216.             {
  217.                 //move to the next node
  218.                 temp_pointer->children[letter_position] = malloc(sizeof(node));
  219.                 temp_pointer->children[letter_position]->is_word = false;
  220.             }
  221.             temp_pointer = temp_pointer->children[letter_position];
  222.             count++;
  223.         }
  224.  
  225.         else if (c == '\n')
  226.         {
  227.             temp_pointer->is_word = true;
  228.             count = 0;
  229.         }
  230.     }
  231.  
  232. fclose(fp);
  233. return true;
  234. }
  235.  
  236.  
  237.  /**
  238.  * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  239.  */
  240. unsigned int size(void)
  241. {
  242.     // if dictionary is loaded, return number of words
  243.     if (words > 0)
  244.     return words;
  245.  
  246.     // if dictionnary hasn't been loaded, return 0
  247.     else
  248.     return 0;
  249. }
  250.  
  251. /**
  252.  * Unloads dictionary from memory. Returns true if successful else false.
  253.  */
  254. bool unload(void)
  255. {
  256.        //FILE *fp = NULL;
  257.  
  258.     //go through node's children  // make sure to free the childrens and come back to the parent
  259.     for (int letter_position = 0; letter_position < LENGTH; letter_position++)
  260.     {
  261.         //if children is a pointer so a parent to his children, check also that children)
  262.         if (root->children[letter_position] != NULL)
  263.         {
  264.             freeNode(root->children[letter_position]);
  265.             return false;
  266.         }
  267.         else
  268.         return true;
  269.     }
  270.  
  271. return true;
  272. }
  273.  
  274. void freeNode(node *temp_pointer)
  275. {
  276.     //go through node's children  // make sure to free the childrens and come back to the parent
  277.     for (int letter_position = 0; letter_position < LENGTH; letter_position++)
  278.     {
  279.         //if children is a pointer so a parent to his children, check also that children)
  280.         if (temp_pointer->children[letter_position] != NULL)
  281.         {
  282.             freeNode(temp_pointer->children[letter_position]);
  283.         }
  284.  
  285.         // when all children are null, it time to free node
  286.         if (root == NULL)
  287.         {
  288.             free(temp_pointer);
  289.         }
  290.     }
  291. }
Advertisement
Add Comment
Please, Sign In to add comment