0

I know this question has been asked many times before, but so far none of the posts i read seem congruent with the exact problem i'm experiencing. When i run Speller with the (default) large dictionary and ralph.txt this is the result:

MISSPELLED WORDS

When
I
I
I
Principal
Skinner
Ralph
Wiggum

WORDS MISSPELLED:     8
WORDS IN DICTIONARY:  143091
WORDS IN TEXT:        20
TIME IN load:         0.11
TIME IN check:        0.00
TIME IN size:         0.01
TIME IN unload:       0.02
TIME IN TOTAL:        0.14

The correct outcome should only flag one word (Wiggum) as misspelled, instead i get eight (three of which are a recurring capital I). These are my load, check and search functions:

LOAD:

bool load(const char *dictionary)
{
    char word[46] = {'\0'}; // create array to hold words that are being read
    uint32_t hash[4];                /* Output for the hash */
    uint32_t seed = 42;              /* Seed value for hash */
    uint32_t index = 0;     // variable to store index obtained from hash

    FILE* inptr = fopen(dictionary, "r"); // open the dictionary file

    while (fscanf(inptr, "%s", word) != EOF) // read dictionary word by word
    {
        MurmurHash3_x64_128(word, strlen(word), seed, hash);
        index = hash[3] & hashmask(18);

        if(hash_table[index] == NULL)
        {
            hash_table[index] = create_ll(word);
            if(hash_table[index] == NULL)
                return false;
        }
        else
        {
            hash_table[index] = insert_ll(hash_table[index], word);
            if(hash_table[index] == NULL)
                return false;
        }

            for(int i = 0; i < 45; i++) // flush the char array so it can be used again upon next iteration
        {
            word[i] = '\0';
        }

    }

    fclose(inptr);
    return true;
}

CHECK:

bool check(const char *word)
{
    bool found = false;
    uint32_t hash[4];                /* Output for the hash */
    uint32_t seed = 42;              /* Seed value for hash */
    uint32_t index = 0;     // variable to store index obtained from hash

    char* temp = malloc(strlen(word)+1); // allocate enough space to copy string
    strcpy(temp, word);

    for(int j = 0; j < strlen(temp); j++)
        tolower(temp[j]);

    MurmurHash3_x64_128(temp, strlen(word), seed, hash);
    index = hash[3] & hashmask(18);

    if(hash_table[index] != NULL)
        found = search(hash_table[index], temp);

    free(temp);
    return found;
}

SEARCH:

bool search(node* list, const char* word)
{
    node* trav = list;

    while(trav != NULL)
    {
        if(strcmp(word, trav->word) == 0)
        {
                return true;
        }
        else
            trav = trav->next;
    }

    return false;
}

I have a feeling something is somehow going wrong with the decapitalization, but i can't quite seem to figure it out. Any help would be greatly appreciated.

2

Without doing an in-depth check, something jumped out at me immediately. All of the "misspelled" words start with a capital letter. Then, I see this:

for(int j = 0; j < strlen(temp); j++)
    tolower(temp[j]);

The call to tolower() takes whatever is inside the parentheses and returns the lowercase version of that char. It doesn't alter what is inside the parentheses. The code needs to store the return value in a var. As coded, the return value is not stored anywhere, so it is discarded and temp[j] remains unaltered. Bottom line is this for loop does nothing, so later, the hash of the word is incorrect.

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

1
  • With regards to all the "misspelled" words starting with a capital letter, i noticed the same thing. Hence my suspicion that something was going awry with the decapitalization. You hit the nail on the head, i should've realized that the argument to tolower() is passed by value. As soon as i made the appropriate changes, the whole program worked as expected. Thanks a lot for your help! :-) – PvtWitt Feb 12 '18 at 22:41

You must log in to answer this question.

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