-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews_final.py
More file actions
560 lines (503 loc) · 20.4 KB
/
views_final.py
File metadata and controls
560 lines (503 loc) · 20.4 KB
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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import Http404
from rest_framework.views import APIView
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from django.http import JsonResponse
from django.core import serializers
from django.conf import settings
import requests
from bs4 import BeautifulSoup as Soup
import re
from whoosh.qparser import QueryParser
from whoosh import scoring
from whoosh.index import open_dir
from operator import itemgetter
import csv as csv
import nltk
from nltk import PorterStemmer
from nltk.corpus import stopwords
import math
import string
import itertools
#nltk.download('stopwords')
#Insert URL list map
urllistfile = "C:\\Users\\mohan\\OneDrive\\Documents\\Index_IR\\urllistDict.csv"
stopwords = ['a', 'all', 'an', 'and', 'any', 'are', 'as', 'be', 'been', 'but', 'by', 'few', 'for', 'have', 'he',
'her',
'here', 'him', 'his', 'how', 'i', 'in', 'is', 'it', 'its', 'many', 'me', 'my', 'none', 'of', 'on',
'or', 'our',
'she', 'some', 'the', 'their', 'them', 'there', 'they', 'that', 'this', 'us', 'was', 'what', 'when',
'where',
'which', 'who', 'why', 'will', 'with', 'you', 'your']
indexPath = 'C:\\Users\\mohan\\OneDrive\\Documents\\Index_IR\\index\\index'
#Stemming Query
def getStemmedQuery(queryStr):
stemmed_query = []
stemmed_querystr = ""
ps = PorterStemmer()
for word in queryStr.split(' '):
if word not in stopwords:
stemmed_query.append(ps.stem(word))
stemmed_querystr = ' '.join(stemmed_query)
return stemmed_querystr
#trim URL /
def refineURL(urllist):
newUrlList =[]
for url in urllist:
if url[-1]=='/':
newUrlList.append(url[:-1])
else:
newUrlList.append(url)
return newUrlList
#Helper method for google and bing
def refineLink(link):
clean = re.compile('<.*?>')
return [re.sub(clean, '', str(l)) for l in link]
#Method for bing results
def getBingResult(query):
try:
query = str(query)
for start in range(0, 10):
url = "http://www.bing.com/search?q=" + query + "&start=" + str(start * 10)
page = requests.get(url)
soup = Soup(page.content, features='html')
links = soup.findAll('cite')
refined_links = refineLink(links)
return refined_links
except ValueError as e:
return []
#Method for google results
def getGoogleResult(query):
try:
query = str(query)
for start in range(0, 10):
url = "http://www.google.com/search?q=" + query + "&start=" + str(start * 10)
page = requests.get(url)
soup = Soup(page.content, features='html')
links = soup.findAll('cite')
refined_links = refineLink(links)
return refined_links
except ValueError as e:
return []
#method for our search engine -Whoosh Indexer
def ourSearchEngineResult(query):
try:
indexPath = 'C:\\Users\\mohan\\OneDrive\\Documents\\Index_IR\\index\\index'
resulturllist = []
topN = 15
queryStr = getStemmedQuery(str(query))
resulturllist = []
ix = open_dir(indexPath)
with ix.searcher(weighting=scoring.Frequency) as searcher:
query = QueryParser("content", ix.schema).parse(queryStr)
results = searcher.search(query, limit=topN)
# print(type(results))
numOfResults = len(results)
if numOfResults >= topN:
resultIndex = topN
else:
resultIndex = numOfResults - 1
if resultIndex > 0:
resulturllist = [results[i]['URL'] for i in range(0, resultIndex)]
else:
resulturllist = []
except ValueError as e:
return []
resulturllist = [str(l) for l in resulturllist]
return resulturllist
#Helper for page rank and hits - searchindex
def searchIndex(queryStr, index, topN):
resultdict = {}
ix = open_dir(index)
with ix.searcher(weighting=scoring.Frequency) as searcher:
query = QueryParser("content", ix.schema).parse(queryStr)
results = searcher.search(query, limit=topN)
for hit in results:
resultdocid, resulturl = hit['title'], hit['URL']
resultdict[resultdocid] = resulturl
return resultdict
#Helper for page rank - PageRankMap
def calculatePageRank(inlinkMap, outlinkMap):
pagerankmap = {}
for doc in inlinkMap.keys():
pagerankmap[doc] = 0.15
for inlink in inlinkMap[doc]:
pagerankmap[inlink] = 0.15
iter = 50
while (iter > 0):
for doc in inlinkMap.keys():
pr = 0
if len(inlinkMap[doc]) > 0:
for inlink in inlinkMap[doc]:
if inlink > 0 and inlink in outlinkMap.keys():
pr_inlink = pagerankmap[inlink]
c_inlink = len(outlinkMap[inlink])
pr += (pr_inlink / c_inlink)
pr *= 0.85
pagerankmap[doc] = 0.15 + pr
else:
pagerankmap[doc] = 1 / len(inlinkMap.keys())
iter -= 1
return pagerankmap
#UrlMap
def createURLMap(urllistfile):
URLMap = {}
with open(urllistfile, encoding = 'ISO-8859-1') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
URLMap[row['DOCID']] = {'URL': row['URL'],'ParentList': eval(str(row['PARENTID']).replace('{', '[').replace('}', ']'))}
return URLMap
#INlinks
def createInlinkMap(URLMap):
inlinkmap = {}
for key in URLMap.keys():
inlinkmap[key] = URLMap[key]['ParentList']
return inlinkmap
#OUTlinks
def createOutlinkMap(URLMap):
outlinkMap= {}
#URLMap =createURLMap(urllistfile)
for docID in URLMap.keys():
parentIDlist = URLMap[docID]['ParentList']
for parent in parentIDlist:
if parent not in outlinkMap:
childList = set()
childList.add(docID)
outlinkMap[parent] = childList
else:
childList = outlinkMap[parent]
childList.add(docID)
outlinkMap[parent] = childList
return outlinkMap
#Method to get Results by page rank
def getResultsByPagerank(query_str, indexoppath, topN):
query_str = getStemmedQuery(query_str)
resultdict = searchIndex(query_str, indexoppath,topN)
URLMap = createURLMap(urllistfile)
inlinkMap = createInlinkMap(URLMap)
outlinkMap = createOutlinkMap(URLMap)
pagerankmap = calculatePageRank(inlinkMap, outlinkMap)
resultpagerank = {}
resultURL = []
for key in resultdict.keys():
resultpagerank[key] = pagerankmap[key]
if len(resultpagerank) > 0:
for docid, value in sorted(resultpagerank.items(), key=itemgetter(1), reverse=True):
resultURL.append(resultdict[docid])
else:
return []
return resultURL
###########################HIT SCORES#################################
#normalizehitscore
def normalizehitscores(hitsscore):
minhub = min(hitsscore, key=lambda x: float(hitsscore[x]['hubscore']))
maxhub = max(hitsscore, key=lambda x: float(hitsscore[x]['hubscore']))
minauth = min(hitsscore, key=lambda x: float(hitsscore[x]['authscore']))
maxauth = max(hitsscore, key=lambda x: float(hitsscore[x]['authscore']))
for docid in hitsscore.keys():
if maxhub!=minhub:
normalizedhubscore = (float(hitsscore[docid]['hubscore']) - float(minhub)) / (float(maxhub) - float(minhub))
hitsscore[docid]['hubscore'] = normalizedhubscore
normalizedauthscore = (float(hitsscore[docid]['authscore']) - float(minauth)) / (float(maxauth) - float(minauth))
hitsscore[docid]['authscore'] = normalizedauthscore
return hitsscore
#calculate hub scores
def calchubscores(hitsscore,inlinkmap,outlinkMap):
resHitsscore ={}
for k in range(0, 25):
for docid in hitsscore.keys():
authscore, hubscore = 1.0, 1.0
inlinklist = inlinkmap[docid] # this list can be empty of hold just 0
outlinklist = []
if len(inlinklist):
for inlink in inlinklist:
if inlink != -1:
authscore += hitsscore[str(inlink)]['hubscore']
if docid in outlinkMap:
outlinklist = outlinkMap[docid]
for outlink in outlinklist:
hubscore += hitsscore[outlink]['authscore']
hitsscore[docid]['hubscore'] = hubscore
hitsscore[docid]['authscore'] = authscore
resHitsscore = normalizehitscores(hitsscore)
k += 1
return resHitsscore
#initiallizehubscores
def initializehubscores(resultMap,inlinkmap):
hitsscore = {}
for docID in resultMap.keys():
hitsscore[docID] = {'hubscore': 1.0, 'authscore': 1.0}
inlinklist = inlinkmap[docID]
if len(inlinklist) > 0 and inlinklist[0] != -1:
for inlink in inlinklist:
if inlink!= -1:
hitsscore[str(inlink)] = {'hubscore': 1.0, 'authscore': 1.0}
return hitsscore
# HIT score
def getResultsByHitScore(query_str, indexoppath, topN):
query_str = getStemmedQuery(query_str)
resultdict = searchIndex(query_str, indexoppath, topN)
resulthubscore = {}
resultauthorityscore = {}
resultURLhubscore, resultURLauthscore = [], []
URLMap = createURLMap(urllistfile)
inlinkmap = createInlinkMap(URLMap)
outlinkmap = createOutlinkMap(URLMap)
temphitscores = initializehubscores(resultdict,inlinkmap)
hitsscore = calchubscores(temphitscores,inlinkmap,outlinkmap)
for key, entry in hitsscore.items():
resulthubscore[key] = entry['hubscore']
resultauthorityscore[key] = entry['authscore']
for docid, value in sorted(resulthubscore.items(), key=itemgetter(1), reverse=True):
resultURLhubscore.append(URLMap[docid]['URL'])
for docid, value in sorted(resultauthorityscore.items(), key=itemgetter(1), reverse=True):
resultURLauthscore.append(URLMap[docid]['URL'])
return resultURLhubscore[:10], resultURLauthscore[:10]
###############################QUERY EXPANSION - METRIC###################################################
def searchIndexQE(queryStr, index, topN ):
keywords = []
ix = open_dir(index)
with ix.searcher(weighting=scoring.Frequency) as searcher:
query = QueryParser("content", ix.schema).parse(queryStr)
results = searcher.search(query, limit=topN)
keywords = [keyword for keyword, score in results.key_terms("textdata", docs=10, numterms=2)]
for new_query in keywords:
queryStr += ' ' + new_query
new_results = searchIndexedData(queryStr, index, 10)
return queryStr,new_results
##############################QUERY EXPANSION-ROCCHIO##########################################
alpha = 2.0
beta = 0.75
gamma = 0.15
N= 10
def searchIndexedData(queryStr, index, topN ):
resultURLList = []
resultdict = searchIndex(queryStr, index, topN )
for key , entry in resultdict.items():
resultURLList.append(entry)
return resultURLList
def rocchio(qvec, tfidf, data, word_set, old_query):
# record relevant & irrelevant count
rel_count = 6
irr_count = N - rel_count
# compute new qvec
new_qvec = [alpha * x for x in qvec]
for i in range(N):
if data[i]:
new_qvec = [q + beta / float(rel_count) * r for q, r in zip(new_qvec, tfidf[i])]
else:
new_qvec = [q - gamma / float(irr_count) * r for q, r in zip(new_qvec, tfidf[i])]
# extract new query, order matters
qwords = [word.lower() for word in old_query.split()] # may need restore later
# top 2 largest
sorted_qvec = [(new_qvec[i], i) for i in range(len(new_qvec))]
sorted_qvec.sort(reverse=True)
# number of words we can augment each time
quota = 2
for vec_val, index in sorted_qvec:
if quota <= 0:
break
elif word_set[index] not in qwords:
# if negative, ignore
if vec_val <= 0:
break
qwords.append(word_set[index])
quota -= 1
qwords.sort(key=lambda w: new_qvec[word_set.index(w)], reverse=True)
queryStr = ' '.join(qwords)
return queryStr
# Calculate tfidf scores
def tfidfvec(query, data):
# extract docs form raw data
docs = [item for item in data]
# build a list of tokenized docs
wordsl = []
for doc in docs:
s = []
for word in nltk.word_tokenize(doc):
if ((word not in stopwords) and (word not in string.punctuation)):
s.append(word.lower())
wordsl.append(s)
# a list of all words, with duplicates
all_words = list(itertools.chain(*wordsl))
# a list of all words, without duplicates - for vector bases
word_set = list(set(all_words))
# construct tf vectors
tf_vecs = [0 for i in range(N)]
for i in range(N):
tf_vecs[i] = [wordsl[i].count(w) for w in word_set]
# compute idf values
idf_all_words = list(itertools.chain(*[set(doc_words) for doc_words in wordsl]))
idfs = [math.log(float(N) / idf_all_words.count(w), 10) for w in word_set]
# compute tf-idf & normalize
tfidf = [0 for i in range(N)]
for i in range(N):
tfidf[i] = [tf * idf for tf, idf in zip(tf_vecs[i], idfs)]
nom = math.sqrt(sum(x ** 2 for x in tfidf[i]))
tfidf[i] = [x / nom for x in tfidf[i]]
# now let's work on the query vector
qwords = [word.lower() for word in query.split()]
# tf vector
qvec = [qwords.count(w) for w in word_set]
# normalize
nom = 1+math.sqrt(sum(x ** 2 for x in qvec))
qvec = [x / nom for x in qvec]
return qvec, tfidf, word_set
def adjustQuery(queryStr, data):
qvec, tfidf, word_set = tfidfvec(queryStr, data)
new_queryStr = rocchio(qvec, tfidf, data, word_set, queryStr)
return new_queryStr
def searchIndexRocchio(queryStr, index, topN ):
keywords = []
queryStr = getStemmedQuery(queryStr)
data ,old_results= [],[]
ix = open_dir(index)
with ix.searcher(weighting=scoring.Frequency) as searcher:
query = QueryParser("content", ix.schema).parse(queryStr)
results = searcher.search(query, limit=topN)
for key, entry in results.items():
old_results.append(entry)
if len(results)!= 0:
for hits in results:
data.append(hits['textdata'])
new_query = adjustQuery(queryStr, data)
if new_query!=queryStr:
new_results = searchIndexedData(new_query, index, 10)
return new_query,new_results
else:
return 'No Expansion', old_results
return 'No expansion','No-Results'
##########################################################################
def checkList(urllsit):
if len(urllsit)==0:
return ["No Results"]
else:
return urllsit
@api_view(["POST"])
def getAll(requestQuery):
try:
result = {}
indexPath = 'C:\\Users\\mohan\\OneDrive\\Documents\\New Folder\\index\\index'
query = str(requestQuery.body)
googleResult = getGoogleResult(query)
googleResult = checkList(googleResult)
googleResult = refineURL(googleResult)
bingResult = getBingResult(query)
bingResult = checkList(bingResult)
bingResult = refineURL(bingResult)
ourSearchResult = ourSearchEngineResult(query)
ourSearchResult = checkList(ourSearchResult)
ourSearchResult = refineURL(ourSearchResult)
pageRankResult = getResultsByPagerank(query,indexPath,10)
ourSearchResult = checkList(ourSearchResult)
pageRankResult = refineURL(pageRankResult)
Query_1, MetricResults = searchIndexQE(query.replace("b'","").replace("'",""), indexPath, 10)
MetricResults = checkList(MetricResults)
MetricResults = refineURL(MetricResults)
queryR = query
Query_2, RocchioResults = searchIndexRocchio(queryR.replace("b'","").replace("'",""), indexPath, 10)
RocchioResults = checkList(RocchioResults)
RocchioResults = refineURL(RocchioResults)
#Cluster Results []cid[]urls
result = {'Google-results': googleResult
,'Bing-results': bingResult
,'Our-results': ourSearchResult
,'Our-PageRank-results': pageRankResult
, 'Query_Rocchio': Query_2
,'Rocchio-Results': RocchioResults
,'Query_Metric': Query_1
,'Metric-Results': MetricResults
}
return JsonResponse(result, safe=False)
except ValueError as e:
return Response(e.args[0],status.HTTP_400_BAD_REQUEST)
########################################################################################################################
@api_view(["POST"])
def getMetricresults(requestQuery):
try:
query = str(requestQuery.body)#.replace("b'","").replace("'","")
print(query)
indexPath = 'C:\\Users\\mohan\\OneDrive\\Documents\\Index_IR\\index\\index'
OneResult, TwoResult = searchIndexQE(query, indexPath, 10)
result = {'Results': str(OneResult),
'URLResults': [str(l) for l in TwoResult]}
return JsonResponse(result, safe=False)
except ValueError as e:
print(e)
return Response(e.args[0],status.HTTP_400_BAD_REQUEST)
@api_view(["POST"])
def getRocchioresults(requestQuery):
try:
query = str(requestQuery.body).replace("b'","").replace("'","")
print(query)
indexPath = 'C:\\Users\\mohan\\OneDrive\\Documents\\Index_IR\\index\\index'
OneResult, TwoResult = searchIndexRocchio(query, indexPath, 10)
result = {'Results': str(OneResult),
'URLResults': [str(l) for l in TwoResult]}
return JsonResponse(result, safe=False)
except ValueError as e:
print(e)
return Response(e.args[0],status.HTTP_400_BAD_REQUEST)
@api_view(["POST"])
def getHitResults(requestQuery):
try:
query = str(requestQuery.body)
indexPath = 'C:\\Users\\mohan\\OneDrive\\Documents\\Index_IR\\index\\index'
hubBasedResult, authBasedResult = getResultsByHitScore(query, indexPath, 10)
result = {'Hub-Results': [str(l) for l in hubBasedResult],
'Auth-Results':[str(l) for l in authBasedResult]}
return JsonResponse(result, safe=False)
except ValueError as e:
return Response(e.args[0],status.HTTP_400_BAD_REQUEST)
@api_view(["POST"])
def getPageRankResults(requestQuery):
try:
query = str(requestQuery.body)
indexPath = 'C:\\Users\\mohan\\OneDrive\\Documents\\New Folder\\index\\index'
resulturllist = getResultsByPagerank(query, indexPath, 5)
result = {'results': [str(l) for l in resulturllist]}
return JsonResponse(result, safe=False)
except ValueError as e:
return Response(e.args[0],status.HTTP_400_BAD_REQUEST)
@api_view(["POST"])
def getBing(requestQuery):
try:
query = str(requestQuery.body)
print(query)
for start in range(0, 10):
url = "http://www.bing.com/search?q=" + query + "&start=" + str(start * 10)
page = requests.get(url)
soup = Soup(page.content, features='html')
links = soup.findAll('cite')
refined_links = refineLink(links)
result = {'results': [str(l) for l in refined_links]}
return JsonResponse(result, safe=False)
except ValueError as e:
print(e)
return Response(e.args[0],status.HTTP_400_BAD_REQUEST)
@api_view(["POST"])
def getGoogle(requestQuery):
try:
query = str(requestQuery.body)
for start in range(0, 10):
url = "http://www.google.com/search?q=" + query + "&start=" + str(start * 10)
page = requests.get(url)
soup = Soup(page.content, features='html')
links = soup.findAll('cite')
refined_links = refineLink(links)
result = {'results': [str(l) for l in refined_links]}
return JsonResponse(result, safe=False)
except ValueError as e:
return Response(e.args[0],status.HTTP_400_BAD_REQUEST)
@api_view(["POST"])
def ourSearchEngine(query):
try:
ourSearchResult = ourSearchEngineResult(query)
ourSearchResult = refineURL(ourSearchResult)
result = {'results': [str(l) for l in ourSearchResult]}
return JsonResponse(result, safe=False)
except ValueError as e:
return Response(e.args[0],status.HTTP_400_BAD_REQUEST)