-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionary.c
158 lines (130 loc) · 2.82 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
/****************************************************************************
* dictionary.c
*
* Computer Science 50
* Problem Set 6
*
* Implements a dictionary's functionality.
***************************************************************************/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dictionary.h"
#define ALPHABETS 27
// Trie node
typedef struct trieNode {
bool isLeaf;
struct trieNode *child[ALPHABETS];
} trieNode;
// Prototypes
trieNode *getNode(void);
void insertInTrie(char*);
int getRelativePos(char);
bool inTrie(const char*);
bool deleteNode(trieNode*);
// Global variables
trieNode *rootNode;
size_t dictionarySize = 0;
int mc = 0;
/*
* Returns true if word is in dictionary else false.
*/
bool
check(const char *word)
{
char lower[LENGTH];
int i;
for (i = 0; i < strlen(word); ++i)
if(word[i] >= 'A' && word[i] <= 'Z') lower[i] = (char) word[i] + 32;
else lower[i] = (char) word[i];
lower[i] = '\0';
return inTrie(lower);
}
/*
* Loads dictionary into memory. Returns true if successful else false.
*/
bool
load(const char *dictionary)
{
char *word = NULL;
size_t len = 0;
FILE *fp = fopen(dictionary, "r");
if (fp == NULL) return false;
rootNode = getNode();
while (getline(&word, &len, fp) != -1) {
if (word[strlen(word) - 1] == '\n') word[strlen(word) - 1] = '\0'; // remove new line character
insertInTrie(word);
}
free(word);
fclose(fp);
return true;
}
/*
* Returns number of words in dictionary if loaded else 0 if not yet loaded.
*/
unsigned int
size(void)
{
return dictionarySize;
}
/*
* Unloads dictionary from memory. Returns true if successful else false.
*/
bool
unload(void)
{
return deleteNode(rootNode);
}
/*
* Get new node
*/
trieNode *getNode() {
trieNode *T = (trieNode *) malloc(sizeof(trieNode));
T -> isLeaf = false;
for (int i = 0; i < ALPHABETS; ++i)
T -> child[i] = NULL;
return T;
}
/*
* returns the pos relative to 'a'
*/
int getRelativePos(char ch) {
return (ch == '\'') ? 27 : ch % 'a';
}
void insertInTrie(char *word) {
trieNode *temp, *node = rootNode;
for (int i = 0; i < strlen(word); ++i)
{
int pos = getRelativePos(word[i]);
if (node -> child[pos] == NULL) {
temp = getNode();
node -> child[pos] = temp;
node = temp;
} else {
node = node -> child[pos];
}
}
node -> isLeaf = true;
++dictionarySize;
}
bool inTrie(const char *word) {
trieNode *node = rootNode;
for (int i = 0; i < strlen(word); ++i)
{
int pos = getRelativePos(word[i]);
if (node -> child[pos] == NULL)
return false;
node = node -> child[pos];
}
if (node -> isLeaf) return true;
else return false;
}
bool deleteNode(trieNode *node) {
if (node == NULL) return true;
for (int i = 0; i < ALPHABETS; ++i)
if (node -> child[i] != NULL) deleteNode
(node -> child[i]);
free(node);
return true;
}