File size: 7,331 Bytes
f1db1e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
from src.semantic.relevance import SemanticRelevance
from src.semantic.nli_distortion import DistortionDetectorNLI
from src.sesgos.sesgos import BiasDetector
from src.mineria.mining import MiningFeatures
from datetime import datetime


class AnalysisEngine:
    def __init__(self):
        self.relevance = SemanticRelevance()
        self.distortion = DistortionDetectorNLI()
        self.bias = BiasDetector()
        self.mining = MiningFeatures()

    def map_veredicto(self, decision: str):
        if not decision:
            return "indefinido"

        decision = decision.lower()

        if "gravemente" in decision:
            return "distorsion"
        if "parcial" in decision:
            return "parcial"
        if "neutral" in decision:
            return "neutral"

        return "correcto"

    def analyze(self, user_text: str, document_paragraphs: list, title: str):
        # 1️⃣ RELEVANCIA GENERAL
        rel = self.relevance.relate(user_text, title, document_paragraphs)

        # ✅ FILTRO ESPECIAL PARA RAG
        if title == "RAG_CONTEXT":
            best_score = rel["best_paragraph"]["score"]
            if best_score < 0.25:
                return {
                    "status": "rag_irrelevante",
                    "relevance": rel,
                    "message": "El contexto recuperado por el RAG no es relevante al comentario del usuario.",
                }

        # ✅ FILTRO NORMAL PARA SCRAPING
        if title != "RAG_CONTEXT" and rel["decision_document"] in [
            "no relacionado",
            "tangencial",
        ]:
            return {
                "status": "poco_relevante",
                "relevance": rel,
            }

        # 2️⃣ DISTORSIÓN (NLI + HEURÍSTICAS)
        distortion = self.distortion.analyze_user_comment(
            user_text, document_paragraphs
        )

        # 3️⃣ SESGOS DEL DOCUMENTO
        full_document_text = (
            title + ". " + " ".join(p.get("text", "") for p in document_paragraphs)
        )
        biases_document = self.bias.detect(full_document_text)

        # 4️⃣ SESGOS DEL USUARIO
        biases_user = self.bias.detect(user_text)

        # 5️⃣ MINERÍA DE TEXTO
        mining = self.mining.extract(user_text)

        # ✅ ARMADO FINAL PROTEGIDO CONTRA ERRORES DE CLAVES
        contradicciones_formateadas = []

        for d in distortion.get("detalles", []):
            scores = d.get("scores_detail", {}) or {}

            contradicciones_formateadas.append(
                {
                    "parrafo": d.get("paragraph", ""),
                    "oracion_usuario": d.get("sentence", ""),
                    "claim_extraido": d.get("cleaned_claim"),
                    "claim_transformado": d.get(
                        "transformed_claim", d.get("cleaned_claim")
                    ),
                    "negacion_detectada": d.get("is_negation", False),
                    "tipo_distorsion": d.get("best_label", "neutral"),
                    "puntaje_principal": round(d.get("best_score", 0.0), 3),
                    "puntajes_detallados": {
                        "contradiccion": round(scores.get("contradiction", 0.0), 3),
                        "neutral": round(scores.get("neutral", 0.0), 3),
                        "coincidencia": round(scores.get("entailment", 0.0), 3),
                    },
                }
            )

        return {
            "scraped_content": {
                "title": title,
                "url": "",
                "fecha_recoleccion": datetime.utcnow().isoformat(),
                "segmentos_contenido": [
                    {
                        "type": p.get("type", "p"),
                        "text": p.get("text", ""),
                    }
                    for p in document_paragraphs
                ],
            },
            "analisis": {
                "document_sesgo": {
                    "sesgos_encontrados": [
                        {
                            "label": s.get("sesgo", s.get("label", "desconocido")),
                            "score": s.get("confianza", s.get("score", 0.0)),
                        }
                        for s in biases_document.get("sesgos_detectados", [])
                    ],
                    "explicacion": "Sesgos detectados en el documento mediante análisis heurístico.",
                },
                "user_sesgo": {
                    "sesgos_encontrados": [
                        {
                            "label": s.get("sesgo", s.get("label", "desconocido")),
                            "score": s.get("confianza", s.get("score", 0.0)),
                        }
                        for s in biases_user.get("sesgos_detectados", [])
                    ],
                    "explicacion": "Sesgos detectados en el comentario del usuario.",
                },
                "document_distorsion": {
                    "veredicto": self.map_veredicto(distortion.get("decision")),
                    "contradicciones": contradicciones_formateadas,
                },
                "mineria": mining,
            },
        }

        # return {
        #     "scraped_content": {
        #         "title": title,
        #         "url": "",
        #         "fecha_recoleccion": datetime.utcnow().isoformat(),
        #         "segmentos_contenido": [
        #             {"type": p["type"], "text": p["text"]} for p in document_paragraphs
        #         ],
        #     },
        #     "analisis": {
        #         "document_sesgo": {
        #             "sesgos_encontrados": [
        #                 {
        #                     "label": s.get("sesgo", s.get("label", "desconocido")),
        #                     "score": s.get("confianza", s.get("score", 0.0)),
        #                 }
        #                 for s in biases["sesgos_detectados"]
        #             ],
        #             "explicacion": "Sesgos detectados mediante análisis heurístico y de objetividad.",
        #         },
        #         "document_distorsion": {
        #             "veredicto": self.map_veredicto(distortion["decision"]),
        #             "contradicciones": [
        #                 {
        #                     "parrafo": d["paragraph"],
        #                     "oracion_usuario": d["sentence"],
        #                     "claim_extraido": d["cleaned_claim"],
        #                     "claim_transformado": d.get("transformed_claim", d["cleaned_claim"]),
        #                     "negacion_detectada": d["is_negation"],
        #                     "tipo_distorsion": d["best_label"],
        #                     "puntaje_principal": d["best_score"],
        #                     "puntajes_detallados": {
        #                         "contradiccion": round(d["scores_detail"]["contradiction"], 3),
        #                         "neutral": round(d["scores_detail"]["neutral"], 3),
        #                         "coincidencia": round(d["scores_detail"]["entailment"], 3),
        #                     },
        #                 }
        #                 for d in distortion["detalles"]
        #             ],
        #         },
        #         "mineria": mining,
        #     },
        # }