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.
<proverbs.txt>
All's well that ends well.
Bad news travels fast.
Well begun is half done.
Birds of a feather flock together.
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)
File :proverbs.txt
All's well that ends well.
Bad news travels fast.
Well begun is half done.
Birds of a feather flock together.
file = open("proverbs.txt")
table =dict()
for line in file :
words = line.split()
for i in words:
i=i.lower()
if i[-1]=="." or i[-1]==",":
i=i[:len(i)-1]
table[i]=table.get(i,0)+1
print(table)
RESULT:
{ 'all's': 1, 'well': 3, 'that': 1, 'ends': 1, 'bad': 1, 'news': 1, 'travels': 1, 'fast': 1, 'begun': 1, 'is': 1, 'half': 1, 'done': 1, 'birds': 1, 'of': 1, 'a': 1, 'feather': 1, 'flock': 1, 'together': 1 }
Get Answers For Free
Most questions answered within 1 hours.