-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
42 lines (35 loc) · 1.17 KB
/
utils.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
"""
utils.py contains useful utility functions used in bot modules
"""
import pickle, os
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
from bot import Platform, TwitterBot
def train_bot(bot: ChatBot, *paths_to_corpus: str):
"""
train_bot trains a chatterbot.ChatBot object passed as an argument
:param
bot: ChatBot
paths_to_corpus: [str]
"""
trainer = ChatterBotCorpusTrainer(bot)
print(f'*** starting training bot ***')
trainer.train(*paths_to_corpus)
print(f'*** finished training bot ***')
def create_or_restore_platform_instance() -> Platform:
"""
create new ChatBot instance if stored instance could not be restored
:returns Platform
"""
try:
if os.path.exists("platform_state.pickle"):
with open("platform_state.pickle", "rb") as state:
bot_state = pickle.load(state)
return TwitterBot(**bot_state)
else:
return TwitterBot()
except:
return TwitterBot()
def update_platform_state(**kwargs):
with open("platform_state.pickle", "wb") as state:
pickle.dump(kwargs, state)