如何分离 Chatterbot 的 JSON 输出
How to separate JSON output for Chatterbot
我正在使用 Python 和 chatterbot 库创建一个简单的聊天机器人。我已经能够得到一些能够工作的东西,用户输入一些东西,机器人根据 .json
的模式和响应做出响应。
每次我执行机器人时,它都能分辨出 "tag" 用户输入的内容,并仅以这种格式做出相应的响应:"patterns": ["cya", "See you later", "Goodbye", "I am Leaving", "Have a Good day"],
但我希望它删除引号、逗号等,只选择一个响应随机的。我试过这个标记化的东西?似乎不起作用
代码
## 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 !", "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 教程 btw 获得的。
您可以通过删除顶层 'intents'(不需要)并将列表加载到变量中,将 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
]
然后您可以使用 next
和 generator
访问列表中的 dict
selected_intent = next((i for i in intents if i['tag'] == 'greeting'), None)
现在,如果 tag
是 greeting
(在本例中),selected_intent
将成为列表中的意图 dict
,但如果没有意图具有该标记将 return None
- 所以一定要处理这个。
您现在可以通过键访问意图 dict
的任何部分,例如selected_intent['patterns']
- 这将 return 此标签的模式列表。所以你可以这样做;
import random
patterns = selected_intent['patterns']
return_pattern = patterns[ random.randint(0, len(patterns)-1) ]
print(return_pattern) # output: whats up
这会生成一个随机数int
,介于0和列表长度-1之间,用作从列表中提取随机字符串的索引。
p.s。您的 if
语句可以用 else
块压缩,因为第二个 if 只是第一个的倒数。
if message.strip() != "Bye":
response = bot.get_response(message)
print("ChatBot: ", response)
else:
print("ChatBot: Farewell")
break
我正在使用 Python 和 chatterbot 库创建一个简单的聊天机器人。我已经能够得到一些能够工作的东西,用户输入一些东西,机器人根据 .json
的模式和响应做出响应。
每次我执行机器人时,它都能分辨出 "tag" 用户输入的内容,并仅以这种格式做出相应的响应:"patterns": ["cya", "See you later", "Goodbye", "I am Leaving", "Have a Good day"],
但我希望它删除引号、逗号等,只选择一个响应随机的。我试过这个标记化的东西?似乎不起作用
代码
## 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 !", "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 教程 btw 获得的。
您可以通过删除顶层 'intents'(不需要)并将列表加载到变量中,将 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
]
然后您可以使用 next
和 generator
dict
selected_intent = next((i for i in intents if i['tag'] == 'greeting'), None)
现在,如果 tag
是 greeting
(在本例中),selected_intent
将成为列表中的意图 dict
,但如果没有意图具有该标记将 return None
- 所以一定要处理这个。
您现在可以通过键访问意图 dict
的任何部分,例如selected_intent['patterns']
- 这将 return 此标签的模式列表。所以你可以这样做;
import random
patterns = selected_intent['patterns']
return_pattern = patterns[ random.randint(0, len(patterns)-1) ]
print(return_pattern) # output: whats up
这会生成一个随机数int
,介于0和列表长度-1之间,用作从列表中提取随机字符串的索引。
p.s。您的 if
语句可以用 else
块压缩,因为第二个 if 只是第一个的倒数。
if message.strip() != "Bye":
response = bot.get_response(message)
print("ChatBot: ", response)
else:
print("ChatBot: Farewell")
break