Spelling checker in Python
Last Updated : 07 Oct, 2020
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 - Modify Strings Python provides an wide range of built-in methods that make string manipulation simple and efficient. In this article, we'll explore several techniques for modifying strings in Python.Start with doing a simple string modification by changing the its case:Changing CaseOne of the simplest ways to modi
3 min read
SpongeBob Mocking Text Generator - Python SpongeBob mocking text (spongemock) is a style of writing in which the alphabets are randomly written in upper and lower case. It is used in internet communities to mock something. The text originated as a meme from the SpongeBob cartoon series.Examples : Input : The quick brown fox jumps over the l
3 min read
Python String Input Output In Python, input and output operations are fundamental for interacting with users and displaying results. The input() function is used to gather input from the user and the print() function is used to display output.Input operations in PythonPythonâs input() function allows us to get data from the u
3 min read
Convert String to Tuple - Python When we want to break down a string into its individual characters and store each character as an element in a tuple, we can use the tuple() function directly on the string. Strings in Python are iterable, which means that when we pass a string to the tuple() function, it iterates over each characte
2 min read
First N letters String Construction - Python The task of constructing a string from the first N letters of a given string in Python involves extracting a specific portion of the string, starting from the beginning and including the first N characters. For example, if we have the string "GeeksForGeeks" and want to extract the first 5 characters
3 min read
Python - Convert case of elements in a list of strings In Python, we often need to convert the case of elements in a list of strings for various reasons such as standardizing input or formatting text. Whether it's converting all characters to uppercase, lowercase, or even swapping cases. In this article, we'll explore several methods to convert the case
3 min read