from src.utils.segregate import Segregator from src.dto.dto import ExplanationDto UPPER_COLOR = "#D4EFDF" # green MID_COLOR = "#FBFBB8BF" # amber LOW_COLOR = "black" class Visualizer: def __init__(self, show_mid_features: bool = True, show_low_features: bool = True): self.__show_mid_features = show_mid_features self.__show_low_features = show_low_features def visualize( self, segregator: Segregator, explanations: ExplanationDto, output_from_explanations: str, avoid_exp_label: bool = False, ) -> str: highlighted_text = output_from_explanations pos_features, mid_features, low_features = segregator.segregate( explanations=explanations ) if not self.__show_mid_features: mid_features = [] if not self.__show_low_features: low_features = [] for explanation in explanations.explanations: score = round(explanation.score, 2) if explanation.feature in pos_features: token_str = ( '' + explanation.feature + "" ) elif explanation.feature in mid_features: token_str = ( '' + explanation.feature + "" ) else: token_str = ( '' + explanation.feature + "" ) highlighted_text = highlighted_text.replace(explanation.feature, token_str) if avoid_exp_label: vis = "

" + highlighted_text + "

" else: vis = "

Explanations:
" + highlighted_text + "

" vis = vis.replace("\n", "
") legend = "

' + "💡" + "" ) legend += " " legend += ( '' + "not important" + "" ) legend += " ⇢ " legend += ( '' + " important " + "" ) legend += " ⇢ " legend += ( '' + " very important " + "" ) legend += "

" html_str = legend + vis return html_str