-
Notifications
You must be signed in to change notification settings - Fork 2
/
BKTree.h
59 lines (49 loc) · 1.69 KB
/
BKTree.h
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
/*******************************************************************************
Spell Checker using BK-Tree
This program is an implementation of a spell checker for English text. The
spell checker incorporares a database of known words, which is built from a
simple word list. The Burkhard-Keller Tree (BK-Tree for short) organizes the
word list for efficient searching.
BK-Tree implementation is based of the description in this article:
http://blog.notdot.net/2007/4/Damn-Cool-Algorithms-Part-1-BK-Trees
OTHER RESOURCES:
https://en.wikipedia.org/wiki/Levenshtein_distance
http://people.cs.vt.edu/shaffer/Book/C++3elatest.pdf (pg. 215 Sec. 6.3.2)
Created by Kyle Bludworth on 11/30/15.
Copyright © 2015 Kyle Bludworth. All rights reserved.
*******************************************************************************/
#ifndef BKTree_h
#define BKTree_h
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using std::string;
using std::vector;
using std::basic_string;
struct Node
{
string word;
size_t distance;
Node* leftChild;
Node* rightSibling;
};
class BKTree
{
private:
Node* root;
Node* createNode(string w, size_t d);
int min(int a, int b, int c);
size_t levenshteinDistance(string w1, string w2);
void recursiveSearch(Node* node, vector<string>& suggestions, string w,
size_t t, bool& wordFound);
bool inRange(size_t curDist, size_t minDist, size_t maxDist);
void printSuggestions(vector<string>& suggestions, bool wordFound);
public:
BKTree();
~BKTree();
void add(string w);
void cleanString(basic_string<char>& s);
void search(string w, int t);
};
#endif /* BKTree_h */