feat(engine): R1 token normalization — all operands to V, digits to N. 3-agent verified.

This commit is contained in:
Allaun Silverfox 2026-06-23 06:35:26 -05:00
parent f930d038d0
commit 3dc721a2d5

View file

@ -19,6 +19,7 @@ Usage:
print(f"Classified as: {concept.name} (d={distance:.6f})") print(f"Classified as: {concept.name} (d={distance:.6f})")
""" """
import re
import numpy as np import numpy as np
from typing import List, Tuple, Optional, Dict from typing import List, Tuple, Optional, Dict
from dataclasses import dataclass, field from dataclasses import dataclass, field
@ -35,6 +36,27 @@ PHI = (1 + np.sqrt(5)) / 2
PSI = 2 * np.pi / (PHI ** 2) PSI = 2 * np.pi / (PHI ** 2)
# ============================================================
# R1: TOKEN NORMALIZATION [VERIFIED: all 4 test cases identical]
# ============================================================
def normalize(s: str) -> str:
"""R1 structural reinforcement: normalize surface variation.
Rules:
1. Lowercase everything
2. Replace all digit sequences with 'N'
3. Replace all letter names with 'V'
Verified: "A+B=C", "a+b=c", "foo+bar=baz", "123+456=579"
all produce F = [0, 0.2, 0, 0.2, 0.6, 0, 0, 0] (3 agents).
"""
s = s.lower()
s = re.sub(r'[0-9]+', 'N', s)
s = re.sub(r'[a-z]+', 'V', s)
return s
# ============================================================ # ============================================================
# BYTE CLASSIFICATION # BYTE CLASSIFICATION
# ============================================================ # ============================================================
@ -61,12 +83,14 @@ def byte_class(c: str) -> int:
# ============================================================ # ============================================================
def F(string: str) -> np.ndarray: def F(string: str) -> np.ndarray:
"""Byte-frequency probability vector [VERIFIED]. """Byte-frequency probability vector [VERIFIED: 008, R1].
Maps string Δ₇ (8-dim probability simplex). Maps string Δ₇ (8-dim probability simplex).
Uses R1 normalization before counting.
""" """
norm = normalize(string)
counts = [0] * 8 counts = [0] * 8
for c in string: for c in norm:
counts[byte_class(c)] += 1 counts[byte_class(c)] += 1
total = sum(counts) total = sum(counts)
return np.array(counts) / total if total > 0 else np.zeros(8) return np.array(counts) / total if total > 0 else np.zeros(8)
@ -330,12 +354,13 @@ class SilverSight:
return dist > threshold, dist return dist > threshold, dist
def _detect_operator(self, equation: str) -> str: def _detect_operator(self, equation: str) -> str:
"""Simple operator detection from equation string.""" """Detect dominant operator from equation string."""
if '+' in equation: return "addition" norm = normalize(equation)
if '/' in equation: return "division" if '+' in norm: return "addition"
if '*' in equation: return "multiplication" if '/' in norm: return "division"
if '-' in equation: return "subtraction" if '*' in norm: return "multiplication"
if '=' in equation: return "equality" if '-' in norm: return "subtraction"
if '=' in norm: return "equality"
return "literal" return "literal"
def summary(self): def summary(self):