summaryrefslogtreecommitdiffstats
path: root/labb5/lib/StanfordCPPLib/lexicon.cpp
blob: 242542f7788f1d85025be825b50ed14139a29339 (plain) (blame)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * File: lexicon.cpp
 * -----------------
 * A lexicon is a word list. This lexicon is backed by two separate data
 * structures for storing the words in the list:
 *
 * 1) a DAWG (directed acyclic word graph)
 * 2) a Set<string> of other words.
 *
 * Typically the DAWG is used for a large list read from a file in binary
 * format.  The STL set is for words added piecemeal at runtime.
 *
 * The DAWG idea comes from an article by Appel & Jacobson, CACM May 1988.
 * This lexicon implementation only has the code to load/search the DAWG.
 * The DAWG builder code is quite a bit more intricate.
 */

#include <fstream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <stdint.h>
#include "error.h"
#include "lexicon.h"
#include "strlib.h"
using namespace std;

static void toLowerCaseInPlace(string & str);

/*
 * The DAWG is stored as an array of edges. Each edge is represented by
 * one 32-bit struct.  The 5 "letter" bits indicate the character on this
 * transition (expressed as integer from 1 to 26), the  "accept" bit indicates
 * if you accept after appending that char (current path forms word), and the
 * "lastEdge" bit marks this as the last edge in a sequence of childeren.
 * The bulk of the bits (24) are used for the index within the edge array for
 * the children of this node. The children are laid out contiguously in
 * alphabetical order.  Since we read edges as binary bits from a file in
 * a big-endian format, we have to swap the struct order for little-endian
 * machines.
 */

Lexicon::Lexicon() {
   edges = start = NULL;
   numEdges = numDawgWords = 0;
}

Lexicon::Lexicon(string filename) {
   edges = start = NULL;
   numEdges = numDawgWords = 0;
   addWordsFromFile(filename);
}

Lexicon::~Lexicon() {
   if (edges) delete[] edges;
}

/*
 * Swaps a 4-byte long from big to little endian byte order
 */

static uint32_t my_ntohl(uint32_t arg) {
   uint32_t result = ((arg & 0xff000000) >> 24) |
                     ((arg & 0x00ff0000) >> 8) |
                     ((arg & 0x0000ff00) << 8) |
                     ((arg & 0x000000ff) << 24);
   return result;
}

/*
 * Implementation notes: readBinaryFile
 * ------------------------------------
 * The binary lexicon file format must follow this pattern:
 * DAWG:<startnode index>:<num bytes>:<num bytes block of edge data>
 */

void Lexicon::readBinaryFile(string filename) {
   long startIndex, numBytes;
   char firstFour[4], expected[] = "DAWG";
   ifstream istr(filename.c_str(), IOS_IN | IOS_BINARY);
   if (false) my_ntohl(0);
   if (istr.fail()) {
      error("Couldn't open lexicon file " + filename);
   }
   istr.read(firstFour, 4);
   istr.get();
   istr >> startIndex;
   istr.get();
   istr >> numBytes;
   istr.get();
   if (istr.fail() || strncmp(firstFour, expected, 4) != 0
                   || startIndex < 0 || numBytes < 0) {
      error("Improperly formed lexicon file " + filename);
   }
   numEdges = numBytes/sizeof(Edge);
   edges = new Edge[numEdges];
   start = &edges[startIndex];
   istr.read((char *)edges, numBytes);
   if (istr.fail() && !istr.eof()) {
      error("Improperly formed lexicon file " + filename);
   }

#if defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN
   uint32_t *cur = (uint32_t *) edges;
   for (int i = 0; i < numEdges; i++, cur++) {
      *cur = my_ntohl(*cur);
   }
#endif

   istr.close();
   numDawgWords = countDawgWords(start);
}

int Lexicon::countDawgWords(Edge *ep) const {
   int count = 0;
   while (true) {
      if (ep->accept) count++;
      if (ep->children != 0) {
         count += countDawgWords(&edges[ep->children]);
      }
      if (ep->lastEdge) break;
      ep++;
   }
   return count;
}

/*
 * Check for DAWG in first 4 to identify as special binary format,
 * otherwise assume ASCII, one word per line
 */

void Lexicon::addWordsFromFile(string filename) {
   char firstFour[4], expected[] = "DAWG";
   ifstream istr(filename.c_str());
   if (istr.fail()) {
      error("Couldn't open lexicon file " + filename);
   }
   istr.read(firstFour, 4);
   if (strncmp(firstFour, expected, 4) == 0) {
      if (otherWords.size() != 0) {
         error("Binary files require an empty lexicon");
      }
      readBinaryFile(filename);
      return;
   }
   istr.seekg(0);
   string line;
   while (getline(istr, line)) {
      add(line);
   }
   istr.close();
}

int Lexicon::size() const {
   return numDawgWords + otherWords.size();
}

bool Lexicon::isEmpty() const {
   return size() == 0;
}

void Lexicon::clear() {
   if (edges) delete[] edges;
   edges = start = NULL;
   numEdges = numDawgWords = 0;
   otherWords.clear();
}

/*
 * Implementation notes: findEdgeForChar
 * -------------------------------------
 * Iterate over sequence of children to find one that
 * matches the given char.  Returns NULL if we get to
 * last child without finding a match (thus no such
 * child edge exists).
 */

Lexicon::Edge *Lexicon::findEdgeForChar(Edge *children, char ch) const {
   Edge *curEdge = children;
   while (true) {
      if (curEdge->letter == charToOrd(ch)) return curEdge;
      if (curEdge->lastEdge) return NULL;
      curEdge++;
   }
}

/*
 * Implementation notes: traceToLastEdge
 * -------------------------------------
 * Given a string, trace out path through the DAWG edge-by-edge.
 * If a path exists, return last edge; otherwise return NULL.
 */

Lexicon::Edge *Lexicon::traceToLastEdge(const string & s) const {
   if (!start) return NULL;
   Edge *curEdge = findEdgeForChar(start, s[0]);
   int len = (int) s.length();
   for (int i = 1; i < len; i++) {
      if (!curEdge || !curEdge->children) return NULL;
      curEdge = findEdgeForChar(&edges[curEdge->children], s[i]);
   }
   return curEdge;
}

bool Lexicon::containsPrefix(string prefix) const {
   if (prefix.empty()) return true;
   toLowerCaseInPlace(prefix);
   if (traceToLastEdge(prefix)) return true;
   foreach (string word in otherWords) {
      if (startsWith(word, prefix)) return true;
      if (prefix < word) return false;
   }
   return false;
}

bool Lexicon::contains(string word) const {
   toLowerCaseInPlace(word);
   Edge *lastEdge = traceToLastEdge(word);
   if (lastEdge && lastEdge->accept) return true;
   return otherWords.contains(word);
}

void Lexicon::add(string word) {
   toLowerCaseInPlace(word);
   if (!contains(word)) {
      otherWords.add(word);
   }
}

Lexicon::Lexicon(const Lexicon & src) {
   deepCopy(src);
}

Lexicon & Lexicon::operator=(const Lexicon & src) {
   if (this != &src) {
      if (edges != NULL) delete[] edges;
      deepCopy(src);
   }
   return *this;
}

void Lexicon::deepCopy(const Lexicon & src) {
   if (src.edges == NULL) {
      edges = NULL;
      start = NULL;
   } else {
      numEdges = src.numEdges;
      edges = new Edge[src.numEdges];
      memcpy(edges, src.edges, sizeof(Edge)*src.numEdges);
      start = edges + (src.start - src.edges);
   }
   numDawgWords = src.numDawgWords;
   otherWords = src.otherWords;
}

void Lexicon::mapAll(void (*fn)(string)) const {
   foreach (string word in *this) {
      fn(word);
   }
}

void Lexicon::mapAll(void (*fn)(const string &)) const {
   foreach (string word in *this) {
      fn(word);
   }
}

void Lexicon::iterator::advanceToNextWordInSet() {
   if (setIterator == setEnd) {
      currentSetWord = "";
   } else {
      currentSetWord = *setIterator;
      ++setIterator;
   }
}

void Lexicon::iterator::advanceToNextWordInDawg() {
   if (edgePtr == NULL) {
      edgePtr = lp->start;
   } else {
      advanceToNextEdge();
   }
   while (edgePtr != NULL && !edgePtr->accept) {
      advanceToNextEdge();
   }
}

void Lexicon::iterator::advanceToNextEdge() {
   Edge *ep = edgePtr;
   if (ep->children == 0) {
      while (ep != NULL && ep->lastEdge) {
         if (stack.isEmpty()) {
            edgePtr = NULL;
            return;
         } else {
            ep = stack.pop();
            currentDawgPrefix.resize(currentDawgPrefix.length() - 1);
         }
      }
      edgePtr = ep + 1;
   } else {
      stack.push(ep);
      currentDawgPrefix.push_back(lp->ordToChar(ep->letter));
      edgePtr = &lp->edges[ep->children];
   }
};

static void toLowerCaseInPlace(string & str) {
   int nChars = str.length();
   for (int i = 0; i < nChars; i++) {
      str[i] = tolower(str[i]);
   }
}