File size: 2,727 Bytes
0ef767c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
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 = ['<div style="line-height: 2; font-size: 16px; padding: 20px; background: #fafafa; border-radius: 10px;">']
        
        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'<span style="text-decoration: line-through; color: #adb5bd; opacity: 0.6; padding: 2px 4px;">'
                    f'{sent}</span> '
                )
            else:
                html_parts.append(
                    f'<span style="background-color: {color}; border: {border}; '
                    f'border-radius: 4px; padding: 2px 6px; margin: 1px; '
                    f'transition: all 0.3s;" title="Relevância: {score:.2f}">'
                    f'{sent}</span> '
                )
        
        html_parts.append('</div>')
        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}<br>Score: %{z:.2f}<extra></extra>'
        ))
        
        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