6/12/2023 0 Comments Words for spelling correctorThe first string will eventually end up in the array but the second string is leaked. Here, each substr call allocates a string. For example: array = concat(substr(word, 0, i), substr(word, i+1, word_len-(i+1))) All of your functions allocate space for new strings, but you never free your temporary strings. Memory leaksĪfter reading your concatenation code again, I see a lot of memory leaks. At first I couldn't understand why you were taking the maximum of an array and an int, until I realized that you wrote a function called max() that didn't just return the maximum of two values. Res = checked_realloc(res, sizeof(char *) * res_max) Int deletion(char *word, char **array, int start_idx) Static void append(char *dst, int *dstLen, const char *src, int srcBegin, * srcBegin Starting index in the source string to copy from. * be updated with the new length after appending. * dstLen Current length of the destination string. * Takes a part of the source string and appends it to the destination string. Here is a rewrite of your concatenation functions, which doubled the speed of your program: static void *checked_malloc(int len) Copy the relevant parts of the string into that final string.You know this length because you are either deleting, modifying, or inserting into the original string. Allocate one string the length of the final string.If str2 is NULL, there is a memory leak.īeyond that, between concat() and substr(), you end up allocating, reallocating, and freeing many temporary strings.Doesn't check return value of malloc and realloc.First of all, the function concat() has a couple of problems: The way you build new strings is very messy. You could do this by initializing data to some function like this: dict.data = score_word(w) It would be nice if the words in the dictionary were scored in some fashion and the highest scored word would be returned (for example, words with common letters could score higher than ones with uncommon letters). Although it seems like a waste to find all possible words just to return the first one. I changed the code to just return the first word in the array and it did start to work. Perhaps yours had duplicate words such as Miro and miro. I didn't see anywhere in the program where data was ever incremented, except if you had a duplicate word in the dictionary. I suspect the problem was in the function max: if (e & ((int) e->data > max_size)) check mirorįirst off, I ran your program but it didn't ever find any word. The runtime speeds are faster than the original Python program in the post above, but I feel could still be improved upon. If (!readFile(dictionary, dict)) return -1 Ĭhar *corrected_word = correct(argv) Īs you can see, the accuracy is somewhat low. Void array_cleanup(char **array, int rows) If (fd str_size) || ((offset + limit) > str_size) || (str_size data > max_size)) Int readFile(const char* fileName, ENTRY dict) Refinement: In what ways could I shorten my code down a bit?Ĭhar *dictionary = "/usr/share/dict/words" Ĭonst char alphabet = "abcdefghijklmnopqrstuvwxyz0123456789" įor (char *s = word *s ++s) *s = tolower(*s) Speed: Are there any improvements that could be made to improve runtime speeds? I recently stumbled across this article on how to write a spelling corrector, and figured I'd try to have a go at it in C (mainly because the link at the end of the page for the C code is broken).Īccuracy: What can I do to make the program output a more accurate prediction of what it thinks the correct word should be?
0 Comments
Leave a Reply. |