Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Calculate the normalized letter frequencies. In order to calculate letter frequencies, you need a list of letters and the string in all upper-case letters.

image text in transcribedimage text in transcribedimage text in transcribed

1. Calculate the normalized letter frequencies. In order to calculate letter frequencies, you need a list of letters and the string in all upper-case letters. To avoid confusion, we will rename this built-in string ascii_uppercase as alphabet when we import it. : #grade from string import ascii_uppercase as alphabet # use \alphabet' as ascii_uppercase from now on print(alphabet) : #grade # Our example text. text = 'Jackdaws love my big Sphinx of Quartz.' text = text.upper() print(text) Next we create an empty frequency dictionary letter_freq. Loop over each letter of the alphabet and count the number of times each letter occurs in text. Add this count to letter_freq #grade letter_freq {} # a blank dictionary # Loop over the alphabet. for letter in alphabet: # For each letter, get the number of times it occurs in the string 'text. letter_count = text.count(letter) letter_freq[letter] = letter_count letter_freq The final step is to normalize the values. To do this, we need to calculate the total number of letters in text (letters, NOT whitespace or punctuation). Since this is a bit involved, the following lines of code will give us a copy of text without whitespace or punctuation: #grade # These are built-in collections of characters, useful for just this sort of filtering from string import whitespace, punctuation, digits print(whitespace, punctuation, digits) for character in whitespace+punctuation+digits: text = text.replace(character, '') Now set each frequency value in the dictionary to its normalized value. #grade for key in letter_freq.keys(): letter_freq[key] = letter_freq[key] / len(text) letter_freq Now we will turn the above process into a general function to process a string into its letter frequency

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

Relational Database Design With Microcomputer Applications

Authors: Glenn A. Jackson

1st Edition

0137718411, 978-0137718412

More Books

Students also viewed these Databases questions

Question

Define Administration and Management

Answered: 1 week ago

Question

Define organisational structure

Answered: 1 week ago