Advertisement
Guest User

SPELLER

a guest
Jul 28th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. // Implements a dictionary's functionality
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4. #include <stdlib.h>
  5. #include <strings.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8. #include "dictionary.h"
  9.  
  10.  
  11.  
  12. // Represents a node in a hash table
  13. typedef struct node
  14. {
  15. char word[LENGTH + 1];
  16. struct node *next;
  17. }
  18. node;
  19.  
  20. // Number of buckets in hash table
  21. const unsigned int N = 17576;
  22.  
  23. // Hash table
  24. node *table[N];
  25.  
  26. // Assign qty of words imported from dictionary as a global value.
  27. int words = 0;
  28.  
  29. // Returns true if word is in dictionary else false
  30. bool check(const char *word)
  31. {
  32. //maximum length of any word is 45 char so allocating space for 45 char
  33. char *lowerWord = malloc(45 * sizeof(char));
  34. // Change word to lower case for has table
  35. for (int i = 0, n = strlen(word); i < n; i++)
  36. {
  37. lowerWord[i] = tolower(word[i]);
  38. }
  39.  
  40. int bucket = hash(lowerWord);
  41.  
  42. node *cursor = table[bucket];
  43.  
  44. //find matching word by traversing list one by one
  45. while(cursor != NULL)
  46. {
  47. if(strcasecmp((cursor -> word),word)== 0)
  48. {
  49. return true;
  50. }
  51. cursor = cursor -> next;
  52. }
  53. free(lowerWord);
  54. free(cursor);
  55. return false;
  56. }
  57.  
  58. // Hashes word to a number
  59. // REFERENCE: https://twpower.github.io/160-hash-table-implementation-in-cpp-en
  60.  
  61. unsigned int hash(const char *word)
  62. {
  63. int hash = 401;
  64.  
  65. while (*word != '\0') {
  66. hash = ((hash << 4) + (unsigned int)(*word)) % N;
  67. word++;
  68. }
  69.  
  70. return hash % N;
  71. }
  72.  
  73. // Loads dictionary into memory, returning true if successful else false
  74. bool load(const char *dictionary)
  75. {
  76. //open dictionary file
  77. FILE *file = fopen(dictionary, "r");
  78. if (file == NULL)
  79. {
  80. return false;
  81. }
  82. char tempword[LENGTH + 1];
  83. int index = 0;
  84.  
  85. //read strings from file
  86. while (fscanf(file, "%s", tempword)!= EOF)
  87. {
  88. // Determine hash
  89. unsigned int bucket = hash(tempword);
  90.  
  91. //allocate new storage location to extend the list
  92. node *n = malloc(sizeof(node));
  93. if (n == NULL)
  94. {
  95. return false;
  96. }
  97.  
  98. //write temp word to word paramter
  99. strcpy(n -> word, tempword);
  100.  
  101. // revise pointers
  102. n -> next = table[bucket];
  103. table[bucket] = n;
  104.  
  105. words++;
  106.  
  107. //close file - finished manipulating it
  108. fclose(file);
  109. }
  110.  
  111. if (words == 143091)
  112. {
  113. return true;
  114. }
  115. else
  116. {
  117. return false;
  118. }
  119.  
  120.  
  121. }
  122.  
  123. // Returns number of words in dictionary if loaded else 0 if not yet loaded
  124. unsigned int size(void)
  125. {
  126. return words;
  127. }
  128.  
  129. // Unloads dictionary from memory, returning true if successful else false
  130. bool unload(void)
  131. {
  132. for(int i = 0; i < N; i++)
  133. {
  134. node *cursor = table[i];
  135. node *temp = cursor;
  136. free(temp);
  137. cursor = cursor -> next;
  138.  
  139. }
  140. return true;
  141. }
  142.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement