gpt4 book ai didi

python - 如何分离 Chatterbot 的 JSON 输出

转载 作者:行者123 更新时间:2023-12-01 07:14:59 24 4
gpt4 key购买 nike

我正在使用 Python 和 chatterbot 库创建一个简单的聊天机器人。我已经能够得到一些能够工作的东西,用户输入一些内容,机器人就会根据模式和响应的 .json 进行响应。
每次我执行时,机器人都能够判断用户输入的“标签”,并仅以以下格式进行相应响应: "patterns": ["cya", "See you later", "Goodbye", "I am离开", "祝你有美好的一天"], 但我希望它删除引号、逗号等,并随机选择一个响应。我试过这个标记化的东西吗?似乎不起作用
代码

## MODULES
from chatterbot import ChatBot #This imports the chatbot
from chatterbot.trainers import ListTrainer #Method used for training the chatbot
import chatterbot #Just in case
import os

## BOT VARIABLES
bot = ChatBot("Test") #This creates the chatbot and names it 'Test'
trainer = ListTrainer(bot) #Creates the trainer
data = "C:/Users/PAVILION/Desktop/ChatBotTest2/INTS/"

for files in os.listdir(data):
conv = open(data + files, 'r').readlines() #This opens the direrctory full of data
trainer.train(conv) #This trains the chatbot trainer with data from the directory

## CHATTERBOT OUTPUT
while True:
message = input("You: ")

if message.strip() != "Bye":
response = bot.get_response(message)
print("ChatBot: ", response)

if message.strip() == "Bye":
print("ChatBot: Farewell")
break

Intentions.json

{"intents": [
{"tag": "greeting",
"patterns": ["Hi", "How are you", "Is anyone there?", "Hello", "Good day", "Whats up"],
"responses": ["Hello!", "Good to see you again!", "Hi there, how can I help?"],
"context_set": ""
},
{"tag": "goodbye",
"patterns": ["cya", "See you later", "Goodbye", "I am Leaving", "Have a Good day"],
"responses": ["Sad to see you go :(", "Talk to you later", "Goodbye!"],
"context_set": ""
},
{"tag": "age",
"patterns": ["how old", "how old is tim", "what is your age", "how old are you", "age?"],
"responses": ["I am 18 years old!", "18 years young!"],
"context_set": ""
},
{"tag": "name",
"patterns": ["what is your name", "what should I call you", "whats your name?"],
"responses": ["You can call me Tim.", "I'm Tim!", "I'm Tim aka Tech With Tim."],
"context_set": ""
},
{"tag": "shop",
"patterns": ["Id like to buy something", "whats on the menu", "what do you reccommend?", "could i get something to eat"],
"responses": ["We sell chocolate chip cookies for $2!", "Cookies are on the menu!"],
"context_set": ""
},
{"tag": "hours",
"patterns": ["when are you guys open", "what are your hours", "hours of operation"],
"responses": ["We are open 7am-4pm Monday-Friday!"],
"context_set": ""
}
]
}

顺便说一句,json 是从 Tech with Tim 教程中获得的。

最佳答案

您可以通过删除顶级“意图”(不需要)并将列表加载到变量中,将该 json 加载到 python 对象中;

intents = [

{"tag": "greeting",
"patterns": ["Hi", "How are you", "Is anyone there?", "Hello", "Good day", "Whats up"],
"responses": ["Hello!", "Good to see you again!", "Hi there, how can I help?"],
"context_set": ""
},

# rest of json pasted here
]

然后,您可以使用 nextgenerator 访问列表中的 dict

selected_intent = next((i for i in intents if i['tag'] == 'greeting'), None) 

现在,如果标签greeting(在本例中),selected_intent将是列表中的意图dict ),但如果没有意图具有该标记,它将返回 None - 所以一定要处理这个问题。

您现在可以通过键访问意图 dict 的任何部分,例如selected_intent['patterns'] - 这将返回该标签的模式列表。所以你可以这样做;

import random

patterns = selected_intent['patterns']
return_pattern = patterns[ random.randint(0, len(patterns)-1) ]

print(return_pattern) # output: whats up

这将生成一个介于 0 和列表长度 -1 之间的随机 int,它用作从列表中提取随机字符串的索引。

附:您的 if 语句可以用 else block 来压缩,因为第二个 if 只是第一个 if 的逆。

if message.strip() != "Bye":
response = bot.get_response(message)
print("ChatBot: ", response)
else:
print("ChatBot: Farewell")
break

关于python - 如何分离 Chatterbot 的 JSON 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58021767/

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