-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_query.py
More file actions
234 lines (212 loc) · 7.72 KB
/
run_query.py
File metadata and controls
234 lines (212 loc) · 7.72 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
from models.es_built_in_model import BuiltInModel
from models.okapi_tf_model import OkapiTFModel
from models.tf_idf_model import TFIDFModel
from models.okapi_bm25 import OkapiBM25
from models.laplace_unigram_lm_model import LaplaceUnigramLMModel
from models.jelinekmercer_unigram_lm_model import JelinekMercerUnigramLMModel
from models.pseudo_relevance_feedback_model import PseudoRelevanceFeedbackModel
from utils.constants import Constants
from utils.es import get_term_statistics
from utils.statistics import DocumentStatistics
from utils.text import build_query_list, get_stopwords, get_file_list, write_output
import sys
import os
import threading
import numpy as np
document_statistics = {}
tf_for_queries = {}
wfd_collection = {}
total_tf_wd = {}
query_list = {}
total_length = 0
def run_built_in():
print("Processing: built in model")
built_in = BuiltInModel()
for key in query_list:
results = built_in.query(query_list[key])['hits']['hits']
rank = 1
write_output(
model = 'es',
query_no = str(key),
doc_no = result['_id'],
rank = str(rank),
score = str(result['_score']))
rank += 1
def run_okapi_tf():
print("Processing: Okapi TF model")
okapi_tf = OkapiTFModel(document_statistics)
for q_no in query_list:
query = query_list[q_no]
results = okapi_tf.query(query, tf_for_queries[q_no])
rank = 1
for key, value in sorted(iter(results.items()), key=lambda k_v: (k_v[1],k_v[0]), reverse=True):
# if rank > Constants.MAX_OUTPUT:
# break
write_output(
model = 'okapi_tf',
query_no = str(q_no),
doc_no = str(key),
rank = str(rank),
score = str(value))
rank += 1
print("Okapi TF Done")
def run_tf_idf():
print("Processing: TF-IDF model")
tfidf = TFIDFModel(document_statistics)
for q_no in query_list:
query = query_list[q_no]
results = tfidf.query(query, wfd_collection, tf_for_queries[q_no])
rank = 1
for key, value in sorted(iter(results.items()), key=lambda k_v1: (k_v1[1],k_v1[0]), reverse=True):
# if rank > Constants.MAX_OUTPUT:
# break
write_output(
model = 'tfidf',
query_no = str(q_no),
doc_no = str(key),
rank = str(rank),
score = str(value))
rank += 1
print("TF-IDF Done")
def run_bm25():
print("Processing: Okapi BM25 model")
bm25 = OkapiBM25(document_statistics)
for q_no in query_list:
query = query_list[q_no]
results = bm25.query(query, wfd_collection, tf_for_queries[q_no])
rank = 1
for key, value in sorted(iter(results.items()), key=lambda k_v2: (k_v2[1],k_v2[0]), reverse=True):
# if rank > Constants.MAX_OUTPUT or value <= 0:
# break
write_output(
model = 'bm25',
query_no = str(q_no),
doc_no = str(key),
rank = str(rank),
score = str(value))
rank += 1
print("BM25 Done")
def run_laplace_unigram():
print("Processing: Unigram LM with Laplace model")
laplace_unigram = LaplaceUnigramLMModel(document_statistics)
for q_no in query_list:
query = query_list[q_no]
results = laplace_unigram.query(query, tf_for_queries[q_no])
rank = 1
for key, value in sorted(iter(results.items()), key=lambda k_v3: (k_v3[1],k_v3[0]), reverse=True):
# if rank > Constants.MAX_OUTPUT:
# break
write_output(
model = 'laplace_unigram',
query_no = str(q_no),
doc_no = str(key),
rank = str(rank),
score = str(value))
rank += 1
print("Unigram LM with Laplace done")
def run_jelmer_unigram():
print("Processing: Unigram LM with Jelinek-Mercer model")
jelmer_unigram = JelinekMercerUnigramLMModel(document_statistics)
for q_no in query_list:
query = query_list[q_no]
results = jelmer_unigram.query(
query,
tf_for_queries[q_no],
total_tf_wd[q_no],
total_length)
rank = 1
for key, value in sorted(iter(results.items()), key=lambda k_v4: (k_v4[1],k_v4[0]), reverse=True):
# if rank > Constants.MAX_OUTPUT:
# break
write_output(
model = 'jelmer_unigram',
query_no = str(q_no),
doc_no = str(key),
rank = str(rank),
score = str(value))
rank += 1
print("Unigram LM with Jelinek Mercer done")
def run_pseudo_feedback():
print("Processing: Pseudo Relevance Feedback model")
pseudo_feedback = PseudoRelevanceFeedbackModel(document_statistics)
for q_no in query_list:
query = query_list[q_no]
results = pseudo_feedback.query(
query,
total_length)['hits']['hits']
rank = 1
for result in results:
write_output(
model = 'pseudo_feedback',
query_no = str(q_no),
doc_no = result['_id'],
rank = str(rank),
score = str(result['_score']))
rank += 1
print("Pseudo feedback done")
def build_document_statistics():
print("Building document statistics")
total_length = 0
file_list = get_file_list()
for doc_no in file_list:
stats = DocumentStatistics(doc_no)
document_statistics[doc_no] = stats.length
total_length += stats.length
return total_length
def clean_results_folder():
print("Removing all files in the results folder")
result_files = os.listdir(Constants.RESULTS_PATH)
for result_file in result_files:
os.remove(Constants.RESULTS_PATH + result_file)
def build_tf_for_queries():
print("Collecting the tf values")
for q_no in query_list:
query = query_list[q_no]
tf_collection = []
words = query.split(' ')
for word in words:
w_d, tf = get_term_statistics(word)
wfd_collection[word] = w_d
tf_collection.append(tf)
tf_for_queries[q_no] = tf_collection
def build_total_tf_wd(q_no):
query = query_list[q_no]
words = query.split(' ')
total_tf_list = []
for i in range(len(words)):
total_tf_list.append(np.sum(list(tf_for_queries[q_no][i].values())))
total_tf_wd[q_no] = total_tf_list
if __name__ == '__main__':
"""Main function
"""
threads = []
total_tf_threads = []
clean_results_folder()
query_list = build_query_list()
thread_tf = threading.Thread(target=build_tf_for_queries)
thread_tf.start()
total_length = build_document_statistics()
thread_tf.join()
for q_no in query_list:
total_tf_threads.append(threading.Thread(
target=build_total_tf_wd, args=[q_no]))
for t in total_tf_threads:
t.start()
# t0 = threading.Thread(target=run_built_in)
# threads.append(t0)
t1 = threading.Thread(target=run_okapi_tf)
threads.append(t1)
t2 = threading.Thread(target=run_tf_idf)
threads.append(t2)
t3 = threading.Thread(target=run_bm25)
threads.append(t3)
t4 = threading.Thread(target=run_laplace_unigram)
threads.append(t4)
for thread in threads:
thread.run()
# for thread in threads:
# thread.join()
for t in total_tf_threads:
t.join()
run_jelmer_unigram()
# run_pseudo_feedback()