TokenSortMatch 0.3.0

dotnet add package TokenSortMatch --version 0.3.0
                    
NuGet\Install-Package TokenSortMatch -Version 0.3.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TokenSortMatch" Version="0.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TokenSortMatch" Version="0.3.0" />
                    
Directory.Packages.props
<PackageReference Include="TokenSortMatch" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TokenSortMatch --version 0.3.0
                    
#r "nuget: TokenSortMatch, 0.3.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TokenSortMatch@0.3.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TokenSortMatch&version=0.3.0
                    
Install as a Cake Addin
#tool nuget:?package=TokenSortMatch&version=0.3.0
                    
Install as a Cake Tool

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 Vector256 pass (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. TokenSortIndex is immutable and thread-safe.
  • ScoreAll: item i gets Ratio(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. The Span<int> overload allocates nothing; it writes the first Count entries.

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 own Fuzz.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.Full removes every non-ASCII letter, so its token-sort scores differ on Cyrillic and other scripts. Its Fuzz.Ratio computes (len1 + len2 − dist) / (len1 + len2), which rounds a few exact .5 scores the other way.

License

MIT

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last Updated
0.3.0 122 10/31/2026
0.2.0 133 9/23/2026
0.1.0 158 7/9/2026