dict = {} def AddWord(word): """add a word to dictionary return true if sucessful add, otherwise false""" dict[word] = word def DelWord(word): """delete a word from dictionary""" """return true if sucessful, otherwise false""" dict[word] = None def CheckWord(word): """check if a word is in the dictionary""" upper = (word[0]).upper() + word[1:] lower = (word[0]).lower() + word[1:] """return 'word' if the word is mispelled""" if (dict.get(word) == None) and (dict.get(upper) == None) and (dict.get(lower) == None): return word else: return None def CheckSent(sent): """spell check the given sent return an array of mispelled word, and [] if no mispelled words""" for char in '0123456789,./;\'[]\\=<>?:"{}|+-_)(*&^%$#@!~': sent = sent.replace(char, ' ') res = []; for word in sent.split(): if (CheckWord(word) != None): res.append(word) return res def LoadDic(filename): """add all the words in the given file to dictionary""" file = open(filename) for line in file: for word in line.split(): AddWord(word) def IsWord(word): """return true if the input is a word""" # if it doesn't contain space, it's word # if it contain '~', it's not word if (word.replace('~', ' ') == word): return None if (word.replace(' ', '~') == word): return word def IsSent(sent): """return true if input is a sentence""" return sent def IsFN(filename): """return true if the input is a filename""" # the filename must end with .??? if len(filename) <= 4: return None if (filename[-4] != '.'): return None # the file can not contain any funny symbols for fn in filename: for char in '!@#$%^&*(){}[]|<>,?': if (fn == char): return None return filename def SpellCheck(target): """Spellcheck the target target could be a word, a sentence, an essay, or a filename""" if IsFN(target): file = open(target) res = [] for line in file: res.extend(CheckSent(line)) return res else: return CheckSent(target)