Spell Check


Not Complete
Views: 41
Published Date: Jan. 6, 2019, 3:20 p.m.
Section: Strings

Join Our Mailing List

Get the latest tutorials right to your inbox and you can unsubscribe anytime.


Spell Check Python Challenge

Welcome to Master Code Online's, spell check Python challenge. We challenge you to build a program that will spell check the inputted string. We intentionally left out the whole database worth of words do to the time limit of our challenges. 

We can think of more features we can add to this little program but for now, our database of words is only like 10 words long and we only check the spelling nothing more than that. If you guys want to add more features let us know and we will expand on this program.

The Challenge

Write a program that will spell check the inputted string. If a word is misspelled the program should suggest the correct spelling of the word. 

Input

We will test the following inputs to check if our program is working correctly:

Input a string: i like doogs

Input a string: i like hockiy

Output

Our program should return the following:

Did you mean dogs, dog instead of doogs?

Did you mean hockey instead of hockiy?

The Solution

Let's take a look at how Master Code Online solves this program.

1. We will start off this solution by importing a module that will allow us to compare sequences of data.

import difflib

2. Now we will create our little database of words. This should actually be a database of actual words but to save time we omitted this.

import difflib
words = ['i', 'like', 'dogs', 'dog', 'hockey']

3. Now we will create a function that will compare the user's input to the words in our database. The parameter (s) will hold the users input.

import difflib
words = ['i', 'like', 'dogs', 'dog', 'hockey']

def word_check(s):

4. We will now take the users input and make it lower case and split each word into a separate string and put in a list. This is all done as we loop through the input.

import difflib
words = ['i', 'like', 'dogs', 'dog', 'hockey']

def word_check(s):
    for word in s.casefold().split():

5. Now we will check if each word in the input() is in the words database. If the word is not found then we will suggest a word.

import difflib
words = ['i', 'like', 'dogs', 'dog', 'hockey']

def word_check(s):
    for word in s.casefold().split():
        if word not in words:

6. Now the most import feature of this program we are going to suggest a word to the user. We use the difflib.get_close_matchs().

import difflib
words = ['i', 'like', 'dogs', 'dog', 'hockey']

def word_check(s):
    for word in s.casefold().split():
        if word not in words:
            suggestion = difflib.get_close_matches(word, words)

7. We will now return the suggestions to the users. We will take the suggested words and remove them from a list and put them in a string using the join method.

import difflib
words = ['i', 'like', 'dogs', 'dog', 'hockey']

def word_check(s):
    for word in s.casefold().split():
        if word not in words:
            suggestion = difflib.get_close_matches(word, words)
            print(f"Did you mean {', '.join(str(x) for x in suggestion)} instead of {word}?")

8. We will now get the users input.

import difflib
words = ['i', 'like', 'dogs', 'dog', 'hockey']

def word_check(s):
    for word in s.casefold().split():
        if word not in words:
            suggestion = difflib.get_close_matches(word, words)
            print(f"Did you mean {', '.join(str(x) for x in suggestion)} instead of {word}?")


s = input("Input a string: ")

9. Finally, run the function.

import difflib
words = ['i', 'like', 'dogs', 'dog', 'hockey']

def word_check(s):
    for word in s.casefold().split():
        if word not in words:
            suggestion = difflib.get_close_matches(word, words)
            print(f"Did you mean {', '.join(str(x) for x in suggestion)} instead of {word}?")


s = input("Input a string: ")
word_check(s)

Conclusion

In this Python Challenge, we gave you the tools to build a spell checker for your next project. If you have any questions let us know.



Ask A Question


Similar Tutorials


Spell Check With GUI

Find A Substring

Spell Check

Sort String In Alphabetical Order And Remove Duplicates

Users Name Return A Message

Last 5 Tutorials


Spell Check With GUI

Spell Check

Math Game

Find A Substring

Sort String In Alphabetical Order And Remove Duplicates

Most Popular


Introduction To Command Line and Python Commands

Install Python 3.7 On Mac OS X

Upload A CSV File To Django

Introduction To Python 3.7 Tutorial Series

How To Use The Django For Loop Counter