-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ibis.py
267 lines (213 loc) · 9.11 KB
/
test_ibis.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
from ibis_rules import *
from more_itertools import peekable
import singleUser_ibis
import os
import studip
import travel
import re
######################################################################
# Overwritten classes - DM must contain the generator, and the IO must be replaced
######################################################################
class IBISController(DialogueManager):
def IO(self):
"""The IBIS control algorithm."""
if not self.IS.private.plan:
self.IS.private.agenda.push(Greet())
self.print_state()
output = True
while output:
output = []
self.select() # puts the next appropriate thing onto the agenda
while self.NEXT_MOVES:
self.generate() # sets output
output.append(self.output(None))
self.update() # integrates answers, ..., loads & executes plan
self.print_state()
if self.PROGRAM_STATE.get() == ProgramState.QUIT:
break
if output:
input = yield(output)
if input:
if input.startswith("U> "): input = input[3:]
self.INPUT.set(input)
self.LATEST_SPEAKER.set(Speaker.USR)
res = self.interpret() # obviously also runs it
if res == "exit":
break
self.update()
self.print_state()
class DebugOutput(SimpleOutput):
@update_rule
def output(NEXT_MOVES, OUTPUT, LATEST_SPEAKER, LATEST_MOVES):
"""Print the string in OUTPUT to standard output.
After printing, the set of NEXT_MOVES is moved to LATEST_MOVES,
and LATEST_SPEAKER is set to SYS.
"""
res = "S: "+(str(OUTPUT.get()) or "[---]")
LATEST_SPEAKER.set(Speaker.SYS)
LATEST_MOVES.clear()
if settings.MERGE_SUBSQ_MESSAGES:
LATEST_MOVES.update(NEXT_MOVES)
NEXT_MOVES.clear()
else:
LATEST_MOVES.update([NEXT_MOVES.elements[0]])
del NEXT_MOVES.elements[0]
return res
#alles genauso wie das IBIS, aber überschreibe Output mit DebugOutput und diesem DialogueManager, der IO hat
class IBIS(DebugOutput, singleUser_ibis.IBIS, IBISController, singleUser_ibis.IBISInfostate, StandardMIVS, SimpleInput, DialogueManager):
pass
class IBIS3(IBIS, singleUser_ibis.IBIS1): #inherit the rule_groups and the update-loop from singeUser.IBIS1
def __init__(self, *args, **kwargs):
settings.SAVE_IS = False
settings.USE_SAVED = False
super().__init__(*args, **kwargs)
########################################################################################################################
########################################################################################################################
####################################################### main ###########################################################
########################################################################################################################
########################################################################################################################
def loadIBIS(forwhat, language):
if forwhat == "studip":
apiconnector = studip.create_studip_APIConnector()
grammar = studip.create_studip_grammar(language)
domain = studip.create_studip_domain(apiconnector)
elif forwhat == "travel":
apiconnector = travel.create_travel_APIConnector()
grammar = travel.create_travel_grammar()
domain = travel.create_travel_domain()
ibis = IBIS3(domain, apiconnector, grammar)
return ibis
# def test_answer():
# assert func(3) == 5
def check_sentence(ibis, sentence):
parts = sentence.split("\n")
parts2 = []
parts3 = []
for i in parts:
if any(j.isalnum() for j in i):
while i[0] == " ": i = i[1:]
while i[-1] == " ": i = i[:-1]
parts2.append(i)
tmpstring = ""
for i in parts2:
if i.startswith("S: ") or i.startswith("U> "):
if len(tmpstring) > 0: parts3.append(tmpstring[:-1])
tmpstring = ""
tmpstring += i+"\n"
if len(tmpstring) > 0: parts3.append(tmpstring[:-1])
sentence_iterator = peekable(iter([None] + parts3)) #erst die beiden in ne list, dann daraus nen iter, daraus peekable
generator = ibis.IO()
for nextSentence in sentence_iterator:
if nextSentence is None or nextSentence.startswith("U> "):
syssent = generator.send(nextSentence)
syssent_iter = iter(syssent)
try:
while sentence_iterator.peek().startswith("S: "):
next_system_sent = remove_spaces(next(syssent_iter))
nextSentence = next(sentence_iterator)
try:
tmp = list(nextSentence)
for i in range(len(nextSentence)):
if tmp[i] == "&":
tmp = tmp[:i]
next_system_sent = next_system_sent[:i]
break
if tmp[i] == "#":
tmp[i] = next_system_sent[i] if i <= len(next_system_sent) else "X"
nextSentence = "".join(tmp)
assert nextSentence == next_system_sent
except AssertionError as e:
print("------------------------------", file=sys.stderr)
print("SHOULD BE:", nextSentence, file=sys.stderr)
print("-", file=sys.stderr)
print("IS:", next_system_sent, file=sys.stderr)
print("------------------------------", file=sys.stderr)
raise e
except StopIteration:
continue
def remove_spaces(text):
res = []
for i in text.split("\n"):
while i[0] == " ": i = i[1:]
while i[-1] == " ": i = i[:-1]
res.append(i)
return "\n".join(res)
def check_commands():
for dom in ["travel", "studip"]:
for lan in ["en", "de"]:
print("------ testing "+dom+" in "+lan+" ------")
ibis = loadIBIS(dom, lan)
ibis.init()
filename = os.path.join(settings.PATH, "test_strings/", dom+"_"+lan)
if os.path.exists(filename):
for i in collect_string(filename):
if i == "<restart>":
ibis = loadIBIS(dom, lan)
ibis.init()
else:
yield ibis, i
def preprocess(string):
vars = [i[1:] for i in re.findall('(\$[a-zA-Z_]*)', string)]
for i in vars:
if i in os.environ:
string = string.replace("$"+i, os.environ[i])
return string
def collect_string(filename):
with open(filename, "r") as file:
tmp = ""
firsttime = True
for i in file:
if i.startswith("---"):
yield tmp
tmp = ""
firsttime = False
elif i.startswith("<restart>"):
yield "<restart>"
else:
if firsttime or (not firsttime and i != "S: Hello.\n"):
tmp += preprocess(i)
yield tmp
def sent_short(sent):
spl = sent.split("\n")
if spl[0] == "S: Hello.":
return spl[1]
return spl[0]
if __name__=='__main__':
fail_count = 0
ges_count = 0
for ibis, sent in check_commands():
try:
check_sentence(ibis, sent)
except AssertionError:
print("The following sentence sucked:", sent_short(sent), file=sys.stderr)
fail_count += 1
else:
print("The following sentence worked:", sent_short(sent))
ges_count += 1
print("Amount of errors:"+str(fail_count)+"/"+str(ges_count))
#TODO mit dem "Was kannst du?"-commando die tests selbst auf completeness prüfen
########################################################################################################################
############################################ for pytest, not running ###################################################
########################################################################################################################
def test_travel_en():
ibis = loadIBIS("travel", "en")
ibis.init()
filename = os.path.join(settings.PATH, "test_strings/travel_en")
if os.path.exists(filename):
for i in collect_string(filename):
if i == "<restart>":
ibis = loadIBIS("travel", "en")
ibis.init()
else:
check_sentence(ibis, i)
def test_studip_en():
ibis = loadIBIS("studip", "en")
ibis.init()
filename = os.path.join(settings.PATH, "test_strings/studip_en")
if os.path.exists(filename):
for i in collect_string(filename):
if i == "<restart>":
ibis = loadIBIS("studip", "en")
ibis.init()
else:
check_sentence(ibis, i)