Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C Programming & Data Structures: Need help ASAP on spellchecker code for a hashtable! The code compiles but there is an issue the code that

C Programming & Data Structures: Need help ASAP on spellchecker code for a hashtable!

The code compiles but there is an issue the code that compares levenshtein values and places the value in the hashtable. Please help me correct it ASAP!! Code is provided below. ALSO, please check the "create & print array" section! Thank you!!

CODE:

#include "hashMap.h" #include #include #include #include #include

/** * Allocates a string for the next word in the file and returns it. This string * is null terminated. Returns NULL after reaching the end of the file. * @param file * @return Allocated string or NULL. */ char* nextWord(FILE* file) { int maxLength = 16; int length = 0; char* word = malloc(sizeof(char) * maxLength); while (1) { char c = fgetc(file); if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '\'') { if (length + 1 >= maxLength) { maxLength *= 2; word = realloc(word, maxLength); } word[length] = c; length++; } else if (length > 0 || c == EOF) { break; } } if (length == 0) { free(word); return NULL; } word[length] = '\0'; return word; }

/** * Loads the contents of the file into the hash map. * @param file * @param map */ void loadDictionary(FILE* file, HashMap* map) { // FIXME: implement char* word = nextWord(file); while (word) { hashMapPut(map, word, 1); free(word); word = nextWord(file); }

free(word); }

/************************* * Minimum function--returns the min * ******************/ int min(int a, int b, int c) { int minimum; minimum = a; if(b < minimum) { minimum = b; } if(c < minimum) { minimum = c; } return minimum; }

/********************* * min2 function * ********************/ int min2(int a, int b) { int min; if (a < b) min = a; else min = b; return min; }

/********************************** * HELPER FUNCTION * code sourced from https://en.wikipedia.org/wiki/Levenshtein_distance#Computing_Levenshtein_distance * code has been modified to work with hashtables * *********************************/

// len_s and len_t are the number of characters in string s and t respectively int LevenshteinDistance(const char *s, int len_s, const char *t, int len_t) { int cost;

/* base case: empty strings */ if (len_s == 0) return len_t; if (len_t == 0) return len_s;

/* test if last characters of the strings match */ if (s[len_s-1] == t[len_t-1]) cost = 0; else cost = 1;

/* return minimum of delete char from s, delete char from t, and delete char from both */ return min(LevenshteinDistance(s, len_s - 1, t, len_t ) + 1, LevenshteinDistance(s, len_s , t, len_t - 1) + 1, LevenshteinDistance(s, len_s - 1, t, len_t - 1) + cost); }

/** * Prints the concordance of the given file and performance information. Uses * the file input1.txt by default or a file name specified as a command line * argument. * @param argc * @param argv * @return */ int main(int argc, const char** argv) { // FIXME: implement HashMap* map = hashMapNew(1000); FILE* file = fopen("dictionary.txt", "r"); //if file not found if(file==NULL) { perror("Error"); } clock_t timer = clock(); loadDictionary(file, map); timer = clock() - timer; printf("Dictionary loaded in %f seconds ", (float)timer / (float)CLOCKS_PER_SEC); fclose(file); char inputBuffer[256]; int quit = 0; while (!quit) //Step 4: continue to prompt user for word until they type quit { printf("Enter a word or \"quit\" to quit: "); scanf("%s", inputBuffer); // Implement the spell checker code here..

/***** NEED HELP WITH THIS FUNCTION!!!! * Compare input Buffer to words in the dictionary, computing their Levenshtein distance. * Store that distance as the value for each key in the table * *********************/ for (int i = 0; i < map->capacity; i++) { HashLink* link = map->table[i]; if (link != NULL) { while (link != NULL) { int bLength = strlen(inputBuffer); int mapL = map->capacity; char* word = link->key; int minVal = LevenshteinDistance(word, mapL, inputBuffer, bLength); hashMapPut(map, word, minVal); link = link->next; } } }

/***** * Traverse down hash table, checking each bucket. * Jump out if you find an exact matching dictionary word * print a message that "word spelled correctly" * **********************/ if (hashMapContainsKey(map, inputBuffer)) { printf("That word is spelled correctly! "); }

/************ * If the input Buffer did not match any of the dictionary words exactly, * generate an array of 5 words that are the closest matches to input Buffer based * on the LOWEST Levenshtein distance. * print the array INCLUDING the message "did you mean ...?" * ********************/ else { char* array[6]; for(int idx = 0; idx < 6; idx++){ for (int i = 0; i < map->capacity; i++) { HashLink* link = map->table[i]; HashLink* nLink = map->table[i+1]; if (nLink != NULL) { int val = *(hashMapGet(map, link->key)); int val2 = *(hashMapGet(map, nLink->key)); if(min2(val, val2) == val){ array[idx] = link->key; } else { array[idx] = nLink->key; } nLink = nLink->next; } link = link->next; } }

/* Print the new map */ for (int i = 0; i < 6; i++) { printf("%sDid you mean ...? ", array[i]); printf(" "); } }

if (strcmp(inputBuffer, "quit") == 0) { quit = 1; } } hashMapDelete(map); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

More Books

Students also viewed these Databases questions

Question

Persuasive Speaking Organizing Patterns in Persuasive Speaking?

Answered: 1 week ago