python聊天机器人代码
时间: 2023-10-14 10:20:15 浏览: 84
以下是一个简单的 Python 聊天机器人代码,使用了 `nltk` 库来进行自然语言处理:
```python
import nltk
from nltk.chat.util import Chat, reflections
pairs = [
['my name is (.*)', ['Hello %1, how are you today?']],
['(hi|hello|hey)', ['Hello!', 'Hi there!']],
['what is your name?', ['My name is Bot.', 'I am Bot.']],
['how are you?', ['I am doing well, thank you.', 'Not too bad, thank you for asking.']],
['what can you do?', ['I can help you with various tasks like setting reminders, sending emails, etc.']],
['(.*) created you?', ['I was created by a team of developers.']],
['(.*) (weather|temperature) in (.*)', ['The weather in %3 is currently %2.']],
['quit', ['Bye-bye!', 'Take care.']]
]
chatbot = Chat(pairs, reflections)
chatbot.converse()
```
在这个代码中,我们定义了一些输入输出的模式对,然后使用 `Chat()` 函数来创建一个聊天机器人。使用 `converse()` 函数来启动聊天机器人。
你可以根据需要添加更多的输入输出模式对来扩展聊天机器人的能力。
阅读全文