-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatterbot.py
42 lines (33 loc) · 1.49 KB
/
Chatterbot.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
import os
from chatterbot import ChatBot
from chatterbot.trainers import UbuntuCorpusTrainer, ChatterBotCorpusTrainer
class ChatterbotBot:
"""This is a (very simple) conversational bot using chatterbot, a popular open-source chatbot framework. The bot
can be trained easily. This is just a 'tech-demo' that could be fleshed out to a higher degree, such as adding more
options, flexibility, and documentation. However, as this was a quick and easy option to build up, I decided to give
it its own file for testing and display purposes.."""
def __init__(self):
self.bot = ChatBot('ChatterbotBot',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
preprocessors=['chatterbot.preprocessors.clean_whitespace'],
logic_adapters=['chatterbot.logic.MathematicalEvaluation', 'chatterbot.logic.TimeLogicAdapter'],
database_uri='sqlite:///chatterbotbot.db'
)
if not os.path.exists("chatterbotbot.db"):
self.train()
def train(self, path=None):
if not path:
trainer = UbuntuCorpusTrainer(self.bot)
trainer.train()
else:
trainer = ChatterBotCorpusTrainer(self.bot)
trainer.train(path)
def talk(self, statement):
return self.bot.get_response(statement)
def main(self):
while True:
statement = input()
print("\n" + self.talk(statement) + "\n")
if __name__ == "__main__":
bot = ChatterbotBot()
bot.main()