-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprincess.py
executable file
·307 lines (270 loc) · 12 KB
/
princess.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
297
298
299
300
301
302
303
304
305
#! /usr/bin/python
import getopt
import os
import sys
import time
from pymongo import MongoClient
from document.document import Document
from document.feature import Feature
from game import *
# Test round robin
#robin = RoundRobin()
#leaders = [Document("First document"), Document("Second document")]
#robin.competitors = leaders
#robin.schedule()
#robin.runCompetition()
#print(robin.ranking)
dictQRels = {}
def evaluateQRels(c):
if "trec8" in c:
# print 'ici'
with open("/osirim/sig/CORPUS/TREC-ADHOC/QRELS/qrels.401-450.disk4.disk5", "r") as qrels:
for l in qrels:
tab = l.split(" ")
# print tab
q = tab[0]
doc = tab[2]
pertinent = False
if "1" in tab[3]:
pertinent = True
if q not in dictQRels.keys():
dictQRels[q] = {}
dictQRels[q][doc] = pertinent
elif "trec7" in c:
with open("/osirim/sig/CORPUS/TREC-ADHOC/QRELS/qrels.351-400.disk4.disk5", "r") as qrels:
# with open("/osirim/sig/CORPUS/CLUEWEB12/QRELS/qrels.all.web2014.txt","r") as qrels :
for l in qrels:
tab = l.split(" ")
# print tab
q = tab[0]
doc = tab[2]
pertinent = False
if "1" in tab[3]:
pertinent = True
if q not in dictQRels.keys():
dictQRels[q] = {}
dictQRels[q][doc] = pertinent
else:
with open("/osirim/sig/CORPUS/CLUEWEB12/QRELS/qrels.all.web2014dedup.txt", "r") as qrels:
for l in qrels:
tab = l.split(" ")
# print tab
q = tab[0]
doc = tab[2]
pertinent = False
if "1" in tab[3]:
pertinent = True
if q not in dictQRels.keys():
dictQRels[q] = {}
dictQRels[q][doc] = pertinent
# print dictQRels
def main():
# Handle user options
try:
opts, args = getopt.getopt(sys.argv[1:], "dht:i:l:s:n:r:f:vc:b:g:am:o:p:",
["debug", "help", "type=", "impact=", "life=", "strategy=", "nbFeats=", "rounds=",
"featureList=", "verbose", 'collection=', 'group=', 'accepted', 'model=', 'optim',
'process'])
except getopt.GetoptError as err:
# print help information and exit:
print str(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
# Set the default values
optim = "order"
debug = False
nb_rounds = 10
nb_groups = 10
nb_documents = 16
nbFeats = 0
group = 1
type_tournament = "robin"
collection_name = 'trec_adhoc_lee'
output_directory = "../../result/"
impact = 0
features_to_remove = []
strategy = 1
process = 100
life = 0
best = 0.1
accepted = False
verbose = False
model = "f45"
strategy = ['f48', 'f17', 'f19', 'f16', 'f46', 'f9', 'f21', 'f3', 'f39', 'f7', 'f40', 'f6', 'f37', 'f42', 'f2',
'f15', 'f25', 'f33', 'f36', 'f10', 'f30', 'f51', 'f28', 'f43', 'f45', 'f34', 'f24', 'f13', 'f50', 'f27',
'f31', 'f1', 'f35', 'f14', 'f47', 'f41', 'f4', 'f22', 'f12', 'f8', 'f26', 'f44']
for o, a in opts:
if o in ("-v", "--verbose"):
verbose = True
elif o in ("-a", "--accepted"):
accepted = True
elif o in ("-o", "--optim"):
optim = a
elif o in ("-d", "--debug"):
debug = True
elif o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-t", "--type"):
type_tournament = a
elif o in ("-g", "--group"):
group = int(a)
elif o in ("-f", "--featureList"):
features_to_remove = str(a).split(",")
print features_to_remove
elif o in ("-r", "--rounds"):
nb_rounds = int(a)
elif o in ("-m", "--model"):
model = str(a)
elif o in ("-p", "--process"):
process = int(a)
elif o in ("-b", "--best"):
best = float(a)
elif o in ("-c", "--collection"):
collection_name = a
elif o in ("-i", "--impact"):
impact = int(a)
elif o in ("-l", "--life"):
life = int(a)
elif o in ("-n", "--nbFeats"):
nbFeats = int(a)
elif o in ("-s", "--strategy"):
strategy = int(a)
else:
assert False, "unhandled option"
# One tournament per query
connection = MongoClient(host='co2-ni01.irit.fr', port=28018)
db = connection.princess
collection = db[collection_name]
queries = collection.distinct('query')
if debug:
evaluateQRels(collection_name)
outputFolderName = ''
if len(features_to_remove) > 0:
outputFolderName = 't:' + type_tournament + '-o:' + optim + '-r:' + str(nb_rounds) + '-b:' + str(
best) + '-c:' + collection_name + '-i:' + str(impact) + '-l:' + str(life) + '-n:' + str(
nbFeats) + '-s:' + str(strategy) + '-g:' + str(group) + '-f:' + ','.join(features_to_remove)
if accepted:
outputFolderName += '-a'
else:
outputFolderName = 't:' + type_tournament + '-o:' + optim + '-r:' + str(nb_rounds) + '-b:' + str(
best) + '-c:' + collection_name + '-i:' + str(impact) + '-l:' + str(life) + '-n:' + str(
nbFeats) + '-s:' + str(strategy) + '-g:' + str(group)
outputFolderName += '/'
output_directory += outputFolderName
if not os.path.exists(output_directory):
os.makedirs(output_directory)
begin = time.time()
for q in queries:
# print "Query "+q
dictQRels.setdefault(q, {})
list = collection.find({'query': q}, {'_id': 0, 'docs': 1})
count = 0
list_doc = []
for i in list:
for d in i['docs']:
# print "**********"
count += 1
name = d['doc_name']
# list_feat = []
list_feat = {}
for f in d['features']:
# print f
list_feat[f] = Feature(f, d['features'][f])
# if float(d['features'][f]) > 1.0 :
# print f + " = "+ str(d['features'][f])
if model not in list_feat:
list_feat[model] = Feature(model, 0.0)
list_doc.append(Document(name, list_feat))
# sys.exit()
colName = collection_name + "_std"
print colName
collection_std = db[colName]
listStd = {}
res = collection_std.find({'query': q}, {'_id': 0})
listStd = res[0]['stds']
if type_tournament == "robin":
to = RoundRobin(query=q, impact=impact, health=life, nbFeat=nbFeats, strategy=strategy, nbRound=nb_rounds,
featsToRemove=features_to_remove, qrel=dictQRels[q], accepted=accepted, optim=optim,
listStd=listStd, process=process)
elif type_tournament == "return":
to = RoundRobinReturnMatch(query=q, impact=0, health=life, nbFeat=nbFeats, strategy=strategy,
nbRound=nb_rounds, featsToRemove=features_to_remove, accepted=accepted,
optim=optim, listStd=listStd)
elif type_tournament == "swiss":
to = SwissSystem(query=q, impact=impact, health=life, nbFeat=nbFeats, strategy=strategy, nbRound=nb_rounds,
featsToRemove=features_to_remove, accepted=accepted, optim=optim, listStd=listStd,
process=process)
elif type_tournament == "random":
to = RandomTournament(query=q, impact=impact, health=life, nbFeat=nbFeats, strategy=strategy,
nbRound=nb_rounds, featsToRemove=features_to_remove, qrel=dictQRels[q],
accepted=accepted, optim=optim, listStd=listStd)
elif type_tournament == "grouprobin":
to = GroupStage(query=q, impact=impact, health=life, nbFeat=nbFeats, strategy=strategy, nbGroups=group,
featsToRemove=features_to_remove, qrel=dictQRels[q], best=best, accepted=accepted,
optim=optim, listStd=listStd)
elif type_tournament == "grouprobinoptim":
to = GroupStageOptim(query=q, impact=impact, health=life, nbFeat=nbFeats, strategy=strategy, nbGroups=group,
featsToRemove=features_to_remove, qrel=dictQRels[q], best=best, accepted=accepted,
model=model, optim=optim, listStd=listStd, process=process)
elif type_tournament == "groupswiss":
to = GroupSwiss(query=q, impact=impact, health=life, nbFeat=nbFeats, strategy=strategy, nbGroups=group,
nbRound=nb_rounds, featsToRemove=features_to_remove, qrel=dictQRels[q], best=best,
accepted=accepted, optim=optim, listStd=listStd, process=process)
elif type_tournament == "groupswissoptim":
to = GroupSwissOptim(query=q, impact=impact, health=life, nbFeat=nbFeats, strategy=strategy, nbGroups=group,
nbRound=nb_rounds, featsToRemove=features_to_remove, qrel=dictQRels[q], best=best,
accepted=accepted, model=model, optim=optim, listStd=listStd, process=process)
elif type_tournament == "seed":
to = Seed(query=q, impact=impact, health=life, nbFeat=nbFeats, strategy=strategy, nbRound=nb_rounds,
featsToRemove=features_to_remove, qrel=dictQRels[q], accepted=accepted, model=model, optim=optim,
listStd=listStd)
elif type_tournament == "upper":
to = Upper(query=q, impact=impact, health=life, nbFeat=nbFeats, strategy=strategy, nbRound=nb_rounds,
featsToRemove=features_to_remove, qrel=dictQRels[q], accepted=accepted, model=model, optim=optim,
listStd=listStd)
print "setCompetitors"
to.setCompetitors(list_doc)
print len(list_doc)
print "runCompetition"
to.runCompetition()
print "printResults"
to.printResults(output_directory)
print "[ n=", process, type_tournament, "] total time:", (time.time() - begin), "ms"
"""
# Generation aleatoire de documents
leaders = []
random.seed()
for i in range(nb_documents):
feats =[]
for j in range(nb_features):
feats.append(Feature("F"+str(j),random.randint(0,10)))
doc = Document("Document "+str(i),feats)
print doc
leaders.append(doc)
swiss.competitors = leaders
for id_x in range(1,nb_rounds):
swiss.schedule(id_x)
swiss.run_round_k(id_x)
swiss.print_results()
sys.exit()
"""
def usage():
print "Usage: \
-h [--help] Print this help \n\
-t [--type] Set the type of the tournament \n\
-d [--documents] Set the number of documents that shall participate to the final tournament (preferably a power of 2) \n \
-r [--rounds] Set the number of rounds in the swiss style tournament \n\
-g [--groups] Set the nuumber of groups in the swiss style tournament \n\
-v [--verbose] Activate the verbose mode (for debugging purpose)"
def function_to_debug():
doc_a = Document("DocA",
[Feature("f0", 10), Feature("f1", 5), Feature("f2", 10), Feature("f3", 20), Feature("f4", 10),
Feature("f5", 10), Feature("f6", 10)])
doc_b = Document("DocB", [Feature("f0", 10), Feature("f3", 5), Feature("f5", 8), Feature("f6", 1)])
m = Match(doc_a, doc_b)
m.elaborated_match_v2()
sys.exit(0)
if __name__ == "__main__":
main()
#function_to_debug()