Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Compose a function calc_freq which accepts a string text. calc_freq should return a dictionary containing the normalized frequency by letter You should use the above

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Compose a function calc_freq which accepts a string text. calc_freq should return a dictionary containing the normalized frequency by letter You should use the above process just outlined to write this function. When diagnosing the behavior of your code, we encourage you to use print statements freely. #grade # define your function here from string import whitespace, punctuation, digits from string import ascii_uppercase as alphabet def calc_freq(text): Calculate the frequency in the text of each letter Args : string: a piece of text Returns: dict: the frequency of each letter in a dictionary (e.g. letter_freq['A') gives 0.06) # Create an empty frequency dictionary Letter_freq. letter_freq_dict = {} # Initialize values in the letter_freq_dict ## YOUR CODE HERE # Make text upper-case. ## YOUR CODE HERE # Loop over each Letter of the alphabet: for letter in alphabet: # Count the number of times each letter occurs in text. ## YOUR CODE HERE # Add this count to letter_freq. ## YOUR CODE HERE # Make a copy of text without non-alphabet characters. from string import whitespace, punctuation, digits for character in whitespace+punctuation+digits: text = text.replace(character, "') # Normalize the frequencies and put the results back into letter freq. for key in letter_freq.keys(): letter_freq_dict[key] = letter_freq_dict[key] / len(text) # Finally, return the dict Letter freq. return letter_freq_dict # test your code here. You may edit this cell, and you may use any sample text, but the following is provided for conve text = ""'"Neither the naked hand nor the understanding left to itself can effect much. It is by instruments and helps th which are as much wanted for the understanding as for the hand. And as the instruments of the hand either give motion or instruments of the mind supply either suggestions for the understanding or cautions. (Francis Bacon, Novum Organon, Aph calc_freq(text) # it should pass this test---do NOT edit this cell from numpy import isclose test_text1 = """The study of nature with view to works is engaged in by the mechanic, the mathematician, the physiciar the magician; but by all (as things now are) with slight endeavor and scanty success. (Francis Bacon, Novum Organon, Af result_text1 = calc_freq(test_text1) assert isclose(result_text1['T'], 0.09045226130653267) and isclose(result_text1['0'], 0.0) and isclose(result_text1['Y'], 0.0251256281407035) print('Success!') # it should pass this test---do NOT edit this cell test_text2 = """In order to penetrate into the inner and further recesses of nature, it is necessary that both notions from things by a more sure and guarded way, and that a method of intellectual operation be introduced altogether better (Francis Bacon, Novum Organon, Aphorism XVIII)"" result_text2 = calc_freq(test_text2) assert isclose(result_text2['K'], 0.0) and isclose(result_text2['N'], 0.09523809523809523) and isclose(result_text2['1'], 0.015873015873015872) print('Success!')

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

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

More Books

Students also viewed these Databases questions