Advertisement
YaBoiSwayZ

Dictionary

Jul 28th, 2023 (edited)
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.38 KB | Source Code | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <stdbool.h>
  5. #include <string.h>
  6.  
  7. #include "dictionary.h"
  8.  
  9. #define NUM_BUCKETS 675
  10.  
  11. // Node/linked list for hash table
  12. typedef struct node
  13. {
  14.     char word[LENGTH + 1];
  15.     struct node *next;
  16. } node;
  17.  
  18. // Represents hash table
  19. typedef struct
  20. {
  21.     node *next[NUM_BUCKETS];
  22. } hash_table;
  23.  
  24. hash_table *table;
  25. unsigned int word_count = 0;
  26.  
  27. // Hashes word to a number
  28. unsigned int hash(const char *word)
  29. {
  30.     // A simple hash function based on the first two characters of the word
  31.     int b = 26 * (toupper(word[0]) - 'A') + toupper(word[1]) - 'A';
  32.     return b % NUM_BUCKETS;
  33. }
  34.  
  35. // Loads dictionary into memory, returning true if successful, else false
  36. bool load(const char *dictionary)
  37. {
  38.     // Allocate memory for the hash table
  39.     table = malloc(sizeof(hash_table));
  40.     if (table == NULL)
  41.     {
  42.         return false;
  43.     }
  44.  
  45.     // Initialize hash table and linked lists to NULL
  46.     for (int i = 0; i < NUM_BUCKETS; i++)
  47.     {
  48.         table->next[i] = NULL;
  49.     }
  50.  
  51.     // Open dictionary file
  52.     FILE *file = fopen(dictionary, "r");
  53.     if (file == NULL)
  54.     {
  55.         return false;
  56.     }
  57.  
  58.     char word[LENGTH + 1];
  59.  
  60.     // Load words from the dictionary into the hash table
  61.     while (fscanf(file, "%s", word) != EOF)
  62.     {
  63.         // Create a new node for each word
  64.         node *new_node = malloc(sizeof(node));
  65.         if (new_node == NULL)
  66.         {
  67.             fclose(file);
  68.             return false;
  69.         }
  70.  
  71.         // Copy the word into the node
  72.         strcpy(new_node->word, word);
  73.  
  74.         // Hash the word to get the index in the hash table
  75.         int index = hash(word);
  76.  
  77.         // Insert the node at the beginning of the linked list
  78.         new_node->next = table->next[index];
  79.         table->next[index] = new_node;
  80.  
  81.         // Increment word count
  82.         word_count++;
  83.     }
  84.  
  85.     // Close the file
  86.     fclose(file);
  87.  
  88.     return true;
  89. }
  90.  
  91. // Returns true if word is in dictionary else false
  92. bool check(const char *word)
  93. {
  94.     // Convert the word to lowercase for case-insensitive comparison
  95.     char lowercase_word[LENGTH + 1];
  96.     for (int i = 0; word[i] != '\0'; i++)
  97.     {
  98.         lowercase_word[i] = tolower(word[i]);
  99.     }
  100.     lowercase_word[strlen(word)] = '\0';
  101.  
  102.     // Hash the word to get the index in the hash table
  103.     int index = hash(lowercase_word);
  104.  
  105.     // Traverse the linked list at the given index
  106.     node *cursor = table->next[index];
  107.     while (cursor != NULL)
  108.     {
  109.         // Compare the word with the node's word in the linked list
  110.         if (strcmp(cursor->word, lowercase_word) == 0)
  111.         {
  112.             return true;
  113.         }
  114.         cursor = cursor->next;
  115.     }
  116.  
  117.     return false;
  118. }
  119.  
  120. // Returns number of words in dictionary if loaded, else 0 if not yet loaded
  121. unsigned int size(void)
  122. {
  123.     return word_count;
  124. }
  125.  
  126. // Unloads dictionary from memory, returning true if successful, else false
  127. bool unload(void)
  128. {
  129.     for (int i = 0; i < NUM_BUCKETS; i++)
  130.     {
  131.         // Traverse the linked list at each index and free memory for each node
  132.         node *cursor = table->next[i];
  133.         while (cursor != NULL)
  134.         {
  135.             node *temp = cursor;
  136.             cursor = cursor->next;
  137.             free(temp);
  138.         }
  139.     }
  140.  
  141.     // Free memory for the hash table
  142.     free(table);
  143.     return true;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement