-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregular_expressions.py
304 lines (142 loc) · 5.31 KB
/
regular_expressions.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
268
269
270
271
272
273
274
275
276
277
278
279
import re
import glob
from trace_processor import get_pad_classes, transform_arrays_into_same_size
import string
import room_and_corridor_finder
def getRegularExpressionStringsFromFiles(file_path, slice_num, map_name = "Level1", map_granularity = 20):
"""
A function that turns trace data into expressions that can be validated using Regular Expressions
"""
_, groups, player_pos = room_and_corridor_finder.load(map_name)
arousal_files_location = sorted(glob.glob(file_path + "/Arousal/Traces_Position*.txt"))
arousal_files_pad =sorted(glob.glob(file_path + "/Arousal/Traces_Arousal*.txt"))
# print(len(arousal_files_location))
# print(len(arousal_files_pad))
r_e_list = []
for i in range(len(arousal_files_location)):
print(arousal_files_location[i])
f = open(arousal_files_location[i])
locations = f.readlines()
#locations = list(actions_to_string_translator(file_to_actions_translator(arousal_files_location[i])))
pad_classes = get_pad_classes(arousal_files_pad[i], slice_number = slice_num)
# print(len(locations))
# print(len(pad_classes))
pad_classes, locations = transform_arrays_into_same_size(pad_classes, locations)
# print(len(pad_classes))
# print(len(locations))
r_e = ""
#print(groups[0])
for cat, loc in zip(pad_classes, locations):
#print(cat)
#print(loc)
loc = loc.replace("\n","").split("_")
#print("The moving locations: ", loc)
#print("Them after the division: ",int(loc[1])//20, " and ", int(loc[0])//20 )
#print("Test: ", (int(loc[0])//20) + player_pos[0])
aux_loc = (int(loc[1])//20 + player_pos[0], int(loc[0])//20 + player_pos[1])
room_num = None
#print((aux_loc[0], aux_loc[1]))
for group in groups:
if (aux_loc[0], aux_loc[1]) in group:
#print("Found it! It's in group ", groups.index(group))
room_num = groups.index(group)
break
#room_num = groups.index((loc[1], loc[0]))
#print(player_pos)
if room_num == None:
print("Can't find the room that corresponds to the position!")
exit()
#Debugging
possible_characters = string.ascii_lowercase + string.ascii_uppercase
# print(loc)
# print(room_num)
# print(possible_characters[room_num])
if cat == 0:
r_e += "R" + str(room_num) + "AdPn"
else:
r_e += "R" + str(room_num) + "AiPn"
r_e_list.append(r_e)
print("\n")
print(r_e)
print("\n")
return r_e_list
def getRegularExpressionStringsFromPADClassesAndLocationFile(pad_classes_list, location_file_path, slice_num, map_name = "Level1", map_granularity = 20):
"""
A function that turns trace data into expressions that can be validated using Regular Expressions
"""
_, groups, player_pos = room_and_corridor_finder.load(map_name)
files_location = sorted(glob.glob(location_file_path + "/Bot_Position*"))
r_e_list = []
for i in range(len(files_location)):
#print(arousal_files_location[i])
f = open(files_location[i])
locations = f.readlines()
# print(len(locations))
# print(len(pad_classes))
pad_classes = pad_classes_list[i]
pad_classes, locations = transform_arrays_into_same_size(pad_classes, locations)
# print(len(pad_classes))
# print(len(locations))
r_e = ""
#print(groups[0])
for cat, loc in zip(pad_classes, locations):
#print(cat)
#print(loc)
loc = loc.replace("\n","").split("_")
#print("The moving locations: ", loc)
#print("Them after the division: ",int(loc[1])//20, " and ", int(loc[0])//20 )
#print("Test: ", (int(loc[0])//20) + player_pos[0])
aux_loc = (int(loc[1])//20 + player_pos[0], int(loc[0])//20 + player_pos[1])
room_num = None
#print((aux_loc[0], aux_loc[1]))
for group in groups:
if (aux_loc[0], aux_loc[1]) in group:
#print("Found it! It's in group ", groups.index(group))
room_num = groups.index(group)
break
#room_num = groups.index((loc[1], loc[0]))
#print(player_pos)
if room_num == None:
room_num = "n"
print("Can't find the room that corresponds to the position!")
#exit()
#Debugging
possible_characters = string.ascii_lowercase + string.ascii_uppercase
# print(loc)
# print(room_num)
# print(possible_characters[room_num])
if cat == 0:
r_e += "R" + str(room_num) + "AdPn"
else:
r_e += "R" + str(room_num) + "AiPn"
r_e_list.append(r_e)
return r_e_list
def satisfiesRegEx(string, reg_ex):
return re.fullmatch(reg_ex, string)
def listSatisfiesRegEx(string_list, reg_ex):
fail_accept_list = []
for reggy in string_list:
x = re.fullmatch(reg_ex, reggy)
if x:
fail_accept_list.append("A")
else:
fail_accept_list.append("F")
return fail_accept_list
def main():
#test_expression = "(R.AdP.)*(R.AiP.)*(R.AdP.)+(R.AiP.)+(R.AdP.)*"
test_expression = ".*(R.AiP.){6}.*" # ".*(R4AiP.)+.*"
print(satisfiesRegEx("R1AdPnR1AiPnR1AiPnR2AiPnR1AiPnR1AiPnR1AiPnR1AdPnR1AdPnR1AdPn", test_expression))
exit()
r_e_list = getRegularExpressionStringsFromFiles("./First_Study", slice_num = 3)
fail_accept_list = []
for reggy in r_e_list:
x = re.fullmatch(test_expression, reggy)
if x:
fail_accept_list.append("A")
else:
fail_accept_list.append("F")
print(fail_accept_list)
print("Number of accepted: ", fail_accept_list.count("A"))
print("Number of failures: ", fail_accept_list.count("F"))
if __name__=="__main__":
main()