Text quickstart - 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.

HomeText

Quickstart

Compare two strings in a few lines.

Install

dotnet add package Lodestar.Text

The package has no external dependencies on net10.0: it's pure .NET, with no Python at runtime. On netstandard2.0 it takes System.Memory, System.Numerics.Vectors and System.Text.Json — all three in-box on the modern target, so nothing new is actually being pulled in for consumers there.

Compare two strings

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.

Unicode: choosing the comparison unit

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/0001-the-foundations-target-frameworks-comparison-unit-persistence-and-versioning.md.

Next

⚠️ **GitHub.com Fallback** ⚠️