Download this file.
Feb 18, 04:00 - Richard (line 22): Code is easy to follow and clean. It was a little hard to understand the if word: return word on line 22. A brief comment may have helped.
Feb 18, 22:28 - Richard (line 22): Oh okay, nevermind. I got it now. =P
Feb 22, 17:54 - Ping (line 11): You don't need to declare these variables global. Whenever a variable is mentioned outside of a function, it is already global.
Feb 22, 17:56 - Ping (line 16): The only declaraction you need to make your program work is global word . The global keyword is for declaring global variables that are assigned within the function; you can always use global variables even when they're not declared global.
Please log in if you would like to add comments. | |
1 | #!/usr/bin/env python | 2 | | 3 | ## $Id: misspels.py,v 1.3 2003/02/07 07:25:38 quarl Exp $ | 4 | | 5 | ## Spell check a document given Nadia's speling.py. | 6 | import nadiah_speling | 7 | import re | 8 | | 9 | nadiah_speling.import_dictionary("dict.txt") | 10 | | 11 | global input, word, words | 12 | word = words = None | 13 | input = open('input.txt') | 14 | | 15 | def get_next_word(): | 16 | global input, word, words | 17 | word = None | 18 | while 1: | 19 | while words: | 20 | word = words[0] | 21 | words = words[1:] | 22 | if word: return word | 23 | | 24 | line = input.readline() | 25 | if not line: return None | 26 | | 27 | words = re.split('[^A-Za-z]+', line) | 28 | | 29 | bad_words = {} | 30 | | 31 | while get_next_word(): | 32 | if not nadiah_speling.exists(word): | 33 | bad_words[word] = bad_words.get(word, 0) + 1 | 34 | | 35 | for word in bad_words: | 36 | print "%s (%d)" % (word, bad_words[word]) |
|