I am using tries to complete the spellcheck but I just can't keep up with the logic. I've implemented load()
and check()
as much as I could but I'm sure it has some logic flaw. The unload()
function is a mess; I don't know how it works. The walkthorughs told me nothing about the size()
, so that's not going to be completed anytime soon. I also have no idea how to travel through the tries -- how to go from one letter to the next.
I've watched all the shorts and walkthroughs more than once. Over two weeks now and it's getting very annoying and boring since I don't have a solid understanding of its logic.
If someone could guide me from here and perhaps maybe give me some hints, I would appreciate it. I have really hit a wall here. Anything would be helpful. Thanks.
/**
* Implements a dictionary's functionality.
*/
#include <stdbool.h>
#include <stdio.h>
#include "dictionary.h"
// create a variable to count number of words loaded
int wordCounter = 0;
// create structure for tries
typedef struct node
{
bool is_word;
struct node *children[27];
}
node;
// create root for starting point
node *root = malloc(sizeof(node));
if (root == NULL)
{
unload();
return false;
}
/**
* Returns true if word is in dictionary else false.
*/
bool check(const char *word2)
{
node *cursor = root;
// iterate through letters in word
for (int i = 0, x < strlen(word2); i < x; i++)
{
// calculate the letter placement
if (isalpha(word2[i]))
{
int placement = tolower(word2[i]) - 97;
}
else if (word2[i] == '\'')
{
int placement = 26;
}
// check for misspelled word first
if (cursor->children[i] == NULL)
{
printf("Misspelled word.\n");
return false;
}
// otherwise move to next letter
else
{
cursor = cursor->children[placement];
}
}
// check if that word exists once done iterating
if (cursor->is_word == true)
{
return true;
}
return false;
}
/**
* Loads dictionary into memory. Returns true if successful else false.
*/
bool load(const char *dictionary)
{
// open dictionary
FILE *openDictionary = fopen(dictionary, "r");
if (openDictionary == NULL)
{
fprintf(stderr, "Could not open Dictionary.\n");
return false;
}
// the longest word (45 chars) + 1 char for null terminator
char word[46];
/*
* scan dictionary word by word
*/
while (fscanf(openDictionary, "%s", word) != EOF)
{
// iterate through the characters of the source word
for (int i = 0, x = strlen(word); i < x; i++)
{
// calculate the place in child to place the chararcter
if (isalpha(word[i]))
{
int placeChild = tolower(word[i]) - 97;
}
else if (word[i] == '\'')
{
int placeChild = 26;
}
// there's no character in the root child
if (root->children[placeChild] == NULL)
{
// malloc a node * for the new character
node *new_node = malloc(sizeof(node));
if (new_node == NULL)
{
unload();
return false;
}
// add character from source to the new child
strcat(new_node->children[placeChild], tolower(word[i]));
// make the latest linked child point to the new node *
root->children[placeChild] = new_node;
// move to that child
root = root->children[placeChild];
}
// if there's already a character in array of trie
else
{
// move to next child
root = root->children[placeChild];
}
}
// mark character as word's final letter
new_node->is_word = true;
// increment word counter
wordCounter++;
}
// dictionary sucessfuly loaded
return true;
}
/**
* Returns number of words in dictionary if loaded else 0 if not yet loaded.
*/
unsigned int size(void)
{
// the counter has had some variable change (denotes the running of load())
if (wordCounter > 0)
{
return wordCounter;
}
// load() has not yet been loaded (based on counter variable change)
else
{
return 0;
}
}
/**
* Unloads dictionary from memory. Returns true if successful else false.
*/
bool unload(void)
{
node *cursor = root;
while (cursor != NULL)
{
node *tmp = cursor;
cursor = cursor->next;
free(tmp);
}
// unload unsuccessful
return false;
}