##Basic Frequency Analysis of a Text File ## ##Fill in the missing statements so that this script ##reads in an input text file, builds a frequency dict, ##displays the top 10 most frequent words with how many ##times they occur, and writes a tag cloud to an ##output html file import os #useful for exiting the program if a file can't be opened import string #can use the constant string.punctuation to clean up words def count_words(line, freq_dict): """Process a string of text, adding the number of times each word appears to a frequency dictionary""" pass def display_top_words(freq_dict, n): """displays the top n most frequent words in a frequency dictionary, along with the number of times they appear""" pass def add_html_headers(body_text): """Adds header and footer around html body text to make a complete html file.""" htm="""
""" + body_text + "
" return htm def make_html_word(word, font_size): htm='' + word + '' return htm min_font = 5 # set the minimum font size for the output html file def make_tag_cloud(freq_dict): """creates an html tag cloud from a frequency dictionary. turns each word into an html word and then adds html headers. returns a html page as a string.""" return "html string goes here" ####Example of using filedialog to prompt the user for a file ##from tkinter import filedialog ##filename = filedialog.askopenfilename() ##print(filename) # test ## Load input file try: text = open("Lecture_9_input.txt", 'r') except IOError: print("Can't open input file") os._exit(1) freq_dict = {} ## build frequency dictionary here text.close() ## output top words print("Number of distinct words:", len(freq_dict)) display_top_words(freq_dict, 10) ## create output file out_file = open("output.htm", "w") print(make_tag_cloud(freq_dict), file=out_file) out_file.close()