The Wayback Machine - https://web.archive.org/web/20240708021114/https://www.geeksforgeeks.org/spelling-checker-in-python/
Open In App

Spelling checker in Python

Last Updated : 07 Oct, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

For any type of text processing or analysis, checking the spelling of the word is one of the basic requirements. This article discusses various ways that you can check the spellings of the words and also can correct the spelling of the respective word.
 

Using textblob library

First, you need to install the library textblob using pip in command prompt. 

pip install textblob

You can also install this library in Jupyter Notebook as: 
 

Python3




import sys
!{sys.executable} - m pip install textblob


  
Program for Spelling checker – 
 

Python3




from textblob import TextBlob
 
a = "cmputr"           # incorrect spelling
print("original text: "+str(a))
 
b = TextBlob(a)
 
# prints the corrected spelling
print("corrected text: "+str(b.correct()))


Output: 
 

original text: cmputr
corrected text: computer


 

Using pyspellchecker library

You can install this library as below:
Using pip: 

pip install pyspellchecker


In Jupyter Notebook: 
 

Python3




import sys
!{sys.executable} - m pip install pyspellchecker


  
Spelling Checker program using pyspellchecker – 
 

Python3




from spellchecker import SpellChecker
 
spell = SpellChecker()
 
# find those words that may be misspelled
misspelled = spell.unknown(["cmputr", "watr", "study", "wrte"])
 
for word in misspelled:
    # Get the one `most likely` answer
    print(spell.correction(word))
 
    # Get a list of `likely` options
    print(spell.candidates(word))


Output: 
 

computer
{'caput', 'caputs', 'compute', 'computor', 'impute', 'computer'}
water
{'water', 'watt', 'warr', 'wart', 'war', 'wath', 'wat'}
write
{'wroe', 'arte', 'wre', 'rte', 'wrote', 'write'}


Using JamSpell

To achieve the best quality while making spelling corrections dictionary-based methods are not enough. You need to consider the word surroundings. JamSpell is a python spell checking library based on a language model. It makes different corrections for a different context.

1) Install swig3

apt-get install swig3.0   # for linux
brew install swig@3       # for mac

2) Install jamspell

pip install jamspell

3) Download a language model for your language

Python3




# Create a corrector
corrector = jamspell.TSpellCorrector()
 
# Load Language model -
# argument is a downloaded model file path
corrector.LoadLangModel('Downloads/en_model.bin')
 
# To fix text automatically run FixFragment:
print(corrector.FixFragment('I am the begt spell cherken!'))
 
# To get a list of possible candidates
# pass a splitted sentence, and a word position
print(corrector.GetCandidates(['i', 'am', 'the', 'begt', 'spell', 'cherken'], 3))
 
print(corrector.GetCandidates(['i', 'am', 'the', 'begt', 'spell', 'cherken'], 5))


Output:

u'I am the best spell checker!'
(u'best', u'beat', u'belt', u'bet', u'bent')
(u'checker', u'chicken', u'checked', u'wherein', u'coherent', ...)



Similar Reads

Python - Spelling checker using Enchant
Enchant is a module in Python, which is used to check the spelling of a word, gives suggestions to correct words. Also, gives antonym and synonym of words. It checks whether a word exists in the dictionary or not. Enchant can also be used to check the spelling of words. The check() method returns True if the passed word is present in the language d
2 min read
Spelling Correction using K-Gram Overlap
While the minimum edit distance discussed in this article provides a good list of possibly correct words, there are far too many words in the English dictionary to consider finding the edit distance between all pairs. To simplify the list of candidate words, the k-gram overlap is used in typical IR and NLP systems. K-Grams K-grams are k-length subs
3 min read
Amazon product availability checker using Python
As we know Python is a multi-purpose language and widely used for scripting. Its usage is not just limited to solve complex calculations but also to automate daily life task. Let’s say we want to track any Amazon product availability and grab the deal when the product availability changes and inform the user of availability through email. It will b
3 min read
Grammar Checker in Python using Language-check
Python is an open-source programming language. It offers a vast variety of libraries that provide greater functionalities. The one such library is language_check. The language_check library doesn't come bundled with Python 3. Instead, you have to manually download it from the command line or download it from the pypi.org and then install it manuall
2 min read
Flight-price checker using Python and Selenium
Python is a scripting language with many extended libraries and frameworks. It is used in various fields of computer science such as web development and data science. Also, Python can be used to automate some minor tasks which can be really helpful in the long run. The Python script mentioned in this article will fetch the prices from paytm.com usi
4 min read
Important differences between Python 2.x and Python 3.x with examples
In this article, we will see some important differences between Python 2.x and Python 3.x with the help of some examples. Differences between Python 2.x and Python 3.x Here, we will see the differences in the following libraries and modules: Division operatorprint functionUnicodexrangeError Handling_future_ modulePython Division operatorIf we are p
5 min read
Python program to build flashcard using class in Python
In this article, we will see how to build a flashcard using class in python. A flashcard is a card having information on both sides, which can be used as an aid in memoization. Flashcards usually have a question on one side and an answer on the other. Particularly in this article, we are going to create flashcards that will be having a word and its
2 min read
Python | Merge Python key values to list
Sometimes, while working with Python, we might have a problem in which we need to get the values of dictionary from several dictionaries to be encapsulated into one dictionary. This type of problem can be common in domains in which we work with relational data like in web developments. Let's discuss certain ways in which this problem can be solved.
4 min read
Reading Python File-Like Objects from C | Python
Writing C extension code that consumes data from any Python file-like object (e.g., normal files, StringIO objects, etc.). read() method has to be repeatedly invoke to consume data on a file-like object and take steps to properly decode the resulting data. Given below is a C extension function that merely consumes all of the data on a file-like obj
3 min read
Python | Add Logging to a Python Script
In this article, we will learn how to have scripts and simple programs to write diagnostic information to log files. Code #1 : Using the logging module to add logging to a simple program import logging def main(): # Configure the logging system logging.basicConfig(filename ='app.log', level = logging.ERROR) # Variables (to make the calls that follo
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg