-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionary.c
172 lines (138 loc) · 3.16 KB
/
dictionary.c
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
// Implements a dictionary's functionality
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "dictionary.h"
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
// Number of buckets in hash table
const unsigned int N = 676; //26*26=676
// Hash table
node *table[N];
//count total number of words in dictionary
int word_count = 0;
//function for unloading dictionary
void unloading(node *tmp);
// Returns true if word is in dictionary, else false
bool check(const char *word)
{
char tmp[LENGTH + 1];
strcpy(tmp, word);
for (int i = 0 ; i <= strlen(tmp) ; i++)
{
tmp[i] = tolower(tmp[i]);
}
int h = hash(word);
node *search = table[h];
while (search != NULL)
{
if (strcmp(search->word, tmp) == 0)
{
return true;
}
else
{
search = search->next;
}
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
int o;
if (word[1] == '\0')
{
char c = word[0];
c = tolower(c);
o = (((int)c - 97) * 26) + 1; //by taking aa = 1, ab = 2, ac = 3....and so on till zz = 676
o = o - 1; //because 0 to 675 array
}
else if (word[1] == '\'')
{
char c = word[0];
c = tolower(c);
o = (((int)c - 97) * 26) + 1; //by taking aa = 1, ab = 2, ac = 3....and so on till zz = 676
o = o - 1; //because 0 to 675 array
}
else
{
char c[2];
c[0] = word[0];
c[1] = word[1];
for (int i = 0; i < 2; i++)
{
c[i] = tolower(c[i]);
}
o = (((int)c[0] - 97) * 26) + ((int)c[1] - 96); //by taking aa = 1, ab = 2, ac = 3....and so on till zz = 676
o = o - 1; //because 0 to 675 array
}
return o;
}
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
return false;
}
char word[LENGTH + 1];
while (fscanf(file, "%s", word) != EOF)
{
node *p = malloc(sizeof(node));
if (p == NULL)
{
unload();
return false;
}
p->next = NULL;
strcpy(p->word, word);
int h = hash(word);
node *first = table[h];
//add struct at starting as order doesn't matter to us
if (first == NULL)
{
table[h] = p;
word_count++;
}
else
{
p->next = table[h];
table[h] = p;
word_count++;
}
}
fclose(file);
return true;
}
// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
return word_count;
}
// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
for (int i = 0 ; i < N ; i++)
{
unloading(table[i]);
}
return true;
}
void unloading(node *tmp)
{
if (tmp == NULL)
{
return;
}
unloading(tmp -> next);
free(tmp);
}