Question: 189 PYTHON PROGRAM WRITE A PYTHON PROGRAM POST A SCREENSHOT OF RESULT Write a program letterfreq.py that computes the frequency with which each letter appears

189

PYTHON PROGRAM

WRITE A PYTHON PROGRAM

POST A SCREENSHOT OF RESULT

Write a program letterfreq.py that computes the frequency with which each letter appears in a given file. Handle case appropriately.

EXAMPLE OF WORD FREQUENCY

Word Frequency

1 # wordfreq.py

2

3 import string

4

5 def getwords(fname):

6 with open(fname) as f:

7 s = f.read().lower()

8 s = s.translate(s.maketrans("", "", tring.punctuation))

9 return s.split()

10

11 def frequency(words):

12 d = {}

13 for word in words:

14 if word in d:

15 d[word] += 1

16 else:

17 d[word] = 1

18 return d

19

20 def display(d):

21 for key in sorted(d):

22 print(key, d[key])

23

24 def main():

25 words = getwords("moby.txt")

26 display(frequency(words))

27

28 main ( )

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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!