· 8 years ago · Apr 16, 2018, 05:50 PM
1// Implements a dictionary's functionality
2
3#include <ctype.h>
4#include <stdio.h>
5#include <stdbool.h>
6#include <stdlib.h>
7#include <string.h>
8#include <strings.h>
9
10#include "dictionary.h"
11
12// definition to avoid magic numbers in hash function/table
13#define ALPHABET 26
14
15// node data structure
16typedef struct nodedef
17{
18 char word[LENGTH + 1];
19 struct nodedef *next;
20}
21node;
22
23// hash function prototype
24unsigned int myHash(char *word);
25
26// global variables
27int indexHash = 0;
28int sizeDict = 0;
29
30// array for hash table
31node *hashtable[ALPHABET] = {NULL};
32
33
34
35
36
37// Returns true if word is in dictionary else false
38bool check(const char *word)
39{
40 char tmp[LENGTH + 1];
41 int len = strlen(word);
42
43 for (int i = 0; i < len; i++)
44 {
45 tmp[i] = tolower(word[i]);
46 }
47
48 tmp[len] = '\0';
49
50 // hash word to find index
51 indexHash = myHash(tmp);
52
53 // head pointer node
54 node *head = hashtable[indexHash];
55
56 // cursor pointer points to same memory location as head pointer at index
57 node *cursor = head;
58
59 // traverse linked list
60 while (cursor != NULL)
61 {
62 if (strcasecmp(cursor->word, tmp) != 0)
63 cursor = cursor->next;
64 else
65 return true;
66 }
67
68 return false;
69}
70
71
72
73// Loads dictionary into memory, returning true if successful else false
74bool load(const char *dictionary)
75{
76 // open dictionary file
77 FILE *dict = fopen(dictionary, "r");
78
79 // check if dictionary file exists and can be opened
80 if (!dict)
81 {
82 fprintf(stderr, "Could not open dictionary file.\n");
83 return false;
84 }
85
86 char word[LENGTH + 1];
87
88 // scan dictionary file word by word
89 while (fscanf(dict, "%s", word) != EOF)
90 {
91 // obtain index through hashing the word
92 indexHash = myHash(word);
93
94 //TODO move all of the lines for adding words to a separate addWord() function
95
96 // create new node pointer for new word
97 node *newNode = malloc(sizeof(node));
98
99 // in case there's no memory for new node
100 if (newNode == NULL)
101 {
102 unload();
103 return false;
104 }
105
106 // add word to node
107 strcpy(newNode->word, word);
108
109 // head pointer node
110 node *head = hashtable[indexHash];
111
112 // in case hashtable array at indexHash is empty
113 if (hashtable[indexHash] == NULL)
114 {
115 head = newNode;
116 newNode->next = NULL;
117 }
118 // in case hashtable array at indexHash is not empty
119 else
120 {
121 newNode->next = head;
122 head = newNode;
123 }
124
125 // TODO move all of the above to addWord() function
126
127 // increase dictionary word count
128 sizeDict++;
129
130 free(newNode);
131
132 }
133
134 // close dictionary
135 fclose(dict);
136 return true;
137}
138
139
140
141// Returns number of words in dictionary if loaded else 0 if not yet loaded
142unsigned int size(void)
143{
144 return sizeDict;
145}
146
147
148
149// Unloads dictionary from memory, returning true if successful else false
150bool unload(void)
151{
152
153 // head pointer node
154 node *head = hashtable[indexHash];
155
156 node *cursor = head;
157 while(cursor != NULL)
158 {
159 node *temp = cursor;
160 cursor = cursor->next;
161 free(temp);
162 }
163
164 return false;
165}
166
167
168
169//uber simple temporary hash function to begin solving speller
170unsigned int myHash(char *word)
171{
172 int sum = 0;
173
174 for (int i = 0, j = strlen(word); i < j; i++)
175 {
176 sum += word[i];
177 }
178
179 return sum % ALPHABET;
180}