Download this file.
Feb 11, 22:32 - David (line 30): I wish sort() returned the list so I could use it as a part of larger expressions.
Feb 11, 22:44 - David (line 22): I should have used .get() to avoid this 'if'.
Please log in if you would like to add comments. | |
1 | # David Wallace
| 2 | # Homework 3.1: check spelling of words in input.txt
| 3 |
| 4 | # Unfortunately I didn't find a partner, so
| 5 | # I wrote 'speling' first, then the checker of 'input.txt'.
| 6 |
| 7 | import speling
| 8 |
| 9 | def checkfile(filename):
| 10 | """Show spelling mistakes in a file"""
| 11 |
| 12 | infile = open(filename, 'r')
| 13 | words = speling.split_text_to_words(infile.read())
| 14 |
| 15 | # find and count all the misspelled words
| 16 | print "checking spelling... (%d words)" % len(words)
| 17 | misspellings = {}
| 18 | for ii in range(len(words)):
| 19 | if ii%10==0: print ii, # print progress as we go
| 20 | word = words[ii]
| 21 | if not speling.correct(word):
| 22 | if word in misspellings:
| 23 | misspellings[word] += 1
| 24 | else:
| 25 | misspellings[word] = 1
| 26 |
| 27 | # print results
| 28 | print "\n\nmisspelled words:"
| 29 | results = misspellings.items()
| 30 | results.sort()
| 31 | for (word, num) in results:
| 32 | print '%s (%d)' % (word, num)
| 33 |
| 34 | # gentlemen, start your engines.
| 35 | checkfile('input.txt') |
|