CHARS_TO_REMOVE = '.,"\';:!()?[]' def normalize_word(word): '''return a word lowercase and stripped of punctuation''' for char in CHARS_TO_REMOVE: word = word.replace(char, '') return word.lower() def count_words(filename): '''returns a dictionary mapping words to the number of times the word appears in filename''' word_counts = {} file = open(filename) for line in file: line = line.strip() # throw away newline words = line.split() # get all the words in the line for word in words: word = normalize_word(word) word_counts[word] = word_counts.get(word, 0) + 1 file.close() return word_counts