同义词库实验室作业

问题描述 投票:0回答:4

我有一项家庭作业,我似乎无法弄清楚。这 问题是:

给定一组包含不同单词同义词的文本文件, 完成主程序以输出特定单词的同义词。 每个文本文件都包含文件中指定单词的同义词 名称,文件中的每一行都列出了该单词的同义词 以相同的字母开头,以空格分隔。该程序读取一个 来自用户的单词和一封信,并打开关联的文本文件 与输入的单词。然后程序存储文本的内容 文件存储到程序中预定义的字典中。最后是节目 搜索字典并输出以以下开头的所有同义词 输入字母,每行一个同义词,如果没有同义词则显示一条消息 找到以输入字母开头的。

提示:存储时使用同义词的第一个字母作为键 同义词入词典。假设所有字母均为小写。

例如:如果程序的输入是:

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.

我目前只有一个文件正在关注:

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()

输入时:

educate
b

我得到:

{'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.

我知道我可以单独获取值,但似乎无法通过用 print(word) 替换 print(同义词) 来换行每个条目,但目前我不知所措。

编辑: 我稍微修改了代码并想出了:

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}.')

但是请注意,由于它从上到下读取行,因此当将起始字母更改为“c”时,它会输出:

No synonyms for educate begin with c.
civilize
coach
cultivate

如何让它不看上面的字母?

python
4个回答
0
投票

你的

if ord(str(start_letter)) != ord(str(word[0])):
                print(f'No synonyms for educate begin with {start_letter}.')

针对每行运行。因此,它将为 education.txt 中不以目标字母开头的每一行打印 no synonyms 语句。

你必须确保它只打印一次,可能是这样的

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}.')

此外,您的 if 语句不起作用,因为 or 'educate' 子句始终为 true。你应该这样做

if initial_input == 'Educate' or initial_input == 'educate'

或者,可能更好,

if initial_input.lower() == 'educate'

0
投票

这是一个很难解决的问题,而且时间很长。 我尝试尽可能多地写下我所做的事情的说明。 我希望这对那里的人有帮助。

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

0
投票

这就是我解决问题的方法。我知道晚了三个月,但我希望它对某人有帮助

 synonyms = {}   # Define dictionary

user = input()
letter = input()

with open(user+'.txt','r') as f:
    lines = f.readlines()
    for word in lines:
       key=word[0]
       words=word.split()
       synonyms[key]=words

if letter in synonyms:
    for i in synonyms[letter]:
        print (i)
else:
    print (f'No synonyms for {user} begin with {letter}.')e

0
投票
file_name = input()
start_letter = input()
count = 0

if file_name == 'educate':
    with open('educate.txt', 'r') as f:
        lines = f.readlines()
        for word in lines:
            if word[0] == start_letter:
                sep = '\n'
                key = word.split()
                print(sep.join(key))
                count = 1
        if count == 0:
            print(f'No synonyms for {file_name} begin with {start_letter}.')
if file_name == 'happy':
    with open('happy.txt', 'r') as f:
        lines = f.readlines()
        for word in lines:
            if word[0] == start_letter:
                sep = '\n'
                key = word.split()
                print(sep.join(key))
                count = 1
        if count == 0:
            print(f'No synonyms for {file_name} begin with {start_letter}.')
if file_name == 'goal':
    with open('goal.txt', 'r') as f:
        lines = f.readlines()
        for word in lines:
            if word[0] == start_letter:
                sep = '\n'
                key = word.split()
                print(sep.join(key))
                count = 1
         if count == 0:
             print(f'No synonyms for {file_name} begin with {start_letter}.')
© www.soinside.com 2019 - 2024. All rights reserved.