-
Notifications
You must be signed in to change notification settings - Fork 3
/
bot.py
52 lines (43 loc) · 1.24 KB
/
bot.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import abc
import math
import os
import random
import sys
import time
import logging
class Bot(object):
__metaclass__ = abc.ABCMeta
def __init__(self, mode='train'):
if mode == 'converse':
self.init_for_conversation()
elif mode == 'train':
self.init_and_train()
else:
raise Exception('Incorrect mode string')
@abc.abstractmethod
def init_for_conversation(self, model_path, model_args):
return
@abc.abstractmethod
def init_and_train(self, train_data, model_args):
return
@abc.abstractmethod
def get_response(self, query):
return
def converse(self):
sys.stdout.write("---")
sys.stdout.flush()
sys.stdout.write("---")
sys.stdout.flush()
sys.stdout.write("Hello, I'm Lexi. Let's talk.\n")
sys.stdout.flush()
sys.stdout.write("> ")
sys.stdout.flush()
sentence = sys.stdin.readline()
while sentence:
print(self.get_response(sentence))
print("> ", end="")
sys.stdout.flush()
sentence = sys.stdin.readline()