-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaregamapa_search.py
89 lines (61 loc) · 2.21 KB
/
saregamapa_search.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
"""
Created on Tue Nov 21 17:38:54 2017
@author: Valerio Guarrasi
"""
import math
import heapq
class Saregamapa_Search:
smeta = {}
songs_dict = {}
def apply_search(self, diz_tf_idf):
q = self.smeta["sQuery"]
q = q.split()
diz_qcos = {}
diz_norm = {}
#print(q)
l = []
for word in q:
if word.lower() in diz_tf_idf.keys():#if the word exixsts
for i in set([e[0] for e in diz_tf_idf[word.lower()]]):
l.append(i)
l = list(set(l))
for doc in l:
#numerator
#print(doc)
num = 0
for word in q:
for i in range(len(diz_tf_idf[word])):
if diz_tf_idf[word][i][0]==doc:
num += diz_tf_idf[word][i][1]
diz_qcos[doc]=num
#denominator
norm = 0
for word in diz_tf_idf.values():
for i in range(len(word)):
if word[i][0]==doc:
norm += word[i][1]**2
diz_norm[doc]=math.sqrt(norm)
#print(diz_qcos)
#print(diz_norm)
for doc,num in diz_qcos.items():
if diz_norm[doc]!=0:
diz_qcos[doc] = num/(math.sqrt(len(q))*diz_norm[doc])
return diz_qcos
def apply_heap_toresults(self, diz_qcos):
h = []
results = []
for elem in diz_qcos.keys():
curDoc = self.songs_dict[str(elem)]
heapq.heappush(h,(diz_qcos[elem], curDoc[0], curDoc[1], curDoc[3]))
heapq._heapify_max(h)
for i in range(10):
results.append(list(heapq.heappop(h)))
#print(heapq.heappop(h))
heapq._heapify_max(h)
return results
def search(self):
diz_qcos = self.apply_search(self.smeta["sindexes"])
return self.apply_heap_toresults(diz_qcos)
def __init__(self, smeta):
self.smeta = smeta
self.songs_dict = smeta["songs_dict"]