Newer
Older
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import logging
from abc import abstractmethod
from eliza import Eliza
class ElizaState():
def __init__(self, hystericeliza):
self.hystericeliza = hystericeliza
@abstractmethod
def switch_state(self, output):
""" Decide how to react next based on the current output """
@abstractmethod
def process_output(self, output):
""" React on the current output by formatting it according to the state """
class Normal(ElizaState):
""" Answer normally """
pass
class Angry(ElizaState):
""" ANSWER ONLY IN UPPERCASE (use String.upper() to do this) """
pass
class Sad(ElizaState):
""" answer only in lowercase (use String.lower() to do this) """
pass
class HystericEliza():
def __init__(self):
self.eliza = Eliza()
self.state = "Normal"
def load(self, replies):
self.eliza.load(replies)
def process_output(self, output):
if self.state == "Normal":
if output.startswith("Please"):
self.state = "Sad"
elif "n't " in output:
self.state = "Angry"
elif self.state == "Angry":
if output.startswith("Do you") or output.startswith("Please"):
self.state = "Normal"
elif output.startswith("Why"):
self.state = "Sad"
elif self.state == "Sad":
if output.startswith("Do "):
self.state = "Normal"
if self.state == "Normal":
return output
elif self.state == "Angry":
return output.upper()
elif self.state == "Sad":
return output.lower()
def run(self):
initial = self.process_output(self.eliza.initial())
print(initial)
while True:
sent = input('> ')
output = self.eliza.respond(sent)
if output is None:
break
formatted = self.process_output(output)
print(formatted)
final = self.process_output(self.eliza.final())
print(final)
def main():
eliza = HystericEliza()
eliza.load('doctor.txt')
eliza.run()
if __name__ == '__main__':
logging.basicConfig()
main()