-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_bm25_negatives.py
189 lines (159 loc) · 6.23 KB
/
build_bm25_negatives.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
import os
import json
import argparse
from tqdm import tqdm
from utils.indexing_utils import SparseIndexer, DocumentCollection
from utils import get_logger
logger = get_logger(__name__)
def construct_reverse_passage_mapper(collection, logging_step=10000):
reverse_mapper = {}
all_length = len(collection)
logger.info(f"Constructing reverse pid mapper fisrt! Total number of passages: {all_length}")
for idx in range(all_length):
pid = collection.get_pid(idx)
reverse_mapper[pid] = idx
if idx % logging_step == 0:
logger.info(f"{idx}/{all_length} ...")
return reverse_mapper
def read_orconvqa_data(dataset, read_by="all_questions", is_test=False):
examples = []
for idx, data in tqdm(enumerate(dataset)):
context = []
for idx, q in enumerate(data["history"]):
text = q["question"]
context.append(text)
guid = data["qid"]
target_question = data["question"]
truth_answer = ""
if read_by == "all_questions":
x = context + [target_question]
x = " ".join(x)
elif read_by == "all_questions_without_this":
x = " ".join(context)
elif read_by == "original":
x = target_question
else:
raise Exception("Unsupported option!")
examples.append([guid, x])
if is_test:
logger.info(f"{guid}: {x}")
if len(examples) == 10:
break
return examples
def read_qrecc_data(dataset, read_by="all", is_test=False):
context_map = {}
prev_id = None
examples = []
for data in tqdm(dataset):
guid = f"{data['Conversation_no']}_{data['Turn_no']}"
did = data["Conversation_no"]
context = context_map.get(did, [])
assert len(context) % 2 == 0
if not context:
context_map[did] = []
target_question = data["Question"]
if read_by == "all":
x = context + [target_question]
x = " ".join(x)
elif read_by == "all_without_this":
x = context
x = " ".join(x)
elif read_by == "rewrite":
x = data["Truth_rewrite"]
elif read_by == "original":
x = data["Question"]
elif read_by == "previous_answer":
if context:
pa = context[-1]
else:
pa = ""
x = [pa, data["Question"]]
x = " ".join(x)
elif read_by == "previous_answer_only":
if context:
pa = context[-1]
else:
pa = ""
x = pa
elif read_by == "this_answer":
x = [data["Question"], data["Truth_answer"]]
x = " ".join(x)
elif read_by == "this_answer_only":
x = data["Truth_answer"]
else:
raise Exception("Unsupported option!")
examples.append([guid, x])
context_map[did].append(data["Question"])
context_map[did].append(data["Truth_answer"])
if is_test:
logger.info(f"{guid}: {x}")
if len(examples) == 10:
break
return examples
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--task', type=str, default=None)
parser.add_argument('--split', type=str, default="train")
parser.add_argument('--read_by', type=str, default="all")
parser.add_argument('--raw_data_path', type=str, default=None)
parser.add_argument('--preprocessed_data_path', type=str, default=None)
parser.add_argument('--pyserini_index_path', type=str, default=None)
parser.add_argument('--top_k', type=int, default=100)
args = parser.parse_args()
if "qrecc" in args.task:
k_1 = 0.82
b = 0.68
else:
k_1 = 0.9
b = 0.4
indexer = SparseIndexer(args.pyserini_index_path)
indexer.set_retriever(k_1, b)
if args.task == "orconvqa":
data = []
for line in open(f"{args.raw_data_path}/{args.task}/{args.split}.json", "r", encoding="utf-8"):
data.append(json.loads(line))
raw_examples = read_orconvqa_data(data, args.read_by)
else:
data = json.load(open(f"{args.raw_data_path}/{args.task}/{args.split}.json", "r", encoding="utf-8"))
raw_examples = read_qrecc_data(data, args.read_by)
if not os.path.exists(f"{args.preprocessed_data_path}/{args.task}/reverse_pids.json"):
collection = DocumentCollection(f"{args.preprocessed_data_path}/{args.task}/test_collections/data.h5")
revserse_pids = construct_reverse_passage_mapper(collection)
json.dump(reverse_pids, open(f"{args.preprocessed_data_path}/{args.task}/reverse_pids.json", "w", encoding="utf-8"))
else:
reverse_pids = json.load(open(f"{args.preprocessed_data_path}/{args.task}/reverse_pids.json", "r", encoding="utf-8"))
scores = {}
indices = {}
for idx, line in enumerate(raw_examples):
qid, q = line
if not q:
scores[qid] = {}
continue
retrieved_passages = indexer.retrieve(q, args.top_k)
score = {}
index = []
for passage in retrieved_passages:
score[passage["id"]] = passage["score"]
index.append(reverse_pids[passage["id"]])
scores[qid] = score
indices[qid] = index
logger.info(f"{idx}/{len(raw_examples)}")
json.dump(
scores,
open(os.path.join(args.preprocessed_data_path, f"{args.split}_bm25_scores.json"), "w"),
indent=4
)
json.dump(
indices,
open(os.path.join(args.preprocessed_data_path, f"{args.split}_bm25_indices.json"), "w"),
indent=4
)
examples = load_processed_data(f"{args.preprocessed_data_path}/{args.task}/{args.split}.json")
logger.info(f"Hard Negatives Mining is done")
with open(os.path.join(args.preprocessed_data_path, f"{args.split}_bm25_negs.json"), "w") as f:
for example in examples:
negative_ids = indices[example.guid]
negative_scores = scores[example.guid]
example.hard_negative_ids = negative_ids
example.hard_negative_scores = negative_scores
f.write(json.dumps(example.to_dict()) + "\n")