'Thesaurus Lab Assignment
I have a homework assignment that I can't seem to figure out. The problem is:
Given a set of text files containing synonyms for different words, complete the main program to output the synonyms for a specific word. Each text file contains synonyms for the word specified in the file’s name, and each row within the file lists the word’s synonyms that begin with the same letter, separated by a space. The program reads a word and a letter from the user and opens the text file associated with the input word. The program then stores the contents of the text file into a dictionary predefined in the program. Finally the program searches the dictionary and outputs all the synonyms that begin with the input letter, one synonym per line, or a message if no synonyms that begin with the input letter are found.
Hints: Use the first letter of a synonym as the key when storing the synonym into the dictionary. Assume all letters are in lowercase.
Ex: If the input of the program is:
educate c the program opens the file educate.txt, which contains: brainwash brief civilize coach cultivate develop discipline drill edify enlighten exercise explain foster improve indoctrinate inform instruct mature nurture rear school train tutor then the program outputs: civilize coach cultivate Ex: If the input of the program is: educate a then the program outputs: No synonyms for educate begin with a.
I currently have this with only one file being focused right now:
synonyms = {} # Define dictionary
initial_input = input()
start_letter = input()
if initial_input == 'Educate' or 'educate':
with open('educate.txt', 'r') as e:
lines = e.readlines()
for word in lines:
if ord(str(word[0])) == ord(str(start_letter)):
key = start_letter
split = word.split(' ')
synonyms[key] = split
print(synonyms)
if ord(str(start_letter)) != ord(str(word[0])):
print(f'No synonyms for educate begin with {start_letter}.')
elif initial_input == 'Happy' or 'happy':
with open('happy.txt', 'r') as h:
lines = h.readlines()
elif initial_input == 'Goal' or 'goal':
with open('goal.txt', 'r') as e:
lines = e.readlines()
and when inputting:
educate
b
I get:
{'b': ['brainwash', 'brief\n']}
No synonyms for educate begin with b.
No synonyms for educate begin with b.
No synonyms for educate begin with b.
No synonyms for educate begin with b.
No synonyms for educate begin with b.
No synonyms for educate begin with b.
No synonyms for educate begin with b.
No synonyms for educate begin with b.
No synonyms for educate begin with b.
No synonyms for educate begin with b.
I know I can get the values individually but cannot seem to new line each entry by replacing print(synonyms) with print(word), but currently I am at a loss.
Edit: I have modified the code a little bit and came up with:
if initial_input.lower() == 'educate':
with open('educate.txt', 'r') as e:
lines = e.readlines()
for word in lines:
if word[0] == start_letter:
split = word.split(' ')
synonyms[start_letter] = split
for x in synonyms[start_letter]:
print(x)
break
else:
print(f'No synonyms for educate begin with {start_letter}.')
but see that since it reads the lines top to bottom, when changing the starting letter to 'c' for example, it outputs:
No synonyms for educate begin with c.
civilize
coach
cultivate
How would you make it not look at the letters above?
Solution 1:[1]
Your
if ord(str(start_letter)) != ord(str(word[0])):
print(f'No synonyms for educate begin with {start_letter}.')
Runs for each line. Therefore, it will print the no synonyms statement for each line in educate.txt that does not begin with your target letter.
You have to make sure it only prints once, likely with something like
for word in lines:
if word[0] == start_letter:
split = word.split(' ')
synonyms[start_letter] = split
print(synonyms)
break
else:
print(f'No synonyms for educate begin with {start_letter}.')
Additionally, your if statements don't work, since the or 'educate' clause is always true. You should either do
if initial_input == 'Educate' or initial_input == 'educate'
Or, likely better,
if initial_input.lower() == 'educate'
Solution 2:[2]
This was a difficult one to solve and quite long. I tried to write as much instructions of what I did as possible. I hope this helps someone out there.
synonyms = {} # Define dictionary
# Type your code here
file_name = input()
user_input = input()
file = open(file_name + '.txt', 'r') # open file
content = file.readlines() # add content to a list (e.g., every line converts to an element on a list)
# empty lists
first_letters = []
fl_matching_words = []
stored_answer = []
# convert every element in the content variable list into lists
# add every converted elemental lists to the fl_matching_words variable list
for x in content:
fl_matching_words.append(x.split())
# append the first letter of each element in the content variable list to the first_letters variable list
for x in content:
first_letters.append(x[0])
# add every element in the first_letters list as keys and every element in fl_matching_words as values into the synonyms dictionary
for x in range(len(first_letters)):
synonyms[first_letters[x]] = fl_matching_words[x]
# if user_input equals a key in the synonyms dictionary, store all answers or lines in the stored_answer variable list
for x in synonyms:
if user_input == x:
stored_answer.append(synonyms[x])
# if user_input not in first_letters print out a message
# otherwise, print the words stored in a list within a list
if user_input not in first_letters:
print('No synonyms for {} begin with {}.'.format(file_name, user_input))
else:
for x in stored_answer:
for y in x:
print(y)
file.close() # close file
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | M. Chak |
| Solution 2 | Python4Me |
