Answered step by step
Verified Expert Solution
Question
1 Approved Answer
python skeleton import string from operator import itemgetter def add_word( word_map, word ): # YOUR COMMENT if word not in word_map: word_map[ word ] =
python skeleton
import string from operator import itemgetter def add_word( word_map, word ): # YOUR COMMENT if word not in word_map: word_map[ word ] = 0 # YOUR COMMENT word_map[ word ] += 1 def build_map( in_file, word_map ): for line in in_file: # YOUR COMMENT word_list = line.split() for word in word_list: # YOUR COMMENT word = word.strip().strip(string.punctuation) add_word( word_map, word ) def display_map( word_map ): word_list = list() # YOUR COMMENT for word, count in word_map.items(): word_list.append( (word, count) ) # YOUR COMMENT freq_list = sorted( word_list, key=itemgetter(1) ) print( " {:15s}{:5s}".format( "Word", "Count" ) ) print( "-"*20 ) for item in freq_list: print( "{:15s}{:>5d}".format( item[0], item[1] ) ) def open_file(): try: in_file = open( "document1.txt", "r" ) except IOError: print( " *** unable to open file *** " ) in_file = None return in_file word_map = dict() in_file = open_file() if in_file != None: build_map( in_file, word_map ) display_map( word_map ) in_file.close()
data1.txt
Name Score Joe 20 Mary 70 Rich 50 Jose 90
data2.txt
Name Score Sarah 80 Ming 20 Joe 65 Rich 30
document1.txt
Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation, so conceived and so dedicated, can long endure. We are met on a great battlefield of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we can not dedicate - we can not consecrate - we can not hallow - this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us - that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion - that we here highly resolve that these dead shall not have died in vain - that this nation, under God, shall have a new birth of freedom - and that government of the people, by the people, for the people, shall not perish from the earth.Lab Exercise #8 Assignment Overview This lab exercise provides practice with dictionaries in Python. A. Modify a program that uses Dictionaries Consider the file named "lab08a.py". That file contains the skeleton of a Python program to do a simple analysis of a text file: it will display the number of unique words which appear in the file, along with the number of times each word appears. Case does not matter: the words "pumpkin", "Pumpkin" and PUMPKIN" should be treated as the same word. (The word "map" is used in identifiers because sometimes a dictionary is called a "map.") Execute the program (which currently uses "documentl.txt" as the data file) and inspect the output. a. Replace each of the lines labeled "YOUR COMMENT" with meaningful comments to describe the work being done in the next block of statements. Use more than one comment line, if necessary. b. Add doc strings to each function to describe the work being done in the function c. The program currently processes the empty string as a word. Revise the program to exclude empty strings from the collection of words. d. The program currently processes words such as "The" and "the" as different words. Revise the program to ignore case when processing wo e. The program currently always uses "document1.txt" as the input file. Revise the program to prompt the user for the name of the input file f. Revise the program to display the collection of words sorted by greatest frequency of occurrence to least frequency, and sorted alphabetically for words with the same frequency count. Since the sorted function and the sort method are stable sorts, you can first sort the words alphabetically, and then sort them by frequency (with reverse-True). (You do the two sorts in that order because you do the primary key last, frequency is the primary key in this case.) By default sorting is done on the first item in a list or tuple. To sort on other items use itemgetter from the operator module. See documentation here, focus on the students tuple example: https://docs.python.org/3/howto/sorting.html g. Test the revised program. There are two sample documents available: "document 1.txt (The Declaration of Independence) and "document2.txt'. (The Gettysburg Address) Demonstrate your completed program to your TA. On-line students should submit the completed program (named Iab08a.py") for grading via the Mimir system
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started