pyspellchecker Documentation

repository·master·Indexed 21 days ago

https://github.com/barrust/pyspellchecker

A pure Python spell checking library based on Peter Norvig's algorithm. It uses Levenshtein Distance and word frequency lists to detect misspellings and suggest corrections. The library provides the SpellChecker class for identifying unknown words and retrieving candidates, the WordFrequency class for managing dictionary data, and support for multiple languages including English, Spanish, French, and others.

Tokens
3.3K
Snippets
15
Records
19
Agent score
74%

What's inside pyspellchecker

  1. Quickstart: Find and correct misspelled words

    master

    To use pyspellchecker, import SpellChecker, identify misspelled words using .unknown(), and then retrieve corrections using .correction() or a list of possibilities using .candidates().

    from spellchecker import SpellChecker
    
    spell = SpellChecker()
    
    # find those words that may be misspelled
    misspelled = spell.unknown(['something', 'is', 'hapenning', 'here'])
    
    for word in misspelled:
        # Get the one `most likely` answer
        print(spell.correction(word))
    
        # Get a list of `likely` options
        print(spell.candidates(word))
  2. Quickstart with pyspellchecker

    master
    To get started with pyspellchecker, follow the quickstart guide to learn how to identify misspelled words, retrieve suggested corrections, and manage word lists. The library allows you to check for spelling errors, find the most likely corrections, and handle custom word lists or non-English dictionaries.
  3. Adjust Levenshtein distance for long words

    master

    The default Levenshtein distance is 2. For longer words, it is highly recommended to reduce the distance to 1 to improve performance and accuracy. You can set this during initialization or update the property on the instance later.

    from spellchecker import SpellChecker
    
    # Set distance at initialization
    spell = SpellChecker(distance=1)
    
    # Or set the distance parameter after the fact
    spell.distance = 2
  4. Use pyspellchecker with PyInstaller

    master

    When bundling an executable with PyInstaller, you must manually include the required dictionary resource files. These files must be placed in a folder named spellchecker/resources/ within your executable to match the library's expected lookup path.

    # Linux/macOS
    pyinstaller --add-binary="spellchecker/resources/en.json.gz:spellchecker/resources" my_prog.py
    
    # Windows
    pyinstaller --add-binary="spellchecker/resources/en.json.gz;spellchecker/resources" my_prog.py
  5. Install pyspellchecker

    master

    You can install pyspellchecker using pip. For Python 3 (the recommended version), use the standard install command. If you require legacy support for Python 2.7, you must install version 0.5.6 specifically.

    # Standard installation for Python 3
    pip install pyspellchecker
    
    # For Python 2.7 support
    pip install pyspellchecker==0.5.6
  6. Build and export a custom dictionary

    master

    To build a new dictionary from scratch, initialize SpellChecker with language=None. You can then load your corpus and export the result for later use using spell.export(filename, gzipped=True).

    from spellchecker import SpellChecker
    
    # Initialize without built-in languages
    spell = SpellChecker(language=None, case_sensitive=True)
    
    # Load your custom data
    spell.word_frequency.load_dictionary('./my_dict.json')
    
    # Export for later use
    spell.export('my_custom_dictionary.gz', gzipped=True)
  7. Customize the word frequency list

    master

    You can improve the spell checker's accuracy for your specific domain by loading text from a file or manually adding specific words to the frequency list. This ensures that domain-specific terms are not flagged as misspelled.

    from spellchecker import SpellChecker
    
    spell = SpellChecker()  # loads default word frequency list
    
    # Load additional text to generate a custom frequency list
    spell.word_frequency.load_text_file('./my_free_text_doc.txt')
    
    # Manually add words to ensure they are not flagged as misspelled
    spell.word_frequency.load_words(['microsoft', 'apple', 'google'])
    
    # Verify the words are now known
    print(spell.known(['microsoft', 'google']))  # returns both
  8. Configure Levenshtein distance

    master

    The distance parameter controls the Levenshtein distance used for corrections. The default is 2. For processing long words more quickly, you can set it to 1 during initialization or update the property on the instance.

    from spellchecker import SpellChecker
    
    # Set distance at initialization
    spell = SpellChecker(distance=1)
    
    # Or update the property later
    spell.distance = 2
  9. Use non-English dictionaries

    master

    You can initialize SpellChecker with different languages by passing the language parameter. Supported language codes include:

    • English: 'en' (default)
    • Spanish: 'es'
    • French: 'fr'
    • Portuguese: 'pt'
    • German: 'de'
    • Italian: 'it'
    • Russian: 'ru'
    • Arabic: 'ar'
    • Basque: 'eu'
    • Latvian: 'lv'
    • Dutch: 'nl'
    • Persian: 'fa'
    from spellchecker import SpellChecker
    
    english = SpellChecker()           # default is English
    spanish = SpellChecker(language='es')
    russian = SpellChecker(language='ru')
    arabic = SpellChecker(language='ar')
  10. Get spelling corrections and candidates

    master

    Once a word is identified as misspelled, you can retrieve suggestions:

    • spell.correction(word): Returns the single most likely replacement for the word.
    • spell.candidates(word): Returns a set of all possible candidate words.
    from spellchecker import SpellChecker
    
    spell = SpellChecker()
    
    # Get the single best correction
    misspelled = spell.unknown(['hapenning'])  # {'hapenning'}
    for word in misspelled:
        print(spell.correction(word))  # 'happening'
    
    # Get all possible candidates
    print(spell.candidates('hapenning'))  # {'penning', 'happening', 'henning'}