美文网首页学习提升Python
用chatterbot从零开始搭建一个聊天机器人(二)

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

作者: 文知道 | 来源:发表于2020-03-25 22:29 被阅读0次

    一、获取语料

    二、训练

    1、上传语料包

    • 上传处理好的小黄鸡语料到服务器上,有条件的可以使用 Google colab进行训练

    2、在服务器上安装 ChatterBot

    pip3 install chatterbot
    pip3 install chatterbot-corpus
    

    3、试运行chatterbot

    • 运行以下代码将会自动安装nltk_data,此过程可能会很久
    from chatterbot import ChatBot
    from chatterbot.trainers import ListTrainer
    
    chatbot = ChatBot("bot")
    trainer = ListTrainer(chatbot)
    

    4、修改chatterbot中nltk_data的目录

    • 找到python的第三方包site-packages的路径下的chatterbot
    cd /usr/local/lib/python3.6/site-packages/chatterbot/
    vim utils.py
    
    • 修改内容如下
    def download_nltk_stopwords():
    
    """
    
    Download required NLTK stopwords corpus if it has not already been downloaded.
    
    """
    
    nltk_download_corpus('corpora/stopwords')
    
    def download_nltk_wordnet():
    
    """
    
    Download required NLTK corpora if they have not already been downloaded.
    
    """
    
    nltk_download_corpus('corpora/wordnet')
    
    def download_nltk_averaged_perceptron_tagger():
    
    """
    
    Download the NLTK averaged perceptron tagger that is required for this algorithm
    
    to run only if the corpora has not already been downloaded.
    
    """
    
    nltk_download_corpus('taggers/averaged_perceptron_tagger')
    
    def download_nltk_vader_lexicon():
    
    """
    
    Download the NLTK vader lexicon for sentiment analysis
    
    that is required for this algorithm to run.
    
    """
    nltk_download_corpus('sentiment/vader_lexicon')
    

    5、创建一个 Chat Bot并进行训练

    from chatterbot import ChatBot
    from chatterbot.trainers import ListTrainer
    
    chatbot = ChatBot("小明")
    trainer = ListTrainer(chatbot)
    with open('part.data', encoding='utf-8') as f:
    data = f.read().replace('\t', '\n')
    data = data.split("\n")
    
    trainer.train(data)
    

    6、本地测试

    from chatterbot import ChatBot
    import sys
    
    bot = ChatBot(
        '小明',
        database_uri='sqlite:///db.sqlite3'
     )
     
    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
    

    三、部署成服务

    • 安装flask
    pip3 install flask
    
    • 安装uwsgi
    yum install -y pcre pcre-devel pcre-static
    yum install -y python3-devel
    pip3 install uwsgi --no-cache-dir
    
    • 新建api.py文件
    vim api.py
    
    • 添加如下内容
    from flask import Flask, render_template, request, jsonify
    from chatterbot import ChatBot
     
    app = Flask(__name__)
     
    bot = ChatBot(
        'С˼',
        database_uri='sqlite:///db.sqlite3'
    )
    
    @app.route("/")
    def home():
        return render_template("index.html")
    
    @app.route("/api/<text>")
    def get_bot_api(text):
        res = str(bot.get_response(text))
        return jsonify(res), 200
    
    
    if __name__ == "__main__":
        app.run(host='0.0.0.0')
    
    • 新建在项目目录下,添加uwsgi配置
    vim uwsgi.ini
    
    • 添加如下内容
    [uwsgi]
    
    http = 0.0.0.0:5000
    chdir = /usr/share/nginx/html/chatbot/chatterbot
    wsgi-file = api.py
    callable = app
    processes = 4
    threads = 2
    master = true
    vacuum = true
    
    • 运行uwsgi
    uwsgi uwsgi.ini
    
    • 调用
    $url = "http://127.0.0.1:5000/api/{$word}";
    $reply = $this->getData($url);
    

    附:源码

    https://github.com/Wc241/chatterbot

    相关文章

      网友评论

        本文标题:用chatterbot从零开始搭建一个聊天机器人(二)

        本文链接:https://www.haomeiwen.com/subject/ghrxuhtx.html