-
Notifications
You must be signed in to change notification settings - Fork 0
/
penny.py
65 lines (52 loc) · 1.8 KB
/
penny.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from chatterbot import ChatBot
from chatterbot.comparisons import JaccardSimilarity, LevenshteinDistance
from chatterbot.trainers import ChatterBotCorpusTrainer
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
import sys
def init_chatbot():
chatbot = ChatBot(
'Penny',
read_only=True,
preprocessors=['chatterbot.preprocessors.clean_whitespace'],
statement_comparison_function=JaccardSimilarity,
#statement_comparison_function=LevenshteinDistance,
storage_adapter='chatterbot.storage.SQLStorageAdapter',
logic_adapters=[
{
'import_path': 'chatterbot.logic.BestMatch',
'default_response': 'I am sorry, but I do not understand.',
'maximum_similarity_threshold': 0.8
}
],
database_uri='sqlite:///database.sqlite3'
)
return chatbot
def train_chatbot(chatbot):
corpus_trainer = ChatterBotCorpusTrainer(chatbot)
corpus_trainer.train('data/greetings.yml')
corpus_trainer.train('data/faq.yml')
corpus_trainer.train('data/shortcuts.yml')
corpus_trainer.train('data/user_guide.yml')
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
chatbot = init_chatbot()
@app.route('/')
def index():
return render_template('index.html')
@socketio.on('message')
def handle_message(message):
print('received message: ' + message)
response = str(chatbot.get_response(message))
emit('response', '[Penny]: ' + response, broadcast=True)
def main(argv):
print (argv)
if len(argv) == 1 and argv[0] == 'train':
train_chatbot(chatbot)
print("Chatbot trained!")
exit(0)
else:
socketio.run(app, debug=True)
if __name__ == "__main__":
main(sys.argv[1:])