Advertisement
Guest User

Speller

a guest
Jul 17th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. // Implements a dictionary's functionality
  2.  
  3. #include <stdbool.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8.  
  9. #include "dictionary.h"
  10.  
  11. // Represents number of children for each node in a trie
  12. #define N 27
  13.  
  14. // Represents a node in a trie
  15. typedef struct node
  16. {
  17. char character;
  18. bool is_word;
  19. struct node *children[N];
  20. }
  21. node;
  22.  
  23. // Represents a trie
  24. node *root;
  25.  
  26. node *trav;
  27.  
  28. int dicsize;
  29. bool isloaded;
  30.  
  31. void recurfree(node *);
  32.  
  33. // Loads dictionary into memory, returning true if successful else false
  34. bool load(const char *dictionary)
  35. {
  36. dicsize = 0;
  37.  
  38. // Initialize trie
  39. root = malloc(sizeof(node));
  40. if (root == NULL)
  41. {
  42. return false;
  43. }
  44. else {
  45. isloaded = true;
  46. }
  47. root->is_word = false;
  48. for (int i = 0; i < N; i++)
  49. {
  50. root->children[i] = NULL;
  51. root->character = '\0';
  52. }
  53.  
  54. // Open dictionary
  55. FILE *file = fopen(dictionary, "r");
  56. if (file == NULL)
  57. {
  58. unload();
  59. return false;
  60. }
  61.  
  62. // Buffer for a word
  63. char word[LENGTH + 1];
  64.  
  65. // Insert words into trie
  66. while (fscanf(file, "%s", word) != EOF)
  67. {
  68.  
  69. // /printf("%s\n", word);
  70.  
  71. trav = root;
  72.  
  73. for (int i = 0; i < strlen(word); i++) {
  74.  
  75. // /printf("%c ", word[i]);
  76.  
  77. int letloc = 0;
  78.  
  79. char curlet = tolower(word[i]);
  80.  
  81. if (word[i] == '\'') {
  82. letloc = 26;
  83. }
  84. else {
  85. letloc = curlet - 97;
  86. }
  87.  
  88. if (trav->children[letloc] == NULL) {
  89.  
  90. // /printf(" newnode ");
  91.  
  92. node *newnode = malloc(sizeof(node));
  93.  
  94. trav->children[letloc] = newnode;
  95.  
  96. trav = newnode;
  97. }
  98. else {
  99.  
  100. trav = trav->children[letloc];
  101.  
  102. }
  103.  
  104.  
  105. }
  106.  
  107. dicsize++;
  108.  
  109. trav->is_word = true;
  110. // /printf("%d", trav->is_word);
  111.  
  112. // /printf("\n");
  113.  
  114. }
  115.  
  116. // Close dictionary
  117. fclose(file);
  118.  
  119. // Indicate success
  120. return true;
  121. }
  122.  
  123. // Returns number of words in dictionary if loaded else 0 if not yet loaded
  124. unsigned int size(void)
  125. {
  126. if (isloaded == true) {
  127. // printf("%i\n", dicsize);
  128. return dicsize;
  129. }
  130. else {
  131. return 0;
  132. }
  133. }
  134.  
  135. // Returns true if word is in dictionary else false
  136. bool check(const char *word)
  137. {
  138.  
  139. int len = strlen(word);
  140. // printf("%i ", len);
  141.  
  142. int letlocCheck = 0;
  143. trav = root;
  144.  
  145. for (int i = 0; i < len; i++) {
  146.  
  147. char letter = tolower(word[i]);
  148.  
  149. if (letter == '\'') {
  150.  
  151. letlocCheck = 26;
  152.  
  153. }
  154. else {
  155. letlocCheck = letter - 97;
  156. // printf("%i", letlocCheck);
  157. }
  158.  
  159. if (trav->children[letlocCheck] == NULL) {
  160.  
  161. // printf("false ");
  162. return false;
  163.  
  164. }
  165. else {
  166.  
  167. trav = trav->children[letlocCheck];
  168.  
  169. }
  170.  
  171. }
  172.  
  173. if (trav->is_word == true) {
  174.  
  175. // printf("true\n");
  176. return true;
  177.  
  178. }
  179. else {
  180.  
  181. // printf("false");
  182. return false;
  183.  
  184. }
  185.  
  186. }
  187.  
  188. // Unloads dictionary from memory, returning true if successful else false
  189. bool unload(void)
  190. {
  191.  
  192. node *cursor = root;
  193.  
  194. recurfree(cursor);
  195.  
  196. free(root);
  197.  
  198. return true;
  199. }
  200.  
  201. void recurfree(node *subroot) {
  202.  
  203. for (int i = 0; i < N; i++) {
  204.  
  205. if (subroot->children[i] != NULL) {
  206.  
  207. node *child = subroot->children[i];
  208.  
  209. recurfree(child);
  210.  
  211. free(child);
  212.  
  213. }
  214.  
  215. }
  216.  
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement