public List <string> Search(string word, int tolerance = 2) { List <string> results = new List <string>(); if (word == null || word.Length == 0 || _root == null) { return(results); } Queue <Node> nodesToSearch = new Queue <Node>(); nodesToSearch.Enqueue(_root); while (nodesToSearch.Count > 0) { Node curr = nodesToSearch.Dequeue(); int dist = FuzzyStringMatching.LevenshteinDistance(curr.Word, word); int minDist = dist - tolerance; int maxDist = dist + tolerance; if (dist <= tolerance) { results.Add(curr.Word); } foreach (int key in curr.Keys.Where(key => key >= minDist && key <= maxDist)) { nodesToSearch.Enqueue(curr[key]); } } return(results); }
public void AddWord(string word) { if (_root == null) { _root = new Node(word); return; } Node curr = _root; int dist = FuzzyStringMatching.LevenshteinDistance(curr.Word, word); while (curr.ContainsKey(dist)) { if (dist == 0) { return; } curr = curr[dist]; dist = FuzzyStringMatching.LevenshteinDistance(curr.Word, word); } curr.AddChild(dist, word); }