-
Notifications
You must be signed in to change notification settings - Fork 0
/
ndgc.py
604 lines (558 loc) · 21.3 KB
/
ndgc.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#!/usr/bin/python
import cgitb
from whoosh.qparser import QueryParser
from os import listdir #for getting the list of all files inside a directory
from os.path import isfile,join #for same as above
from whoosh.index import create_in,exists_in,open_dir #for creating schema
from whoosh.fields import * #for defining fields in schema
from whoosh import scoring
import cgi
from BeautifulSoup import BeautifulSoup #for extracting only text
import sys
import os
from whoosh import qparser
from whoosh import analysis, fields, formats, query, highlight
import time
def searchpage():
global results, time_tf
print ('''
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="Bootstrap/css/bootstrap.css" rel="stylesheet">
<link href="Bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="Bootstrap/css/bootstrap-theme.css" rel="stylesheet">
<link href="Bootstrap/css/bootstrap-theme.min.css" rel="stylesheet">
<link href="BootstrapSwitch/css/main.css" rel="stylesheet">
<link href="BootstrapSwitch/css/highlight.css" rel="stylesheet">
<link href="BootstrapSwitch/dist/css/bootstrap3/bootstrap-switch.css" rel="stylesheet">
<link href="BootstrapSwitch/css/other.css" rel="stylesheet">
<script src="BootstrapSwitch/js/jquery.min.js"></script>
<script src="BootstrapSwitch/dist/js/bootstrap-switch.js"></script>
<script src="Bootstrap/js/bootstrap.min.js"></script>
<script src="Graph/js/highcharts.js"></script>
<script src="Graph/js/modules/exporting.js"></script>
<script>
$(function(argument) {
$('[type="checkbox"]').bootstrapSwitch();
})
</script>
<title>InfoZeal</title>
</head>
<body>''')
print ('''
<div class="panel panel-default">
<div class="panel-heading">
<form class="form-inline" id="myForm" role="form" action="#" method="post">
<div class="row">
<div class="col-lg-6">
<div class="input-group">
<div class="row">
<div class="col-lg-2">
<img src="images/logo.jpg" class="image-logo"/>
</div>
<div class="col-lg-6">
<div class="input-group">
<input type="hidden" name="hiddenOption" id="hiddenOption">
<input type="hidden" name="relTf" id="relTf" value=''')
print str(relTf)
print ('''>
<input type="hidden" name="relTfidf" id="relTfidf" value=''')
print str(relTfidf)
print ('''>
<input type="hidden" name="relBm" id="relBm" value=''')
print str(relBm)
print ('''>
<input type="hidden" name="count" id="count" value=''')
print str(count)
print ('''>
<input type="text" class="search_box" id ="query" name="query" value=
''')
print ("'" + query +"'")
print (''' >
<span class="input-group-btn">
<input type="submit" id="submitButton" class="btn btn-default">Search
</span>
</div>
</div>
</div>
</div>
</div>
                     
Stemming   <input class="form-control" name="stem" id="stem" value="stemming" type="checkbox">
        
Stop Word   <input class="form-control" type="checkbox" name="stop" value="stopping" id="stop">
                   
   
<div class="btn-group">
<button type="button" class="btn btn-primary">Options</button>
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">
<li><a onclick="hitOption(0)">MAP</a></li>
<li><a onclick="hitOption(1)">NDCG</a></li>
<li><a onclick="hitOption(2)">Search Engine</a></li>
</ul>
</div>
</div>
</div>
</form>
<div class="result-panel-body-main">
<div class="row">
<div class="col-lg-7">
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#tf">Term Frequency</a></li>
<li ><a data-toggle="tab" href="#tfidf">Invert Document Term Frequency</a></li>
<li ><a data-toggle="tab" href="#bm25">Best Match 25</a></li>
</ul>
</div>
<div class="col-lg-2">
<span class="input-group-btn">
<button class="btn btn-success" id="bn" type="button" onclick="cal()">Calculate NDCG</button>
</span>
</div>
</div>
<div class="tab-content">
<div id="tf" class="tab-pane fade in active">
<h6>  
''')
if int(count)==10000:
print ('''<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Normal Discounted Cumulative Gain TF </h3>
</div>
<div class="analytics-panel-body">''')
print relTf
print('''
</div>''')
else:
if time_tf is not None:
print str(len(results_tf)) + " results (" + str(time_tf) +" seconds)"
print (''' </h6>
<p>
''')
for x in results_tf:
print ('''
<div class="result-panel-heading">
<div class="result_title">
<div class="row">
<div class="col-lg-9">
<h6 class="result-title-heading">
<a href="''')
print x['path'].encode('utf-8')
print ('''" >
''')
print x['title'].encode('utf-8')
print (''' </a></div>
<div class="col-lg-3">
<select class="tfRel">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
</div>
</h6>
<h6 class="result-title-path">
''')
print x['path'].encode('utf-8')
print (''' </h6>
</div>
<div class="result-panel-body">
''')
print x.highlights("content").encode('utf-8')
print (''' </div>
</div>
''')
print (''' </p>
</div>
<div id="tfidf" class="tab-pane fade">
<h6>  ''')
if int(count)==10000:
print ('''<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Normal Discounted Cumulative Gain TFIDF </h3>
</div>
<div class="analytics-panel-body">''')
print relTfidf
print('''
</div>''')
else:
if time_tfidf is not None:
print str(len(results_tfidf)) + " results (" + str(time_tfidf) +" Seconds)"
print (''' </h6>
<p>
''')
for x in results_tfidf:
print ('''
<div class="result-panel-heading">
<div class="result_title">
<div class="row">
<div class="col-lg-9">
<h6 class="result-title-heading">
<a href="''')
print x['path'].encode('utf-8')
print ('''" >
''')
print x['title'].encode('utf-8')
print (''' </a></div>
<div class="col-lg-3">
<select class="tfidfRel">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
</div>
</h6>
<h6 class="result-title-path">
''')
print x['path'].encode('utf-8')
print (''' </h6>
</div>
<div class="result-panel-body">
''')
print x.highlights("content").encode('utf-8')
print (''' </div>
</div>
''')
print (''' </p>
</div>
<div id="bm25" class="tab-pane fade">
<h6>  ''')
if int(count)==10000:
print ('''<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Normal Discounted Cumulative Gain BM25 </h3>
</div>
<div class="analytics-panel-body">''')
print relBm
print('''
</div>''')
else:
if time_bm25 is not None:
print str(len(results_bm25)) + " results (" + str(time_bm25) +" Seconds)"
print (''' </h6>
<p>
''')
for x in results_bm25:
print ('''
<div class="result-panel-heading">
<div class="result_title">
<div class="row">
<div class="col-lg-9">
<h6 class="result-title-heading">
<a href="''')
print x['path'].encode('utf-8')
print ('''" >
''')
print x['title'].encode('utf-8')
print (''' </a></div>
<div class="col-lg-3">
<select class="bmRel">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
</div>
</h6>
<h6 class="result-title-path">
''')
print x['path'].encode('utf-8')
print (''' </h6>
</div>
<div class="result-panel-body">
''')
print x.highlights("content").encode('utf-8')
print (''' </div>
</div>
''')
print (''' </p>
</div>
<div id="an" class="tab-pane fade">''')
print ('''
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Index Size Vs Stop Word</h3>
</div>
<div class="analytics-panel-body">
Size of Index With Stop Word Removal: 224.8 Mb<br>
Size of Index Without Stop Word Removal: 237.3 Mb<br>
Increase in Index Size: 5.8 %<br>
</div>
</div>''')
print ('''
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Latency Time Vs Query Size</h3>
</div>
<div class="analytics-panel-body-graph">
<div id="container" style="width: 700px; height: 400px; margin: 0 auto"></div>
</div>
</div>''')
print ('''
</div>
</div>
</div>''')
if doStem=="false":
print ('''<script>document.getElementById("stem").checked = false;</script>''')
else:
print ('''<script>document.getElementById("stem").checked = true;</script>''')
if doStop=="false":
print ('''<script>document.getElementById("stop").checked = false;</script>''')
else:
print ('''<script>document.getElementById("stop").checked = true;</script>''')
if int(count)==10000:
print ('''<script>document.getElementById("bn").disabled = true;
document.getElementById("query").readOnly = true;
document.getElementById("submitButton").disabled = true;
</script>''')
print('''
<script>
function hitOption(choice){
document.getElementById("hiddenOption").value = choice;
document.getElementById("myForm").submit();
}
function cal(){
var x = document.getElementsByClassName("tfRel");
var x1 = document.getElementsByClassName("tfidfRel");
var x2 = document.getElementsByClassName("bmRel");
var arr = [];
var rel = [];
if(x.length==0){
document.getElementById("relTf").value = "0";
}
else{
for(var i=0;i<x.length;i++){
var selectedNode = x[i].options[x[i].selectedIndex];
if(i==0){
var k = 1.0*Number(selectedNode.value);
arr.push(k);
}
else{
var y = 1.0*Number(selectedNode.value);
var pos = i+1;
var demon = 1.0*Math.log(2);
var nume = 1.0*Math.log(pos);
var l = (nume/demon);
var num = (y/l);
arr.push(arr[i-1]+num);
}
rel.push(Number(selectedNode.value));
}
var init = arr[x.length-1];
rel.sort(function(a, b){return b-a});
arr = [];
for(i=0;i<x.length;i++){
if(i==0){
var k = 1.0*(rel[i]);
arr.push(k);
}
else{
var y = Number(rel[i]);
var pos = i+1;
var demon = 1.0*Math.log(2);
var nume = 1.0*Math.log(pos);
var l = (nume/demon);
var num = (y/l);
arr.push(arr[i-1]+num);
}
}
var ideal = arr[x.length-1];
if(ideal!=0)
document.getElementById("relTf").value = (init/ideal).toString();
else
document.getElementById("relTf").value = "0";
}
/**for tfidf**/
arr = [];
rel = [];
if(x1.length==0){
document.getElementById("relTfidf").value = "0";
}
else{
for(var i=0;i<x1.length;i++){
var selectedNode = x1[i].options[x1[i].selectedIndex];
if(i==0){
var k = 1.0*Number(selectedNode.value);
arr.push(k);
}
else{
var y = 1.0*Number(selectedNode.value);
var pos = i+1;
var demon = 1.0*Math.log(2);
var nume = 1.0*Math.log(pos);
var l = (nume/demon);
var num = (y/l);
arr.push(arr[i-1]+num);
}
rel.push(Number(selectedNode.value));
}
var init = arr[x1.length-1];
rel.sort(function(a, b){return b-a});
arr = [];
for(i=0;i<x1.length;i++){
if(i==0){
var k = 1.0*(rel[i]);
arr.push(k);
}
else{
var y = Number(rel[i]);
var pos = i+1;
var demon = 1.0*Math.log(2);
var nume = 1.0*Math.log(pos);
var l = (nume/demon);
var num = (y/l);
arr.push(arr[i-1]+num);
}
}
var ideal = arr[x1.length-1];
if(ideal!=0)
document.getElementById("relTfidf").value = (init/ideal).toString();
else
document.getElementById("relTfidf").value = "0";
}
/**for Bm25**/
arr = [];
rel = [];
if(x2.length==0){
document.getElementById("bmRel").value = "0";
}
else{
for(var i=0;i<x2.length;i++){
var selectedNode = x2[i].options[x2[i].selectedIndex];
if(i==0){
var k = 1.0*Number(selectedNode.value);
arr.push(k);
}
else{
var y = 1.0*Number(selectedNode.value);
var pos = i+1;
var demon = 1.0*Math.log(2);
var nume = 1.0*Math.log(pos);
var l = (nume/demon);
var num = (y/l);
arr.push(arr[i-1]+num);
}
rel.push(Number(selectedNode.value));
}
var init = arr[x2.length-1];
rel.sort(function(a, b){return b-a});
arr = [];
for(i=0;i<x2.length;i++){
if(i==0){
var k = 1.0*(rel[i]);
arr.push(k);
}
else{
var y = Number(rel[i]);
var pos = i+1;
var demon = 1.0*Math.log(2);
var nume = 1.0*Math.log(pos);
var l = (nume/demon);
var num = (y/l);
arr.push(arr[i-1]+num);
}
}
var ideal = arr[x2.length-1];
if(ideal!=0)
document.getElementById("relBm").value = (init/ideal).toString();
else
document.getElementById("relBm").value = "0";
}
document.getElementById("count").value = "10000";
document.getElementById("myForm").submit();
}
</script>
</body>
</html>
''')
def getData():
global flag,doStem,doStop,option,formData,relTf,relTfidf,relBm, count
flag = 1
formData = cgi.FieldStorage()
doStem = formData.getvalue("stem")
relTf = formData.getvalue("relTf")
relTfidf = formData.getvalue("relTfidf")
relBm = formData.getvalue("relBm")
count = formData.getvalue("count")
if relTf==None:
relTf = "0"
if relTfidf==None:
relTfidf = "0"
if relBm==None:
relBm = "0"
if doStem==None:
doStem = "false"
if count==None:
count = "0"
doStop = formData.getvalue("stop")
option = formData.getvalue("hiddenOption")
if option!=None:
option = int(option)
if doStop==None:
doStop = "false"
if "query" not in formData:
flag = 0
return ""
else:
try:
q = formData.getvalue('query')
except:
pass
return q
def searchModule(qq):
global results_bm25, results_tfidf, results_tf, results ,idx, time_bm25, time_tfidf, time_tf
results_bm25 = results_tfidf = results_tf = results = []
time_bm25 = time_tfidf = time_tf = None
if flag == 1:
if doStem!="false" and doStop!="false":
idx = open_dir("Indexed/Index_stsw")
elif doStem!="false" and doStop=="false":
idx = open_dir("Indexed/Index_st")
elif doStem=="false" and doStop!="false":
idx = open_dir("Indexed/Index_sw")
else:
idx = open_dir("Indexed/Index")
qp = qparser.QueryParser("content", schema=idx.schema)
q = qp.parse(unicode(qq)) #by default, the parser treats the words as if they were connected by AND
s = idx.searcher()
results = s.search(q)
results.fragmenter.surround = 50
start = time.time()
results_bm25 = idx.searcher().search(q)
time_bm25 = (time.time()-start)
results_bm25.fragmenter.surround = 50
start = time.time()
results_tfidf = idx.searcher(weighting=scoring.TF_IDF()).search(q)
time_tfidf = (time.time()-start)
results_tfidf.fragmenter.surround = 50
start = time.time()
results_tf = idx.searcher(weighting=scoring.Frequency()).search(q)
time_tf = (time.time()-start)
results_tf.fragmenter.surround = 50
def main():
global query
query = getData()
if(option==0):
print "Location:http://172.16.114.65/IR/cgiFiles/map.py"
if option==1:
print "Location:http://172.16.114.65/IR/cgiFiles/ndgc.py"
if option==2:
print "Location:http://172.16.114.65/IR/cgiFiles/search.py"
print "Content-type:text/html; image/jpg"
print
searchModule(query)
searchpage()
if __name__== "__main__":
main()