Skip to content

Instantly share code, notes, and snippets.

@napoler
Created March 8, 2019 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save napoler/b1d08e3611b4bacb6d16edb41e22459a to your computer and use it in GitHub Desktop.
Save napoler/b1d08e3611b4bacb6d16edb41e22459a to your computer and use it in GitHub Desktop.
ChatterBot.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "ChatterBot.ipynb",
"version": "0.3.2",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/napoler/b1d08e3611b4bacb6d16edb41e22459a/chatterbot.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"metadata": {
"id": "tkDI_lxxW958",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"挂载google硬盘"
]
},
{
"metadata": {
"id": "Fx78M9f-W9m9",
"colab_type": "code",
"outputId": "4a0578c1-37b4-4f3f-fa08-b9c877301bc1",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
}
},
"cell_type": "code",
"source": [
"## 挂载google硬盘\n",
"from google.colab import drive\n",
"drive.mount('/content/gdrive', force_remount=True)\n",
"root_dir = \"/content/gdrive/My Drive/\"\n",
"base_dir = root_dir + 'fastai-v3/'\n",
"app_dir = base_dir+\"ChatterBot\"\n",
"\n",
"!mkdir /content/gdrive/My\\ Drive/fastai-v3/ChatterBot"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"Mounted at /content/gdrive\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "vf2sdwdSbLsB",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
""
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "r3d1xdCBWw1I",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# ChatterBot\n",
"\n",
"ChatterBot is a machine learning, conversational dialog engine for creating chat bots https://chatterbot.readthedocs.io"
]
},
{
"metadata": {
"id": "lNgrucM-XItc",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"## 安装"
]
},
{
"metadata": {
"id": "G1OC_XSuWunD",
"colab_type": "code",
"outputId": "66a40e39-3dbf-42db-911e-49deb6535403",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 197
}
},
"cell_type": "code",
"source": [
"!pip install chatterbot"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"Requirement already satisfied: chatterbot in /usr/local/lib/python3.6/dist-packages (1.0.4)\n",
"Requirement already satisfied: chatterbot-corpus<1.3,>=1.2 in /usr/local/lib/python3.6/dist-packages (from chatterbot) (1.2.0)\n",
"Requirement already satisfied: pymongo<4.0,>=3.3 in /usr/local/lib/python3.6/dist-packages (from chatterbot) (3.7.2)\n",
"Requirement already satisfied: python-dateutil<2.8,>=2.7 in /usr/local/lib/python3.6/dist-packages (from chatterbot) (2.7.5)\n",
"Requirement already satisfied: sqlalchemy<1.3,>=1.2 in /usr/local/lib/python3.6/dist-packages (from chatterbot) (1.2.18)\n",
"Requirement already satisfied: pint>=0.8.1 in /usr/local/lib/python3.6/dist-packages (from chatterbot) (0.9)\n",
"Requirement already satisfied: nltk<4.0,>=3.2 in /usr/local/lib/python3.6/dist-packages (from chatterbot) (3.2.5)\n",
"Requirement already satisfied: mathparse<0.2,>=0.1 in /usr/local/lib/python3.6/dist-packages (from chatterbot) (0.1.2)\n",
"Requirement already satisfied: PyYAML<4.0,>=3.12 in /usr/local/lib/python3.6/dist-packages (from chatterbot-corpus<1.3,>=1.2->chatterbot) (3.13)\n",
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.6/dist-packages (from python-dateutil<2.8,>=2.7->chatterbot) (1.11.0)\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "WVSBTCLjXa9V",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"## 测试使用"
]
},
{
"metadata": {
"id": "CqQ_pEPxXbf-",
"colab_type": "code",
"outputId": "4467d4da-3ce5-41b5-f1ec-f93b10ec321a",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 521
}
},
"cell_type": "code",
"source": [
"from chatterbot import ChatBot\n",
"from chatterbot.trainers import ChatterBotCorpusTrainer\n",
"\n",
"chatbot = ChatBot('Ron Obvious')\n",
"\n",
"# Create a new trainer for the chatbot\n",
"trainer = ChatterBotCorpusTrainer(chatbot)\n",
"\n",
"# Train the chatbot based on the english corpus or chinese\n",
"trainer.train(\"chatterbot.corpus.english\")\n",
"\n",
"# Get a response to an input statement\n",
"chatbot.get_response(\"Hello, how are you today?\")"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[nltk_data] Downloading package averaged_perceptron_tagger to\n",
"[nltk_data] /root/nltk_data...\n",
"[nltk_data] Package averaged_perceptron_tagger is already up-to-\n",
"[nltk_data] date!\n",
"[nltk_data] Downloading package punkt to /root/nltk_data...\n",
"[nltk_data] Package punkt is already up-to-date!\n",
"[nltk_data] Downloading package stopwords to /root/nltk_data...\n",
"[nltk_data] Package stopwords is already up-to-date!\n",
"Training ai.yml: [####################] 100%\n",
"\n",
"Training computers.yml: [####################] 100%\n",
"Training conversations.yml: [####################] 100%\n",
"Training emotion.yml: [####################] 100%\n",
"Training food.yml: [####################] 100%\n",
"Training gossip.yml: [####################] 100%\n",
"Training greetings.yml: [####################] 100%\n",
"Training health.yml: [####################] 100%\n",
"Training history.yml: [####################] 100%\n",
"Training humor.yml: [####################] 100%\n",
"\n",
"Training money.yml: [####################] 100%\n",
"Training movies.yml: [####################] 100%\n",
"Training politics.yml: [####################] 100%\n",
"Training psychology.yml: [####################] 100%\n",
"Training science.yml: [####################] 100%\n",
"Training sports.yml: [####################] 100%\n",
"Training trivia.yml: [####################] 100%\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"<Statement text:I'm not bragging, I'm just that awesome.>"
]
},
"metadata": {
"tags": []
},
"execution_count": 13
}
]
},
{
"metadata": {
"id": "GaRxTXuxX0xt",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"## 进阶使用"
]
},
{
"metadata": {
"id": "Vog-zFRaX-k-",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"##Training data"
]
},
{
"metadata": {
"id": "uJZdxQiaX8Xd",
"colab_type": "code",
"outputId": "af24f930-6979-4cad-c6b7-a0959dadb223",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 341
}
},
"cell_type": "code",
"source": [
"from chatterbot import ChatBot\n",
"from chatterbot.trainers import ChatterBotCorpusTrainer\n",
"from chatterbot.conversation import Statement\n",
"import logging\n",
"# bot = ChatBot('叨叨')\n",
"# 初始化数据库\n",
"bot = ChatBot(\n",
" '叨叨',\n",
"# storage_adapter='chatterbot.storage.MongoDatabaseAdapter',\n",
" logic_adapters=[\n",
" 'chatterbot.logic.BestMatch',\n",
" 'chatterbot.logic.MathematicalEvaluation',\n",
" # 'chatterbot.logic.TimeLogicAdapter'\n",
" ],\n",
" input_adapter='chatterbot.input.TerminalAdapter', # 命令行端\n",
" output_adapter='chatterbot.output.TerminalAdapter',\n",
" storage_adapter=\"chatterbot.storage.SQLStorageAdapter\"\n",
" # database_uri='mongodb://localhost:27017/chatterbot-database'\n",
")\n",
"\n",
"\n",
"# # # Create a new trainer for the chatbot\n",
"\n",
"\n",
"# # 载入基本训练\n",
"# # Create a new trainer for the chatbot\n",
"# trainer = ChatterBotCorpusTrainer(bot)\n",
"\n",
"# # # Train the chatbot based on the english corpus\n",
"# # # trainer.train(\"chatterbot.corpus.chinese\")\n",
"\n",
"\n",
"# trainer.train(\n",
"# # \"chatterbot.corpus.chinese\",\n",
"# # \"./data/greetings_corpus/custom.corpus.json\",\n",
"# \"./Dialog_Corpus/\"\n",
"# )\n",
"\n",
"# 对话测试\n",
"# The following loop will execute each time the user enters input\n",
"# while True:\n",
"# try:\n",
"# user_input = input()\n",
"\n",
"# bot_response = bot.get_response(user_input)\n",
"\n",
"# print(bot_response)\n",
"\n",
"# # Press ctrl-c or ctrl-d on the keyboard to exit\n",
"# except (KeyboardInterrupt, EOFError, SystemExit):\n",
"# break\n",
"\n",
"\n",
"# while True:\n",
"# message = input('You: ')\n",
"# if message.strip() != 'Bye':\n",
"# reply = bot.get_response(message)\n",
"# print('Bot: ', reply)\n",
"# if message.strip() == 'bye':\n",
"# print('Bot : Bye')\n",
"# break\n",
"\n",
"\n",
"\n",
"# 对话训练\n",
"\n",
"\n",
"def get_feedback():\n",
"\n",
" text = input()\n",
"\n",
" if 'y' in text.lower():\n",
" return True\n",
" elif 'n' in text.lower():\n",
" return False\n",
" else:\n",
" print('是否训练 \"Yes(y)\" or \"No(n)\"')\n",
" return get_feedback()\n",
"\n",
"\n",
"print('输入要开始的东西......')\n",
"\n",
"# The following loop will execute each time the user enters input\n",
"while True:\n",
" try:\n",
" input_statement = Statement(text=input('>'))\n",
" response = bot.generate_response(\n",
" input_statement\n",
" )\n",
"\n",
" print('>>>\\n 机器人返回: \"{}\" 是否与:\"{}\" 相关吗? \\n 是否需要训练 \"Yes(y)\" or \"No(n)\"'.format(\n",
" response.text,\n",
" input_statement.text\n",
" ))\n",
" if get_feedback():\n",
" print('请输入正确的')\n",
" correct_response = Statement(text=input('>'))\n",
" bot.learn_response(correct_response, input_statement)\n",
" print('已经修正机器人')\n",
" print('————————————————————————')\n",
" print('———开始新的训练————————————')\n",
" print('————————————————————————')\n",
"\n",
" # Press ctrl-c or ctrl-d on the keyboard to exit\n",
" except (KeyboardInterrupt, EOFError, SystemExit):\n",
" break\n"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[nltk_data] Downloading package averaged_perceptron_tagger to\n",
"[nltk_data] /root/nltk_data...\n",
"[nltk_data] Package averaged_perceptron_tagger is already up-to-\n",
"[nltk_data] date!\n",
"[nltk_data] Downloading package punkt to /root/nltk_data...\n",
"[nltk_data] Package punkt is already up-to-date!\n",
"[nltk_data] Downloading package stopwords to /root/nltk_data...\n",
"[nltk_data] Package stopwords is already up-to-date!\n",
"输入要开始的东西......\n"
],
"name": "stdout"
},
{
"output_type": "stream",
"text": [
"No value for search_text was available on the provided input\n"
],
"name": "stderr"
},
{
"output_type": "stream",
"text": [
">>>\n",
" 机器人返回: \"What is AI?\" 是否与:\"hah\" 相关吗? \n",
" 是否需要训练 \"Yes(y)\" or \"No(n)\"\n",
"是否训练 \"Yes(y)\" or \"No(n)\"\n"
],
"name": "stdout"
},
{
"output_type": "stream",
"text": [
"No value for search_text was available on the provided input\n"
],
"name": "stderr"
},
{
"output_type": "stream",
"text": [
">>>\n",
" 机器人返回: \"What is AI?\" 是否与:\"狗子\" 相关吗? \n",
" 是否需要训练 \"Yes(y)\" or \"No(n)\"\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "XsR3yF41cPep",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"## 导出训练数据"
]
},
{
"metadata": {
"id": "nIRr2Xi1cOEL",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# Now we can export the data to a file\n",
"trainer.export_for_training( app_dir+'/my_export.json')"
],
"execution_count": 0,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment