我的代码的目标是要求以字典格式输入数据和目标。输入是单词,目标是定义,我想返回输出,这是目标或定义。我在尝试另一种方法,你能帮我吗?
def search(input, target):
data_words = []
for x in input:
data_words += [x[0].lower()]
if target.lower() not in data_words:
return "Word does not exist"
else:
index = data_words.index(target.lower())
return (input[index][-1])发布于 2022-04-27 14:17:54
据我所知,你想要的是明确的词语。例如:
苹果的定义:玫瑰科的一棵树的圆形果实,通常有薄的绿色或红色的皮和脆的果肉。
您可以创建一个dict并将您的目标(在本例中是apple)和定义添加到该dict中。
首先,您应该安装nltk包。
只需在终端上运行pip install nltk即可。
下面是代码:
# Use nltk to tokenize words
import nltk
nltk.download('punkt')
from nltk.tokenize import word_tokenize
# Create your definitions dictionary
dictionary = {'apple': 'the round fruit of a tree of the rose family, which typically has thin green or red skin and crisp flesh.',
'banana': 'a long curved fruit which grows in clusters and has soft pulpy flesh and yellow skin when ripe.'}
def search(sentence):
sentence = sentence.lower()
tokenize = word_tokenize(sentence)
for i in tokenize:
if i in dictionary:
print(f"The definition of {i} is:", dictionary[i])
getDefinition = input("The word that you want to see it's definiton: ")
search(getDefinition)https://stackoverflow.com/questions/72030106
复制相似问题