-
Notifications
You must be signed in to change notification settings - Fork 48
/
bm25_create_pairs.py
72 lines (59 loc) · 2.81 KB
/
bm25_create_pairs.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
import os
import json
import pickle
import numpy as np
from tqdm import tqdm
from rank_bm25 import *
from utils import bm25_tokenizer, load_json
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--top_pair", default=20, type=int)
parser.add_argument("--model_path", default="saved_model/bm25_Plus_04_06_model_full_manual_stopword", type=str)
parser.add_argument("--data_path", default="zac2021-ltr-data", type=str, help="path to input data")
parser.add_argument("--save_pair_path", default="pair_data/", type=str, help="path to save pair sentence directory")
args = parser.parse_args()
train_path = os.path.join(args.data_path, "train_question_answer.json")
training_items = load_json(train_path)["items"]
with open(args.model_path, "rb") as bm_file:
bm25 = pickle.load(bm_file)
with open("saved_model/doc_refers_saved", "rb") as doc_refer_file:
doc_refers = pickle.load(doc_refer_file)
doc_data = json.load(open(os.path.join("generated_data", "legal_dict.json")))
save_pairs = []
top_n = args.top_pair
for idx, item in tqdm(enumerate(training_items)):
question_id = item["question_id"]
question = item["question"]
relevant_articles = item["relevant_articles"]
actual_positive = len(relevant_articles)
tokenized_query = bm25_tokenizer(question)
doc_scores = bm25.get_scores(tokenized_query)
predictions = np.argpartition(doc_scores, len(doc_scores) - top_n)[-top_n:]
# Save positive pairs
for article in relevant_articles:
save_dict = {}
save_dict["question"] = question
concat_id = article["law_id"] + "_" + article["article_id"]
save_dict["document"] = doc_data[concat_id]["title"] + " " + doc_data[concat_id]["text"]
save_dict["relevant"] = 1
save_pairs.append(save_dict)
# Save negative pairs
for idx, idx_pred in enumerate(predictions):
pred = doc_refers[idx_pred]
check = 0
for article in relevant_articles:
if pred[0] == article["law_id"] and pred[1] == article["article_id"]:
check += 1
if check == 0:
save_dict = {}
save_dict["question"] = question
concat_id = pred[0] + "_" + pred[1]
save_dict["document"] = doc_data[concat_id]["title"] + " " + doc_data[concat_id]["text"]
save_dict["relevant"] = 0
save_pairs.append(save_dict)
save_path = args.save_pair_path
os.makedirs(save_path, exist_ok=True)
with open(os.path.join(save_path, f"bm_25_pairs_top{top_n}"), "wb") as pair_file:
pickle.dump(save_pairs, pair_file)
print(len(save_pairs))