0
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

#include "dictionary.h"

#define HASHTABLE_SIZE 10000

typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

node *hashtable[HASHTABLE_SIZE];
int wordcount = 0;

bool check(const char *word)
{
    int n = strlen(word);
    char word_copy[LENGTH + 1];
    for (int i = 0; i < n; i++)
    {
        word_copy[i] = tolower(word[i]);
    }
    word_copy[n] = '\0';
    int h = hash(word_copy);
    node *cursor = hashtable[h];

    while (cursor != NULL)
    {
        if (strcasecmp(cursor->word, word_copy) == 0)
        {
            return true;
        }
        
        else
        {
            cursor = cursor->next;
        }
    }
    return false;
}



unsigned int hash(const char *word)
{
    unsigned int hash_index = 0;
    for (int i = 0, n = strlen(word); i < n; i++)
    {
        hash_index = (hash_index << 2) ^ word[i];
    }
    return hash_index % HASHTABLE_SIZE;
}
// taken from: https://gist.github.com/choaimeloo/ffb96f7e43d67e81f0d44c08837f5944


bool load(const char *dictionary)
{
    //open dictionary file
    FILE *file = fopen(dictionary, "r");
    //NULL CONDITION
    if (file == NULL)
    {
        return false;
    }
    //redefine word after opening file
    char word[LENGTH + 1];
    //scan word data from file
    while (fscanf(file, "%s", word) != EOF) {
        //define new node
        node *newnode = malloc(sizeof(node));
        //NULL condition
        if (newnode == NULL)
        {
            //free malloced data
            free(newnode);
            return false;
        }
        //copy word into word array in newnode
        strcpy(newnode->word, word);
        //define index as:
        //word array within newnode
        int index = hash(newnode->word);
        //define new pointer as head which:
        //points to hashtable array with "index" as argument
        //hence hastable size = index = newnode->word
        node *head = hashtable[index];
        if (head == NULL)
        {
            //NULL condition
            hashtable[index] = newnode;
            wordcount++;
        }
        //if head(i.e hashtable[index]) isn't empty
        else
        {
            //next string in newnode is allotted to:
            //hashtable array taking arg. index
            newnode->next = hashtable[index];
            //re assign hashtable[index] to newnode 
            hashtable[index] = newnode;
            wordcount++;
        }
    }
    fclose(file);
    return false;
}

unsigned int size(void)
{
    return wordcount;
}

bool unload(void)
{
    node *head = NULL;
    node *cursor = head;
    // freeing linked lists
    while (cursor != NULL)
    {
        node *temp = cursor;
        cursor = cursor->next;
        free(temp);
    }
    return false;
}

on running: ./speller texts/lalaland.txt

it gives: Could not load dictionaries/large.

i've been trying to find the issue for a while and i just can't seem to put my finger on what's wrong in the code. any suggestions?

0

After the load function does all it's work, it will return false;.

What happens when load returns false to speller.c instead of true?

| improve this answer | |

You must log in to answer this question.

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