首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我在python中尝试了一种不同的方法。

我在python中尝试了一种不同的方法。
EN

Stack Overflow用户
提问于 2022-04-27 13:58:19
回答 1查看 31关注 0票数 -2

我的代码的目标是要求以字典格式输入数据和目标。输入是单词,目标是定义,我想返回输出,这是目标或定义。我在尝试另一种方法,你能帮我吗?

代码语言:javascript
运行
复制
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])
EN

回答 1

Stack Overflow用户

发布于 2022-04-27 14:17:54

据我所知,你想要的是明确的词语。例如:

苹果的定义:玫瑰科的一棵树的圆形果实,通常有薄的绿色或红色的皮和脆的果肉。

您可以创建一个dict并将您的目标(在本例中是apple)和定义添加到该dict中。

首先,您应该安装nltk包。

只需在终端上运行pip install nltk即可。

下面是代码:

代码语言:javascript
运行
复制
# 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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72030106

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档