Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I could use a lot of help on this Python 3+ program I have to use this python skeleton code: ------------------------------------------------------------------ import string from operator

I could use a lot of help on this Python 3+ program

I have to use this python skeleton code:

------------------------------------------------------------------

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() 

--------------------------------------------------------------------------------------------------------------

document1.txt contains:

 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. 

image text in transcribed

The correct output is (Chegg copy and pastes funny, all the counts should be in line going down):

Word Count -------------------- that 13 the 11 we 10 here 8 to 8 a 7 and 6 can 5 for 5 have 5 it 5 nation 5 not 5 of 5 dedicated 4 in 4 this 4 are 3 dead 3 great 3 is 3 people 3 shall 3 so 3 they 3 us 3 who 3 be 2 but 2 conceived 2 dedicate 2 devotion 2 far 2 from 2 gave 2 living 2 long 2 men 2 new 2 on 2 or 2 our 2 rather 2 these 2 war 2 what 2 which 2 above 1 add 1 advanced 1 ago 1 all 1 altogether 1 any 1 as 1 battlefield 1 before 1 birth 1 brave 1 brought 1 by 1 cause 1 civil 1 come 1 consecrate 1 consecrated 1 continent 1 created 1 detract 1 did 1 died 1 do 1 earth 1 endure 1 engaged 1 equal 1 fathers 1 field 1 final 1 fitting 1 forget 1 forth 1 fought 1 four 1 freedom 1 full 1 god 1 government 1 ground 1 hallow 1 highly 1 honored 1 increased 1 larger 1 last 1 liberty 1 little 1 live 1 lives 1 measure 1 met 1 might 1 never 1 nobly 1 nor 1 note 1 now 1 perish 1 place 1 poor 1 portion 1 power 1 proper 1 proposition 1 remaining 1 remember 1 resolve 1 resting 1 say 1 score 1 sense 1 seven 1 should 1 struggled 1 take 1 task 1 testing 1 their 1 those 1 thus 1 under 1 unfinished 1 vain 1 whether 1 will 1 work 1 world 1 years 1

Modify a program that uses Dictionaries. Consider the file named "lab08a.py". That file contains the skeleton ofa 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 "document .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 words. 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 hon.org/3/howto/sorting html

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke

1st Edition

0130086509, 978-0130086501

More Books

Students also viewed these Databases questions