Advertisement
Guest User

Dictionary.c

a guest
Jul 20th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.34 KB | None | 0 0
  1. /**
  2.  * Implements a dictionary's functionality.
  3.  */
  4.  
  5. #include <ctype.h>
  6. #include <stdbool.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9.  
  10. #include "dictionary.h"
  11.  
  12. #define ALPHLEN 26 // length of alphabet
  13.  
  14. // define trie struct
  15. typedef struct trie
  16. {
  17.     bool isWord;
  18.     struct trie *alph[ALPHLEN+1];
  19. } trie;
  20.  
  21. // function declarations
  22. void insert(char *word);
  23. bool makeEmpty(trie *node);
  24.  
  25. // global variables
  26. int dSize = 0;
  27. trie *root = NULL;
  28.  
  29. /**
  30.  * Returns true if word is in dictionary else false.
  31.  */
  32. bool check(const char *word)
  33. {
  34.     // create traversal pointer
  35.     trie *ptr = root;
  36.    
  37.     // iterate through characters of word
  38.     for (int i = 0; word[i] != '\0'; i++)
  39.     {
  40.         // store character
  41.         char c = word[i];
  42.        
  43.         // assigns index of 0-25 for a-z, 26 for apostrophe
  44.         int index = (c == '\'') ? ALPHLEN : toupper(c) - 65;
  45.        
  46.         // if index cointains null pointer, return failure
  47.         if (ptr->alph[index] == NULL)
  48.         {
  49.             return false;
  50.         }
  51.        
  52.         // advance pointer
  53.         ptr = ptr->alph[index];
  54.     }
  55.    
  56.     // check if word
  57.     if (ptr->isWord)
  58.     {
  59.         return true;
  60.     }
  61.     return false;
  62. }
  63.  
  64. /**
  65.  * Loads dictionary into memory. Returns true if successful else false.
  66.  */
  67. bool load(const char *dictionary)
  68. {
  69.     // open dictionary file
  70.     FILE *d = fopen(dictionary, "r");
  71.    
  72.     // check that file opened correctly
  73.     if (d == NULL)
  74.     {
  75.         return false;
  76.     }
  77.    
  78.     //initialize trie
  79.     root = malloc(sizeof(trie));
  80.     root->isWord = false;
  81.    
  82.     // variable for storing strings
  83.     char word[LENGTH];
  84.    
  85.     // read string until end of file
  86.     while (fgets(word, LENGTH+2, d))
  87.     {
  88.         insert(word);
  89.         dSize++;
  90.     }
  91.    
  92.     // close dictionary file
  93.     fclose(d);
  94.    
  95.     // return success
  96.     return true;
  97. }
  98.  
  99. /**
  100.  * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  101.  */
  102. unsigned int size(void)
  103. {
  104.     return dSize;
  105. }
  106.  
  107. /**
  108.  * Unloads dictionary from memory. Returns true if successful else false.
  109.  */
  110. bool unload(void)
  111. {
  112.     makeEmpty(root);
  113.     return true;
  114. }
  115.  
  116.  
  117. // utility functions
  118.  
  119. // insert one word into trie
  120. void insert(char *word)
  121. {
  122.     // create traversal pointer
  123.     trie *ptr = root;
  124.    
  125.     // iterate through characters of word
  126.     for(int i = 0; word[i] != '\n'; i++)
  127.     {
  128.         // store character
  129.         char c = word[i];
  130.        
  131.         // assigns index of 0-25 for a-z, 26 for apostrophe
  132.         int index = (c == '\'') ? ALPHLEN : toupper(c) - 65;
  133.        
  134.         // if index contains null pointer, create new trie
  135.         if (ptr->alph[index] == NULL)
  136.         {
  137.             ptr->alph[index] = malloc(sizeof(trie));
  138.             ptr->alph[index]->isWord = false;
  139.         }
  140.        
  141.         // advance pointer
  142.         ptr = ptr->alph[index];
  143.     }
  144.    
  145.     // set bool to true at end of word
  146.     ptr->isWord = true;
  147. }
  148.  
  149. // frees a node and all children
  150. bool makeEmpty(trie *node)
  151. {
  152.     // iterate through array in trie
  153.     for (int i = 0; i < ALPHLEN; i++)
  154.     {
  155.         if (node->alph[i] != NULL)
  156.         {
  157.             makeEmpty(node->alph[i]);
  158.         }
  159.     }
  160.    
  161.     // if all pointers in array are null, free it
  162.     free(node);
  163.    
  164.     //return success
  165.     return true;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement