Paul's Python CoursePPC

Project 14 of 15, for Paul

Spell Checker

A spell checker for Paul's end-of-term quiz: the fewest changes between two words, worked out with a table, the nearest real words suggested from a list counted from two old novels, and every answer marked right, near enough or wrong.

About an hour and a half, in 12 steps.

Sign up free to open itLog in

Act 4 needs a free account, so your work is saved as you go. Act 1 is open to everyone.

The brief

  • PaulI'm marking the end-of-term quiz on capital cities, and Riya wrote Copenhagan, which I'd give her.
  • PaulYahya wrote Kito for Quito and reckons that's near enough too. I need a proper rule before he starts arguing.
  • YahyaAnd nobody got the bonus question, which was how many ways there are to make fifty pence.
  • NeilBoth of those come apart into smaller questions, and the trick is never working anything out twice.

words.csv is a word list with a count for each word: every word that turns up at least twice in two old novels, Great Expectations and Treasure Island, and how many times. load_counts reads it into a dictionary, and words_in gives back the words of a text in lower case. In spell.py, write:

  1. edit_distance(given, correct): the fewest changes that turn one word into the other, where a change puts in a letter, takes one out, or swaps one letter for another. Fill a table, as Paul does for the Arcade's tokens. Two words of sixty letters should take a blink.
  2. suggest(word, counts, limit=3): up to limit known words no more than MOST_CHANGES (2) changes from word: the fewest changes first; on a tie, the word with the bigger count; on a tie again, alphabetical order. An empty list if nothing is that near.
  3. check(text, counts): a list of (word, suggestions) for each word in the text that isn't in the list, in the order they come, each word once.
  4. mark(given, correct) gives back "right", "near enough" or "wrong", ignoring capitals and spaces at either end. Near enough is Paul's rule: no more than one change for every five letters of the right answer, rounded down.

The main block marks Paul's three disputed answers, then checks each line typed until a blank one.

What it prints
Knows 7709 words.
'Copenhagan' for 'Copenhagen': near enough
'Kito' for 'Quito': wrong
'Copenahgen' for 'Copenhagen': near enough
Check? The captin sailed round the iland at nite.
  captin: captain, cap'n, cabin
  iland: island, land, and
  nite: nine, note, bite
Check? Fifteen men on the dead man's chest
Every word known.
Check?

What you learn

What's in it

  1. The same small questions
  2. Coins are not crates
  3. Each handful once
  4. Each handful once
  5. Fill a table, smallest first
  6. Fewest coins
  7. A table for two words
  8. Kito and nite
  9. How Paul pays at the Arcade
  10. Make the spell checker
  11. Ship it