0

When it comes to checking a word, it seems like there is no connection between the nodes.

This where I am not getting the desired return. It's right under the check function.

if(checker -> childern[temp] != NULL){
        checker = checker -> childern[temp];

        printf("\not Null \n");
}

typedef struct node{
    bool is_word;
    struct node* childern[27];
}
node;
node* pointer;

int dictionarySize = 0;

bool check(const char* word)
{
    node* checker = pointer;
    for(int i = 0; i < strlen(word); i++){
        if(isalpha(word[i])){
            int temp = tolower(word[i]) - 'a';
            // printf("Check Letter: %d\n", temp);
            if(checker -> childern[temp] != NULL){
               checker = checker -> childern[temp];

               printf("\not Null \n");
            }

            else{
                    // printf("\nSize: %lu\n", strlen(word));
                    // printf("\ni: %d\n", i);

                    // printf("\nWe have a null\n");
                    return false;
                }
            }
    }

    return true;
}

/**
 * Loads dictionary into memory.  Returns true if successful else false.
 */
bool load(const char* dictionary)
{
    //Reading the dictionary
    FILE* book = fopen(dictionary, "r");
    if(book == NULL){
        printf("Unable to open the dictionary !");
        return false;
    }

    // Declaring nodes.
    node* root = calloc(1, sizeof(node));
    pointer = root;
    // root -> is_word = false;

    //First letter from dictionary
    int currentLetter = fgetc(book);
    int index;

    while(currentLetter != EOF ){

        if(currentLetter == '\n'){
            pointer -> is_word = true; //THIS NEEDS TO BE FIXED
            pointer = root;
            dictionarySize ++;

            printf("\n");
        }

        else if(isalpha(currentLetter)){
            index = tolower(currentLetter) - 'a';
            if(pointer -> childern[index] == NULL){
                pointer -> childern[index] = calloc(1, sizeof(node));
                pointer = pointer -> childern[index];

                //  printf("Letter: %c\n", index + 97);
            }

            else{
                pointer = pointer -> childern[index];
                // printf("No Null\n");
            }
        }
        currentLetter = fgetc(book);


    }
    if(!ferror(book)){
        fclose(book);
        return true;
    }
    else{
        fclose(book);
        return false;
    }


}

1 Answer 1

1

Your problem lies in what's not in the code - two oversights.

The code doesn't actually check is_word when it actually gets to the end of a word in the trie. Say that your dictionary had the words cat and caterpillar. Because it doesn't make the check, it would accept caterp as a word.

The code doesn't handle apostrophes. It gets by because, mathematically, it treats an apostrophe as an 'e', so the error is never exposed.

There may be other issues, but these are certainly the most important.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

4
  • Thank you for responding. I am still unclear about your explanation "The code doesn't actually check is_word when it actually gets to the end of a word in the trie." Are you referring to the check or load function? If it's the check function, doesn't the for loop state that we have reached the end of the word? I will deal with apostrophes after I get my basic word check working. I am using the small dictionary and a text file that I created myself. Apr 13, 2016 at 16:13
  • I think the problem lies within { if(checker -> childern[temp] != NULL){ checker = checker -> childern[temp]; printf("\not Null \n"); } } My printf() never executes. Apr 13, 2016 at 16:19
  • Checking for is_word is done in check(). Just because the code gets to the end of a word being tested in the trie, it doesn't mean that it's actually a word. I gave an example in my answer. You MUST check that is_word is actually set. You could be testing a "word" that is just "q". If queen is in the dictionary, and you get to the q node, it's still not a word.
    – Cliff B
    Apr 13, 2016 at 16:44
  • Fair enough. I think before I can even check for is_word, my node links are lost right after the load function. pointer is a global variable. node* checker = pointer; int temp = tolower(word[i]) - 'a'; if(checker -> childern[temp] != NULL) is always false. Apr 13, 2016 at 17:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .