In: Computer Science
Python File
program 5: Word Frequencies (Concordance) 20 pts
1. Use a text editor to create a text file (ex: myPaper.txt) It should contain at least 2 paragraphs with around 200 or more words.
2. Write a Python program (HW19.py) that
3. Your program will produce a dictionary of words with the number of times each occurs. It maps a word to the number of times the word occurs
NOTES:
BONUS +5 pts:
from os import path
import sys
def fileExists(fileName):
a=path.isfile(fileName)
if(a):
return True
else:
return False
def readWords(fname):
f = open(fname, "r")
data = f.read()
words=data.split()
return words
def populateDictionary(words):
d={}
for i in words:
d=addWordToDictionary(i, d)
return d
def addWordToDictionary(i,d):
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
i=i.casefold()
for p in i:
if p in punctuations:
i=i.replace(p,"")
if i in d:
d[i]=d[i]+1
else:
d[i]=1
return d
def checkDictionary(i,d):
i=i.casefold()
if i in d:
return True
else:
return False
def deleteWordInDictionary(i,d):
e=checkDictionary(i, d)
if(e):
d.pop(i)
return True
else:
return False
def printDictionary(d):
print("{:<15} {:<15}".format("Word","Frequency"))
for i in sorted(d):
print("{:<15} {:<15}".format(i,d[i]))
def menu(s):
if(int(s)==1):
print("Enter the word to add to the dictionary:")
r=input()
addWordToDictionary(r, d1)
print("After adding word to dictionary:")
printDictionary(d1)
elif(int(s)==2):
print("Enter the word to check")
c=input()
c1=checkDictionary(c, d1)
if(c1):
print("The word exists in the dictionary")
else:
print("The word does not exists in the dictionary")
elif(int(s)==3):
print("Enter the word to delete from dictionary:")
d2=input()
q3=deleteWordInDictionary(d2,d1)
if(q3):
print("The word ",d2," is deleted from the dictionary")
print("After deleting word from dictionary:")
printDictionary(d1)
else:
print("The word does not exists in the dictionary.So it cannot be deleted")
elif(int(s)==4):
printDictionary(d1)
elif(int(s)==5):
sys.exit()
else:
print("Enter the valid option")
fname = input("Please enter the input file with extension: ")
f=fileExists(fname)
if(f):
words=readWords(fname)
d1=populateDictionary(words)
print("The frequency of words is ")
printDictionary(d1)
print("\nMENU\n")
print("1.Add word to dictionary\n2.Check whether the word is already in the dictionary \n3.Delete the word from dictionary\n",
"4.Print the entries in table form\n5.Quit the program\n Enter your option")
s=input()
while(s):
menu(s)
print("1.Add word to dictionary\n2.Check whether the word is already in the dictionary \n3.Delete the word from dictionary\n",
"4.Print the entries in table form\n5.Quit the program\n Enter your option")
s=input()
else:
print("The entered file does not exists")
Screenshot of the code:
myPaper.txt file:
All of us, at least once in our lives, have experienced our parents hollering at us for sitting idle.
When they see us roaming around unnecessarily or sitting without any work, they seemingly ask don't we have better things to do?
We always take it as unneeded screaming and fail to realize the deeper meaning it holds. Life spent doing constructive work, is life well spent.
The great dramatist Shakespeare rightly observed that life should be measured by deeds, not years. Age is no criterion for the meaning of life.
It is the actions, good deeds which give meaning to life and make man immortal. Long life is desired by all, but if one does not do any noble deeds,
then such a life has no worth. Great leaders like Mahatma Gandhi, Lal Bahadur Shastri, Abraham Lincoln, Swami Vivekananda, Mother Teresa and many others
are remembered even after so many years after their passing. People still take inspiration from their lifestyles and preachings.
It is only the great deeds of these leaders that have inspired many generations.
A lily flower lives just for a day, but it is remembered for its fragrance and sweetness.
Respect is earned by actions and not acquired by years. Existence becomes exciting when it is lived for others or when it does something beneficial
to mankind. We generally work for the whole day, earn money and spend it on our needs. These are some common things that all do, but we should all do
some noble deeds as well. We should share our smile, advice, cheer, and help with our fellow beings. These things may give them happiness and they may
remember us when we are not around. Hence the saying, "We live in deeds, not in years," proves to be true.
Screenshot:
Screenshot of the output: