Correct Your Spellings Using Python

Trending 9 months ago

A spell checker is simply a captious instrumentality to thief amended nan accuracy of your written communications. When writing, you tin easy place typos aliases misspellings that a spell checker tin easy observe for you. This not only saves clip and effort but besides helps you to debar embarrassing mistakes.

You tin easy create a GUI exertion successful which you tin type a connection and get its corrected pronunciation utilizing Python's Tkinter module. Tkinter provides astonishing customizable widgets that make improvement easy and fun.

The Tkinter and nan Spell Checker Module

The default GUI room for Python is called Tkinter. You tin create unthinkable GUI programs pinch Tkinter. You mightiness build a elemental calculator, a to-do database app, matter editor, euphony player, aliases quiz game. You tin use Python and Tkinter to create elemental desktop apps and put your knowledge into believe while advancing your programming skills.

To instal Tkinter successful your system, unfastened nan terminal and execute:

pip install tkinter

Another module, you will usage greatly successful this task is nan Spell Checker module. The activity of Peter Norvig forms nan ground of this module. It uses nan Levenshtein Distance algorithm to find permutations successful a connection wave list. It past assumes that nan greater nan wave of nan word, nan much apt it is to beryllium nan correct spelling.

To instal nan spellchecker module, execute nan pursuing command:

pip install pyspellchecker

How to Build a Spelling Corrector App Using Python

Follow these steps to build a GUI pronunciation corrector utilizing Python.

This illustration codification is disposable successful this GitHub repository and is free for you to usage nether nan MIT license.

Implement nan Underlying Logic and Functionality

The first measurement is to import Tkinter and nan SpellChecker people from nan spellchecker module. Create an lawsuit of SpellChecker and shop it successful a adaptable named corrector.

from tkinter import *
from spellchecker import SpellChecker

corrector = SpellChecker()

Define a function, clearAll(), that will swipe retired nan information displayed connected nan introduction field. To execute this usage nan delete() usability to region each entries from 0 to nan past scale connected some introduction fields, word1_field and word2_field.

def clearAll():
    word1_field.delete(0, END)
    word2_field.delete(0, END)

Define different usability named correction() that sounds a connection from nan first input section and adds its correct pronunciation to nan second. Use nan get() method connected nan word1_field to get its worth arsenic a string. Pass this retrieved connection to nan correction() function, past clear nan word2_field widget earlier utilizing nan insert() method to insert nan corrected connection astatine nan opening of nan field.

def correction():
    input_word = word1_field.get()
    corrected_word = corrector.correction(input_word)
    word2_field.delete(0, END)
    word2_field.insert(0, corrected_word)

Create nan User Interface

Initialize nan Tkinter lawsuit and show nan guidelines window. Use nan configure() method and group nan inheritance colour of nan model to your desired colour specified arsenic aqua. Set nan dimensions of nan model to beryllium 900 pixels wide and 450 pixels gangly utilizing nan geometry() method. Also group an due model title.

guidelines = Tk()
root.title("Spellings Corrector")

Use Label widgets to show useful accusation astir nan application. The constructor accepts a genitor model successful which you want to spot it, nan matter it should display, nan font color, nan inheritance color, and nan font size.

headlabel = Label(root, text='Spellings Corrector', fg='white', bg="blue", font=10)
label1 = Label(root, text="Input Word", fg='black', bg='violet', font=10)
label2 = Label(root, text="Corrected Word", fg='black', bg='violet', font=10)

Grid is simply a geometry head that organizes widgets successful a two-dimensional table. You tin envision a layout of 5 rows and 2 columns.

Place each explanation successful an due statement and column, retrieve that their indexes statesman from zero. You tin usage padding to align your widgets; successful this example, label2 has a padding of 100 pixels on nan X-axis.

headlabel.grid(row=0, column=1)
label1.grid(row=1, column=0)
label2.grid(row=3, column=0, padx=100)

Define 2 introduction widgets, 1 for nan input and 1 for nan correction. Set nan font size of some widgets to 10. Set nan misspelled introduction widget successful nan 2nd statement and 2nd column, pinch a padding of 100 on nan X-axis and 50 on nan Y-axis. Do nan aforesaid for nan corrected introduction widget, but spot it successful nan 4th row.

word1_field = Entry(font=10)
word2_field = Entry(font=10)
word1_field.grid(row=1, column=1, padx=100, pady=50)
word2_field.grid(row=3, column=1, padx=100, pady=50)

Use nan Button() widget to create 2 buttons: Correction and Clear. Pass successful nan guidelines model arsenic their owner, nan button’s explanation arsenic a string, nan inheritance colour arsenic Orange, nan font colour arsenic black, and nan font size arsenic 8. You tin proviso a usability sanction present utilizing nan command argument; this will tally erstwhile nan personification clicks nan button.

Place each fastener successful an due statement and column.

button1 = Button(root, text="Correction", bg="orange", fg="black", font=8, command=correction)
button1.grid(row=2, column=1),
button2 = Button(root, text="Clear", bg="orange", fg="black", font=8, command=clearAll)
button2.grid(row=4, column=1)

The mainloop() usability tells Python to tally nan Tkinter arena loop and perceive for events (such arsenic fastener presses) until you adjacent nan window:


Put each this codification together and tally nan programme to watch your pronunciation corrector exertion successful action.

The Output of nan Spelling Corrector App

When you tally this program, you should spot a mini model pinch a bluish inheritance color. Enter a misspelled connection successful nan first input field, past property nan Correction button. You should spot nan correct pronunciation look successful nan 2nd matter field. You tin usage nan Clear fastener to reset some matter fields.

Spellings Corrector Using Tkinter Python

Python GUI Frameworks for Developers

Python GUI frameworks are a awesome assets for developers. You tin quickly build an exertion pinch an attractive, useful GUI. Some of nan frameworks you tin use, different than Tkinter, see Qt designer, Kivy, Toga, and BeeWare. Using these frameworks, you tin build thing from mini inferior apps to afloat functional products.

With Python, you get an added advantage. As Python GUI frameworks support cross-platform development, you tin usage nan aforesaid codification connected different operating systems. With each these features and galore more, Python has emerged arsenic an perfect connection for creating graphical personification interfaces pinch accrued reliability and reduced costs.

Source useuf