from typing import List, Dict class CustomPllLabel: def __init__( self ) -> None: self.html_head = """ """ self.html_footer ="" def __progressbar( self, percentage: int, sent: str, ratio: float, score: float, size: int=15 ) -> str: html = f"""
x{round(ratio,3)}

{sent}

""" return html def __render( self, sents: List[str], scores: List[float], ratios: List[float] ) -> str: max_ratio = max(ratios) ratio2percentage = lambda ratio: int(ratio*100/max_ratio) html = "" for sent, ratio, score in zip(sents, ratios, scores): html += self.__progressbar( percentage=ratio2percentage(ratio), sent=sent, ratio=ratio, score=score ) return self.html_head + html + self.html_footer def __getProportions( self, scores: List[float], ) -> List[float]: min_score = min(scores) return [min_score/s for s in scores] def compute( self, pll_dict: Dict[str, float] ) -> str: sorted_pll_dict = dict(sorted(pll_dict.items(), key=lambda x: x[1], reverse=True)) sents = list(sorted_pll_dict.keys()) # Scape < and > marks from hightlight word/s sents = [s.replace("<","<").replace(">",">")for s in sents] scores = list(sorted_pll_dict.values()) ratios = self.__getProportions(scores) return self.__render(sents, scores, ratios)