Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
BitchX
GitHub Repository: BitchX/BitchX1.3
Path: blob/master/dll/europa/cse476/lexicon.cpp
1074 views
1
// Copyright (c) 1998-99, Ed Schlunder
2
3
#include <iostream.h>
4
#include <fstream.h>
5
#include "lexicon.h"
6
7
// ---------------------------------------------------------------------------
8
// Class Implementation: Lexicon
9
// ---------------------------------------------------------------------------
10
Lexicon::Lexicon(char *fileName) {
11
char buffer[1000];
12
ifstream inFile;
13
14
// specify that this is an empty lexicon currently
15
rootPtr = currPtr = NULL;
16
17
// try to open the lexicon file
18
inFile.open(fileName);
19
if(!inFile) {
20
cout << "Lexicon file can not be opened." << endl;
21
return;
22
}
23
24
// read in each lexicon entry and insert it into our BST
25
while(inFile) {
26
inFile.getline(buffer, 999);
27
28
// ignore blank lines
29
if(strlen(buffer) < 2) continue;
30
31
insert(new genericNode(buffer));
32
}
33
}
34
35
void Lexicon::insert(genericNode *newWord) {
36
#ifdef DEBUG
37
cout << "insertLexicon: [" << *newWord << "]" << endl;
38
#endif
39
40
// if this is an empty lexicon, insert at the top of the BST
41
if(rootPtr == NULL) {
42
rootPtr = new lexicalNode;
43
rootPtr->node = newWord;
44
rootPtr->leftPtr = NULL;
45
rootPtr->rightPtr = NULL;
46
}
47
else
48
insert(newWord, rootPtr);
49
}
50
51
void Lexicon::insert(genericNode *newWord, lexicalNode *root) {
52
lexicalNode *tmp;
53
54
// does newWord belong to the right of this node?
55
if(strcmp(root->node->word(), newWord->word()) > 0)
56
// ASSERT: newWord belongs to the right of this word
57
if(root->rightPtr) {
58
// nodes already exist to the right, follow link down and try again
59
insert(newWord, root->rightPtr);
60
return;
61
}
62
else
63
// make a new node to the right
64
tmp = root->rightPtr = new lexicalNode;
65
else
66
if(root->leftPtr) {
67
insert(newWord, root->leftPtr);
68
return;
69
}
70
else
71
tmp = root->leftPtr = new lexicalNode;
72
73
tmp->node = newWord;
74
tmp->leftPtr = NULL;
75
tmp->rightPtr = NULL;
76
}
77
78
Lexicon::~Lexicon() {
79
// -----------------------
80
// THIS NEEDS IMPLEMENTING
81
// -----------------------
82
}
83
84
genericNode *Lexicon::currNode(void) {
85
if(currPtr)
86
return currPtr->node;
87
88
return NULL;
89
}
90
91
// ---------------------------------------------------------------------------
92
93
bool Lexicon::lookupNext(void) {
94
if(currPtr->leftPtr == NULL)
95
return false;
96
else
97
return lookupWord(currPtr->leftPtr, currPtr->node->word());
98
}
99
100
bool Lexicon::lookupWord(char *word) {
101
return lookupWord(rootPtr, word);
102
}
103
104
bool Lexicon::lookupWord(lexicalNode *root, char *word) {
105
int cmp;
106
107
// if we've hit a dead end, we know this word doesn't exist in the tree
108
if(root == NULL) return false;
109
110
// is this word the one we were looking for?
111
cmp = strcmp(root->node->word(), word);
112
if(cmp == 0) {
113
currPtr = root;
114
return true;
115
}
116
117
if(cmp > 0)
118
return lookupWord(root->rightPtr, word);
119
else
120
return lookupWord(root->leftPtr, word);
121
}
122
123