forked from sunnweiwei/RankGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
download_dataset.py
149 lines (134 loc) · 5.85 KB
/
download_dataset.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
THE_INDEX = {
"dl19": "msmarco-v1-passage",
"dl20": "msmarco-v1-passage",
"covid": "beir-v1.0.0-trec-covid.flat",
"arguana": "beir-v1.0.0-arguana.flat",
"touche": "beir-v1.0.0-webis-touche2020.flat",
"news": "beir-v1.0.0-trec-news.flat",
"scifact": "beir-v1.0.0-scifact.flat",
"fiqa": "beir-v1.0.0-fiqa.flat",
"scidocs": "beir-v1.0.0-scidocs.flat",
"nfc": "beir-v1.0.0-nfcorpus.flat",
"quora": "beir-v1.0.0-quora.flat",
"dbpedia": "beir-v1.0.0-dbpedia-entity.flat",
"fever": "beir-v1.0.0-fever-flat",
"robust04": "beir-v1.0.0-robust04.flat",
"signal": "beir-v1.0.0-signal1m.flat",
"mrtydi-ar": "mrtydi-v1.1-arabic",
"mrtydi-bn": "mrtydi-v1.1-bengali",
"mrtydi-fi": "mrtydi-v1.1-finnish",
"mrtydi-id": "mrtydi-v1.1-indonesian",
"mrtydi-ja": "mrtydi-v1.1-japanese",
"mrtydi-ko": "mrtydi-v1.1-korean",
"mrtydi-ru": "mrtydi-v1.1-russian",
"mrtydi-sw": "mrtydi-v1.1-swahili",
"mrtydi-te": "mrtydi-v1.1-telugu",
"mrtydi-th": "mrtydi-v1.1-thai",
}
THE_TOPICS = {
"dl19": "dl19-passage",
"dl20": "dl20-passage",
"covid": "beir-v1.0.0-trec-covid-test",
"arguana": "beir-v1.0.0-arguana-test",
"touche": "beir-v1.0.0-webis-touche2020-test",
"news": "beir-v1.0.0-trec-news-test",
"scifact": "beir-v1.0.0-scifact-test",
"fiqa": "beir-v1.0.0-fiqa-test",
"scidocs": "beir-v1.0.0-scidocs-test",
"nfc": "beir-v1.0.0-nfcorpus-test",
"quora": "beir-v1.0.0-quora-test",
"dbpedia": "beir-v1.0.0-dbpedia-entity-test",
"fever": "beir-v1.0.0-fever-test",
"robust04": "beir-v1.0.0-robust04-test",
"signal": "beir-v1.0.0-signal1m-test",
"mrtydi-ar": "mrtydi-v1.1-arabic-test",
"mrtydi-bn": "mrtydi-v1.1-bengali-test",
"mrtydi-fi": "mrtydi-v1.1-finnish-test",
"mrtydi-id": "mrtydi-v1.1-indonesian-test",
"mrtydi-ja": "mrtydi-v1.1-japanese-test",
"mrtydi-ko": "mrtydi-v1.1-korean-test",
"mrtydi-ru": "mrtydi-v1.1-russian-test",
"mrtydi-sw": "mrtydi-v1.1-swahili-test",
"mrtydi-te": "mrtydi-v1.1-telugu-test",
"mrtydi-th": "mrtydi-v1.1-thai-test",
}
import pandas as pd
from rank_gpt import run_retriever, sliding_windows, write_eval_file
from pyserini.search import LuceneSearcher, get_topics, get_qrels
from tqdm import tqdm
import tempfile
import os
import json
import shutil
from pathlib import Path
openai_key = os.environ.get("OPENAI_API_KEY", None)
# for data in ['dl19', 'dl20', 'covid', 'nfc', 'touche', 'dbpedia', 'scifact', 'signal', 'news', 'robust04']:
for data in ["signal", "news"]:
print("#" * 20)
print(f"Evaluation on {data}")
print("#" * 20)
# Retrieve passages using pyserini BM25.
# Get a specific doc:
# * searcher.num_docs
# * json.loads(searcher.object.reader.document(4).fields[1].fieldsData) -> {"id": "1", "contents": ""}
searcher = LuceneSearcher.from_prebuilt_index(THE_INDEX[data])
topics = get_topics(THE_TOPICS[data] if data != "dl20" else "dl20")
qrels = get_qrels(THE_TOPICS[data])
# Create a folder for the dataset
data_folder = Path(__file__).parent / "data" / data
data_folder.mkdir(exist_ok=True, parents=True)
# Store JSON in rank_results to a file
with open(data_folder / "queries.jsonl", "w") as f:
for key, value in topics.items():
f.write(
json.dumps({"_id": str(key), "text": value["title"]}, ensure_ascii=False) + "\n"
)
# Store the QRELS of the dataset
(data_folder / "qrels").mkdir(exist_ok=True, parents=True)
with open(data_folder / "qrels" / "test.tsv", "w") as f:
qrels_list = []
for query, value in qrels.items():
for doc, rel in value.items():
qrels_list += [[query, doc, rel]]
qrels_data = pd.DataFrame(
qrels_list, columns=["query-id", "corpus-id", "score"]
)
qrels_data.to_csv(f, sep="\t", index=False, header=True)
# Retrieve all the documents in the corpus
with open(data_folder / "corpus.jsonl", "w") as f:
for i in tqdm(range(searcher.num_docs)):
doc = json.loads(searcher.object.reader.document(i).fields[1].fieldsData)
doc["_id"] = str(doc["_id"])
f.write(json.dumps(doc, ensure_ascii=False) + "\n")
# for data in ['mrtydi-ar', 'mrtydi-bn', 'mrtydi-fi', 'mrtydi-id', 'mrtydi-ja', 'mrtydi-ko', 'mrtydi-ru', 'mrtydi-sw', 'mrtydi-te', 'mrtydi-th']:
# print('#' * 20)
# print(f'Evaluation on {data}')
# print('#' * 20)
# # Retrieve passages using pyserini BM25.
# try:
# searcher = LuceneSearcher.from_prebuilt_index(THE_INDEX[data])
# topics = get_topics(THE_TOPICS[data] if data != 'dl20' else 'dl20')
# qrels = get_qrels(THE_TOPICS[data])
# rank_results = run_retriever(topics, searcher, qrels, k=100)
# rank_results = rank_results[:100]
# # Store JSON in rank_results to a file
# with open(f'rank_results_{data}.json', 'w') as f:
# json.dump(rank_results, f, indent=2)
# # Store the QRELS of the dataset
# with open(f'qrels_{data}.json', 'w') as f:
# json.dump(qrels, f, indent=2)
# except:
# print(f'Failed to retrieve passages for {data}')
# # # Run sliding window permutation generation
# # new_results = []
# # for item in tqdm(rank_results):
# # new_item = sliding_windows(item, rank_start=0, rank_end=100, window_size=20, step=10,
# # model_name='gpt-3.5-turbo', openai_key=openai_key)
# # new_results.append(new_item)
# # # Evaluate nDCG@10
# # from trec_eval import EvalFunction
# # temp_file = tempfile.NamedTemporaryFile(delete=False).name
# # write_eval_file(new_results, temp_file)
# # EvalFunction.eval(['-c', '-m', 'ndcg_cut.10', THE_TOPICS[data], temp_file])
# # # Rename the output file to a better name
# # shutil.move(output_file, f'eval_{data}.txt')