TokenSortMatch 0.3.0
dotnet add package TokenSortMatch --version 0.3.0
NuGet\Install-Package TokenSortMatch -Version 0.3.0
<PackageReference Include="TokenSortMatch" Version="0.3.0" />
<PackageVersion Include="TokenSortMatch" Version="0.3.0" />
<PackageReference Include="TokenSortMatch" />
paket add TokenSortMatch --version 0.3.0
#r "nuget: TokenSortMatch, 0.3.0"
#:package TokenSortMatch@0.3.0
#addin nuget:?package=TokenSortMatch&version=0.3.0
#tool nuget:?package=TokenSortMatch&version=0.3.0
TokenSortMatch
Fast token-sort fuzzy best-matching for .NET. Zero dependencies.
Scores are bit-identical to RapidFuzz's
fuzz.token_sort_ratio(a, b, processor=utils.default_process) with banker's rounding
(apart from a few character classes, see Compatibility) — verified by
a randomized oracle test suite and a RapidFuzz cross-check — while batch matching runs
~7-10× faster than RapidFuzz's native cdist on short items (~7.7× on long ones)
and ~50-450× faster than per-pair Raffinert.FuzzySharp, under plain AVX2
(benchmarks),
via:
- transposed pattern bitmasks + bit-parallel LCS (Hyyrö 2004) in length-bucketed
16/32/64-bit lanes — 16, 8 or 4 items per
Vector256pass (auto opt-in; scalar fallback for CPUs without 256-bit SIMD), - allocation-free query normalization (char-map table, in-place token sort),
- exactly-rounded score upper bounds pruning item groups against the running best,
- 4-lane vectorized multi-block path (per-lane carry chains) for items whose normalized form exceeds 64 chars,
- bitmask tables that only hold rows for their own items' characters, so indexes over large alphabets (CJK) stay compact.
Usage
using TokenSortMatch;
// Pairwise
int score = TokenSort.Ratio("Лекционные занятия", "занятия ЛЕКЦИОННЫЕ!"); // 100
string norm = TokenSort.Normalize("The-Quick! Brown?? fox"); // "brown fox quick the"
// Batch: build an index over candidate items once, query many times
var index = new TokenSortIndex(["Лекционные занятия", "Практические занятия", "Экзамен"]);
BestMatch? best = index.FindBest("занятия лекционные 5", threshold: 80);
// -> BestMatch(ItemIndex: 0, Score: 95)
int[] scores = index.FindBestScores(queries, threshold: 80); // parallel, -1 if below
BestMatch?[] all = index.FindBestMatches(queries, threshold: 80); // parallel, null if below
// One query against every item: a score per item, in constructor order
int[] each = index.ScoreAll("занятия лекционные 5", threshold: 0); // -> [95, 60, 30]
index.ScoreAll("занятия лекционные 5", threshold: 80, buffer); // into a Span<int>: [95, -1, -1]
Semantics:
Normalize: letters and digits are lowercased culture-invariantly (İ→i, as in Unicode's simple case mapping), everything else → spaces, tokens sorted ordinally, joined with single spaces.- Score:
round(100 · (1 − indelDistance / (len1 + len2)))with banker's rounding, evaluated with the same double operations as RapidFuzz, so exact .5 scores round the same way; empty normalized strings score 0. FindBest*: thresholds ≤ 0 always match (scores are non-negative); ties return one deterministic best-scoring item.TokenSortIndexis immutable and thread-safe.ScoreAll: itemigetsRatio(query, items[i])if it reaches the threshold, else -1, so a threshold ≤ 0 returns every score. It runs on the calling thread and skips item groups whose lengths cannot reach the threshold. TheSpan<int>overload allocates nothing; it writes the firstCountentries.
Compatibility
- RapidFuzz
token_sort_ratio+default_process: identical, except that characters in Unicode categories No/Nl (²,½,Ⅳ,①) and characters above U+FFFF are separators here, and a few dozen letters added in recent Unicode versions are letters here but not in RapidFuzz's tables. - Raffinert.FuzzySharp:
Fuzz.Ratio(TokenSort.Normalize(a), TokenSort.Normalize(b)), rounded, is identical. Its ownFuzz.TokenSortRatio(a, b, StringPreprocessor.Full)sorts tokens with a culture-aware comparison and keepsİ, so it can differ on mixed-script text. - FuzzySharp (the original package):
PreprocessMode.Fullremoves every non-ASCII letter, so its token-sort scores differ on Cyrillic and other scripts. ItsFuzz.Ratiocomputes(len1 + len2 − dist) / (len1 + len2), which rounds a few exact .5 scores the other way.
License
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.