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

Problem 2(a). Letter Frequencies. ? Write Python code that reads a text file into memory and...
Problem 2(a). Letter Frequencies. ? Write Python code that reads a text file into memory and creates a dict object with a frequency count for each letter. For example, for encryptedA.txt, your output should contain the key:value pairs 'a': 78 and 'b': 31. Notes Do not distinguish between uppercase and lowercase letters. Ignore punctuation. Punctuation counts must not appear in your dict If a given letter does not appear in the text, there must be a key:value pair with value...
(IN PYTHON) Write a function that accepts a line of text and a single letter as...
(IN PYTHON) Write a function that accepts a line of text and a single letter as input (case insensitive) and returns the number of times the letter is the last character of a word. Note your program should be able to handle different cases. And check if the user input is a single letter.
Write a function that accepts a dictionary and produces a sorted list of tuples The dictionary...
Write a function that accepts a dictionary and produces a sorted list of tuples The dictionary looks like this: {‘US’: [{'Chicago, IL': ('2/1/2020 19:43', 2, 0, 0)}, {'San Benito, CA': ('2/3/2020 3:53', 2, 0, 0)}, {'Santa Clara, CA': ('2/3/2020 0:43', 2, 0, 0)}, {'Boston, MA': ('2/1/2020 19:43', 1, 0, 0)}, {'Los Angeles, CA': ('2/1/2020 19:53', 1, 0, 0)}, {'Orange, CA': ('2/1/2020 19:53', 1, 0, 0)}, {'Seattle, WA': ('2/1/2020 19:43', 1, 0, 0)}, {'Tempe, AZ': ('2/1/2020 19:43', 1, 0, 0)}], 'Australia'...
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...
Write a function child_indices(parent, branch_factor) that returns a list containing the list of indices where the...
Write a function child_indices(parent, branch_factor) that returns a list containing the list of indices where the children of the node at index parent will be stored for a heap list with the given branch_factor (ie, a branch_factor-heap). Python language Notes: The function should return all possible indices and the indices should be in order from smallest to biggest. The values in the heap list start at index 1 - that is the root of the heap is at index 1...
Python pls create a function called search_position. This function returns a dictionary. team1 = {'Fiora': {'Top':...
Python pls create a function called search_position. This function returns a dictionary. team1 = {'Fiora': {'Top': 1, 'Mid': 4, 'Bottom': 3},'Olaf': {'Top': 3, 'Mid': 2, 'Support': 4},'Yasuo': {'Mid': 2, 'Top': 5},'Shaco': {'Jungle': 4, 'Top': 2, 'Mid': 1}} def search_position(team1): should return {'Top': {'Fiora': 1, 'Yasuo':5,'Olaf':3,'Shaco':}, 'Jungle': {'Shaco': 4}, 'Mid': {'Yasuo', 2, 'Fiora': 4,'Olaf':2}, 'Bottom': {'Fiora': 3}, 'Support': {'Olaf': 4}} *******IMPORTANT*************** The result should be alphabetical order.
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...
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 Scheme function that takes a list of integers and returns all odd integers on...
Write a Scheme function that takes a list of integers and returns all odd integers on the list in the original order: (odd-numbers `(2 4 9 16 25 7)) (9 25 7) Hint: 2 (remainder 13 5) 3 Please explain every step
Write a Scheme function that takes a positive integer n and returns the list of all...
Write a Scheme function that takes a positive integer n and returns the list of all first n positive integers in decreasing order: (decreasing-numbers 10) (10 9 8 7 6 5 4 3 2 1) Please explain every step
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT