# Nerissa Ying - cs198-ad # CS198 - Craft #2 # Date Created: 02/02/2003 # Date Last Modified: 02/09/2003 # Description: Design for module speling. # speling.py import re dictionary = {} def addWord(wordStr): """Adds a word to the dictionary. """ pass def removeWord(wordStr): """Removes a word from the dictionary. """ pass def checkWord(wordStr): """Checks to see if the wordStr matches a word in the dictionary. If wordStr is found within the dictionary, returns 1 (true). Else, it returns 0 (false). """ if dictionary.get(wordStr): return 1 elif dictionary.get(wordStr.lower()): return 1 else: return 0 def checkSentence(sentenceStr): """Checks to make sure all the words in the sentence are spelled correctly. Returns a list of misspelled words in the 'sentence'. A 'sentence' is simply a line of text for this function. """ sentence = sentenceStr.strip() punctiation = re.compile('[!-&(-@[-_{-~]+') sentence = punctiation.sub('', sentence) word_list = sentence.split() misspell_list = [] for word in word_list: if checkWord(word) != 1: misspell_list.append(word) return misspell_list def checkPassage(passage): """Checks to make sure all the words in the passage are spelled correctly. Returns a list of misspelled words in the whole passage given a text file. """ file = open(passage) misspell_list = [] for line in file: misspell_list.extend(checkSentence(line)) misspell_list.sort() return misspell_list # load dict.txt into the dictionary dict_file = open('dict.txt') for word in dict_file: dictionary[word.strip()] = word.strip()