Text bktree nearest - CyrilB1531/lodestar GitHub Wiki

Development build. This page describes main, not a released package. The latest published Lodestar.Text is 0.6.0 — read its documentation.

HomeTextString indexing

BkTree.Nearest

The n nearest indexed items, however far away they are.

public IReadOnlyList<BkTreeMatch> Nearest(string query, int count)

Parametersquery is the string to search around; count is how many hits to return.

ReturnsIReadOnlyList<BkTreeMatch>, distance ascending, ties by insertion order. Shorter than count when the tree holds fewer items, and empty when it holds none or count is 0.

Example — the three closest entries, with no radius to choose.

using Lodestar.Text.Indexing;

BkTree tree = BkTree.OverLevenshtein();
tree.AddRange(["book", "books", "boo", "cook", "cake"]);

IReadOnlyList<BkTreeMatch> nearest = tree.Nearest("bok", 3);
int closest = nearest[0].Distance;   // => 1
int found = nearest.Count;           // => 3

Remarks — this is the query that genuinely tightens as it runs: the radius starts unbounded and becomes the worst distance held once count hits are found, so every improvement narrows the remaining search. The answer is nevertheless exactly the first count of WithinDistance at an unbounded radius — the shrinking radius is an optimization, and a test asserts it changes nothing.

Use it when you do not know a radius. When you do, WithinDistance prunes from the first node rather than after the count-th hit.

Applies to — net10.0, netstandard2.0.

See alsoBkTree.WithinDistance, BkTreeMatch.