Download l3_paraconsistent.py from 0danielfonseca/Doninha-Artificial-Inteligence: direct link, hf CLI and curl.
- Browser
- Download file 15 kB
-
https://huggingface.co/0danielfonseca/Doninha-Artificial-Inteligence/resolve/main/l3_paraconsistent.py
- Command line
-
hf download hf://0danielfonseca/Doninha-Artificial-Inteligence/l3_paraconsistent.py
-
curl -L -o l3_paraconsistent.py https://huggingface.co/0danielfonseca/Doninha-Artificial-Inteligence/resolve/main/l3_paraconsistent.py
15 kB
| """ | |
| CAMADA L3 — Avaliação Paraconsistente | |
| ====================================== | |
| Implementa a Lógica Anotada de Evidências (LAE / PAL2v) de da Costa & Abe. | |
| Cada proposição recebe um par de anotações: | |
| μ ∈ [0,1] — grau de evidência FAVORÁVEL | |
| λ ∈ [0,1] — grau de evidência CONTRÁRIA | |
| Estados resultantes: | |
| ┌──────────────────────────────────────────────────┐ | |
| │ Verdadeiro : μ alto, λ baixo │ | |
| │ Falso : μ baixo, λ alto │ | |
| │ Inconsistente : μ alto, λ alto (contradição) │ | |
| │ Indeterminado : μ baixo, λ baixo │ | |
| │ Intermediário : valores médios (morno, etc.) │ | |
| └──────────────────────────────────────────────────┘ | |
| Princípio central: | |
| "Contradição Local + Consistência Global ≠ Trivialização" | |
| A explosão é GENTIL: uma contradição local (quente e frio) não | |
| trivializa o sistema — produz o estado "Intermediário" (morno). | |
| """ | |
| from __future__ import annotations | |
| from dataclasses import dataclass | |
| from typing import Dict, List, Tuple, Optional | |
| import math | |
| import re | |
| import torch | |
| try: | |
| from paraconsistent_rules import ( | |
| state_from_rules, | |
| state_12_to_simple, | |
| load_rules_from_fuzzy_file, | |
| ParaconsistentRules, | |
| ) | |
| except Exception: | |
| state_from_rules = None # type: ignore | |
| state_12_to_simple = None # type: ignore | |
| load_rules_from_fuzzy_file = None # type: ignore | |
| ParaconsistentRules = None # type: ignore | |
| try: | |
| # Import opcional do modelo neural; o sistema continua funcional sem ele. | |
| from neural_truth_model import TruthScoringModel, load_tokenizer, neural_annotations | |
| except Exception: # pragma: no cover - fallback em ambientes sem transformers | |
| TruthScoringModel = None # type: ignore | |
| load_tokenizer = None # type: ignore | |
| neural_annotations = None # type: ignore | |
| # ───────────────────────────────────────────────────────────────────────────── | |
| # Constantes de limiar | |
| # ───────────────────────────────────────────────────────────────────────────── | |
| THRESHOLD_TRUE = 0.7 # μ ≥ este valor e λ ≤ (1 - este) → Verdadeiro | |
| THRESHOLD_FALSE = 0.3 # μ ≤ este e λ ≥ (1 - este) → Falso | |
| THRESHOLD_INCONSISTENT = 0.6 # ambos acima → Inconsistente (contradição local) | |
| THRESHOLD_INDETERMINATE= 0.4 # ambos abaixo → Indeterminado | |
| PROPOSITION_TYPE_A = "A" # Universal Afirmativa | |
| PROPOSITION_TYPE_E = "E" # Universal Negativa | |
| PROPOSITION_TYPE_I = "I" # Particular Afirmativa | |
| PROPOSITION_TYPE_O = "O" # Particular Negativa | |
| PROPOSITION_TYPE_LABELS = { | |
| PROPOSITION_TYPE_A: "Universal Afirmativa", | |
| PROPOSITION_TYPE_E: "Universal Negativa", | |
| PROPOSITION_TYPE_I: "Particular Afirmativa", | |
| PROPOSITION_TYPE_O: "Particular Negativa", | |
| } | |
| def infer_proposition_type(text: str) -> Optional[str]: | |
| """Heurística simples para inferir o tipo A / E / I / O a partir do texto.""" | |
| normalized = text.lower().strip() | |
| if not normalized: | |
| return None | |
| # Detecta padrões de proposições particulares negativas antes das afirmativas. | |
| if re.search(r"\b(algum não|alguns não|alguma não|algumas não|nem todos|pelo menos um não)\b", normalized): | |
| return PROPOSITION_TYPE_O | |
| if re.search(r"\b(nenhum|nenhuma|nunca|jamais|sem nenhum|sem nenhuma|não existe|não há)\b", normalized): | |
| return PROPOSITION_TYPE_E | |
| if re.search(r"\b(algum|alguma|alguns|algumas|pelo menos um|há um|há algum|existem|existe)\b", normalized): | |
| return PROPOSITION_TYPE_I | |
| if re.search(r"\b(todo|todos|toda|todas|cada|sempre|qualquer)\b", normalized): | |
| return PROPOSITION_TYPE_A | |
| return None | |
| def type_label(proposition_type: Optional[str]) -> str: | |
| return PROPOSITION_TYPE_LABELS.get(proposition_type, "Desconhecido") | |
| class ParaconsistentValue: | |
| """ | |
| Valor-verdade paraconsistente para uma proposição. | |
| Baseado na Lógica Anotada de Evidências (LAE). | |
| """ | |
| proposition: str | |
| mu: float # evidência favorável ∈ [0,1] | |
| lam: float # evidência contrária ∈ [0,1] | |
| proposition_type: Optional[str] = None | |
| def proposition_kind(self) -> Optional[str]: | |
| """Retorna o tipo de proposição A/E/I/O, inferido do texto se necessário.""" | |
| return self.proposition_type or infer_proposition_type(self.proposition) | |
| def proposition_type_label(self) -> str: | |
| return type_label(self.proposition_kind) | |
| # ── Graus derivados ──────────────────────────────────────────────── # | |
| def certainty(self) -> float: | |
| """Grau de certeza: Gc = μ − λ ∈ [−1, 1]""" | |
| return self.mu - self.lam | |
| def contradiction(self) -> float: | |
| """Grau de contradição: Gct = μ + λ − 1 ∈ [−1, 1]""" | |
| return self.mu + self.lam - 1.0 | |
| def state(self) -> str: | |
| """Estado lógico qualitativo. Usa regras do Fuzzy.txt se disponíveis.""" | |
| if state_from_rules is not None and state_12_to_simple is not None: | |
| state_12 = state_from_rules(self.mu, self.lam) | |
| return state_12_to_simple(state_12) | |
| # Fallback: limiares fixos | |
| if self.mu >= THRESHOLD_TRUE and self.lam <= (1 - THRESHOLD_TRUE): | |
| return "Verdadeiro" | |
| if self.mu <= THRESHOLD_FALSE and self.lam >= (1 - THRESHOLD_FALSE): | |
| return "Falso" | |
| if self.mu >= THRESHOLD_INCONSISTENT and self.lam >= THRESHOLD_INCONSISTENT: | |
| return "Inconsistente_local" # explosão GENTIL — não trivializa | |
| if self.mu <= THRESHOLD_INDETERMINATE and self.lam <= THRESHOLD_INDETERMINATE: | |
| return "Indeterminado" | |
| return "Intermediário" # ex: morno entre quente e frio | |
| def state_12(self) -> Optional[str]: | |
| """Estado lógico de 12 valores (reticulado) conforme Fuzzy.txt, se regras carregadas.""" | |
| if state_from_rules is not None: | |
| return state_from_rules(self.mu, self.lam) | |
| return None | |
| def truth_value(self) -> float: | |
| """Valor-verdade escalar normalizado para saída final.""" | |
| return round((self.mu + (1 - self.lam)) / 2.0, 4) | |
| def __str__(self) -> str: | |
| type_label_text = self.proposition_type_label | |
| return ( | |
| f" μ={self.mu:.3f} λ={self.lam:.3f} " | |
| f"Gc={self.certainty:+.3f} Gct={self.contradiction:+.3f} " | |
| f"v={self.truth_value:.3f} [{self.state}]\n" | |
| f" Tipo={type_label_text} \"{self.proposition}\"" | |
| ) | |
| class ManyValuedRouter: | |
| """Roteador fuzzy para distinguir contradição lógica real de incerteza estatística.""" | |
| REAL_CONTRADICTION = "Contradição_real" | |
| STATISTICAL_UNCERTAINTY = "Incerteza_estatística" | |
| AMBIGUOUS = "Ambíguo" | |
| UNCLASSIFIED = "Não_classificado" | |
| def _pair_strength(left: ParaconsistentValue, right: ParaconsistentValue) -> float: | |
| """Grau fuzzy de suporte conjunto entre duas proposições.""" | |
| return round(min(left.mu, right.mu), 4) | |
| def route_pair( | |
| cls, | |
| left: ParaconsistentValue, | |
| right: ParaconsistentValue, | |
| ) -> Tuple[str, float, str]: | |
| """Classifica o par de proposições como contradição real, incerteza ou ambíguo.""" | |
| left_type = left.proposition_kind | |
| right_type = right.proposition_kind | |
| label = f"{left_type or '?'} vs {right_type or '?'}" | |
| if {left_type, right_type} == {PROPOSITION_TYPE_A, PROPOSITION_TYPE_I}: | |
| strength = cls._pair_strength(left, right) | |
| return cls.REAL_CONTRADICTION, strength, f"Contraposição A/I detectada ({label})" | |
| if {left_type, right_type} == {PROPOSITION_TYPE_E, PROPOSITION_TYPE_I}: | |
| strength = cls._pair_strength(left, right) | |
| return cls.STATISTICAL_UNCERTAINTY, strength, f"Contraposição E/I detectada ({label})" | |
| if left_type is None or right_type is None: | |
| return cls.AMBIGUOUS, 0.0, "Tipo de proposição não identificado" | |
| return cls.UNCLASSIFIED, 0.0, f"Par {label} não corresponde a A/I nem E/I" | |
| def route_pairwise( | |
| cls, | |
| values: List[ParaconsistentValue], | |
| ) -> List[Tuple[ParaconsistentValue, ParaconsistentValue, str, float, str]]: | |
| """Avalia todos os pares de proposições para identificação de rota lógica.""" | |
| routes: List[Tuple[ParaconsistentValue, ParaconsistentValue, str, float, str]] = [] | |
| n = len(values) | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| route, confidence, explanation = cls.route_pair(values[i], values[j]) | |
| if route != cls.UNCLASSIFIED: | |
| routes.append((values[i], values[j], route, confidence, explanation)) | |
| return routes | |
| # ───────────────────────────────────────────────────────────────────────────── | |
| # Motor paraconsistente | |
| # ───────────────────────────────────────────────────────────────────────────── | |
| class ParaconsistentEngine: | |
| """ | |
| Avalia as hipóteses kantiana (L2) e atribui valores-verdade | |
| paraconsistentes a cada uma. | |
| Pode operar em dois modos: | |
| - Modo heurístico (padrão): usa apenas o banco de conhecimento. | |
| - Modo neural: se um TruthScoringModel for fornecido, usa o modelo | |
| para calcular (μ, λ) compatíveis com a Lógica Anotada. | |
| """ | |
| def __init__( | |
| self, | |
| neural_model: Optional["TruthScoringModel"] = None, | |
| neural_tokenizer=None, | |
| device: Optional[torch.device] = None, | |
| ) -> None: | |
| self.neural_model = neural_model | |
| self.neural_tokenizer = neural_tokenizer | |
| self.device = device or torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| def evaluate( | |
| self, | |
| propositions: List[Tuple[str, float]], # (texto, peso_de_prioridade_L2) | |
| knowledge_base: Dict[str, float], # termo → grau de evidência no BD | |
| ) -> List[ParaconsistentValue]: | |
| """ | |
| Para cada proposição: | |
| μ = evidência favorável extraída do banco de dados | |
| λ = evidência contrária = 1 − f(compatibilidade) | |
| """ | |
| results: List[ParaconsistentValue] = [] | |
| for prop_text, l2_priority in propositions: | |
| mu, lam = self._compute_annotations(prop_text, l2_priority, knowledge_base) | |
| pv = ParaconsistentValue( | |
| proposition=prop_text, | |
| mu=mu, | |
| lam=lam, | |
| proposition_type=infer_proposition_type(prop_text), | |
| ) | |
| results.append(pv) | |
| # Ordena por valor-verdade descendente | |
| results.sort(key=lambda pv: pv.truth_value, reverse=True) | |
| return results | |
| def route_contradictions( | |
| self, | |
| values: List[ParaconsistentValue], | |
| ) -> List[Tuple[ParaconsistentValue, ParaconsistentValue, str, float, str]]: | |
| """Retorna rotas de pares de proposições classificadas pelo roteador fuzzy.""" | |
| return ManyValuedRouter.route_pairwise(values) | |
| # ------------------------------------------------------------------ # | |
| # Anotação μ / λ # | |
| # ------------------------------------------------------------------ # | |
| def _compute_annotations( | |
| self, | |
| text: str, | |
| l2_priority: float, | |
| kb: Dict[str, float], | |
| ) -> Tuple[float, float]: | |
| """ | |
| Calcula (μ, λ) para uma proposição. | |
| Se um modelo neural estiver disponível, usa-o para obter anotações | |
| compatíveis com L3. Caso contrário, volta para a heurística original | |
| baseada apenas no banco de conhecimento e em contradições locais. | |
| """ | |
| if self.neural_model is not None and self.neural_tokenizer is not None and neural_annotations is not None: | |
| mu, lam, _, _ = neural_annotations(self.neural_model.to(self.device), self.neural_tokenizer, text) | |
| # Pequena modulação pela prioridade de L2 para manter a integração | |
| mu = min(1.0, mu * (0.5 + 0.5 * l2_priority)) | |
| lam = max(0.0, lam * (1.0 - 0.3 * l2_priority)) | |
| return round(mu, 4), round(lam, 4) | |
| import re | |
| tokens = set(re.findall(r"[a-záàãâéêíóôõúüç]+", text.lower())) | |
| kb_scores = [kb.get(t, 0.0) for t in tokens if kb.get(t, 0.0) > 0] | |
| mu_kb = sum(kb_scores) / len(kb_scores) if kb_scores else 0.3 | |
| contradiction_detected = self._has_antonym_pair(tokens, kb) | |
| lam_base = 0.8 if contradiction_detected else (1.0 - mu_kb) | |
| mu = min(1.0, mu_kb * (0.5 + 0.5 * l2_priority)) | |
| lam = max(0.0, lam_base * (1.0 - 0.3 * l2_priority)) | |
| return round(mu, 4), round(lam, 4) | |
| ANTONYM_PAIRS = [ | |
| ("quente", "frio"), ("quente", "gelado"), | |
| ("verdadeiro", "falso"), ("real", "fictício"), | |
| ("afirmativo", "negativo"), ("possível", "impossível"), | |
| ] | |
| def _has_antonym_pair(self, tokens: set, kb: Dict[str, float]) -> bool: | |
| for a, b in self.ANTONYM_PAIRS: | |
| if a in tokens and b in tokens: | |
| return True | |
| return False | |
| # ------------------------------------------------------------------ # | |
| # Consistência global: verifica se sistema trivializou # | |
| # ------------------------------------------------------------------ # | |
| def check_global_consistency(values: List[ParaconsistentValue]) -> bool: | |
| """ | |
| Retorna True se o sistema é globalmente consistente | |
| (nenhuma trivialização — todos os estados válidos). | |
| Uma trivialização ocorre se TODAS as proposições são | |
| 'Inconsistente_local' sem nenhum 'Verdadeiro' ou 'Intermediário'. | |
| """ | |
| states = {pv.state for pv in values} | |
| if states == {"Inconsistente_local"}: | |
| return False # trivialização global | |
| return True # explosão gentil — sistema consistente | |