Created
May 19, 2017 07:00
-
-
Save anonymous/8647da49958702efbed345650763ef4e to your computer and use it in GitHub Desktop.
Python chatterbot problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from chatterbot import ChatBot | |
from chatterbot.trainers import ListTrainer, ChatterBotCorpusTrainer | |
from flask import Flask, jsonify | |
app = Flask(__name__) | |
print("--- setting up chatbot...", end="") | |
chatbot = ChatBot("Hansbot", | |
storage_adapter="chatterbot.storage.JsonFileStorageAdapter", | |
output_adapter="chatterbot.output.OutputAdapter", | |
output_format="text", | |
logic_adapters=[ | |
"chatterbot.logic.BestMatch" | |
], | |
database="./database.json") | |
print("done") | |
print("--- training chatbot...", end="") | |
chatbot.set_trainer(ChatterBotCorpusTrainer) | |
chatbot.train("chatterbot.corpus.german", | |
"chatterbot.corpus.german.greetings", | |
"chatterbot.corpus.german.conversations") | |
print("done") | |
def ask_chatbot(request): | |
dialog = { | |
"id": 1, | |
"usersay": request, | |
"botsay": chatbot.get_response(request), | |
"botsay2": response | |
} | |
return dialog | |
print("--- defining rest functions", end="") | |
@app.route('/conversation/<request>', methods=['GET']) | |
def conversation(request): | |
print("--- conversation service called") | |
print("request: " + request) | |
dialog = ask_chatbot(request) | |
print("dialog: " + dialog["botsay"]) | |
return jsonify(dialog) | |
print("done") | |
print("--- starting server") | |
if __name__ == '__main__': | |
app.run(debug=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment