/** * Attempts to find the closest match to the search node. * @param node * @return A match that is within the best edit distance of the search node. */ public T findBestNode(BKTreeNode node) { BKTreeNode bestNode; _root.findBestMatch(node, Int32.MaxValue, out bestNode); return((T)bestNode); }
public virtual int findBestMatch(BKTreeNode node, int bestDistance, out BKTreeNode bestNode) { int distanceAtNode = calculateDistance(node); bestNode = node; if(distanceAtNode < bestDistance) { bestDistance = distanceAtNode; bestNode = this; } int possibleBest = bestDistance; foreach (Int32 distance in _children.Keys) { if (distance < distanceAtNode + bestDistance) { possibleBest = _children[distance].findBestMatch(node, bestDistance, out bestNode); if (possibleBest < bestDistance) { bestDistance = possibleBest; } } } return bestDistance; }
public virtual int findBestMatch(BKTreeNode node, int bestDistance, out BKTreeNode bestNode) { int distanceAtNode = calculateDistance(node); bestNode = node; if (distanceAtNode < bestDistance) { bestDistance = distanceAtNode; bestNode = this; } int possibleBest = bestDistance; foreach (Int32 distance in _children.Keys) { if (distance < distanceAtNode + bestDistance) { possibleBest = _children[distance].findBestMatch(node, bestDistance, out bestNode); if (possibleBest < bestDistance) { bestDistance = possibleBest; } } } return(bestDistance); }
/** * This method will find all the close matching Nodes within * a certain threshold. For instance, to search for similar * strings, threshold set to 1 will return all the strings that * are off by 1 edit distance. * @param searchNode * @param threshold * @return */ public Dictionary <T, Int32> query(BKTreeNode searchNode, int threshold) { Dictionary <BKTreeNode, Int32> matches = new Dictionary <BKTreeNode, Int32>(); _root.query(searchNode, threshold, matches); return(copyMatches(matches)); }
/** * Attempts to find the closest match to the search node. * @param node * @return A match that is within the best edit distance of the search node. */ public Dictionary <T, Int32> findBestNodeWithDistance(BKTreeNode node) { BKTreeNode bestNode; int distance = _root.findBestMatch(node, Int32.MaxValue, out bestNode); _matches.Clear(); _matches.Add((T)bestNode, distance); return(_matches); }
public void Add(string value) { if (rootNode != null) { AddToChildren(rootNode, value); } else { rootNode = new BKTreeNode(value); } }
public virtual void add(BKTreeNode node) { int distance = calculateDistance(node); if (_children.ContainsKey(distance)) { _children[distance].add(node); } else { _children.Add(distance, node); } }
private void AddToChildren(BKTreeNode node, string value) { int levenstheinDist = GetLevenstheinDistance(node.Value, value); if (!node.Children.ContainsKey(levenstheinDist)) { node.Children.Add(levenstheinDist, new BKTreeNode(value)); } else { node.Children.TryGetValue(levenstheinDist, out BKTreeNode current); AddToChildren(current, value); } }
private void Match(BKTreeNode node, string word, int tolerance, ICollection <string> result) { int dist = GetLevenstheinDistance(node.Value, word); if (dist <= tolerance) { result.Add(node.Value); } int lowerDist = dist - tolerance > 0 ? dist - tolerance : 1; int upperDist = dist + tolerance; for (int i = upperDist; i >= lowerDist; i--) { if (node.Children.TryGetValue(i, out BKTreeNode child)) { Match(child, word, tolerance, result); } } }
public virtual void query(BKTreeNode node, int threshold, Dictionary <BKTreeNode, Int32> collected) { int distanceAtNode = calculateDistance(node); if (distanceAtNode == threshold) { collected.Add(this, distanceAtNode); return; } if (distanceAtNode < threshold) { collected.Add(this, distanceAtNode); } for (int distance = (distanceAtNode - threshold); distance <= (threshold + distanceAtNode); distance++) { if (_children.ContainsKey(distance)) { _children[distance].query(node, threshold, collected); } } }
public virtual void query(BKTreeNode node, int threshold, Dictionary<BKTreeNode, Int32> collected) { int distanceAtNode = calculateDistance(node); if (distanceAtNode == threshold) { collected.Add(this, distanceAtNode); return; } if (distanceAtNode < threshold) { collected.Add(this, distanceAtNode); } for (int distance = (distanceAtNode - threshold); distance <= (threshold + distanceAtNode); distance++) { if (_children.ContainsKey(distance)) { _children[distance].query(node, threshold, collected); } } }
public int[] Data { get; private set; } // String of symbols // The only required method of abstract class BKTreeNode override protected int calculateDistance(BKTreeNode node) { return DistanceMetric.calculateLeeDistance( this.Data, ((ExampleNodeRecord)node).Data); }
protected abstract int calculateDistance(BKTreeNode node);
/** * Attempts to find the closest match to the search node. * @param node * @return The edit distance of the best match */ public int findBestDistance(BKTreeNode node) { BKTreeNode bestNode; return(_root.findBestMatch(node, Int32.MaxValue, out bestNode)); }
override protected int calculateDistance(BKTreeNode node) { return DistanceMetric.calculateLeeDistance( this.Data, ((TestNode)node).Data); }