Question

An important part of automated document analysis is measuring the frequency at which words and characters...

An important part of automated document analysis is measuring the frequency at which words and characters appear in the document. Search engines and other tools can use this to try to determine whether two documents are related to each other in a significant way (so that both are returned as search results, etc.) We are going to build up to producing frequency analysis for words in documents, by beginning with frequency counts for characters in a string. The frequency with which a character appears is the count of the number of times the character appears in the string. The relative frequency is the frequency divided by the total number of letters in the string. The file "hw2problem2.py" contains a function called frequency(), which takes two arguments: a single character string named c, and a target string named s. Complete the body of the function to compute the relative frequency with which character c appears in string s by doing the following: 1. First, count the number of times that c appears in s. 2. Second, compute the total number of characters in s. 3. Third, divide the result of the step 1 by the result of step 2. 4. Fourth, multiply the result of step 3 by 100. 5. Fifth, round the result to two decimal places (Study the built-in function round()). Return the final value as the result of the function—the relative frequency with which c appears in s. For example, if I call frequency("i", "Mississippi"). The letter "i" appears 4 times in the string "Mississippi". The total number of characters in "Mississippi" is 11. 4 ÷ 11 = 0. 3636തതതത. 0.3636തതതത × 10 = 36.3636തതതത. Rounded to two decimals the result is 36.36 %.

Need help with the coding on Python

Homework Answers

Answer #1

If you have any doubts, please give me comment...

def frequency(c, s):

    # 1. First, count the number of times that c appears in s.

    c_occ = 0

    for ch in s:

        if ch==c:

            c_occ += 1

    # 2. Second, compute the total number of characters in s.

    length_s = len(s)

    # 3. Third, divide the result of the step 1 by the result of step 2

    freq = c_occ/length_s

    # 4. Fourth, multiply the result of step 3 by 100.

    freq_perc = freq*100

    # 5. Fifth, round the result to two decimal places

    freq_perc = round(freq_perc, 2)

    # Return the final value as the result of the function

    return freq_perc

result = frequency("i", "Mississippi")

print("The relative frequency is "+str(result)+"%.")

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Python function that computes the frequency of characters in a string s. The function...
Write a Python function that computes the frequency of characters in a string s. The function should return a map where the key is the character and the value is the count. Make sure you include a main function and include comments to document your code.
C++: Write a function that receives a pointer to a character string consisting of only alphabets...
C++: Write a function that receives a pointer to a character string consisting of only alphabets a-z and returns the character with the maximum number of repetitions. If more than one-character repeats same number of times, return the character with smallest alphabetical order. For example, the string “Mississippi” has three repeated characters (i-4, s-4, p-2). Both ‘i’ and ‘s’ repeated 4 times but ‘i’ is alphabetically smaller hence the function should return ‘i’. Do not count repeated blanks, special characters,...
Part 1 Write a program that reads a line of input and display the characters between...
Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters. For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#! 1) Name your program stars.c. 2) Assume input is no more than 1000 characters. 3) String library functions are NOT allowed in this program. 4) To read a...
C Programming. Do not use a function in the solution. Assignment 6 This assignment focuses on...
C Programming. Do not use a function in the solution. Assignment 6 This assignment focuses on using arrays. Instructions Create a program called arrays.c that does the following: 1. Prompt the user for a string (<= 100 characters). (using the attached file: AS06Data.txt) AS06Data.txt file contains the text " Instructions: When your executing program displays "Enter string 1:" starting with T, select the line of text below, copy with ctrl-c, then type ctrl-v THIS IS a test of string 0123456789...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
ANSWER IN C++ ONLY A string of characters including only alphabets (lowercase letters) is provided as...
ANSWER IN C++ ONLY A string of characters including only alphabets (lowercase letters) is provided as an input. The first task is to compute the frequency of each character appearing in the string. In the output, the characters have to be arranged in the same order as they appear in the input string. Then characters have to be rearranged, such that all the characters having a specific frequency, say xx, come together. Let the frequency of a character, lying in...
Write a python program that will perform text analysis on an input text using all of...
Write a python program that will perform text analysis on an input text using all of the following steps: 1. Take the name of an input file as a command line argument. For example, your program might be called using python3 freq.py example1 to direct you to process a plain text file called example. More information is given on how to do this below. 2. Read the contents of the file into your program and divide the text into a...
Python (Using for reference please comment which is which!) Exercise 1: Store Unique Words as Dictionary...
Python (Using for reference please comment which is which!) Exercise 1: Store Unique Words as Dictionary Keys Write a program that stores unique words as keys in a dictionary. Don't worry about upper/lowercase for now (i.e., it's ok for you to have separate entries for "The" and "the"), and just assign 1 as the value for each key. Use an input loop (while loop with an input function) to accepts values putting them into the dictionary until the user enters...
3. Create a program which allows the user to swap individual words in an input string....
3. Create a program which allows the user to swap individual words in an input string. Given a string of input, your program should count and store the number of words in a 2D array. The user can then select the index of the words they want to swap. Your program should swap those two words and then print the entire string to ouput. • Use string prompt "> ". • Use indices prompt "Enter two indices: ". • Remove...
This assignment involves using a binary search tree (BST) to keep track of all words in...
This assignment involves using a binary search tree (BST) to keep track of all words in a text document. It produces a cross-reference, or a concordance. This is very much like assignment 4, except that you must use a different data structure. You may use some of the code you wrote for that assignment, such as input parsing, for this one. Remember that in a binary search tree, the value to the left of the root is less than the...