"""
Visualizador de Semantic Highlighting para Agent Pruning Lab
"""
import plotly.graph_objects as go
import numpy as np
class SemanticHighlighter:
"""Gera visualizações interativas de relevância semântica"""
def render_semantic_highlighting(self, sentences, scores):
"""
Renderiza texto com highlighting semântico baseado em scores
"""
html_parts = ['
']
for sent, score in zip(sentences, scores):
# Mapeia score para intensidade de cor
intensity = int(score * 255)
if score >= 0.7:
color = f"rgba(255, 135, 135, {score * 0.3})" # Vermelho suave
border = "2px solid #ff8787"
elif score >= 0.4:
color = f"rgba(255, 216, 168, {score * 0.3})" # Laranja suave
border = "2px solid #ffd8a8"
else:
color = f"rgba(255, 243, 191, {score * 0.3})" # Amarelo suave
border = "1px solid #ffe066"
if score < 0.2:
# Texto de baixa relevância - riscado e opaco
html_parts.append(
f''
f'{sent} '
)
else:
html_parts.append(
f''
f'{sent} '
)
html_parts.append('
')
return "".join(html_parts)
def render_heatmap(self, sentences, scores):
"""
Cria heatmap Plotly das sentenças e seus scores
"""
fig = go.Figure(data=go.Heatmap(
z=[scores],
x=[f"S{i+1}" for i in range(len(sentences))],
y=["Relevância"],
colorscale=[
[0, "#fff3bf"],
[0.5, "#ffd8a8"],
[1, "#ff8787"]
],
showscale=True,
text=[[f"{s[:30]}..." for s in sentences]],
hovertemplate='Sentença: %{text}
Score: %{z:.2f}'
))
fig.update_layout(
title="Mapa de Calor de Relevância Semântica",
xaxis_title="Sentenças",
yaxis_title="",
height=200,
template="plotly_white"
)
return fig