|
| 1 | +import models.base as base_module |
| 2 | +import models.T5Rewriter as t5_rewriter |
| 3 | + |
| 4 | +from typing import List, Tuple |
| 5 | + |
| 6 | +import pandas as pd |
| 7 | +import pyterrier as pt |
| 8 | +from pyterrier_t5 import MonoT5ReRanker, DuoT5ReRanker |
| 9 | + |
| 10 | + |
| 11 | +class BaselinePRF(base_module.Pipeline): |
| 12 | + """ |
| 13 | + A class to represent the method that extends the baseline retrieval method with pseudo relevance feedback. |
| 14 | + """ |
| 15 | + |
| 16 | + t5_qr: t5_rewriter.T5Rewriter |
| 17 | + top_docs: Tuple[pt.Transformer, int] |
| 18 | + mono_t5: Tuple[MonoT5ReRanker, int] |
| 19 | + duo_t5: Tuple[DuoT5ReRanker, int] |
| 20 | + |
| 21 | + def __init__( |
| 22 | + self, |
| 23 | + index, |
| 24 | + *, |
| 25 | + bm25_docs: int, |
| 26 | + rm3_fb_docs: int, |
| 27 | + rm3_fb_terms: int, |
| 28 | + mono_t5_docs: int, |
| 29 | + duo_t5_docs: int, |
| 30 | + ): |
| 31 | + """ |
| 32 | + Constructs all the necessary attributes for the baseline retrieval method. |
| 33 | +
|
| 34 | + Parameters |
| 35 | + ---------- |
| 36 | + index : pt.Index |
| 37 | + The PyTerrier index. |
| 38 | + bm25_docs : int |
| 39 | + The number of documents to retrieve with BM25. |
| 40 | + rm3_fb_docs : int |
| 41 | + The number of documents to use for RM3. |
| 42 | + rm3_fb_terms : int |
| 43 | + The number of terms to use for RM3. |
| 44 | + mono_t5_docs : int |
| 45 | + The number of documents to retrieve with MonoT5. |
| 46 | + duo_t5_docs : int |
| 47 | + The number of documents to retrieve with DuoT5. |
| 48 | + """ |
| 49 | + self.t5_qr = t5_rewriter.T5Rewriter() |
| 50 | + bm25 = pt.BatchRetrieve(index, wmodel="BM25", metadata=["docno", "text"]) |
| 51 | + rm3 = pt.rewrite.RM3(index, fb_docs=rm3_fb_docs, fb_terms=rm3_fb_terms) |
| 52 | + self.top_docs = ((bm25 % rm3_fb_docs) >> rm3 >> bm25, bm25_docs) |
| 53 | + self.mono_t5 = (MonoT5ReRanker(), mono_t5_docs) |
| 54 | + self.duo_t5 = (DuoT5ReRanker(), duo_t5_docs) |
| 55 | + |
| 56 | + def transform_input( |
| 57 | + self, query: base_module.Query, context: base_module.Context |
| 58 | + ) -> str: |
| 59 | + history = [] |
| 60 | + for q, _ in context: |
| 61 | + history.append(q.query) |
| 62 | + if len(context) > 0: |
| 63 | + last_docs = context[-1][1] |
| 64 | + if last_docs is not None: |
| 65 | + history.append(last_docs[0].content) |
| 66 | + history.append(query.query) |
| 67 | + new_query = " <sep> ".join(history) |
| 68 | + return new_query |
| 69 | + |
| 70 | + def transform(self, query_df: pd.DataFrame) -> pd.DataFrame: |
| 71 | + rewritten_queries_df = self.t5_qr.transform(query_df) |
| 72 | + |
| 73 | + top_docs_df = self.top_docs[0].transform(rewritten_queries_df.copy()) |
| 74 | + top_docs_df = ( |
| 75 | + top_docs_df.sort_values(["qid", "score"], ascending=False) |
| 76 | + .groupby("qid") |
| 77 | + .head(self.top_docs[1]) |
| 78 | + ) |
| 79 | + |
| 80 | + # Now add in the rewritten queries to the top docs |
| 81 | + top_docs_df = top_docs_df.merge(rewritten_queries_df, on="qid", how="left") |
| 82 | + # And overwrite the "query" column again |
| 83 | + top_docs_df["query"] = top_docs_df[t5_rewriter.COPY_REWRITTEN_QUERY_COLUMN] |
| 84 | + |
| 85 | + mono_t5_df = self.mono_t5[0].transform( |
| 86 | + top_docs_df.groupby("qid").head(self.mono_t5[1]) |
| 87 | + ) |
| 88 | + mono_t5_df = ( |
| 89 | + mono_t5_df.sort_values(["qid", "score"], ascending=False) |
| 90 | + .groupby("qid") |
| 91 | + .head(self.mono_t5[1]) |
| 92 | + ) |
| 93 | + |
| 94 | + duo_t5_df = self.duo_t5[0].transform( |
| 95 | + mono_t5_df.groupby("qid").head(self.duo_t5[1]) |
| 96 | + ) |
| 97 | + duo_t5_df = ( |
| 98 | + duo_t5_df.sort_values(["qid", "score"], ascending=False) |
| 99 | + .groupby("qid") |
| 100 | + .head(self.duo_t5[1]) |
| 101 | + ) |
| 102 | + |
| 103 | + return self.combine_result_stages([top_docs_df, mono_t5_df, duo_t5_df]) |
0 commit comments