|
import json |
|
|
|
import pandas as pd |
|
from Bio import Entrez |
|
from retry import retry |
|
from tqdm import tqdm |
|
|
|
|
|
Entrez.email = "***" |
|
Entrez.api_key = "***" |
|
|
|
|
|
|
|
RAW_EVALUATION_DATASET = "training11b.json" |
|
PATH_TO_PASSAGE_DATASET = "./passages.parquet" |
|
PATH_TO_EVALUATION_DATASET = "./eval.parquet" |
|
|
|
|
|
|
|
MAX_PASSAGES = None |
|
|
|
|
|
@retry() |
|
def get_abstract(passage_id): |
|
with Entrez.efetch( |
|
db="pubmed", id=passage_id, rettype="abstract", retmode="text" |
|
) as response: |
|
|
|
r = response.read() |
|
r = r.split("\n\n") |
|
abstract = max(r, key=len) |
|
return abstract |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
|
with open(RAW_EVALUATION_DATASET) as f: |
|
eval_data = json.load(f)["questions"] |
|
|
|
eval_df = pd.DataFrame(eval_data, columns=["body", "documents", "ideal_answer"]) |
|
eval_df = eval_df.rename( |
|
columns={ |
|
"body": "question", |
|
"documents": "relevant_passages", |
|
"ideal_answer": "answer", |
|
} |
|
) |
|
eval_df.answer = eval_df.answer.apply(lambda x: x[0]) |
|
|
|
eval_df.relevant_passages = eval_df.relevant_passages.apply( |
|
lambda x: [url.split("/")[-1] for url in x] |
|
) |
|
if MAX_PASSAGES: |
|
eval_df["passage_count"] = eval_df.relevant_passages.apply(lambda x: len(x)) |
|
eval_df = eval_df.drop(columns=["passage_count"]) |
|
|
|
|
|
eval_df.relevant_passages = eval_df.relevant_passages.apply(lambda x: set(x)) |
|
eval_df.relevant_passages = eval_df.relevant_passages.apply(lambda x: list(x)) |
|
|
|
|
|
passage_ids = set().union(*eval_df.relevant_passages) |
|
passage_ids = list(passage_ids) |
|
passages = pd.DataFrame(index=passage_ids) |
|
|
|
for i, passage_id in enumerate(tqdm(passages.index)): |
|
passages.loc[passage_id, "passage"] = get_abstract(passage_id) |
|
|
|
|
|
if i % 4000 == 0: |
|
passages.to_parquet(PATH_TO_PASSAGE_DATASET) |
|
|
|
|
|
unavailable_passages = passages[passages["passage"] == "1. "] |
|
passages = passages[passages["passage"] != "1. "] |
|
passages.to_parquet(PATH_TO_PASSAGE_DATASET) |
|
|
|
|
|
unavailable_ids = unavailable_passages.index.tolist() |
|
eval_df["relevant_passages"] = eval_df["relevant_passages"].apply( |
|
lambda x: [i for i in x if i not in unavailable_ids] |
|
) |
|
eval_df.to_parquet(PATH_TO_EVALUATION_DATASET) |
|
|