SymSpell Documentation
repository·master·Indexed 25 days ago
https://github.com/wolfgarbe/symspellA high-performance spelling correction and fuzzy search library based on the Symmetric Delete algorithm. It provides single-word correction via the Lookup method, compound-aware multi-word correction using LookupCompound, and word segmentation for noisy text. Compatible with .NET Standard v2.0, .NET Framework, .NET Core, and XAMARIN. Features include support for frequency dictionaries, bigram dictionaries for compound splitting/merging, and a Python script for generating custom frequency dictionaries.
What's inside SymSpell
- SymSpell relies on high-quality frequency dictionaries to ensure accurate spelling correction. The provided dictionaries combine reliable frequency values from Google Ngram datasets with correct vocabulary from Hunspell dictionary files. While the pre-built dictionaries are highly optimized, users may choose to generate custom frequency dictionaries tailored to specific use-cases or domains.
Overview of SymSpell
masterSymSpell is a high-performance spelling correction and fuzzy search library based on the Symmetric Delete algorithm. It is designed for speed, being significantly faster than standard approaches (like Norvig's algorithm or BK-trees) by using a delete-only edit candidate generation and pre-calculation strategy.
Key capabilities include:
- Single word spelling correction: Fast lookup for individual terms.
- Compound aware multi-word spelling correction: Handles multi-word strings by addressing compound splitting, decompounding, and automatic correction of long text.
- Word Segmentation: Useful for processing noisy text.
Install SymSpell in your project
masterYou can add SymSpell to your project in three ways:
- Manual Integration: Copy
SymSpell.cs,EditDistance.cs, and a frequency dictionary (e.g.,frequency_dictionary_en_82_765.txt) directly into your project files.- Tip: Enabling the compiler option "Prefer 32-bit" significantly reduces memory consumption of the precalculated dictionary.
- NuGet for .NET Framework: Install via the NuGet Package Manager. The frequency dictionary is automatically included.
- NuGet for .NET Core: Install via the NuGet Package Manager. You must manually copy the frequency dictionary file to your project.
SymSpell targets .NET Standard v2.0 and is compatible with .NET Framework, .NET Core, and XAMARIN (iOS, OS X, Android).
- Manual Integration: Copy
Generate a custom frequency dictionary using Python
masterThe repository includes a Python script designed to automate the creation of custom frequency dictionaries. This script handles:
- Downloading the Ngram dataset.
- Decompressing the dataset.
- Merging the Ngram data with existing wordlists to create a unified frequency dictionary.
Configure the frequency dictionary format
masterSymSpell uses plain text files in UTF-8 encoding for dictionaries.
Format Requirements:
- Structure: Each line contains a word-frequency pair.
- Separators: Word and frequency are separated by a space or tab.
- Columns: By default, the word is in the first column and frequency in the second. You can customize this using
termIndexandcountIndexinLoadDictionary()orLoadBigramDictionary(). - Case: Both dictionary terms and input terms are expected to be in lower case.
- Bigrams: When using
LoadBigramDictionary, if no separator is specified, it expects two term parts (a bigram).
Use SymSpell for single-word spelling correction
masterTo correct single words, initialize a
SymSpellobject, load a frequency dictionary, and use theLookupmethod.Parameters for
Lookup:inputTerm: The word to check.suggestionVerbosity: Determines how many suggestions are returned. UseSymSpell.Verbosity.Closest,Top, orAll.maxEditDistanceLookup: The maximum edit distance allowed for the lookup (must be $\le$maxEditDistanceDictionaryused during initialization).
Perform word segmentation on noisy text
masterThe
WordSegmentationmethod divides a string into words by inserting missing spaces. It is designed for noisy text where spaces might be missing (e.g., OCR errors, URLs, or typing errors).Key Features:
- Corrects misspelled words during segmentation.
- Considers existing spaces for optimum segmentation.
- Uses a Triangular Matrix approach for $O(n)$ linear runtime.
Returns: A result object containing
correctedStringanddistanceSum.Perform single word spelling correction with Lookup
masterUse the
Lookupmethod for fast spelling correction of single words. You can control the results using a Verbosity parameter and a Maximum edit distance parameter.Verbosity Levels
Top: Returns only the top suggestion with the highest term frequency among those with the smallest edit distance found.Closest: Returns all suggestions of the smallest edit distance found, ordered by term frequency.All: Returns all suggestions within themaxEditDistance, ordered by edit distance and then by term frequency.
Dictionary Setup
You must provide a word frequency dictionary. This can be done in two ways:
- LoadDictionary: Directly load a dictionary from text files.
- CreateDictionary: Generate a dictionary from a large text corpus.
Perform compound aware multi-word spelling correction with LookupCompound
masterUse
LookupCompoundto perform automatic spelling correction on multi-word input strings. UnlikeLookup, which treats every input as a single term,LookupCompoundhandles:- Compound splitting & decompounding:
- Correcting mistakenly inserted spaces within a single word.
- Correcting mistakenly omitted spaces between two words.
- Handling multiple input terms with or without spelling errors.
- Automatic spelling correction:
- Makes educated choices for automatic correction of long text strings where manual selection is infeasible.
It can handle mixed error types including splitting, concatenation, substitution, transposition, deletion, and insertion errors within the same sequence.
- Compound splitting & decompounding:
Use SymSpell for multi-word compound correction
masterTo support compound splitting and merging (e.g., correcting
whereis th elovetowhere is the love), you must first load a bigram dictionary usingLoadBigramDictionary. Then, useLookupCompoundto find suggestions for multi-word strings.Parameters for
LookupCompound:inputTerm: The multi-word string.maxEditDistanceLookup: The maximum edit distance allowed per single word (not for the whole string).