-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryManagerITC.py
123 lines (98 loc) · 3.16 KB
/
QueryManagerITC.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
# coding=utf-8
__author__ = 'kasper'
from Index import Index
from WordsList import WordsList
import math
from operator import itemgetter
class QueryManagerITC:
def __init__(self, db, cdb):
# self.cacheIndex = Index()
# self.cacheIndex.createFromCursor(cdb.index())
# print self.cacheIndex.find('берзански')
self.cacheIndex = Index()
self.cacheIndex.createFromCursor(cdb.findAll())
self.db = db
self.numDocs = 127000
def toArray(self, query):
return query.split(' ')
def getArray(self, word):
arr = self.cacheIndex.find(word)
if not arr:
arr = self.db.find(word)
if not arr:
arr = []
return arr
def intersect(self, lstA, lstB):
result = []
if not lstA and not lstB:
return []
if not lstA:
return lstB
if not lstB:
return lstA
i = 0
j = 0
lstA.append(None)
lstB.append(None)
while lstA[i] is not None and lstB[j] is not None:
if lstA[i] == lstB[j]:
result.append(lstA[i])
j += 1
i += 1
elif lstA[i] < lstB[j]:
i += 1
elif lstA[i] > lstB[j]:
j += 1
elif lstB is None:
result.append(lstA[i])
i += 1
elif lstA is None:
result.append(lstB[j])
j += 1
return result
def execute(self, query):
qArr = self.toArray(query)
colRes = {}
result = []
wt = {}
val = 0
for word in qArr:
colRes[word] = self.getArray(word)
qw = WordsList(1)
qw.insertList(qArr)
for key in qw.list():
tf = round(1 + math.log10(qw.getWord(key)), 2)
idf = round(math.log10(self.numDocs/len(colRes[key])), 2)
wt[key] = tf*idf
val += tf*idf*tf*idf
val = math.sqrt(val)
for key in qw.list():
wt[key] = wt[key]/val
arrIntersect = []
for i in range(len(qArr)):
tmpArr = []
for j in range(len(colRes[qArr[i]])):
tmpArr.append(colRes[qArr[i]][j][0])
arrIntersect = self.intersect(tmpArr, arrIntersect)
arrIntersect.append(None)
dictIntersect = {}
for word in qArr:
niza = colRes[word]
dictIntersect[word] = []
i = 0
j = 0
while arrIntersect[j] is not None:
if niza[i][0] == arrIntersect[j]:
dictIntersect[word].append(niza[i])
i += 1
j += 1
elif niza[i][0] > arrIntersect[j]:
j += 1
elif niza[i][0] < arrIntersect[j]:
i += 1
for i in range(len(arrIntersect) - 1):
value = 0
for key in dictIntersect.keys():
value += dictIntersect[key][i][1] * wt[key]
result.append((arrIntersect[i], round(value, 2)))
return sorted(result, key=itemgetter(1), reverse=True)