DragonOsman

dictionary.c trie implementation load() and size()

Nov 17th, 2016
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.97 KB | None | 0 0
  1. /**
  2.  * dictionary.c
  3.  *
  4.  * Computer Science 50
  5.  * Problem Set 5
  6.  *
  7.  * Implements a dictionary's functionality.
  8.  */
  9.  
  10. #include <stdbool.h>
  11. #include <stdlib.h>
  12. #include <ctype.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15.  
  16. #include "dictionary.h"
  17.  
  18. /**
  19.  * declaration for trie struct
  20.  * and length of children array
  21.  */
  22.  
  23. #define ARRAY_SIZE 27;
  24.  
  25. typedef struct node
  26. {
  27.     bool is_word;
  28.     struct node* children[ARRAY_SIZE];
  29. }
  30. node;
  31.  
  32. /**
  33.  * Keeping track of first node of trie here
  34.  */
  35.  node* root;
  36.  
  37. /**
  38.  * Returns true if word is in dictionary else false.
  39.  */
  40. bool check(const char* word)
  41. {
  42.     for (int index = 0; index < ARRAY_SIZE; index++)
  43.     {
  44.         if (strcmp((char*)cursor->children[index], word) != 0)
  45.         {
  46.             return false;
  47.         }
  48.     }
  49.     return true;
  50. }
  51.  
  52. /**
  53.  * variable to hold size of dictionary
  54.  */
  55.  unsigned int dict_size = 0;
  56.  
  57. /**
  58.  * Loads dictionary into memory.  Returns true if successful else false.
  59.  */
  60.  
  61. node* new_node;
  62. FILE* dictptr;
  63. bool load(const char* dictionary)
  64. {
  65.     dictptr = fopen(dictionary, "r");
  66.     if (dictptr == NULL)
  67.     {
  68.         exit(1);
  69.     }
  70.     root = calloc(ARRAY_SIZE, sizeof(node));
  71.     if (root == NULL)
  72.     {
  73.         exit(1);
  74.     }
  75.     node* cursor = root;
  76.    
  77.     char word[ARRAY_SIZE + 1];
  78.    
  79.     for (int index = 0, n = strlen(word); index < n; index++)
  80.     {
  81.         new_node = calloc(ARRAY_SIZE, sizeof(node));
  82.         if (new_node == NULL)
  83.         {
  84.             exit(1);
  85.         }
  86.         fscanf(dictptr, "%s", word);
  87.         if (root->children[index] == NULL && root->children[index] == new_node)
  88.         {
  89.             if (isalpha(word[index]) || word[index] == '\'')
  90.             {
  91.                 cursor = cursor->children[index];
  92.                 cursor = new_node;
  93.                 cursor = root;
  94.                 if (word[index] == '\n')
  95.                 {
  96.                     cursor = cursor->children[index];
  97.                     cursor = new_node;
  98.                     cursor->is_word = true;
  99.                     cursor = root;
  100.                     dict_size++;
  101.                 }
  102.             }
  103.             else if (root->children[index] != NULL && root->children[index] == new_node)
  104.             {
  105.                 if (isalpha(word[index]) || word[index] == '\'')
  106.                 {
  107.                     cursor = cursor->children[index];
  108.                     cursor = new_node;
  109.                     cursor = root;
  110.                     if (word[index] == '\n')
  111.                     {
  112.                         cursor = cursor->children[index];
  113.                         cursor = new_node;
  114.                         cursor->is_word = true;
  115.                         cursor = root;
  116.                         dict_size++;
  117.                     }
  118.                 }
  119.             }
  120.         }
  121.     }
  122.     fclose(dictptr);
  123.     return true;
  124. }
  125.  
  126. /**
  127.  * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  128.  */
  129. unsigned int size(void)
  130. {
  131.     return dict_size;
  132. }
Add Comment
Please, Sign In to add comment