FuzzySharp Documentation

repository·master·Indexed 21 days ago

https://github.com/jakebayer/fuzzysharp

A C# .NET implementation of the Python FuzzyWuzzy algorithm for fuzzy string matching. It provides the Fuzz class for similarity scoring methods like Ratio, PartialRatio, and TokenSortRatio, and the Process class for extracting the best matches from collections of strings or complex objects. Includes ScorerCache for managing stateless scorer instances and support for custom preprocessing for non-English characters.

Tokens
1.5K
Snippets
6
Records
6
Agent score
24%

What's inside FuzzySharp

  1. How to handle non-English characters and custom preprocessing

    master

    By default, FuzzySharp's preprocessor is optimized for English and strips non-alphanumeric characters. To support other languages (e.g., accented characters like 'é') or to prevent any string alteration, you can provide a custom lambda function as the process parameter in Process methods.

    Using (s) => s as the processor will pass the string through to the algorithm without any modifications.

    var query = "strng";
    var choices = new [] { "stríng", "stráng", "stréng" };
    // Use (s) => s to prevent stripping accented characters
    var results = Process.ExtractAll(query, choices, (s) => s);
  2. Extract matches from complex objects

    master

    The Process methods can operate on collections of complex objects rather than just strings. You must provide a selector function (the process parameter) that tells the library which property or index of the object should be used for the string comparison.

    var events = new[] 
    {
        new[] { "chicago cubs vs new york mets", "CitiField", "2011-05-11" },
        new[] { "new york yankees vs boston red sox", "Fenway Park", "2011-05-11" }
    };
    var query = new[] { "new york mets vs chicago cubs", "CitiField", "2017-03-19" };
    
    // Match based on the first element (index 0) of the arrays
    var best = Process.ExtractOne(query, events, strings => strings[0]);
    
    // 'best' contains the full array object, the score, and the index
  3. Extract matches from a collection using Process

    master

    The Process class allows you to find the best matches for a query string within a collection of choices. By default, it uses WeightedRatio and a 'full process' (which lowercases strings).

    Available extraction methods:

    • ExtractOne: Returns the single best match as a tuple containing the string, score, and index.
    • ExtractTop: Returns a specified number of top matches.
    • ExtractAll: Returns all matches in the collection.
    • ExtractSorted: Returns all matches, sorted by score in descending order.

    You can use the cutoff parameter in ExtractAll to only return matches above a certain score threshold.

    // Extract the single best match
    Process.ExtractOne("cowboys", new[] { "Atlanta Falcons", "New York Jets", "Dallas Cowboys" });
    // Returns: (string: Dallas Cowboys, score: 90, index: 2)
    
    // Extract top 3 matches
    Process.ExtractTop("goolge", new[] { "google", "bing", "facebook" }, limit: 3);
    
    // Extract all matches above a score of 40
    Process.ExtractAll("goolge", new[] { "google", "bing", "googleplus" }, cutoff: 40);
  4. Use Fuzz scoring methods for string similarity

    master

    The Fuzz class provides various algorithms to calculate similarity scores between two strings. Scores typically range from 0 to 100.

    Available scoring methods include:

    • Ratio: Simple similarity ratio.
    • PartialRatio: Finds the best match of a substring.
    • TokenSortRatio: Sorts tokens alphabetically before matching.
    • TokenSetRatio: Handles duplicate tokens by looking at the intersection of sets.
    • TokenInitialismRatio: Matches based on the initials of words (e.g., 'NASA' vs 'National Aeronautics and Space Administration').
    • TokenAbbreviationRatio: Matches based on abbreviations (requires a PreprocessMode).
    • WeightedRatio: A more complex, weighted similarity algorithm.
    Fuzz.Ratio("mysmilarstring","myawfullysimilarstirng") // 72
    Fuzz.PartialRatio("similar", "somewhresimlrbetweenthisstring") // 71
    Fuzz.TokenSortRatio("order words out of","  words out of order") // 100
    Fuzz.TokenSetRatio("fuzzy was a bear", "fuzzy fuzzy fuzzy bear") // 100
    Fuzz.TokenInitialismRatio("NASA", "National Aeronautics and Space Administration") // 89
    Fuzz.TokenAbbreviationRatio("bl 420", "Baseline section 420", PreprocessMode.Full) // 40
    Fuzz.WeightedRatio("The quick brown fox", "the quick brown fox") // 95
  5. Use ScorerCache to manage scorer instances

    master

    Scoring strategies in FuzzySharp are stateless. To avoid the overhead of instantiating new scorer objects repeatedly, use ScorerCache.Get<T>(). This ensures that only one instance of each scorer type is ever created and reused.

    var ratio = ScorerCache.Get<DefaultRatioScorer>();
    var partialRatio = ScorerCache.Get<PartialRatioScorer>();
    var tokenSet = ScorerCache.Get<TokenSetScorer>();
    var weighted = ScorerCache.Get<WeightedRatioScorer>();