You can jump to SUGGESTION for quick answer.
I plugged your check()
function on my dictionary.c
program. I also have a trie in my dictionary.c
.
I don't know what is causing the segmentation fault. However, I found a behaviour that might be of your interest.
I ran the program with both my check()
function and yours against alice.txt
. I found that among the words that differed from our output, "org" was one of them.
I checked with gdb using a text that only contained that word. My check()
function considered it misspelled, and yours did not.
With that example I was able to notice the difference (using gdb). Your check()
function is evaluating the boolean
too soon.
Based on your pastebin file, on lines 58-61 you have:
if (leaf->is_word == true)
{
return true;
}
But at that point you are not at the end of the word you are checking. Look at it here with the context:
for (int i = 0, n = strlen(word); i < n; i++)
{
int alphaplace2 = tolower(word[i]) - 'a';
if (word[i] == '\'')
{
alphaplace2 = 26;
}
if (leaf->children[alphaplace2] != NULL)
{
leaf = leaf->children[alphaplace2];
//**CHECKING BOOLEAN**
if (leaf->is_word == true)
{
return true;
}
//**CHECKING BOOLEAN**
}
else
{
return false;
}
}
You want to check if it is a word after you know that each and every letter is in your trie in the right order.
If you check at every letter you will find a problem if your dictionary has:
cat
caterpillar
And your text has only this line:
caterina
I already tested this and the speller does not show caterina
as misspelled. This is because the check()
function is checking the boolean at every letter. And it does find a true
is_word
at the end of cat
.
SUGGESTION
I suggest you move your boolean check outside your loop. For example:
bool check(const char* word)
{
node* leaf = root;
for (int i = 0, n = strlen(word); i < n; i++)
{
int alphaplace2 = tolower(word[i]) - 'a';
if (word[i] == '\'')
{
alphaplace2 = 26;
}
if (leaf->children[alphaplace2] != NULL)
{
leaf = leaf->children[alphaplace2];
}
else
{
return false;
}
}
//**NOTICE BOOLEAN CHECK OUTSIDE LOOP**
if (leaf->is_word == true)
{
return true;
}
//**NOTICE BOOLEAN CHECK OUTSIDE LOOP**
return false;
}