# David Wallace # Homework 3.1: check spelling of words in input.txt # Unfortunately I didn't find a partner, so # I wrote 'speling' first, then the checker of 'input.txt'. import re dictfile = "dict.txt" dictdata = None def correct(word): """Decide if a word is spelled correctly""" loaddict() for dictword in dictdata: if word == dictword or word.lower() == dictword: return True return False def split_text_to_words(text): """Convert string to list of words, ignoring punctuation""" notword = re.compile("[^a-zA-Z']") return notword.sub(' ',text).split() # convert non-word chars to spaces # This should be a private function somehow. def loaddict(): """Read dictionary file into memory, if not already present.""" global dictdata if not dictdata: print "loading dictionary..." file = open(dictfile, 'r') dictdata = file.readlines() #for word in dictdata: word = word.strip() # why doesn't this work? dictdata = [word.strip() for word in dictdata] file.close()