forked from yoconana/Information-Retrieval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtogetherIndiceResult.py
executable file
·317 lines (236 loc) · 9.1 KB
/
togetherIndiceResult.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
##link all phase one coding together and output the indice result
## By Xi Chen
from QueryWithTF import outputStringQuery
from QueryWithTF import extractValuePart
from lxml import html
from lxml.html.clean import clean_html
import string
import os
import pickle
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
import utils
import codecs # otherwise, it has ascii encoding error
import preprocess
import inverted_index
import candidatefile
import candidateVector
from tfidf_for_Query import tfidf_Query
from similarity import vectorlength
from similarity import simDistance
from similarity import computerSimilarity
import sys
import csv
from utils import *
from minWindow import smallestWindow
from minWindow import newRankedList
import locate_terms
from relevance_feedback import newQuery
import numpy as np
def together_indice_result(query_string,pageno):
#query = sys.argv
#query.pop(0)
#print query
#query = ' '.join(query)
pageno = int(pageno)
query = preprocess.cleanquery(query_string)
#print query
query = outputStringQuery(query)
#print query
#querylist = query.split()
#query = inverted_index.tf_of_query(querylist)
##extract value for the final vectList
#queryTF = extractValuePart(query)
#df_file = utils.read_datastructure('df_dictonary.pkl')
#queryTFIDF = tfidf_for_Query(query,df_file,tf_file)
candidateIndice = candidatefile.candidate_files(query,get_ir_doc_number())
if candidateIndice == []:
return []
queryTFIDF = tfidf_Query(query,get_ir_df_dictionary())
#VectorList = utils.read_datastructure('doc_tfidf_matrix.pkl')
#VectorList = utils.read_datastructure('doc_tfidf_normalize.pkl')
#print 'length'
#print VectorList[10]
#print candidateIndice
CandidateList = candidateVector.extractCandidates(get_ir_tfidf_matrix(), candidateIndice)
#print len(CandidateList[0])
#print len(queryTFIDF)
RankedDocList = computerSimilarity(queryTFIDF, CandidateList,candidateIndice)
full_path_list = get_ir_file_path_list()
path_list = [full_path_list[i] for i in RankedDocList[0]]
#print RankedDocList[0]
plen = len(path_list)
#print(plen)
if plen == 0:
return []
pagecount = plen/10
lastpage = plen%10
if(lastpage != 0):
pagecount+=1
init_item = (pageno-1)*10
if pageno != pagecount:
end_item = init_item+10
else:
if lastpage != 0:
end_item = init_item+lastpage
else:
end_item = init_item+10
part_path_list = path_list[init_item:end_item]
part_docid_list = RankedDocList[0][init_item:end_item]
#print part_docid_list
highlighted_list,highlighted_str_list = locate_terms.locate_terms_indocs(query,part_docid_list)
allresults_count = [plen]*len(highlighted_list)
#print(part_path_list)
#print(highlighted_list)
return zip(part_path_list,highlighted_list,highlighted_str_list,allresults_count)
#init_all_data()
#print("Loaded")
#together_indice_result('Carlsbad Caverns',1)
def together_indice_result2(query_string,pageno):
#query = sys.argv
#query.pop(0)
#print query
#query = ' '.join(query)
pageno = int(pageno)
query = preprocess.cleanquery(query_string)
#print query
query = outputStringQuery(query)
#print query
#querylist = query.split()
#query = inverted_index.tf_of_query(querylist)
##extract value for the final vectList
#queryTF = extractValuePart(query)
#df_file = utils.read_datastructure('df_dictonary.pkl')
#queryTFIDF = tfidf_for_Query(query,df_file,tf_file)
candidateIndice = candidatefile.candidate_files(query,get_ir_doc_number())
if candidateIndice == []:
return []
queryTFIDF = tfidf_Query(query,get_ir_df_dictionary())
#VectorList = utils.read_datastructure('doc_tfidf_matrix.pkl')
#VectorList = utils.read_datastructure('doc_tfidf_normalize.pkl')
#print 'length'
#print VectorList[10]
#print candidateIndice
CandidateList = candidateVector.extractCandidates(get_ir_tfidf_matrix(), candidateIndice)
#print len(CandidateList[0])
#print len(queryTFIDF)
RankedDocList = computerSimilarity(queryTFIDF, CandidateList,candidateIndice)
docs = get_ir_preprocessed_docs_list()
proximityResult = newRankedList(docs, RankedDocList, query)
#print proximityResult
full_path_list = get_ir_file_path_list()
path_list = [full_path_list[i] for i in proximityResult[0]]
#print proximityResult[0]
plen = len(path_list)
#print(plen)
if plen == 0:
return []
pagecount = plen/10
lastpage = plen%10
if(lastpage != 0):
pagecount+=1
init_item = (pageno-1)*10
if pageno != pagecount:
end_item = init_item+10
else:
if lastpage != 0:
end_item = init_item+lastpage
else:
end_item = init_item+10
part_path_list = path_list[init_item:end_item]
part_docid_list = proximityResult[0][init_item:end_item]
#print part_docid_list
highlighted_list,highlighted_str_list = locate_terms.locate_terms_indocs(query,part_docid_list)
allresults_count = [plen]*len(highlighted_list)
#print(part_path_list)
#print(highlighted_list)
return zip(part_path_list,highlighted_list,highlighted_str_list,allresults_count)
#init_all_data()
#print("Loaded")
#together_indice_result2('Kansas History',1)
def together_indice_result3(query_string,pageno,rankedRelevance):
#query = sys.argv
#query.pop(0)
#print query
#query = ' '.join(query)
pageno = int(pageno)
rankedRelevance = [int(x) for x in rankedRelevance]
#print rankedRelevance
query = preprocess.cleanquery(query_string)
#print query
query = outputStringQuery(query)
#print query
#querylist = query.split()
#query = inverted_index.tf_of_query(querylist)
##extract value for the final vectList
#queryTF = extractValuePart(query)
#df_file = utils.read_datastructure('df_dictonary.pkl')
#queryTFIDF = tfidf_for_Query(query,df_file,tf_file)
candidateIndice = candidatefile.candidate_files(query,get_ir_doc_number())
if candidateIndice == []:
return []
queryTFIDF = tfidf_Query(query,get_ir_df_dictionary())
#VectorList = utils.read_datastructure('doc_tfidf_matrix.pkl')
#VectorList = utils.read_datastructure('doc_tfidf_normalize.pkl')
#print 'length'
#print VectorList[10]
#print candidateIndice
CandidateList = candidateVector.extractCandidates(get_ir_tfidf_matrix(), candidateIndice)
#print len(CandidateList[0])
#print len(queryTFIDF)
RankedDocList = computerSimilarity(queryTFIDF, CandidateList,candidateIndice)
docs = get_ir_preprocessed_docs_list()
b4feedback = newRankedList(docs, RankedDocList, query)
releIndex = np.array([n for n,j in enumerate(rankedRelevance) if j==1])
irreleIndex = np.array([n for n,j in enumerate(rankedRelevance) if j ==2])
releIndex = 10*(pageno-1) + releIndex
irreleIndex = 10*(pageno-1) +irreleIndex
realRindex = [b4feedback[0][i] for i in releIndex]
realIrindex = [b4feedback[0][i] for i in irreleIndex]
#print realIrindex
doc_vectors = get_ir_tfidf_matrix()
releDocs =[doc_vectors[i] for i in realRindex]
irreleDocs =[doc_vectors[i] for i in realIrindex]
#print releDocs
#print type(releDocs)
### here need users to select relevant and irrelevant sites
#releDoc = [CandidateList[i] for i in range(0,10)]
#irreleDoc = [CandidateList[i] for i in range (11,10)]
#print len(releDocs)
#print len(irreleDocs)
newQueryTFIDF = newQuery(queryTFIDF, releDocs, irreleDocs)
#print queryTFIDF == newQueryTFIDF
newRankedDocList = computerSimilarity(newQueryTFIDF, CandidateList,candidateIndice)
docs = get_ir_preprocessed_docs_list()
relevant_feedback = newRankedList(docs, newRankedDocList, query)
#print "11111", b4feedback[0]
#print "22222", relevant_feedback[0]
full_path_list = get_ir_file_path_list()
path_list = [full_path_list[i] for i in relevant_feedback[0]]
#print relevant_feedback[0]
plen = len(path_list)
#print(plen)
if plen == 0:
return []
pagecount = plen/10
lastpage = plen%10
init_item = (pageno-1)*10
if pageno != pagecount:
end_item = init_item+10
else:
if lastpage != 0:
end_item = lastpage
else:
end_item = init_item+10
part_path_list = path_list[init_item:end_item]
part_docid_list = relevant_feedback[0][init_item:end_item]
#print part_docid_list
highlighted_list,highlighted_str_list = locate_terms.locate_terms_indocs(query,part_docid_list)
allresults_count = [plen]*len(highlighted_list)
#print(part_path_list)
#print(highlighted_list)
return zip(part_path_list,highlighted_list,highlighted_str_list,allresults_count)
#init_all_data()
#print("Loaded")
#together_indice_result3('wheels',1, [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])