Question: Can you help me solve the Python problem? Objectives: Read a data file and analyze its contents Use a Python dictionary to store counters reuse

Can you help me solve the Python problem?

Objectives:

Read a data file and analyze its contents

Use a Python dictionary to store counters

reuse the function findFile() from assignment #7

Linguists find it interesting to see how often certain letters of the alphabet are used by various authors and in different languages. For this assignment you will write a program that calculates the frequency of all the letters (a through z) in a file specified by the user.

For this assignment your program will prompt the user for the name of a file, open the file and read it through once. Your program will then report on the frequency of each letter of the alphabet, regardless of case (capital or lower case). Recall that the frequency of a letter is defined to be the number of times that letter appears in the file divided by the total number of letters in the file.

Please submit your source code file, a sample data file and a recording of the run your program generates when it processes the sample data file.

Hints:

Reuse the function you wrote for Assignment #7 that repeatedly asks the user for the name of a file until your program is able to open the file, you must import this function.

Create various data files to test your program with. For example, you can create a file with just "a"s and "A"s and white space and punctuation marks in it to see that the frequency of "a" is output as 100% and the frequency of all the rest of the letters is output as 0%.

The string methods built into Python will be helpful for this assignment also:

https://docs.python.org/3.1/library/stdtypes.html#string-methods (Links to an external site.)Links to an external site.

In order to receive full credit:

import the function you wrote for Assignment #7 that reads the filename and opens the file.

Use a dictionary to keep track of the count of each letter of the alphabet.

Read through the file just once in order to compile all of the required statistics

Code of AS 7 is here:

def readValidFIleName():

#loop till correct file name is entered

while True:

#try open the file

try:

print("What is the name of the file you would like to process?")

inputFile=input()

inputFile=inputFile+".txt"

x=open(inputFile, 'r')

return inputFile

#if file not found raise an error

except FileNotFoundError:

print("That is not a valid filename, please try again.")

#function call to get validated file name

dataFileName=readValidFIleName()

#open the validated file

dataFile=open(dataFileName,"r")

#convert the data in the file to a string

content=str(dataFile.read())

#print the content of the file

print(content)

#print the number of characters i.e the length if the string

print("number of characters:"+str(len(content)))

#print number of words i.e no of spaces - number of ',' and number of '.' + 1 (i.e if there is one space there are two words)

print("number of words:"+str(content.count(" ")+content.count(",")-content.count(".")+1))

#print number if lines i.e (count of no of new line characters +1 )

print("number of lines:"+str(content.count(" ")+1))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Sure lets tackle this problem step by step Well create a Python program that reads a data file and calculates the frequency of each letter from a to z ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!