Spelling Corrector in Python

In this blog, we are going to make an accurate and fast spelling corrector in Python GUI Project with TextBlob Module.

Spelling mistakes are common, and most people are used to software indicating if a mistake was made. From autocorrect on our phones to red understanding in text editors, spelt checking is an essential feature for many different products.

Python offers many modules to use for this purpose, making writing a simple spell checker an easy 20–25 minute ordeal. One of these libraries being TextBlob, which is used for natural language processing.

What is a Spelling Corrector?

  • The spelling Corrector compares every word typed with thousands of correctly spelt words and then uses algorithms to determine the correct spellings.

Let’s get started,

Code for the project:

from tkinter import*

from textblob import TextBlob

root = Tk()

root.title(“Spelling Corrector”)

root.geometry(“600x500”)

def convert_text():

global sc1

sc1.set(‘’)

if entry1.get():

#a = input(“Enter any text:”)

#print(“Original text : “+str(a))

b = TextBlob(entry1.get())

c = (“Corrected Text : “+str(b.correct()))

sc1.set(c)

label_1 = Label(root,text=”Type any text”,font=(“arial”,25),justify=CENTER,fg=”red”)

label_1.place(x=200, y=100)

entry1 = Entry(root,width=40, justify=”center”)

entry1.place(x=180, y=160)

button_2 = Button(root,text=”Click Here!”,font=(“arial”,15),justify=CENTER,fg=”green”,command=convert_text)

button_2.place(x=250,y=200)

sc1=StringVar(‘’)

entry2 = Entry(root,width=40, textvariable=sc1)

entry2.place(x=180, y=260)

root.mainloop()

Define code working:-

from tkinter import*

from textblob import TextBlob

Tkinter is the name of the GUI library in Python. Import * means everything from the library. Import * is allowing a Python file to access the script from another Python file.

TextBlob is a Python library for processing textual data. It provides a consistent API for diving into common natural language processing (NLP) tasks such as part-of-speech tagging, noun phrase extraction, sentiment analysis, and more.

root = Tk()

root.title(“Spelling Corrector”)

root.geometry(“600x500”)

Tk class is used to create a root window. The root is the name of a root window. Here the Tk function provides a window of GUI as well as provides so many features like setting the title, setting the geometry of the GUI window. In these steps, we are giving the title of the GUI root window. Tkinter provides many methods, one of them is the geometry() method.

def convert_text():

global sc1

sc1.set(‘’)

if entry1.get():

#a = input(“Enter any text:”)

#print(“Original text : “+str(a))

b = TextBlob(entry1.get())

c = (“Corrected Text : “+str(b.correct()))

sc1.set(c)

Define the function convert_text. In this line of the code, we define the global variable sc1 for global access. Now in the next line of the code, we define a set function for the sc1 variable with a null value. Now again in the code we define the if condition where the condition used get function and this function applied on the entry1 object. When the condition gets true, the program goes into the next line of the code where we define the b variable that holds the value where applied TextBlob function. Here the C variable holds the correct spelling and the last using set function shows the correct spelling.

label_1 = Label(root,text=”Type any text”,font=(“arial”,25),justify=CENTER,fg=”red”)

label_1.place(x=200, y=100)

entry1 = Entry(root,width=40, justify=”center”)

entry1.place(x=180, y=160)

Coming to the Designing part of the GUI project. Here we are using some Label, Entry Box and Button in a project. So let us start to understand the working of Label, EntryBox and Button in a project according to the project code line by line.

Define a Label label_1 on the GUI window and write text on the label and set font, FG or justify the text of the Label. Now in this line, we are working on the place method for assigning the place of the label_1 object.

Define Entry entry1 on the GUI window. EntryBox is used for taking the input from the user and also we are used to displaying the output of some using set function. Assign the place of the entry1 Entry using x or y coordinates values.

button_2 = Button(root,text=”Click Here!”,font=(“arial”,15),justify=CENTER,fg=”green”,command=convert_text)

button_2.place(x=250,y=200)

sc1=StringVar(‘’)

entry2 = Entry(root,width=40, textvariable=sc1)

entry2.place(x=180, y=260)

root.mainloop()

Now come to the Button part of a project in this Button we are performing an action when we clicked on the Button. So let’s start with how Button works.

Here we define a button_2 Button on the GUI window and set text and font fg and justify button. Give convert_text command on button_2 Button for performing an action when Button clicks. Assign the place of the button_2 Button using x or y coordinates values. Here in the next line of the code we define the sc1 variable to hold some string value in the program. Now again we used one more entry box in our project.

Now coming to the last line of code of the project is the mainloop function that provides an infinite loop. So let’s start to understand how it works. mainloop() is an infinite loop used to run the application, using this function window is not closed as long as.

Here we are completed with our blog, providing sufficient information.

If you enjoyed this blog post, share it with your friends!