Program Arcade Games
With Python And Pygame

Language
English
Russian - Русский
Turkish - Türkçe

Lab 11: Spell Check

This lab shows how to create a spell checker. To prepare for the lab, go to:
ProgramArcadeGames.com/index.php?chapter=examples list
...and download the files listed below. The files are in the “Searching and Sorting Examples” section.

11.1 Requirements

Write a single program in Python that checks the spelling of the first chapter of “Alice In Wonderland.” First use a linear search, then use a binary search. Print the line number along with the word that does not exist in the dictionary.

11.2 Steps to complete:

  1. Create a file for your program, such as lab_11_lastname_first.py.
  2. It is necessary to split apart the words in the story so that they may be checked individually. It is also necessary to remove extra punctuation and white-space. Unfortunately, there is not any good way of doing this with what the book has covered so far. The code to do this is short, but a full explanation is beyond the scope of this class. Include the following function in your program.
    import re
    
    # This function takes in a line of text and returns
    # a list of words in the line.
    def split_line(line):
        return re.findall('[A-Za-z]+(?:\'[A-Za-z]+)?',line)
    
    This code uses a regular expression to split the text apart. Regular expressions are very powerful and relatively easy to learn. To learn more about regular expressions, see:
    http://en.wikipedia.org/wiki/Regular_expression
  3. Read the file dictionary.txt into an array. Go back to the chapter on Searching, or see the searching_example.py for example code on how to do this. This does not have anything to do with the import command, libraries, or modules.
  4. Close the file.
  5. Print --- Linear Search ---
  6. Open the file AliceInWonderLand200.txt
  7. Start a for loop to iterate through each line.
  8. Call the split_line function to split apart the line of text in the story that was just read in. Store the list that the function returns in a new variable named words.
  9. Start a nested for loop to iterate through each word in the line.
  10. Using a linear search, check the current word against the words in the dictionary. Check the chapter on searching or the searching_example.py for example code on how to do this. When comparing to the words in the dictionary, convert the word to uppercase first. For example: word.upper().
  11. If the word was not found, print the word and the line that it was on.
  12. Close the file.
  13. Make sure the program runs successfully before moving on to the next step.
  14. Print --- Binary Search ---
  15. The linear search takes quite a while to run. To temporarily disable it, it may be commented out by using three quotes before and after that block of code. Ask if you are unsure how to do this.
  16. Repeat the same pattern of code as before, but this time use a binary search. Much of the code from the linear search may be copied, and it is only necessary to replace the lines of code that represent the linear search with the binary search.
  17. Note the speed difference between the two searches.
  18. Make sure the linear search is re-enabled, if it was disabled while working on the binary search.
  19. Upload the final program or check in the final program.

11.3 Example Run

--- Linear Search ---
Line 3  possible misspelled word: Lewis
Line 3  possible misspelled word: Carroll
Line 46  possible misspelled word: labelled
Line 46  possible misspelled word: MARMALADE
Line 58  possible misspelled word: centre
Line 59  possible misspelled word: learnt
Line 69  possible misspelled word: Antipathies
Line 73  possible misspelled word: curtsey
Line 73  possible misspelled word: CURTSEYING
Line 79  possible misspelled word: Dinah'll
Line 80  possible misspelled word: Dinah
Line 81  possible misspelled word: Dinah
Line 89  possible misspelled word: Dinah
Line 89  possible misspelled word: Dinah
Line 149  possible misspelled word: flavour
Line 150  possible misspelled word: toffee
Line 186  possible misspelled word: croquet
--- Binary Search ---
Line 3  possible misspelled word: Lewis
Line 3  possible misspelled word: Carroll
Line 46  possible misspelled word: labelled
Line 46  possible misspelled word: MARMALADE
Line 58  possible misspelled word: centre
Line 59  possible misspelled word: learnt
Line 69  possible misspelled word: Antipathies
Line 73  possible misspelled word: curtsey
Line 73  possible misspelled word: CURTSEYING
Line 79  possible misspelled word: Dinah'll
Line 80  possible misspelled word: Dinah
Line 81  possible misspelled word: Dinah
Line 89  possible misspelled word: Dinah
Line 89  possible misspelled word: Dinah
Line 149  possible misspelled word: flavour
Line 150  possible misspelled word: toffee
Line 186  possible misspelled word: croquet

You are not logged in. Log in here and track your progress.