Question

In: Computer Science

python question <proverbs.txt> All's well that ends well. Bad news travels fast. Well begun is half...

python question

<proverbs.txt>

All's well that ends well.

Bad news travels fast.

Well begun is half done.

Birds of a feather flock together.

Please write a program that reads the file you specify and calculates how many times each word stored in the file appears. However, ignore non-alphabetic words and convert uppercase letters to lowercase letters. For example, all's, Alls, alls are considered to be the same words.

What is the output of the Python program when the input file is specified as "proverbs.txt"? That is, in your answer, include the source codes of your word counter program and its output.

Please refer to this

ex)

fname = input("file name: ")

file = open(fname, "r")

table =dict()

for line in file :

words = line. split()

for word= line. split()

if word not in table :

table[word]= 1

else:

table[word] +=1

print(table)

Solutions

Expert Solution

Code:

#Returns False if word does not contain a digit
def anydigit(word):
return any(symbol.isdigit() for symbol in word)

import string
diction = dict()

#Path of proverbs.txt in your machine. Change it according to your location
with open('C:/Users/Pavan Kumar/Desktop/proverbs.txt', 'r') as reader:
for line in reader.readlines():
line = line.strip()
words = line.split(" ")
for each in words:
#If each does not contain a digit process it;Otherwise go to next word
if (anydigit(each)) == False:
# remove punctuation symbols
tab = str.maketrans(dict.fromkeys(string.punctuation))
new_word = each.translate(tab)
#convert it to lower case
new_word = new_word.lower()
#new_word in dictionary increment count
if new_word in diction:
diction[new_word] = diction[new_word] + 1
else:
#new_word not in dictionary initialize it to 1
diction[new_word] = 1
else:
#If each contatins a digit ignore it
continue
for word in list(diction.keys()):
print(word, ":", diction[word])

Please Rate If you like the Answer.........


Related Solutions

QUESTION 3 (30 MARKS) a. „Unemployment is a bad omen for many families as well as...
QUESTION 3 a. „Unemployment is a bad omen for many families as well as the nation at large‟. Discuss the aforementioned statement. b. With the aid of diagrams, explain how fiscal and monetary policies can be used to deal with the problem of unemployment.
Which of the following statements about writing bad-news messages is true? Question 14 options: A) If...
Which of the following statements about writing bad-news messages is true? Question 14 options: A) If you must say "no," it is best to use the direct approach. B) A direct approach may be appropriate if you need to convince your readers that there is a problem they must act to solve. C) The direct approach should be used if you expect your message to shock or anger your reader. D) The indirect approach should be used with all bad-news...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT