### Perform String Comparison Tasks in C# Source: https://context7.com/turnerj/quickenshtein/llms.txt Demonstrates spell checking, fuzzy matching, similarity scoring, and batch processing using Quickenshtein. ```csharp using System; using System.Collections.Generic; using System.Linq; using Quickenshtein; // Spell checking - find closest matches string[] dictionary = { "apple", "application", "apply", "banana", "bandana", "appreciate" }; string misspelled = "aplication"; var suggestions = dictionary .Select(word => new { Word = word, Distance = Levenshtein.GetDistance(misspelled, word) }) .OrderBy(x => x.Distance) .Take(3) .ToList(); Console.WriteLine($"Suggestions for '{misspelled}':"); foreach (var suggestion in suggestions) { Console.WriteLine($" {suggestion.Word} (distance: {suggestion.Distance})"); } // Output: // Suggestions for 'aplication': // application (distance: 1) // appreciate (distance: 5) // apple (distance: 6) // Fuzzy string matching with threshold bool IsSimilar(string s1, string s2, int maxDistance) { return Levenshtein.GetDistance(s1, s2) <= maxDistance; } Console.WriteLine(IsSimilar("Hello", "Hallo", 1)); // Output: True Console.WriteLine(IsSimilar("Hello", "World", 2)); // Output: False // Similarity score calculation (0.0 to 1.0) double GetSimilarity(string s1, string s2) { int distance = Levenshtein.GetDistance(s1, s2); int maxLength = Math.Max(s1.Length, s2.Length); if (maxLength == 0) return 1.0; return 1.0 - ((double)distance / maxLength); } Console.WriteLine($"Similarity: {GetSimilarity("Saturday", "Sunday"):P1}"); // Output: Similarity: 62.5% Console.WriteLine($"Similarity: {GetSimilarity("Hello", "Hello"):P1}"); // Output: Similarity: 100.0% // Batch processing with optimal options var options = CalculationOptions.Default; // Use single-threaded for small strings string[] names = { "John Smith", "Jon Smith", "John Smyth", "Jane Smith" }; string searchName = "John Smith"; var matches = names .Where(name => Levenshtein.GetDistance(searchName, name, options) <= 2) .ToList(); Console.WriteLine($"Fuzzy matches for '{searchName}':"); foreach (var match in matches) { Console.WriteLine($" {match}"); } // Output: // Fuzzy matches for 'John Smith': // John Smith // Jon Smith // John Smyth ``` -------------------------------- ### Configuring CalculationOptions for Levenshtein Source: https://context7.com/turnerj/quickenshtein/llms.txt Shows how to configure CalculationOptions for controlling multi-threading and work distribution. Includes pre-configured options and custom settings for CPU-intensive tasks. ```csharp using Quickenshtein; // Pre-configured options // CalculationOptions.Default: // - EnableThreadingAfterXCharacters = int.MaxValue (threading disabled) // CalculationOptions.DefaultWithThreading (.NET Core): // - EnableThreadingAfterXCharacters = 16000 // - MinimumCharactersPerThread = 4000 // CalculationOptions.DefaultWithThreading (.NET Framework): // - EnableThreadingAfterXCharacters = 4000 // - MinimumCharactersPerThread = 1000 // Custom options for CPU-intensive workloads var highThroughputOptions = new CalculationOptions { // Threshold for enabling multi-threading // Threading kicks in when target string length exceeds this value EnableThreadingAfterXCharacters = 8000, // Minimum characters each thread should process // Prevents overhead from too many small work units // Number of threads = targetLength / MinimumCharactersPerThread // (capped at Environment.ProcessorCount) MinimumCharactersPerThread = 2000 }; // Example: String comparison workflow string[] documents = { new string('a', 10000), new string('b', 10000), new string('c', 10000) }; string searchTerm = new string('a', 10000); foreach (var doc in documents) { int dist = Levenshtein.GetDistance(searchTerm, doc, highThroughputOptions); Console.WriteLine($"Distance to document: {dist}"); } // Output: // Distance to document: 0 // Distance to document: 10000 // Distance to document: 10000 // Memory efficiency check - single-threaded mode has zero allocations var noAllocOptions = CalculationOptions.Default; for (int i = 0; i < 10000; i++) { // This loop generates zero garbage collection pressure Levenshtein.GetDistance("testing", "testing123", noAllocOptions); } ``` -------------------------------- ### Calculate Levenshtein Distance in C# Source: https://github.com/turnerj/quickenshtein/blob/main/readme.md Demonstrates basic usage, enabling multi-threading, and configuring custom calculation thresholds for performance tuning. ```csharp using Quickenshtein; // Common usage (uses default CalculationOptions with threading disabled) var distance1 = Levenshtein.GetDistance("Saturday", "Sunday"); // Enable threading var distance2 = Levenshtein.GetDistance("Saturday", "Sunday", CalculationOptions.DefaultWithThreading); // Custom calculation options (helps with tuning for your specific workload and environment - you should benchmark your configurations on your system) var distance3 = Levenshtein.GetDistance("Saturday", "Sunday", new CalculationOptions { EnableThreadingAfterXCharacters = 10000, MinimumCharactersPerThread = 25000 }); ``` -------------------------------- ### Configure Levenshtein Distance with CalculationOptions Source: https://context7.com/turnerj/quickenshtein/llms.txt Uses CalculationOptions to manage multi-threading thresholds and performance tuning for large string comparisons. ```csharp using Quickenshtein; // Use default threading options (optimized thresholds per runtime) // .NET Core: threading after 16000 chars, 4000 chars minimum per thread // .NET Framework: threading after 4000 chars, 1000 chars minimum per thread int distance1 = Levenshtein.GetDistance( "Saturday", "Sunday", CalculationOptions.DefaultWithThreading ); Console.WriteLine($"Threaded distance: {distance1}"); // Output: Threaded distance: 3 // Explicit default options (single-threaded, no allocations) int distance2 = Levenshtein.GetDistance( "Hello World", "Hello World!", CalculationOptions.Default ); Console.WriteLine($"Default distance: {distance2}"); // Output: Default distance: 1 // Custom calculation options for specific workloads var customOptions = new CalculationOptions { // Enable threading only for very large strings EnableThreadingAfterXCharacters = 10000, // Ensure each thread has enough work MinimumCharactersPerThread = 25000 }; string hugeText1 = new string('a', 20000); string hugeText2 = new string('b', 20000); int hugeDistance = Levenshtein.GetDistance(hugeText1, hugeText2, customOptions); Console.WriteLine($"Huge text distance: {hugeDistance}"); // Output: Huge text distance: 20000 // Disable threading entirely (default behavior) var noThreadingOptions = new CalculationOptions { EnableThreadingAfterXCharacters = int.MaxValue // Never enable threading }; int singleThreaded = Levenshtein.GetDistance("test", "text", noThreadingOptions); Console.WriteLine($"Single-threaded: {singleThreaded}"); // Output: Single-threaded: 1 ``` -------------------------------- ### CalculationOptions Configuration Source: https://context7.com/turnerj/quickenshtein/llms.txt Configures the behavior of the Levenshtein distance calculation, specifically regarding multi-threading thresholds and work distribution. ```APIDOC ## CalculationOptions Configuration ### Description Defines how the library handles multi-threading during distance calculations. Users can specify thresholds to balance CPU usage and performance. ### Parameters #### Request Body - **EnableThreadingAfterXCharacters** (int) - Required - The string length threshold at which multi-threading is enabled. - **MinimumCharactersPerThread** (int) - Required - The minimum number of characters assigned to each thread to minimize overhead. ``` -------------------------------- ### Levenshtein.GetDistance with ReadOnlySpan Source: https://context7.com/turnerj/quickenshtein/llms.txt Demonstrates using the ReadOnlySpan overload for high-performance string comparison without allocations. Ideal for substrings and performance-critical code. ```csharp using System; using Quickenshtein; // Using ReadOnlySpan directly ReadOnlySpan source = "Hello World".AsSpan(); ReadOnlySpan target = "Hello World!".AsSpan(); int distance1 = Levenshtein.GetDistance(source, target); Console.WriteLine($"Span distance: {distance1}"); // Output: Span distance: 1 // Working with substrings without allocations string fullText = "The quick brown fox jumps over the lazy dog"; ReadOnlySpan word1 = fullText.AsSpan(4, 5); // "quick" ReadOnlySpan word2 = fullText.AsSpan(35, 4); // "lazy" int subDistance = Levenshtein.GetDistance(word1, word2); Console.WriteLine($"Substring distance: {subDistance}"); // Output: Substring distance: 4 // With CalculationOptions ReadOnlySpan largeSpan1 = new string('x', 5000).AsSpan(); ReadOnlySpan largeSpan2 = new string('y', 5000).AsSpan(); int spanThreaded = Levenshtein.GetDistance( largeSpan1, largeSpan2, CalculationOptions.DefaultWithThreading ); Console.WriteLine($"Large span distance: {spanThreaded}"); // Output: Large span distance: 5000 // Empty span handling ReadOnlySpan emptySpan = ReadOnlySpan.Empty; ReadOnlySpan testSpan = "test".AsSpan(); int emptyDistance = Levenshtein.GetDistance(testSpan, emptySpan); Console.WriteLine($"Empty span distance: {emptyDistance}"); // Output: Empty span distance: 4 ``` -------------------------------- ### Levenshtein.GetDistance (String Overload) Source: https://context7.com/turnerj/quickenshtein/llms.txt Calculates the Levenshtein Distance between two strings using default options (single-threaded, no allocations). ```APIDOC ## Levenshtein.GetDistance (String Overload) ### Description Calculates the Levenshtein Distance between two strings. This overload accepts standard .NET strings and uses default calculation options (single-threaded, no memory allocations). Returns the minimum number of single-character edits (insertions, deletions, or substitutions) required to transform one string into the other. ### Method GET (conceptual, as this is a library function) ### Endpoint N/A (Library function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```csharp using Quickenshtein; int distance = Levenshtein.GetDistance("Saturday", "Sunday"); Console.WriteLine($"Distance: {distance}"); ``` ### Response #### Success Response (int) - **distance** (int) - The Levenshtein distance between the two input strings. #### Response Example ```json 3 ``` ``` -------------------------------- ### Calculate Levenshtein Distance with String Overload Source: https://context7.com/turnerj/quickenshtein/llms.txt Calculates the edit distance between two strings using default single-threaded settings. Null inputs are treated as empty strings. ```csharp using Quickenshtein; // Basic usage - calculate edit distance between two strings int distance = Levenshtein.GetDistance("Saturday", "Sunday"); Console.WriteLine($"Distance: {distance}"); // Output: Distance: 3 // Comparing similar strings int typoDistance = Levenshtein.GetDistance("Hello World", "He1lo Wor1d"); Console.WriteLine($"Typo distance: {typoDistance}"); // Output: Typo distance: 2 // Case-sensitive comparison int caseDistance = Levenshtein.GetDistance("Hello World", "hello world"); Console.WriteLine($"Case distance: {caseDistance}"); // Output: Case distance: 2 // Empty string handling int emptyDistance = Levenshtein.GetDistance("test", ""); Console.WriteLine($"Empty distance: {emptyDistance}"); // Output: Empty distance: 4 // Identical strings return zero int identical = Levenshtein.GetDistance("abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz"); Console.WriteLine($"Identical: {identical}"); // Output: Identical: 0 // Null handling - null is treated as empty string int nullDistance = Levenshtein.GetDistance(null, "test"); Console.WriteLine($"Null distance: {nullDistance}"); // Output: Null distance: 4 ``` -------------------------------- ### Levenshtein.GetDistance with CalculationOptions Source: https://context7.com/turnerj/quickenshtein/llms.txt Calculates the Levenshtein Distance between two strings with customizable calculation options, including multi-threading. ```APIDOC ## Levenshtein.GetDistance with CalculationOptions ### Description This overload accepts `CalculationOptions` to fine-tune the calculation behavior, including enabling multi-threading for large string comparisons. This is essential for optimal performance when working with strings of 8000+ characters. ### Method GET (conceptual, as this is a library function) ### Endpoint N/A (Library function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```csharp using Quickenshtein; // Use default threading options int distance1 = Levenshtein.GetDistance("Saturday", "Sunday", CalculationOptions.DefaultWithThreading); // Custom calculation options var customOptions = new CalculationOptions { EnableThreadingAfterXCharacters = 10000, MinimumCharactersPerThread = 25000 }; string hugeText1 = new string('a', 20000); string hugeText2 = new string('b', 20000); int hugeDistance = Levenshtein.GetDistance(hugeText1, hugeText2, customOptions); ``` ### Response #### Success Response (int) - **distance** (int) - The Levenshtein distance between the two input strings. #### Response Example ```json 3 ``` ``` -------------------------------- ### Levenshtein.GetDistance (ReadOnlySpan Overload) Source: https://context7.com/turnerj/quickenshtein/llms.txt Calculates the Levenshtein distance between two ReadOnlySpan instances, enabling high-performance comparisons without heap allocations. ```APIDOC ## Levenshtein.GetDistance (ReadOnlySpan Overload) ### Description Calculates the Levenshtein distance between two character spans. This method is optimized for performance and avoids heap allocations, making it suitable for processing substrings or large datasets. ### Parameters #### Request Body - **source** (ReadOnlySpan) - Required - The source character span. - **target** (ReadOnlySpan) - Required - The target character span. - **options** (CalculationOptions) - Optional - Configuration for threading and performance thresholds. ### Response - **int** - The calculated Levenshtein distance between the two spans. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.