Question

In: Computer Science

Write the function letter_frequencies(text) that returns a dictionary containing all of the letter frequencies of all...

Write the function letter_frequencies(text) that returns a dictionary containing all of the letter frequencies of all letters occurring in the parameter text.

For example:

if __name__ == '__main__':

    d = letter_frequencies('hello, world')
    print(d) # show the contents of this dictionary

will produce the following output:

    {'a': 0.0, 'b': 0.0, 'c': 0.0, 'd': 0.1, 'e': 0.1, 'f': 0.0, 'g': 0.0, 
    'h': 0.1, 'i': 0.0, 'j': 0.0, 'k': 0.0, 'l': 0.3, 'm': 0.0, 'n': 0.0, 
    'o': 0.2, 'p': 0.0, 'q': 0.0,'r': 0.1, 's': 0.0, 't': 0.0, 'u': 0.0, 
    'v': 0.0,'w': 0.1, 'x': 0.0, 'y': 0.0, 'z': 0.0}

Solutions

Expert Solution

The code for given problem statement:

def letter_frequencies(text):   #   definition of the function
    text=text.lower()   #   Converts all the letters in lowercase
    finaltext=''    #   new text
    for i in text:  #   removes all the characters other than letters
        if i.isalpha():
            finaltext+=i
    alphabet=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']   #   List of letters
    d={}    #   Dictionary to strore results
    for i in alphabet:  #   Initiallizes the dictionary with all the keys. 
        d[i]=0.0    #   All values are Initiallized by 0.0
    for letter in finaltext: #   For every letter in the text 
        d[letter]+=1/len(finaltext)  #   increaments the value by 0.1
        d[letter]=round(d[letter],1)    #   To eliminate error 
    return d    #   returns updated dictionary 
if __name__ == '__main__':
    print(letter_frequencies('hello, world'))   #   Calls the function 
        
        

Screenshot of the code:

Screenshot of output:

Logic of the code:

The code finds the relative frequency for given text

In 'hello, world' there are 10 characters which are letters: h,e,l,l,o,w,o,r,l,d

As frequency of h is 1, the relative frequency = 1/10=0.1

e=1, 1/10=0.1

l=3, 3/10=0.3

o=2, 2/10=0.2

w=1, 1/10=0.1

r=1, 1/10=0.1

d=1, 1/10=0.1

relative frequency=frequency/ total number of elements

The code first lowers all the letters in the text. Copies all the present letters In a new string named finaltext

There is a list defined 'alphabet' in which all the letters are listed.

One dictionary is defined whose keys are all alphabet and values are initialized to 0.0

Then to record frequency a loop is performed for every character in finaltext.

For every letter in text frequency is increased by 1/(total number of letters)

The float value should be updated as there can be little error in float value. For example if this program is executed without rounding off then frequency of 'l' given by code is:

'l': 0.30000000000000004  instead of 0.3

Note that round function rounds the given number upto mentioned decimal places, in this case code is rounding off the number upto 1 decimal place.

Executing this code for some other inputs:

Here input is Python

python word has all 6 different letters

So frequency of p=y=t=h=o=n = 1/6 = 0.167 (rounding off upto 3 decimal place)


Related Solutions

Write a program that uses a dictionary to assign “codes” to each letter of the alphabet....
Write a program that uses a dictionary to assign “codes” to each letter of the alphabet. For example:      codes = {'A':'%', 'a':'9', 'B':'@', 'b':'#', etc....} Using this example, the letter “A” would be assigned the symbol %, the letter “a” would be assigned the number 9, the letter “B” would be assigned the symbol “@” and so forth. The program should open a specified text file, read its contents, and then use the dictionary to write an encrypted version of...
Please write in Python code please Write a program that creates a dictionary containing course numbers...
Please write in Python code please Write a program that creates a dictionary containing course numbers and the room numbers of the rooms where the courses meet. The dictionary should have the following key-value pairs: Course Number (key) Room Number (value) CS101 3004 CS102 4501 CS103 6755 NT110 1244 CM241 1411 The program should also create a dictionary containing course numbers and the names of the instructors that teach each course. The dictionary should have the following key-value pairs: Course...
You are given a text file containing a short text. Write a program that 1. Reads...
You are given a text file containing a short text. Write a program that 1. Reads a given text file : shortText.txt 2. Display the text as it is 3. Prints the number of lines 4. Prints the occurences of each letter that appears in the text. [uppercase and lowercase letter is treated the same]. 5. Prints the total number of special characters appear in the text. 6. Thedisplayofstep3,4and5aboveshouldbesaveinanoutputfile:occurencesText.txt write it in C++ programing Language
Write a function which receives a list and returns a number. In the list, all numbers...
Write a function which receives a list and returns a number. In the list, all numbers have been repeated twice except one number that is repeated once. The function should return the number that is repeated once and return it.write a python program for this question. use main function.
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them
Write a code to find the following in a text file (Letter). language: Python (a) Find...
Write a code to find the following in a text file (Letter). language: Python (a) Find the 20 most common words (b) How many unique words are used? (c) How many words are used at least 5 times? (d) Write the 200 most common words, and their counts, to a file. text file: Look in thy glass and tell the face thou viewest, Now is the time that face should form another, Whose fresh repair if now thou not renewest,...
Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the...
Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the given wordlist  An anagram dictionary has each word with the letters sorted alphabetically creating a “key”.
Write a function called alter_sum(n)that returns the alternating sum of all the numbers from 1 to...
Write a function called alter_sum(n)that returns the alternating sum of all the numbers from 1 to n so that the first number is added, the second number is subtracted, the third number added, the fourth subtracted, and so on: 1-2+3-4+5-6+7… until you reach n. If n is 0 or less then return 0.
Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name...
Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name it addToDictionary(s,r) that take a string and add it to a dictionary if the string exist increment its frequenc 2) function named freq(s,r) that take a string and a record if the string not exist in the dictinary it return 0 if it exist it should return its frequancy.
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT