debeir.rankers.reranking.reranker
General re-ranking interfaces to be implemented by child classes.
1""" 2General re-ranking interfaces to be implemented by child classes. 3""" 4 5import abc 6from typing import AnyStr, List, Union 7 8from debeir.core.document import Document 9 10 11class ReRanker: 12 """ 13 General interface for a reranking. 14 15 Child classes should implement the abstract methods. 16 17 """ 18 ranked_list: List 19 20 def __init__(self, query, ranked_list: List, *args, **kwargs): 21 self.ranked_list = ranked_list 22 self.query = query 23 24 @classmethod 25 @abc.abstractmethod 26 def _compute_scores(cls, document_repr): 27 pass 28 29 @classmethod 30 @abc.abstractmethod 31 def _get_document_representation(cls, document) -> (AnyStr, AnyStr): 32 pass 33 34 def rerank(self) -> List: 35 """ 36 Re-rank the passed ranked list based on implemented private _compute_scores method. 37 38 :param ranked_list: 39 :return: 40 A ranked list in descending order of the score field (which will be the last item in the list) 41 """ 42 ranking = [] 43 44 for document in self.ranked_list: 45 doc_id, doc_repr = self._get_document_representation(document) 46 score = self._compute_scores(doc_repr) 47 48 ranking.append([doc_id, doc_repr, score]) 49 50 ranking.sort(key=lambda k: k[-1], reverse=True) 51 52 return ranking 53 54 55class DocumentReRanker(ReRanker): 56 """ 57 Reranking interface for a ranked list of Document objects. 58 """ 59 60 def __init__(self, query, ranked_list: List[Document], *args, **kwargs): 61 super().__init__(query, ranked_list, *args, **kwargs) 62 63 @abc.abstractmethod 64 def _compute_scores(self, document_repr): 65 pass 66 67 @classmethod 68 def _get_document_representation(cls, document: Document) -> (Union[int, str, float], Document): 69 return document.doc_id, document 70 71 72class ReRankerPool: 73 # Reranks per topic using threads. 74 pass
class
ReRanker:
12class ReRanker: 13 """ 14 General interface for a reranking. 15 16 Child classes should implement the abstract methods. 17 18 """ 19 ranked_list: List 20 21 def __init__(self, query, ranked_list: List, *args, **kwargs): 22 self.ranked_list = ranked_list 23 self.query = query 24 25 @classmethod 26 @abc.abstractmethod 27 def _compute_scores(cls, document_repr): 28 pass 29 30 @classmethod 31 @abc.abstractmethod 32 def _get_document_representation(cls, document) -> (AnyStr, AnyStr): 33 pass 34 35 def rerank(self) -> List: 36 """ 37 Re-rank the passed ranked list based on implemented private _compute_scores method. 38 39 :param ranked_list: 40 :return: 41 A ranked list in descending order of the score field (which will be the last item in the list) 42 """ 43 ranking = [] 44 45 for document in self.ranked_list: 46 doc_id, doc_repr = self._get_document_representation(document) 47 score = self._compute_scores(doc_repr) 48 49 ranking.append([doc_id, doc_repr, score]) 50 51 ranking.sort(key=lambda k: k[-1], reverse=True) 52 53 return ranking
General interface for a reranking.
Child classes should implement the abstract methods.
def
rerank(self) -> List:
35 def rerank(self) -> List: 36 """ 37 Re-rank the passed ranked list based on implemented private _compute_scores method. 38 39 :param ranked_list: 40 :return: 41 A ranked list in descending order of the score field (which will be the last item in the list) 42 """ 43 ranking = [] 44 45 for document in self.ranked_list: 46 doc_id, doc_repr = self._get_document_representation(document) 47 score = self._compute_scores(doc_repr) 48 49 ranking.append([doc_id, doc_repr, score]) 50 51 ranking.sort(key=lambda k: k[-1], reverse=True) 52 53 return ranking
Re-rank the passed ranked list based on implemented private _compute_scores method.
Parameters
- ranked_list:
Returns
A ranked list in descending order of the score field (which will be the last item in the list)
56class DocumentReRanker(ReRanker): 57 """ 58 Reranking interface for a ranked list of Document objects. 59 """ 60 61 def __init__(self, query, ranked_list: List[Document], *args, **kwargs): 62 super().__init__(query, ranked_list, *args, **kwargs) 63 64 @abc.abstractmethod 65 def _compute_scores(self, document_repr): 66 pass 67 68 @classmethod 69 def _get_document_representation(cls, document: Document) -> (Union[int, str, float], Document): 70 return document.doc_id, document
Reranking interface for a ranked list of Document objects.
DocumentReRanker( query, ranked_list: List[debeir.core.document.Document], *args, **kwargs)
class
ReRankerPool: