-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_traditional.py
292 lines (258 loc) · 11.1 KB
/
search_traditional.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
from gensim.summarization import bm25
from baseline_methods.qlm import QLM
import numpy
import argparse
import os
import json
import re
from tqdm import tqdm
from nltk.corpus import stopwords
import string
import nltk
nltk.download('stopwords')
cachedStopwords = set(tok.lower() for tok in stopwords.words("english"))
string_set =set(string.punctuation)
# if you vector file is in binary format, change to binary=True
def COMBSUM(pid_scores_dict):
for pid in pid_scores_dict:
pid_scores_dict[pid] = sum(pid_scores_dict[pid])
#next sort pid_scores_dict based on the score
sorted_pid_scores = sorted(pid_scores_dict.items(), key=lambda kv: kv[1], reverse=True)
sorted_pid_scores_dict = {}
for i in range(len(sorted_pid_scores)):
sorted_pid_scores_dict[sorted_pid_scores[i][0]] = [sorted_pid_scores[i][1]]
return sorted_pid_scores_dict
def tokenise_collection(collection_file, collection_output_file):
doc_dict = {}
with open(collection_output_file, 'w') as fw:
with open(collection_file) as f:
for line in tqdm(f):
datalist = json.loads(line)
id = datalist["pmid"]
title = datalist["title"]
abstract = datalist["abstract"]
for i in string_set:
title = title.replace(i, '')
for i in string_set:
abstract = abstract.replace(i, '')
title_tokenized = [tok for tok in title.split() if tok.lower() not in cachedStopwords]
abstract_tokenized = [tok for tok in abstract.split() if tok.lower() not in cachedStopwords]
fw.write(json.dumps({"pmid": id, "title": title_tokenized, "abstract": abstract_tokenized})+"\n")
doc_dict[id] = [title_tokenized, abstract_tokenized]
return doc_dict
def get_collection(collection_file):
doc_dict = {}
with open(collection_file) as f:
for line in tqdm(f):
datalist = json.loads(line)
id = datalist["pmid"]
title = datalist["title"]
abstract = datalist["abstract"]
doc_dict[id] = [title, abstract]
return doc_dict
def build_corpus_model(overall_dic):
df = {}
corpus_size = 0
for key in overall_dic:
list_tokens = overall_dic[key]
for word in list_tokens:
if word not in df:
df[word] = 0
df[word] += 1
corpus_size += 1
return df, corpus_size
def build_bm25_model(result_dic):
corpus = []
pids = []
for key in result_dic:
pids.append(key)
list_tokens = result_dic[key]
corpus.append(list_tokens)
#print(corpus)
model = bm25.BM25(corpus)
average_idf = sum(float(val) for val in model.idf.values()) / len(model.idf)
return pids, model, average_idf
def build_qlm_model(result_dic, query_dic):
corpus = []
pids = []
for key in result_dic:
pids.append(key)
list_tokens = result_dic[key]
corpus.append(list_tokens)
model = QLM(corpus, query_dic)
return pids, model
def bm25_rerank_results(query_dict, passage_dict, map_q_p, out_folder, fuse_type):
for qid in tqdm(query_dict):
map_list = map_q_p[qid]
output_file = os.path.join(out_folder, qid + '.trec')
output = open(output_file, 'w')
result_dict = {}
for pid in map_list:
if pid in passage_dict:
result_dict[pid] = passage_dict[pid]
#print(len(result_dict))
queries = query_dict[qid]
pid_scores_dict = {}
for query in queries:
pids, model, average_idf = build_bm25_model(result_dict)
scores = model.get_scores(query, average_idf)
indices = sorted(range(len(scores)), key=lambda k: scores[k])[::-1]
diff = max(scores)-min(scores)
min_s = min(scores)
for i in indices:
if pids[i] not in pid_scores_dict:
pid_scores_dict[pids[i]] = []
if diff==0:
pid_scores_dict[pids[i]].append(1)
else:
pid_scores_dict[pids[i]].append((scores[i]-min_s)/diff)
if len(queries)>1:
if fuse_type == 'COMBSUM':
pid_scores_dict = COMBSUM(pid_scores_dict)
elif fuse_type == 'COMBMNZ':
pid_scores_dict = COMBMNZ(pid_scores_dict)
elif fuse_type == 'BORDA':
pid_scores_dict = BORDA(pid_scores_dict)
order_index = 1
write_lines = []
for pid_ordered in pid_scores_dict:
write_lines.append(qid + ' 0 ' + pid_ordered + ' ' + str(order_index) + ' ' + str(pid_scores_dict[pid_ordered][0]) + ' BM25\n')
order_index = order_index + 1
output.writelines(write_lines)
def qlm_rerank_results(query_dict, passage_dict, map_q_p, out_folder, fuse_type):
df_dic, corpus_size = build_corpus_model(passage_dict)
for qid in tqdm(query_dict):
map_list = map_q_p[qid]
output_file = os.path.join(out_folder, qid + '.trec')
output = open(output_file, 'w')
result_dict = {}
for pid in map_list:
if pid in passage_dict:
result_dict[pid] = passage_dict[pid]
queries = query_dict[qid]
pid_scores_dict = {}
for query in queries:
query_dictionary = {}
for word in query:
if word not in query_dictionary:
query_dictionary[word] = 0
query_dictionary[word] += 1
pids, model = build_qlm_model(result_dict, query_dictionary)
scores = model.get_scores(query_dictionary, df_dic, corpus_size)
indices = sorted(range(len(scores)), key=lambda k: scores[k])[::-1]
diff = max(scores) - min(scores)
min_s = min(scores)
for i in indices:
if pids[i] not in pid_scores_dict:
pid_scores_dict[pids[i]] = []
if diff == 0:
pid_scores_dict[pids[i]].append(1)
else:
pid_scores_dict[pids[i]].append((scores[i] - min_s) / diff)
if len(queries) > 1:
if fuse_type == 'COMBSUM':
pid_scores_dict = COMBSUM(pid_scores_dict)
elif fuse_type == 'COMBMNZ':
pid_scores_dict = COMBMNZ(pid_scores_dict)
elif fuse_type == 'BORDA':
pid_scores_dict = BORDA(pid_scores_dict)
order_index = 1
write_lines = []
for pid_ordered in pid_scores_dict:
write_lines.append(qid + ' 0 ' + pid_ordered + ' ' + str(order_index) + ' ' + str(
pid_scores_dict[pid_ordered][0]) + ' BM25\n')
order_index = order_index + 1
output.writelines(write_lines)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--DATA_DIR", type=str, default="data/sysrev-seed-collection")
parser.add_argument("--collection_file", type=str, default="data/sysrev-seed-collection/all.jsonl")
parser.add_argument("--METHOD", type=str, required=True)
parser.add_argument("--type", type=str, default='title', help="title or openai or alpaca or biogpt")
parser.add_argument("--fuse_type", type=str, default='COMBSUM', help="COMBSUM or COMNMNZ or BORDA")
parser.add_argument("--OUT_DIR", type=str, default="output")
args = parser.parse_args()
DATA_DIR = args.DATA_DIR
method = args.METHOD
type = args.type
if args.type=="seed_fuse":
out_folder = os.path.join(args.OUT_DIR, "/".join(args.DATA_DIR.split("/")), type, method, args.fuse_type)
else:
out_folder = os.path.join(args.OUT_DIR, "/".join(args.DATA_DIR.split("/")), type + "_" + method, "model_bio_bert")
if not os.path.exists(out_folder):
os.makedirs(out_folder)
run_file = os.path.join(DATA_DIR, "preprocessed_input_abstract.jsonl")
if not ((args.type=="title") or (args.type=="boolean") or (args.type=="search_name")):
prompt_file = os.path.join(DATA_DIR, f"generated_from_{type}.jsonl")
prompt_dict = {}
with open(prompt_file) as f:
for line in f:
data_dict = json.loads(line)
prompt_dict[data_dict['id']] = data_dict['generated_query']
desired_collection_file = "/".join(args.collection_file.split("/")[:2]) + "/tokenised_collection.jsonl"
if os.path.exists(desired_collection_file):
collection_dict = get_collection(desired_collection_file)
else:
collection_dict = tokenise_collection(args.collection_file, desired_collection_file)
query_dict = {}
passage_dict = {}
map_q_p = {}
doc_set = set()
for pid in collection_dict:
passage_dict[pid] = collection_dict[pid][0] + collection_dict[pid][1]
with open(run_file) as f:
for line in f:
data_dict = json.loads(line)
qid = data_dict['id']
if type == "boolean":
title = re.sub(r'\[.*?\]', '', data_dict['query'])
elif type=="title":
title = data_dict['title']
elif type=="search_name":
title = data_dict['search_name']
else:
title = prompt_dict[qid]
if "seed_studies" not in data_dict:
data_dict['seed_studies'] = []
seed_studies = data_dict['seed_studies']
searched_docs = data_dict['search_docs']
for i in string_set:
title = title.replace(i, '')
title_tokenized = [tok for tok in title.split() if tok.lower() not in cachedStopwords]
if type == 'title':
query_dict[qid] = [title_tokenized]
elif (type == 'boolean') or (type == 'search_name'):
query_dict[qid] = [title_tokenized]
elif type == 'title_seed':
query_dict[qid] = [title_tokenized]
for i in seed_studies:
seed_study = collection_dict[i.strip()][0]
query_dict[qid][0] += seed_study
elif type == 'seed_fuse':
query_dict[qid] = [title_tokenized]
for i in seed_studies:
seed_study = collection_dict[i.strip()][0]
query_dict[qid].append(seed_study)
else:
query_dict[qid] = [title_tokenized]
for i in searched_docs:
doc_set.add(i)
map_q_p[qid] = searched_docs
not_in_collection = []
in_collection = []
for i in doc_set:
if i not in passage_dict:
not_in_collection.append(i)
else:
in_collection.append(i)
print(len(collection_dict))
print("In collection: " + str(len(in_collection)))
print("Not in collection: " + str(len(not_in_collection)))
print(not_in_collection)
print(run_file)
print("Start processing " + method)
print(query_dict)
if method=="BM25":
bm25_rerank_results(query_dict, passage_dict, map_q_p, out_folder, fuse_type=args.fuse_type)
if method=="QLM":
qlm_rerank_results(query_dict, passage_dict, map_q_p, out_folder, args.fuse_type)