In this assignment – you will develop a program that counts the…

Question Answered step-by-step In this assignment – you will develop a program that counts the… In this assignment – you will develop a program that counts the frequency of words found in a text. The text to be analyzed is stored in a text file, MLK.txt.  MLK.txt=(And so even though we face the difficulties of today and tomorrow, I still have a dream. It is a dream deeply rooted in the American dream.I have a dream that one day this nation will rise up and live out the true meaning of its creed:We hold these truths to be self-evident, that all men are created equal.I have a dream that one day on the red hills of Georgia, the sons of former slaves and the sons of former slave owners will be able to sit down together at the table of brotherhood.I have a dream that one day even the state of Mississippi, a state sweltering with the heat of injustice, sweltering with the heat of oppression, will be transformed into an oasis of freedom and justice.I have a dream that my four little children will one day live in a nation where they will not be judged by the color of their skin but by the content of their character.I have a dream today!I have a dream that one day, down in Alabama, with its vicious racists, with its governor having his lips dripping with the words of interposition and nullification, one day right there in Alabama little black boys and black girls will be able to join hands with little white boys and white girls as sisters and brothers.I have a dream today!I have a dream that one day every valley shall be exalted, and every hill and mountain shall be made low, the rough places will be made plain, and the crooked places will be made straight; and the glory of the Lord shall be revealed and all flesh shall see it together.This is our hope, and this is the faith that I go back to the South with.With this faith, we will be able to hew out of the mountain of despair a stone of hope. With this faith, we will be able to transform the jangling discords of our nation into a beautiful symphony of brotherhood. With this faith, we will be able to work together, to pray together, to struggle together, to go to jail together, to stand up for freedom together, knowing that we will be free one day.And this will be the day, this will be the day when all of God s children will be able to sing with new meaning:My country  tis of thee, sweet land of liberty, of thee I sing.Land where my fathers died, land of the Pilgrim s pride,From every mountainside, let freedom ring!And if America is to be a great nation, this must become true.And so let freedom ring from the prodigious hilltops of New Hampshire.Let freedom ring from the mighty mountains of New York.Let freedom ring from the heightening Alleghenies of Pennsylvania.Let freedom ring from the snow-capped Rockies of Colorado.Let freedom ring from the curvaceous slopes of California.But not only that:Let freedom ring from Stone Mountain of Georgia.Let freedom ring from Lookout Mountain of Tennessee.Let freedom ring from every hill and molehill of Mississippi.From every mountainside, let freedom ring.And when this happens, when we allow freedom ring, when we let it ring from every village and every hamlet, from every state and every city, we will be able to speed up that day when all of God s children, black men and white men, Jews and Gentiles, Protestants and Catholics, will be able to join hands and sing in the words of the old Negro spiritual:Free at last! Free at last!Thank God Almighty, we are free at last!)  You will have to copy this file into the directory in which your Python program is located. Your program will process each line of text from the file as it is read. As it reads a line of text – and before counting the word frequencies, your program will “clean” the data up as follows. TFor this – define and use a function, clean_text. The function has 1 parameter – text_str, the text string which needs to be “cleaned”, and returns the value of the “clean” string. To “clean” the text, this function will: • Convert each line of text to lower case so that words such as “We” and “we” are treated as the same word for frequency counts. • Remove punctuation (commas, periods, colons and semi-colons) from the text. • Remove whitespace – spaces and newline character (“n”) from the beginning and end of each line of text. The word frequencies will be stored in a dictionary where the dictionary key is the words found in the text and the dictionary values are the count of the number of times that word appeared in the text (its frequency). The following words should be excluded from the frequency counts: “the”, “a”, “and”, “of” “is”, “to”, ‘be’, “are”, “on”, “at”, “an” and “but”. After analyzing the text and determining the word frequencies, your program: • Will write the words found in the text, along with their corresponding usage count, into the file “MLKfreq.txt”. The words must be written in ascending alphabetical order with the correct counts. One word and its frequency will be written per line of output. For example, the first few lines of the output file may look like this: able 8 alabama 2 all 4 alleghenies 1 allow 1 almighty 1 america 1 Remember, the .write method only works with string data; numeric data must be converted to a string before it can be written. Also remember to include the newline character (“n”) for each output line and to use the .close method after you have finishing writing to the output file. • Will display (print) the 20 most frequently used words, along with their frequency counts, onto the console. The words will be displayed in descending order of word counts.  lines of output on the console may look like this: The 20 most frequently used words are: will 17 freedom 13 from 12 i 11 dream 11 The file a6q1_starter.py contains a function to help you with this task. This function, sort_dict_by_value (), has 1 parameter – the dictionary that should be sorted in descending order of the dictionary values. It returns a nested list where each of the elements is a list of key_value pairs from the dictionary; the list is sorted in descending order by value. For example, given a dictionary {“salmon” : 4, “wine” : 1, “apple” : 2}, the function would return the nested list [ [“wine”, 1], [“apple”, 2], [“salmon”, 4] Programming Suggestions: You may want to debug your program using the text “red blue green green the the thee thee a red red black” instead of trying to debug it with the large data file. You can thus focus on making sure your dictionary works, ignoring file input. You may want to write a separate program that simply reads from the data file and prints what it has read. Once, this is working, you can incorporate into your solution. Remember, using print statements to display the variables of variables, including a dictionary or list, is a good way to see what your program is actually doing.  a6q1_starter.py=(def sort_dict_by_value (a_dict):   “””   Functions sorts a dictionary by values (descending order)   :param a_dict: the dictionary to sort by values   :return: a nested list. Each element of the list is key:value pair from the dictionary           The key_value pairs are sorted in descending order of the value   “””   # first obtain list of dictionary values and then sort them in desdending order   values_list = sorted(a_dict.values(), reverse=True)   # make a copy of original dictionary so its values are not changed by this function   dup_dict = a_dict.copy()   key_value_pairs_sorted_by_value = []   # initialize list of keu-value pairs to be returned   for a_value in values_list:         # iterate thru sorted list of values …       for item in dup_dict:           # … looking for a corresponding key           if dup_dict[item] == a_value:    # stop the search once a corresponding key found               break       # once corresponding key found, add [key, value] element to the list to be returned       # delete key from duplicate dictionary so it doesn’t show up in another search for       #                   the same value       key_value_pairs_sorted_by_value.append ([item, a_value])       del dup_dict[item]   return key_value_pairs_sorted_by_value   # return nested list that is now sorted by value)  Computer Science Engineering & Technology Python Programming CS 11 Share QuestionEmailCopy link Comments (0)