gpt4 book ai didi

python - 'words' 是一种方法吗?

转载 作者:行者123 更新时间:2023-11-28 22:26:36 27 4
gpt4 key购买 nike

在编写这个聊天机器人教程 ( https://apps.worldwritable.com/tutorials/chatbot/ ) 时,我被困在了第一垒......这可能是一个非常初学者的错误,比如,抱歉。

所以我把这段代码放在 Ipython 中:


GREETING_KEYWORDS = ("hello", "hi", "greetings", "sup", "what's up",)

GREETING_RESPONSES = ["'sup bro", "hey", "nods", "hey you get my snap?"]

def check_for_greeting(sentence):
for word in sentence.words:
if word.lower() in GREETING_KEYWORDS:
return random.choice(GREETING_RESPONSES)

然后调用它:


import numpy as np 
phrase = ["hello"]
np_phrase = np.array(phrase)
check_for_greeting(np_phrase)

也尝试了不同的类型,但总是相同的答案:“x”对象没有属性“words”

“文字”是一种方法吗?它是如何工作的?

提前非常感谢您,我保证,我已经寻找答案很长时间了。

最佳答案

您错过了本教程中的一些重要内容:

parsed = TextBlob(cleaned)

然后该对象被传递给 check_for_greeting() 函数:

resp = check_for_greeting(parsed)

TextBlob 的实例有一个 words attribute .本教程实际上会教您有关该对象的信息,链接到 project documentation :

Python programmers working with NLP have two great high-level libraries to choose from: TextBlob and spaCy. spaCy is easy to use and fast, though it can be memory intensive and doesn’t attempt to cover the whole of statistical NLP. TextBlob wraps the sprawling NLTK library in a very approachable API, so while it can be slower, it’s quite comprehensive. I’ll use TextBlob here, though see my article on text generation for an example using spaCy.

如果您想在本地使用相同的代码,您也必须使用 TextBlob调整函数以处理句子 作为单词序列(删除 .words 属性)。

关于python - 'words' 是一种方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44675385/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com