### Batch Initialization of FuzzySet from Large Datasets Source: https://context7.com/axiak/fuzzyset/llms.txt Details efficient initialization of FuzzySet from large iterables by passing them directly to the constructor, which is more efficient than adding items individually. Includes an example of loading data from a gzipped file and querying the dataset. ```python import gzip from fuzzyset import FuzzySet # Load from file def load_cities(): with gzip.open('cities.gz', 'rb') as f: for line in f: yield line.decode().strip() # Efficient batch initialization fs = FuzzySet(load_cities()) # Query the dataset result = fs.get("San Fransisco") # Misspelled # Returns: [(0.94..., 'San Francisco')] result = fs.get("Steemboot Sprints") # Returns closest city names based on character similarity ``` -------------------------------- ### Fuzzy String Matching with get() in FuzzySet Source: https://context7.com/axiak/fuzzyset/llms.txt Illustrates how to perform fuzzy string matching using the `get()` method of the FuzzySet. This method returns a list of tuples containing (score, matched_string) for similar strings or `None` if no matches are found. It also shows how to provide a default value to handle missing matches gracefully. ```python from fuzzyset import FuzzySet # Build the dataset fs = FuzzySet() fs.add("michael axiak") fs.add("new york city") fs.add("san francisco") # Fuzzy search with typos result = fs.get("micael asiak") # Returns: [(0.8461538461538461, 'michael axiak')] result = fs.get("new yrok") # Returns: [(0.888..., 'new york city')] result = fs.get("completely unrelated") # Returns: None (no matches found) # Handle missing matches safely result = fs.get("xyz", default=[]) # Returns: [] instead of None ``` -------------------------------- ### High-Performance Cython Implementation of FuzzySet Source: https://context7.com/axiak/fuzzyset/llms.txt Explains how to use the `cFuzzySet` for improved performance, offering a 15% speedup with an identical API to the pure Python `FuzzySet`. It shows the recommended import pattern, building a large dataset, and standard usage, including adding elements and getting the size. ```python # Recommended import pattern for performance try: from cfuzzyset import cFuzzySet as FuzzySet except ImportError: from fuzzyset import FuzzySet # Build large dataset cities = [ "New York", "Los Angeles", "Chicago", "Houston", "Phoenix", "Philadelphia", "San Antonio", "San Diego", "Dallas", "San Jose" ] fs = FuzzySet(cities) # Usage identical to pure Python version result = fs.get("Los Angles") # Common typo # Returns: [(0.92..., 'Los Angeles')] # All features work the same fs.add("Seattle") print(len(fs)) # Returns: 11 ``` -------------------------------- ### FuzzySet Integration with Spell Checkers Source: https://context7.com/axiak/fuzzyset/llms.txt Provides an example of using FuzzySet as a backend for a spell-checking system. It defines a `spell_check` function that takes a word and a threshold, returning suggestions from a predefined dictionary if the word is misspelled or not found. ```python from fuzzyset import FuzzySet # Dictionary of correct spellings dictionary = FuzzySet([ "accommodate", "achievement", "acquaintance", "argument", "beginning", "believe", "calendar", "cemetery", "committee", "definitely", "embarrass", "environment", "experience" ]) def spell_check(word, threshold=0.8): """Return spelling suggestions if word is misspelled""" matches = dictionary.get(word) if matches is None: return f"No suggestions for '{word}'" # Perfect match means correctly spelled if matches[0][0] == 1.0: return f"'{word}' is spelled correctly" # Return suggestions above threshold suggestions = [ match for score, match in matches if score >= threshold ] if suggestions: return f"Did you mean: {', '.join(suggestions)}?" else: return f"No close matches for '{word}'" # Test the spell checker print(spell_check("accomodate")) # Did you mean: accommodate? print(spell_check("definately")) # Did you mean: definitely? print(spell_check("environment")) # 'environment' is spelled correctly print(spell_check("xyzabc")) # No suggestions for 'xyzabc' ``` -------------------------------- ### FuzzySet Initialization with Similarity Cutoff Source: https://context7.com/axiak/fuzzyset/llms.txt Demonstrates how to initialize FuzzySet with a list of strings and a relative similarity cutoff. This limits results to matches within 90% of the best match score. It shows how to retrieve results and how the cutoff affects the output. ```python from fuzzyset import FuzzySet fs_strict = FuzzySet( ["Boston", "Houston", "Austin", "Newton"], rel_sim_cutoff=0.9 ) result = fs_strict.get("Auston") # Returns only high-confidence matches # [(0.95..., 'Austin'), (0.91..., 'Boston')] # 'Houston' and 'Newton' excluded if score < 0.9 * top_score fs_all = FuzzySet( ["Boston", "Houston", "Austin", "Newton"], rel_sim_cutoff=1.0 ) result = fs_all.get("Auston") # May return more results including lower-scoring matches ``` -------------------------------- ### Initialize and Add Strings to FuzzySet Source: https://context7.com/axiak/fuzzyset/llms.txt Demonstrates how to initialize a FuzzySet object, either empty or with an initial iterable of strings, and how to add individual strings dynamically. It also shows the return value of the add() method indicating whether a string was newly added. ```python from fuzzyset import FuzzySet # Initialize empty set fs = FuzzySet() # Add strings individually fs.add("Michael Axiak") fs.add("New York") fs.add("Los Angeles") # Initialize with iterable cities = FuzzySet(["San Francisco", "San Diego", "Seattle", "Portland"]) # Check if string was added (returns False if already exists) result = fs.add("New York") # Returns False - already exists result = fs.add("Chicago") # Returns True - newly added ``` -------------------------------- ### Dictionary-Style Access with __getitem__ in FuzzySet Source: https://context7.com/axiak/fuzzyset/llms.txt Shows how to use dictionary-style square bracket notation (`__getitem__`) for fuzzy lookups in FuzzySet. This method raises a `KeyError` if no match is found, mimicking standard Python dictionary behavior. It demonstrates retrieving matches and handling potential `KeyError` exceptions. ```python from fuzzyset import FuzzySet fs = FuzzySet(["New York", "New Jersey", "Los Angeles"]) # Access with square brackets try: matches = fs["ew Kork"] # Returns: [(0.87..., 'New York')] print(f"Best match: {matches[0][1]} with score {matches[0][0]:.2f}") except KeyError: print("No matches found") # Exact matches return perfect score matches = fs["los angeles"] # Returns: [(1.0, 'Los Angeles')] ``` -------------------------------- ### Python FuzzySet Construction Arguments Source: https://github.com/axiak/fuzzyset/blob/master/README.rst Illustrates initializing a FuzzySet with an iterable of strings and specifying construction arguments like gram size and Levenshtein distance usage. These parameters affect how strings are processed and matched. ```python import fuzzyset initial_data = ["apple", "banana", "apricot", "blueberry"] # Initialize with default gram sizes (2, 3) and Levenshtein enabled fs_default = fuzzyset.FuzzySet(initial_data) # Initialize with custom gram sizes and Levenshtein disabled fs_custom = fuzzyset.FuzzySet(initial_data, gram_size_lower=3, gram_size_upper=4, use_levenshtein=False) print(fs_default.get("appel")) print(fs_custom.get("aple")) ``` -------------------------------- ### Python FuzzySet with Cython Optimization Source: https://github.com/axiak/fuzzyset/blob/master/README.rst Shows how to import the optimized Cython version of FuzzySet (cFuzzySet) if available, falling back to the pure Python version otherwise. This can provide a significant performance increase. ```python try: from cfuzzyset import cFuzzySet as FuzzySet except ImportError: from fuzzyset import FuzzySet # Usage remains the same a = FuzzySet() a.add("example string") print(a.get("exampel string")) ``` -------------------------------- ### Python FuzzySet Basic Usage Source: https://github.com/axiak/fuzzyset/blob/master/README.rst Demonstrates the basic usage of FuzzySet for adding strings and performing fuzzy searches. The result is a list of (score, matched_value) tuples, where the score indicates the match quality. ```python import fuzzyset a = fuzzyset.FuzzySet() a.add("michael axiak") result = a.get("micael asiak") print(result) # Expected output: [(0.8461538461538461, u'michael axiak')] ``` -------------------------------- ### Custom N-Gram Configuration in FuzzySet Source: https://context7.com/axiak/fuzzyset/llms.txt Explains how to customize the n-gram range used for indexing and matching by adjusting `gram_size_lower` and `gram_size_upper` parameters during FuzzySet initialization. This allows tuning the trade-off between performance and match quality. ```python from fuzzyset import FuzzySet # Default: uses 2-grams and 3-grams fs_default = FuzzySet(["hello", "world"]) # Use only bigrams (faster but less precise) fs_bigram = FuzzySet( ["hello", "world"], gram_size_lower=2, gram_size_upper=2 ) # Use bigrams and trigrams (default) fs_balanced = FuzzySet( ["mississippi", "massachusetts", "tennessee"], gram_size_lower=2, gram_size_upper=3 ) # Use only trigrams (more precise, slower for large datasets) fs_trigram = FuzzySet( ["concatenate", "concentrate", "consecrate"], gram_size_lower=3, gram_size_upper=3 ) result = fs_trigram.get("consecrate") # More selective matching with larger grams ``` -------------------------------- ### FuzzySet Case Insensitivity and Normalization Source: https://context7.com/axiak/fuzzyset/llms.txt Illustrates FuzzySet's automatic normalization of strings to lowercase and removal of non-word characters (preserving spaces and commas) for matching purposes. It shows that case and extra punctuation do not affect matching accuracy, while original casing is preserved in results. ```python from fuzzyset import FuzzySet fs = FuzzySet() fs.add("New York City") fs.add("São Paulo") fs.add("Hello, World!") # Case doesn't matter for matching result = fs.get("NEW YORK CITY") # Returns: [(1.0, 'New York City')] result = fs.get("new york city") # Returns: [(1.0, 'New York City')] # Original casing is preserved in results result = fs.get("hello world") # Returns: [(1.0, 'Hello, World!')] # Non-word characters are ignored during matching result = fs.get("hello___world") # Still matches 'Hello, World!' with high score ``` -------------------------------- ### FuzzySet Length and Boolean Operations Source: https://context7.com/axiak/fuzzyset/llms.txt Demonstrates how to check the size of a FuzzySet dataset and determine if it contains any entries using standard Python length (`len()`) and boolean (`if fs:`) operators. Shows adding items and verifying that duplicate additions do not increase the count. ```python from fuzzyset import FuzzySet fs = FuzzySet() # Check if empty if not fs: print("FuzzySet is empty") # Add items fs.add("apple") fs.add("banana") fs.add("cherry") # Get count print(len(fs)) # Returns: 3 # Boolean check if fs: print("FuzzySet has data") # Duplicates don't increase count fs.add("apple") # Already exists print(len(fs)) # Still returns: 3 ``` -------------------------------- ### Levenshtein Distance Control in FuzzySet Source: https://context7.com/axiak/fuzzyset/llms.txt Demonstrates how to control the inclusion of Levenshtein distance calculation in FuzzySet matching by using the `use_levenshtein` parameter. Enabling Levenshtein distance improves accuracy for character-level edits like transpositions, at a potential performance cost. ```python from fuzzyset import FuzzySet # With Levenshtein distance (default - more accurate) fs_lev = FuzzySet( ["Michael", "Michelle", "Mitchell"], use_levenshtein=True ) result = fs_lev.get("Micheal") # Common typo: swapped 'ea' and 'ae' # Returns better ranking thanks to Levenshtein # [(0.92..., 'Michael'), (0.85..., 'Michelle'), ...] # Without Levenshtein (faster, cosine similarity only) fs_cosine = FuzzySet( ["Michael", "Michelle", "Mitchell"], use_levenshtein=False ) result = fs_cosine.get("Micheal") # Still works but may have different score ordering ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.