ChatterBot之快速入门01

 本人运行环境为Python 3.5.2;

首先你需要导入chatterbot 的包,如果没有你先需要下载 使用命令 pip install chatterbot

 1 # -*- coding: utf-8 -*-
 2 from chatterbot import ChatBot
 3 
 4 bot = ChatBot(
 5     'Norman',
 6     storage_adapter='chatterbot.storage.SQLStorageAdapter',
 7     input_adapter='chatterbot.input.TerminalAdapter',
 8     output_adapter='chatterbot.output.TerminalAdapter',
 9     logic_adapters=[
10         'chatterbot.logic.MathematicalEvaluation',
11         'chatterbot.logic.TimeLogicAdapter'
12     ],
13     database='./database.sqlite3'
14 )
15 
16 while True:
17     try:
18      bot_input = bot.get_response(None)
19 
20     except(KeyboardInterrupt, EOFError, SystemExit):
21         break

下面是简单测试的结果不过现在的Norman还是傻傻的.

E:\Pythons\python.exe F:/PhyWorkSpeace/bot/botTest.py
hi
hi
你好
The current time is 04:08 PM
^D

Process finished with exit code 0

注意在的第一次执行时,会自动生成库.是系统带的简单的库.


storage_adapter='chatterbot.storage.SQLStorageAdapter',这是储存是储存适配器
input_adapter='chatterbot.input.TerminalAdapter',这是输入适配器
output_adapter='chatterbot.output.TerminalAdapter',这是输出适配器


logic_adapters=[
        'chatterbot.logic.MathematicalEvaluation',        'chatterbot.logic.TimeLogicAdapter'    ],

logic_adapters参数是逻辑适配器的列表。 在ChatterBot中,逻辑适配器是一个接受输入语句并返回该语句的响应的类。

您可以选择使用尽可能多的逻辑适配器。 在这个例子中,我们将使用两个逻辑适配器。 TimeLogicAdapter返回输入语句要求的当前时间。 MathematicalEvaluation适配器解决了使用基本操作的数学问题。

database='./database.sqlite3'#这是数据库

接下来,您将需要创建一个while循环让您的聊天机器人运行。当特定的异常被触发时,通过跳出循环,当用户进入ctrl + d/ctrl + c时,我们可以退出循环并停止程序。

while True:
    try:
     bot_input = bot.get_response(None)

    except(KeyboardInterrupt, EOFError, SystemExit):
        break

这样就是一个简单的机器人出来了,关于如何训练,和使用外部数据库,请听下回分解.

原文地址:https://www.cnblogs.com/DanBrown/p/7891307.html