如何随机化响应?

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

我正在使用 NLTK 在 Python 中创建一个聊天机器人,但我正在考虑为 spaCy 重写它。我正在寻找一种方法,为我的人工智能提供几个预先构建的句子,以便在响应用户时从成对的句子中随机选择。

例如;假设机器人说了些什么,用户问“你想讨论一下吗?”我希望它在“是的,请!”之间进行选择和“不,谢谢!”随机的。

我的代码:

import nltk
from nltk.chat.util import Chat, reflections
import random
nltk.download("punkt")


pairs = [

 (
     r"I am satisfied with my care|quit",
     ["Bye, take care. See you soon!"]
 ),
 (
     r"would you like to discuss that?",
     ["no Thank you.", ]
 ),
 (
     r"hi|hello",
     ["Hello what is your name?", ]
 ),
 (
     r"my name is (.*)",
     ["Hello %1, how can I help you today?", ]
 ),
 (
     r"what is your name?",
     ["My name is ZeroBot and I'm here to help you.", ]
 ),
 (
     r"how are you?",
     ["I'm doing well, thank you!", ]
 ),
 (
     r"sorry (.*)",
     ["It's alright, no problem.", ]
 ),
 (
     r"(.*)",
     ["Hello %1, how can I help you today?", ]
 ),
 (
     r"what are you doing",
     ["I am talking to you.", ]
 ),

]


chatbot = Chat(pairs, reflections)


def chat():
 print("Hi, I'm ZeroBot. How can I help you today? Type 'I am satisfied with my care' to exit.")
 while True:
     user_input = input("You: ")
     response = chatbot.respond(user_input)
     print("ZeroBot:", response)
     if user_input.lower == "I am satisfied with my care":
         break
 
     if user_input.lower == "would you like to discuss that?":
         random_value = random.randint(1, 2)
         match random_value:
             case 1:
                 print("yes please!")
             case 2:
                 print("no thank you!")


chat()
python python-3.x windows nltk
1个回答
2
投票

使用

random
模块,这是一个标准库(您不必安装它)。

使用方法如下:

import random
if (your if statement):
    random_value = random.randint(1, 2)
    match random_value:
        case 1:
            print(“yes please!”)
        case 2:
            print(“no thank you!”)
© www.soinside.com 2019 - 2024. All rights reserved.