""" Counting unique words, and the frequencies of words, from the text stored in a specific file. Improvements from word_count_take_1: - in the count_words function, we convert each line of text to lower case, so that the results are case-insensitive. Remaining problems: - We want to ignore punctuation. - We want to sort results by frequency. """ import os """ function count_words: argument: a filename return value: a dictionary, mapping words to frequencies. description: this function builds a dictionary such that dictionary[word] is the number of times the word occurs in the filename. """ def count_words(filename): if (os.path.isfile(filename) == False): print("\nError: file " + filename + " does not exist.\n") return in_file = open(filename, "r") # initialize the dictionary to empty result = {} for line in in_file: line = line.lower() words = line.split() for word in words: if (word in result): result[word] += 1 else: result[word] = 1 return result """ function print_word_frequencies: argument: a dictionary, mapping words to frequencies return value: nothing is returned. description: this function prints the contents of the dictionary in a way that is easy to read. """ def print_word_frequencies(dictionary): print() for word in dictionary: frequency = dictionary[word] print(word + ":", frequency) print() print(len(dictionary), 'words found\n') def main(): filename = "file1.txt" dictionary = count_words(filename) print_word_frequencies(dictionary) main()