-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathind_inveted.py
242 lines (196 loc) · 4.93 KB
/
ind_inveted.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
from indexer import read_docs
from token_info import TokenInfo
from token_occurence import TokenOccurence
from document import DocRef
from math import log2,sqrt
import pickle
class Index_inverted(object):
h = dict()
freq = dict()
docs_size = dict()
n_doc = 0
Id = 0
def printf(self):
for t in self.h:
print('termo = ',t)
print('idf = ',self.h[t].idf)
print('df = ',self.h[t].df)
for w in self.h[t].get_docs():
print('doc = ',w.doc.file)
print('count = ',w.count)
print('tf = ',w.tf)
print('length = ',w.size_d)
print('len1 = ',w.doc.length)
print()
print(' ----- ')
def sref(self,path):
list_docs = read_docs(path)
self.n_doc = len(list_docs)
d = dict()
c = 0
for path_doc in list_docs:
d = DocRef(path_doc)
v = d.toVector()
c = c + 1
for t in v.keys():
if not self.h.__contains__(t):
self.h[t] = TokenInfo(0,0,[])
if self.freq.__contains__(path_doc):
if v[t] > self.freq[path_doc][0]:
self.freq[path_doc] = (v[t],t)
else:
self.freq[path_doc] = (v[t],t)
self.h[t].add_doc(d,v[t])
self.docs_size[d.get_path()] = 0.0
print(c)
print('calculando tf')
self.calc_tf()
print('finalizou tf')
print('calculando docsize')
self.size_doc()
print('finalizou docsize')
print('salvando no arquivo')
self.write_file()
self.write_file1()
print('salvou')
return self.h
def sref1(self,path):
list_docs = read_docs(path)
self.n_doc = len(list_docs)
d = dict()
c = 0
for path_doc in list_docs:
d = DocRef(path_doc)
v = d.toVector1()
c = c + 1
for t in v.keys():
if not self.h.__contains__(t):
self.h[t] = TokenInfo(0,0,[])
if self.freq.__contains__(path_doc):
if v[t] > self.freq[path_doc][0]:
self.freq[path_doc] = (v[t],t)
else:
self.freq[path_doc] = (v[t],t)
self.h[t].add_doc(d,v[t])
self.docs_size[d.get_path()] = 0.0
print(c)
print('calculando tf')
self.calc_tf()
print('finalizou tf')
print('calculando docsize')
self.size_doc()
print('finalizou docsize')
print('salvando no arquivo')
self.write_file()
self.write_file1()
print('salvou')
return self.h
def size_doc(self):
for t in self.h.keys():
self.h[t].set_idf(log2(self.n_doc/self.h[t].get_df()))
for w in self.h[t].get_docs():
self.docs_size[w.get_doc().get_path()] += (w.count*self.h[t].get_idf())**2
#w.doc.length = (w.get_count()*self.h[t].get_idf())**2
for k in self.docs_size.keys():
self.docs_size[k] = sqrt(self.docs_size[k])
def calc_tf(self):
for t in self.h.keys():
for w in self.h[t].get_docs():
w.set_tf(w.get_count()/self.freq[w.get_doc().get_path()][0])
def write_file(self):
file = open('data.pickle', 'wb')
pickle.dump(self.h, file, pickle.HIGHEST_PROTOCOL)
file.close()
def write_file1(self):
file = open('data1.pickle', 'wb')
pickle.dump(self.docs_size, file, pickle.HIGHEST_PROTOCOL)
file.close()
def read_file1(self):
file = open('data1.pickle', 'rb')
self.docs_size = pickle.load(file)
file.close()
def read_file(self):
file = open('data.pickle', 'rb')
self.h = pickle.load(file)
file.close()
def read_index(self,index):
file = open(index,'rb')
self.h = pickle.load(file)
file.close()
def query1(self,path):
r = dict()
d = DocRef(path)
q = d.toVector1()
peso = 0.0
for t in q.keys():
if not(self.h.__contains__(t)):
continue
I = self.h[t].get_idf()
K = q[t]
W = K*I
peso = peso + (W**2)
L = self.h[t].get_docs()
for O in L:
T = O.get_doc()
D = T.file
C = O.count
if not r.__contains__(D):
r[D] = 0.0
r[D] = r[D] + (W*I*C)
L = 0.0
L = sqrt(peso)
for D in r.keys():
r[D] = r[D] / (L * self.docs_size[D])
list1 = sorted(r,key=r.get,reverse=True)
l1 = []
for w in list1:
aux = w.split('/')
l1.append(aux[len(aux)-1])
#mude pfv, return l1 se e somente se vc estiver fazendo a avaliacao
return l1[:100]
def query(self,path):
r = dict()
d = DocRef(path)
q = d.toVector()
peso = 0.0
for t in q.keys():
if not(self.h.__contains__(t)):
continue
I = self.h[t].get_idf()
K = q[t]
W = K*I
peso = peso + (W**2)
L = self.h[t].get_docs()
for O in L:
T = O.get_doc()
D = T.file
C = O.count
if not r.__contains__(D):
r[D] = 0.0
r[D] = r[D] + (W*I*C)
L = 0.0
L = sqrt(peso)
for D in r.keys():
r[D] = r[D] / (L * self.docs_size[D])
list1 = sorted(r,key=r.get,reverse=True)
l1 = []
for w in list1:
aux = w.split('/')
l1.append(aux[len(aux)-1])
#mude pfv, return l1 se e somente se vc estiver fazendo a avaliacao
return l1[:100]
def busca(self,str1):
file = open('query.story','w')
file.write(str1)
file.close()
return self.query('query.story')
def busca1(self,str1):
file = open('query.story','w')
file.write(str1)
file.close()
return self.query1('query.story')
def esta(self, lst, tk):
for w in lst.keys():
if w.file == tk.file:
return w
return False