-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspell.c
More file actions
executable file
·246 lines (195 loc) · 7.02 KB
/
spell.c
File metadata and controls
executable file
·246 lines (195 loc) · 7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "dictionary.h"
#include <stdbool.h>
#include <string.h>
typedef node* hashmap_t;
// Hash table is an array of linked lists.
/*
* Run istructions: ./spell_check a_tale_of_two_cities.txt wordlist.txt
*
* Your program should contain at least three functions, a function to load the list of words into the hash map (called load_dictionary), a function to check if a word is correctly spelled (called check_word), and a function called check_words to tie them all together. The parameter to check_words will be a file pointer containing lines of words separated by spaces, punctuation, etc. The function prototypes are as follows1:
* int check_words(FILE* fp, hashmap_t hashtable[], char* misspelled[]);
* bool load_dictionary(const char* dictionary_file, hashmap_t hashtable[]);
* bool check_word(const char* word, hashmap_t hashtable[]);
*/
/**
* Array misspelled is populated with words that are misspelled. Returns the length of misspelled.
*/
/**
* Inputs:
* fp: A file pointer to the document to check for spelling errors.
* hashtable: The hash table used to determine spelling
* misspelled: An empty char* array to be populated with misspelled words.
* This array will never be greater than 1000 words long.
*
* Returns:
* int: The number of words in the misspelled arary.
*
* Modifies:
* misspelled: This array will be filled with misspelled words.
*
* Example:
* int num_misspelled = check_words(text_file, hashtable, misspelled);
**/
int check_words(FILE* fp, hashmap_t hashtable[], char * misspelled[])
{
//Set int num_misspelled to 0.
int num_misspelled = 0;
while(num_misspelled <MAX_MISSPELLED) {
char* word = calloc(LENGTH+ 1,sizeof(char));
//While line in fp is not EOF (end of file):
if (fp == NULL) {
printf("No file found");
exit(0);
}
//while(fgets(line,LENGTH, fp) != NULL){
//if(fp)
//{
while(!feof(fp))
{
char* word_c = calloc(LENGTH+ 1,sizeof(char));
//Read the line.
//Split the line on spaces.
fscanf(fp,"%s",word);
//printf("%s\n", word);
////codeblock for cleaning up the word////
//For each word in line:
//Remove punctuation from beginning and end of word.
int i=0;
int j=0;
//cleanup line read, make all characters 0 at the newline
for(i; i<LENGTH+1; i++){
if (isupper(word[i])) {
word_c[j] = tolower(word[i]);
word[i]=0;
j++;
}
else if (isalpha(word[i])){
word_c[j] = word[i];
word[i]=0;
j++;
}
else if (ispunct(word[i]) ){ //skip character
word[i]=0;
}
else{
word[i]=0;
}
}
//printf("%s\n", word_c);
//If not check_word(word):
//append word to misspelled.
//Increment num_misspelled.
if(!check_word(word_c, hashtable)){
printf("%s\n", word_c);
misspelled[num_misspelled] = word_c;
num_misspelled++;
}
//free(word_c);
}
//fclose(fpr);
return num_misspelled;
}
//}
return num_misspelled;
}
//function to check if a word is correctly spelled
bool check_word(const char* word, hashmap_t hashtable[])
{
//Set int bucket to the output of hash_function(word).
int bucket = hash_function(word);
//Set hashmap_t cursor equal to hashmap[bucket].
hashmap_t cursor = (node*) malloc(sizeof(node));
cursor = hashtable[bucket];
//While cursor is not NULL:
if (cursor == NULL){
return false;
}else{
while(cursor != NULL ){
//If word equals cursor->word:
if( strcmp( cursor ->word, word)==0) {//this is failing when it should be true
return true;
}
//Set cursor to cursor->next.*/
cursor = cursor -> next;
}
}return false;
}
bool load_dictionary(const char* dictionary_file, hashmap_t hashtable[]) {
int bucket = 0;
//Initialize all values in hash table to NULL.
for (int i=0; i < HASH_SIZE;i++){
hashtable[i] = NULL;
}
FILE* fp = fopen(dictionary_file, "r");
if (fp == NULL) {
printf("No file found");
exit(0);
}
//Start reading in lines
//initial character for line read
//char *line = (char*) malloc(sizeof(char) * LENGTH);
char* line = calloc(LENGTH+ 1,sizeof(char));
//hashmap_t curr = (node*) malloc(sizeof(node));
while(fgets(line,LENGTH, fp) != NULL){
// For each line, get the word
//initialize char for cleaned up word
//char *word_c = (char*) malloc(sizeof(char) * LENGTH);
char* word_c = calloc(LENGTH+ 1,sizeof(char));
//printf("%s\n", line);
//malloc new node
hashmap_t new_node = (node*) malloc(sizeof(node));
////codeblock for cleaning up the word////
int i=0;
int j=0;
//cleanup line read, make all characters 0 at the newline
for(i; i<LENGTH+1; i++){
if (isupper(line[i])) {
word_c[j] = tolower(line[i]);
line[i]=0;
j++;
}
else if (isalpha(line[i])){
word_c[j] = line[i];
line[i]=0;
j++;
}
else if (ispunct(line[i]) ){ //skip character
line[i]=0;
}
else{
line[i]=0;
}
}
//printf("%s", &word_c);
new_node -> next = NULL;
strncpy(new_node -> word, word_c, LENGTH);
//printf("%s \n", new_node -> word );
bucket = hash_function(word_c);
//curr = hashtable[bucket];
if (hashtable[bucket] == NULL) //if there isn't a word, then add it and point to null
{
//new_node is assigned the word, and pointer next
hashtable[bucket]= new_node;
//hashtable[bucket] = new_node;
//printf("%i\n", bucket);
//printf("%s\n", (hashtable[bucket]->word));
}else
{
/*code for adding to end of list
while(curr-> next != NULL){
curr = curr->next;
}curr ->next = new_node;*/
//Add node to front of list, and
new_node->next = hashtable[bucket];
hashtable[bucket] = new_node;
//hashtable[bucket]=curr;
//printf("%s\n", hashtable[bucket]->word);
}
free(word_c);
}
fclose(fp);
return true;
}