用chatterbot从零开始搭建一个聊天机器人(一)

2020-03-25  本文已影响0人  文知道

简介

安装

pip install chatterbot
pip install chatterbot-corpus

原理

训练

from chatbot import chatbot
from chatterbot.trainers import ListTrainer

chatbot = ChatBot('Training Example')
trainer = ListTrainer(chatbot)

trainer.train([
    "Hi there!",
    "Hello",
])

trainer.train([
    "Greetings!",
    "Hello",
])
from chatbot import chatbot
from chatterbot.trainers import ChatterBotCorpusTrainer

chatbot = ChatBot('Training Example')
trainer = ChatterBotCorpusTrainer(chatbot)

trainer.train(
    "chatterbot.corpus.english"
)
from chatbot import chatbot
from chatterbot.trainers import ChatterBotCorpusTrainer

chatbot = ChatBot('Training Example')
trainer = ChatterBotCorpusTrainer(chatbot)

trainer.train(
    "./data/greetings_corpus/custom.corpus.json",
    "./data/my_corpus/"
)

组成

1、预处理:

chatterbot.preprocessors.clean_whitespace(statement)
chatterbot.preprocessors.unescape_html(statement)
chatterbot.preprocessors.convert_to_ascii(statement)

2、逻辑适配器

chatbot = ChatBot(
    "My ChatterBot",
    logic_adapters=[
        {
            "import_path": "chatterbot.logic.BestMatch",
            "statement_comparison_function": chatterbot.comparisons.levenshtein_distance,
            "response_selection_method": chatterbot.response_selection.get_first_response
        }
    ]
)
User: What time is it?
Bot: The current time is 4:45PM.
User: What is four plus four?
Bot: (4 + 4) = 8
from chatterbot import ChatBot

# Create a new instance of a ChatBot
bot = ChatBot(
    'Exact Response Example Bot',
    storage_adapter='chatterbot.storage.SQLStorageAdapter',
    logic_adapters=[
        {
            'import_path': 'chatterbot.logic.BestMatch'
        },
        {
            'import_path': 'chatterbot.logic.SpecificResponseAdapter',
            'input_text': 'Help me!',
            'output_text': 'Ok, here is a link: http://chatterbot.rtfd.org'
        }
    ]
)

# Get a response given the specific input
response = bot.get_response('Help me!')
print(response)

3、存储适配器

from chatterbot import ChatBot

# Uncomment the following lines to enable verbose logging
# import logging
# logging.basicConfig(level=logging.INFO)

# Create a new instance of a ChatBot
bot = ChatBot(
    'SQLMemoryTerminal',
    storage_adapter='chatterbot.storage.SQLStorageAdapter',
    database_uri=None,
    logic_adapters=[
        'chatterbot.logic.MathematicalEvaluation',
        'chatterbot.logic.TimeLogicAdapter',
        'chatterbot.logic.BestMatch'
    ]
)

# Get a few responses from the bot

bot.get_response('What time is it?')

bot.get_response('What is 7 plus 7?')
from chatterbot import ChatBot

# Uncomment the following lines to enable verbose logging
# import logging
# logging.basicConfig(level=logging.INFO)

# Create a new ChatBot instance
bot = ChatBot(
    'Terminal',
    storage_adapter='chatterbot.storage.MongoDatabaseAdapter',
    logic_adapters=[
        'chatterbot.logic.BestMatch'
    ],
    database_uri='mongodb://localhost:27017/chatterbot-database'
)

print('Type something to begin...')

while True:
    try:
        user_input = input()

        bot_response = bot.get_response(user_input)

        print(bot_response)

    # Press ctrl-c or ctrl-d on the keyboard to exit
    except (KeyboardInterrupt, EOFError, SystemExit):
        break

4、过滤器

chatbot = ChatBot(
    "My ChatterBot",
    filters=[filters.get_recent_repeated_responses]
)

常用配置

上一篇下一篇

猜你喜欢

热点阅读