In: Computer Science
Python:
Word Frequencies (Concordance)
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:
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 8 11:05:06 2020
@author: SYED ZUBAIR
"""
import os
import string
filename = str(input("enter the name of the file : "))
if os.path.isfile("C:/Users/Syed Zubair/Documents/{0}.txt".format(filename)):
print("FILE EXISTS ")
f= open(filename, 'r')
d = dict()
for line in f:
line = line.strip()
# Convert the characters in line to
# lowercase to avoid case mismatch
line = line.lower()
# Remove the punctuation marks from the line
line = line.translate(line.maketrans("", "", string.punctuation))
# Split the line into words
words = line.split(" ")
for word in words:
if word in d:
d[word] = d[word] + 1
else:
d[word] = 1
for key in list(d.keys()):
print(key, ":", d[key])
else:
print("FILE DOES NOT EXIST")