Download this file.
Feb 17, 23:52 - Thanh: I like your docstrings; they're pretty succinct and clear as to what your functions do! It'd be easier for reading/understanding if you had put the if expressions on separate lines; but, it's not that big of a deal. It's just a style issue, I guess.
Please log in if you would like to add comments. | |
1 | #!/usr/bin/env python | 2 | | 3 | # Karl Chen | 4 | | 5 | # $Id: speling.py,v 1.8 2003/02/07 08:33:07 quarl Exp $ | 6 | | 7 | import re | 8 | | 9 | global dict | 10 | | 11 | def read_dictionary(dictionary_file): | 12 | """Load a dictionary file. | 13 | | 14 | DICTIONARY_FILE should be a string filename.""" | 15 | global dict | 16 | dict = {} | 17 | file = open(dictionary_file) | 18 | while 1: | 19 | line = file.readline() | 20 | if not line: break | 21 | dict[line.split('\n')[0]] = 1 | 22 | | 23 | def read_file(file): | 24 | """Reads a file and returns a string of its contents.""" | 25 | return ''.join(open(file).readlines()) | 26 | | 27 | def check_word(word): | 28 | """Returns true if a word is spelled correctly.""" | 29 | return dict.get(word) | 30 | | 31 | def tokenize_text(text): | 32 | """Tokenize a block of text into a list of words. | 33 | | 34 | Input TEXT is any string. Returns a list of words.""" | 35 | words = re.split('[^A-Za-z]+', text) | 36 | if not words: return None | 37 | if words and words[0] == '': words = words[1:] | 38 | if words and words[-1] == '': words = words[0:-1] | 39 | return words |
|