In: Computer Science
Problem 2(a). Letter Frequencies.
? Write Python code that reads a text file into memory and creates a dict object with a frequency count for each letter. For example, for encryptedA.txt, your output should contain the key:value pairs 'a': 78 and 'b': 31.
Notes
? Use Python to determine which letter has the highest frequency in each text file, and print the result.
Problem 2(b). Formatting for R.
? Write your two dictionaries with frequency counts from 2(a) to a pair of suitably named .csv files, with one column for the key and one column for the frequency counted. Include both .csv files with your commit to GitHub.
Solution: Code for each asked problem is as following. Each solution is commented in the code. Go through the code once, and if any query/problem do ask in comment section. In this code I have given the functionality of running code on as many as files user want using menu option. Program as for choice 1 or 2. Enter 1 to run the program, then enter file name on which program will run. Enter 2 to quit the program.
valid = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
while True:
print('Enter 1.For Starting Program | 2.For quit')
choice = int(input())
if(choice==1):
#solution of 2(a) to find frequency of each letter
freqs = dict()
for x in valid:
freqs[x] = 0
file = input('Enter File Name with \'.text\' extension:\n' )
with open(file) as f:
for line in f:
for char in line.lower():
if char in valid:
freqs[char] += 1
print("Dictonary for letters is:\n",freqs)
#Finds the letter with hisghest frequency and displays it on screen
max = 0
letter = ''
for x,y in freqs.items():
if y>max:
max = y
letter = x
print("Letter with maximum frequency is:",letter)
#solution of 2(b) to save distonary in .csv File
outFile = file.split('.')[0]
outFile+='.csv'
with open(outFile, 'w') as f:
for key in freqs.keys():
f.write("%s,%s\n"%(key,freqs[key]))
print("dictonary saved in ",outFile," successfully")
elif(choice==2):
break
else:
print("Enter Valid option")
Sample Output is as following: