Text 0.6.0 quickstart - CyrilB1531/lodestar GitHub Wiki
Lodestar.Text 0.6.0. This page is frozen at that release. Read the current documentation for what
mainsays now. A link to a decision or a migration page followsmain, and leaves the archive.
Compare two strings in a few lines.
dotnet add package Lodestar.TextThe package has no external dependencies on
net10.0: it's pure .NET, with no Python at runtime. Onnetstandard2.0it takesSystem.Memory,System.Numerics.VectorsandSystem.Text.Json— all three in-box on the modern target, so nothing new is actually being pulled in for consumers there.
using Lodestar.Text.Distances;
// Raw edit distance: number of insertions/deletions/substitutions.
int d = Levenshtein.Distance("kitten", "sitting"); // 3
// Normalized similarity in [0, 1]: 1 = identical.
double sim = Levenshtein.NormalizedSimilarity("kitten", "sitting"); // 0.5714…
// Normalized distance: 1 - similarity.
double nd = Levenshtein.NormalizedDistance("kitten", "sitting"); // 0.4286…string literals convert implicitly to ReadOnlySpan<char>, so no buffer is
allocated for the inputs.
Each of the three has a reference entry giving its parameters, its behaviour on
empty inputs and the trap that comes with it:
Levenshtein.Distance,
Levenshtein.NormalizedSimilarity
and Levenshtein.NormalizedDistance.
By default, comparison is over UTF-16 units (char) — the native .NET choice
and the fastest. To reproduce Python / rapidfuzz results exactly on characters
outside the Basic Multilingual Plane (emoji, rare ideographs), request code
point comparison:
using Lodestar.Text; // TextElement lives here, not in .Distances
// "a😀" -> "a": the emoji is ONE code point, but TWO UTF-16 units.
Levenshtein.Distance("a\U0001F600", "a"); // 2 (UTF-16 units)
Levenshtein.Distance("a\U0001F600", "a", TextElement.CodePoint); // 1 (like Python)This is Unicode pitfall #1 when porting from Python; it's documented in detail in
../decisions/0002-unicode-comparison-unit.md.
- Every distance, function by function — the reference entries
- From string to vector — bag of words, TF-IDF, cosine
- Semantic search with embeddings
- Migrating from rapidfuzz
- Which metric? — evaluating a model, and which number to report
- Python → C# equivalence table