def opendictionaryfile(file): """ Every line from file Returned as dictionary type""" dictionary= {} file = open(file) for line in file: dictionary[line]=0 return dictionary def checkifvalidword(word): """ Checks if word is a valid word. Any nonalphabet or ' character in a word is considered not a word. if word return 1 else 0""" import re pat = re.compile('[^a-zA-Z\']') if pat.search(word) == None: return 1 else: return 0 def printmistakes(misspelledwords): "Prints Spelling mistakes in alphabetical order and number of occurances""" alphalist = [] for key in misspelledwords: alphalist.append(key) alphalist.sort() for key in alphalist: print key, print ' (' + str(misspelledwords.get(key)) + ')' def spellcheckfile(txtfile, dictfile): """ Spell check txtfile with dictionary of words specifed in dictfile Prints Misspelled words and # occurances """ dictionary = opendictionaryfile(dictfile) misspelledwords = {} txtfile = open(txtfile) for line in txtfile: for word in line.split(' '): if checkifvalidword(word) == 1: if (word + '\n' in dictionary) == 0: misspelledwords[word] = misspelledwords.get(word, 0) + 1 printmistakes(misspelledwords)