首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何分离Chatterbot的JSON输出

如何分离Chatterbot的JSON输出
EN

Stack Overflow用户
提问于 2019-09-20 04:27:10
回答 1查看 1.8K关注 0票数 1

我正在使用Python和chatterbot库创建一个简单的聊天机器人。我得到了一些能够工作的东西,用户输入了一些东西,而机器人基于模式和响应的.json进行响应。

每次执行bot时,机器人都能够知道用户输入的“标记”是什么,并相应地只以这种格式进行响应:"patterns": ["cya", "See you later", "Goodbye", "I am Leaving", "Have a Good day"],,但我希望它删除引号、逗号等,并随机选择一个响应。我试过这个标记了吗?似乎不起作用

代码语言:javascript
复制
## 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

代码语言:javascript
复制
{"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是从技术学院获得的提姆教程。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-20 04:59:58

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

代码语言:javascript
复制
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访问列表中的generator

代码语言:javascript
复制
selected_intent = next((i for i in intents if i['tag'] == 'greeting'), None) 

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

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

代码语言:javascript
复制
import random

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

print(return_pattern) # output: whats up

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

附注:您的if语句可以被浓缩成一个else块,因为第二个if正好相反于第一个。

代码语言:javascript
复制
if message.strip() != "Bye":
    response = bot.get_response(message)
    print("ChatBot: ", response) 
else:
    print("ChatBot: Farewell")
    break
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58021767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档