-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSaiko_Denshin.py
296 lines (236 loc) · 9.13 KB
/
Saiko_Denshin.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# coding=utf-8
from pyswip import *
from bottle import *
from json import loads
from beaker.middleware import SessionMiddleware
def not_in_question( list_item , list_de_dict) :
# list_item = [almend,tun]
res = []
for item in list_item :
for i in list_de_dict :
if i.keys()[0] == item :
res.append( list_de_dict.index(i) )
break
else :
res.append(-1)
return -1 in res,res
#fin
def incrementer(list_item,list_index , criteria,question):
# exemple : question = { nationality : [ {tn :1} , {allmnd : 2} ] }
# list the first time is empty so niq fn return -1
for i in range( len(list_index) ) :
if list_index[i] == -1 :
tmp = {}
tmp[list_item[i]] = 1
question[criteria].append(tmp)
else :
question[criteria][ list_index[i] ][ list_item[i] ] += 1
return question
#fin
# criteria_value = >25 ou germany
def eliminate_suspects( criteria_value , r , cri,suspects) :
global p
if ">" in criteria_value :
if r == "yes" :
criteria_value = criteria_value.replace(">","l")
else:
criteria_value = criteria_value.replace(">","g")
res = list(p.query("%s2(%s,N)"%(cri,criteria_value)))
elif "<=" in criteria_value :
if r == "yes" :
criteria_value = criteria_value.replace("<=","g")
else:
criteria_value = criteria_value.replace("<=","l")
res = list(p.query("%s2(%s,N)"%(cri,criteria_value)))
else :
res = list(p.query('%s2("%s",%s,N)' %(cri,criteria_value,r)))
for i in res :
try :
suspects.remove(i["N"])
except ValueError as e :
continue
print "suspects : ",
print suspects
return suspects
#fin
def main(response,suspects,criteria_value,ceq,askonce) :
# criteria_value : crite_value,ceq=criteria_to_ask
global p
if response != 'firstime' :
# eliminating suspects we are not interested in !
suspects = eliminate_suspects(criteria_value,response,ceq,suspects)
questions = [
{"nationality" : ["The person is from "] },
{"age" : ["The person is aged "]},
{"height" : ["The person's height is "]},
{"rated_at" : ["The person is rated at "]},
{"fights" : ["The person's fights games "]},
{"wins" : ["The person won "]},
{"wKO" : ["The person won with k.O "]},
{"losses": ["The person lost "]},
{"titles" : ["The person nominated "]}
]
# liste des personnage
criteria = ["nationality","age","height","rated_at","fights","wins","wKO","losses","titles"]
typ = ["str","int","int","str","int","int","int","int","str"]
criteria_to_ask = ""
question = {}
# if we don't find at least 45% occ then we choose the max occ.
# max_nb [cri,item,occ] exm: [natio,tn,2]
max_nb = ["","",0]
# we don't ask the same question 2 times in arrow.
dontask = ""
#construire question
nb_susp = len(suspects)
if nb_susp == 0 : return "","","",suspects,[]
elif nb_susp == 1 : return "","","",suspects,[]
else :
for i in criteria :
if i == ceq or i in askonce :
continue
# show all info criteria of i
result = list(p.query("%s(X,%s)" %(i,str(suspects))))
question[i] = []
t = typ[criteria.index(i)]
# type of info of critiria i is string
if t == "str" :
# calcule occurrence des donnée de criteria i str info
for res in result :
# res[X] can be allmend__armenie
tmp_res = res["X"].split("__")
verif,verif_res = not_in_question( tmp_res , question[i] )
# verif_res = [1,1,-1]
if verif :
for o in range(len(verif_res)) :
if verif_res[o] == -1 :
tmp = {}
tmp[ tmp_res[o] ] = 1
question[i].append(tmp)
del(tmp)
else :
question = incrementer(tmp_res,verif_res, i,question)
# verif_res contain index so i won't recalculate them
else :
# int information
# calculate averge
# question { age : [ {>mo : 5} {<=avg:2}]}
avg = 0
l = len(result)
for res in result :
avg += int(res["X"][3:])
try :
avg = avg / l
except ZeroDivisionError as e :
pass
del(l)
tmp = {}
tmp[">"+str(avg)] = 0
question[i].append(tmp)
del tmp
tmp = {}
tmp["<="+str(avg)] = 0
question[i].append(tmp)
del tmp
for res in result :
if int(res["X"][3:]) <= avg :
question[i][1]["<="+str(avg)] += 1
else :
question[i][0][">"+str(avg)] += 1
# fin calculate averge / occ
# try if we can have 2 big group so wwe can ask about this criteere
for check in question[i] :
nb = check.values()[0]
# check if value present 45% -> 55 % of nb suspects
if nb >= nb_susp * 0.45 and nb <= nb_susp * 0.55 :
# exemple ceq = nationality ; criteria_value = allmend
criteria_to_ask = i
criteria_value = check.keys()[0]
break
if nb > max_nb[2] and nb < nb_susp:
max_nb[0] = i
max_nb[1] = check.keys()[0]
max_nb[2] = nb
# we found what to ask about,so we exit this process
if criteria_to_ask :
break
#when for ends without finding a question
else :
criteria_to_ask = max_nb[0]
criteria_value = max_nb[1]
# end found what to ask loop process
if t == "str" : askonce.append(dontask)
# asking the question (just testing, needs work)
question_tmp =questions[criteria.index(criteria_to_ask)][criteria_to_ask][0] + criteria_value + " ?"
return question_tmp,criteria_value,criteria_to_ask,suspects,askonce
# end main function
session_opts = {
'session.type': 'file',
'session.cookie_expires': True,
'session.data_dir': './data',
'session.auto': True
}
beaker_app = SessionMiddleware(app(),session_opts)
@route("/")
def index() :
return template("jarvis.htm")
@route("/about")
def about() :
return template("about.htm")
@route('/static/<filepath:path>')
def return_static(filepath) :
return static_file(filepath , root = "./static")
@route("/initgame")
def init_game () :
global p
session = request.environ.get('beaker.session')
suspects = [ i['X'] for i in list(p.query("is_boxer(X)")) ]
session['suspects'] = suspects
session['askonce'] = []
session['criteria_value'] = ""
session['ceq'] = ""
session['user_answers'] = []
print "****************************"
@route('/response')
def guessing () :
response = request.query.res
print response
session = request.environ.get('beaker.session')
exist = session.get('suspects',0)
if exist != 0 :
suspects = exist
askonce = session['askonce']
criteria_value = session['criteria_value']
ceq = session['ceq']
else :
redirect('/initgame')
question,criteria_value,ceq,suspects,askonce = main(response,suspects,criteria_value,ceq,askonce)
print "*********************"
if response != "firstime" :
# save old criteria_value + recent res
session["user_answers"].append( [ session['ceq'],session['criteria_value'],response ] )
if question :
session['suspects'] = suspects
session['askonce'] = askonce
session['criteria_value'] = criteria_value
session['ceq'] = ceq
return {"question" : question,"suspects": len(suspects)}
else :
user_answers = session["user_answers"]
session.delete()
return {"suspects" : suspects,"answers" : user_answers }
@route("/save")
def save():
global p
bx = request.query.boxer
bx = loads(bx)
line = "";
for i in bx :
line += i.values()[0] + ","
f = open("boxers.pl","a")
f.write("\nboxeur("+line[:len(line)-1]+").")
f.close()
p.consult("boxers.pl")
if __name__ == "__main__" :
p = Prolog()
p.consult("boxers.pl")
run (app=beaker_app,host="0.0.0.0",port=8080,debug=True,reloader = True)