From 8e39a7fe5d300a8c224f34b346ec70b995701258 Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 08:45:40 +0000 Subject: [PATCH] feat(reversal): expose signal weights snapshot Add a read-only SignalWeights accessor for calibration and debugging. Co-Authored-By: Virgil --- reversal/tokeniser.go | 14 ++++++++++++++ reversal/tokeniser_test.go | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/reversal/tokeniser.go b/reversal/tokeniser.go index 58d51cf..e8f27d0 100644 --- a/reversal/tokeniser.go +++ b/reversal/tokeniser.go @@ -16,6 +16,7 @@ package reversal import ( + "maps" "strings" "unicode/utf8" @@ -644,6 +645,19 @@ func DefaultWeights() map[string]float64 { } } +// SignalWeights returns a copy of the tokeniser's configured signal weights. +// +// The copy keeps calibration and debugging callers from mutating the live +// internal map by accident. +func (t *Tokeniser) SignalWeights() map[string]float64 { + if t == nil || len(t.weights) == 0 { + return nil + } + weights := make(map[string]float64, len(t.weights)) + maps.Copy(weights, t.weights) + return weights +} + func skipDeprecatedEnglishGrammarEntry(key string) bool { switch core.Lower(key) { case "passed", "failed", "skipped": diff --git a/reversal/tokeniser_test.go b/reversal/tokeniser_test.go index 01600a0..b827f85 100644 --- a/reversal/tokeniser_test.go +++ b/reversal/tokeniser_test.go @@ -1305,6 +1305,24 @@ func TestDefaultWeights_ReturnsCopy(t *testing.T) { } } +func TestTokeniserSignalWeights_ReturnsCopy(t *testing.T) { + setup(t) + tok := NewTokeniser(WithWeights(map[string]float64{ + "noun_determiner": 0.5, + "default_prior": 0.1, + })) + + weights := tok.SignalWeights() + if weights["noun_determiner"] != 0.5 { + t.Fatalf("SignalWeights()[noun_determiner] = %v, want 0.5", weights["noun_determiner"]) + } + + weights["noun_determiner"] = 0 + if got := tok.SignalWeights()["noun_determiner"]; got != 0.5 { + t.Fatalf("SignalWeights() should return a fresh copy, got %v", got) + } +} + func TestTokeniser_LowInformationConfidenceFloor(t *testing.T) { setup(t) tok := NewTokeniser() -- 2.45.3