$20
# Analyze a text file to produce
# a list of words (in alphabetical order) with their frequencies
# a list of letters (also in alphabetical order) with their frequencies
#
# Words will all be converted to lower case and have any puncutation
# marks removed.
# ***** function takes a word as input and returns
# ***** as lower case with all punctuation removed.
#
# *****: convert the word to lower case.
# ***** through the word one character at a time, and if it is
# ***** a-z, add it to a new word.
def lower_letters_only(word):
lower_word = ""
word = word.lower()
for letter in word:
if letter in "abcdefghijklmnopqrstuvwxyz":
lower_word+= letter
return lower_word
# This function will repeatedly ask for a file name
# and return a file object when it is able to successfully
# open the file. Hint: use try/except and use a boolean variable
# such as "valid_file" to indicate whether you had a valid file or not.
def get_input_file(fn): #fn -- file name
while True:
print('enter ' + file_type + ' filename: ')
filename = input()
print(filename)
try:
with open(filename, 'r') as f:
my_file = f.read()
return my_file
except FileNotFoundError:
print('No such file. Check file name and path and try again.')
except IOError:
print('File does not exist')
pass
#
# This function will take as its input a dictionary
# and will print the contents with the keys in alphabetical
# order. For example, {'cat': 3, 'ant':4, 'bee': 7} will print as
#
# ant 4
# bee 7
# cat 3
#
# Hint: use list(d.keys()) to get all the dictionary keys as a list.
def display_dictionary(d):
new_list = []
for element in d.keys():
new_list.append(element)
new_list.sort()
for element in new_list:
print(element, d[element])
pass
############# ***** program begins here
# ***** an empty dictionary for the words
words = { }
# ***** an empty dictionary for the letters
letters = { }
# Get the input file
with open('words.txt','r') as f:
# Read the file one line at a time, and:
for line in f:
for word in line.split():
print(word)
# split it into words
# for each word:
# convert to lowercase and remove punctuation
# go through it letter by letter (hint: use for ... in range(),
# updating the letter count in the letters dictionary
for word in len in range(word):
# Close the input file
f.close()
# display the words dictionary
print(words)
# display the letters dictionary
print(letters)