106 lines
3.3 KiB
Go
106 lines
3.3 KiB
Go
package validators
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/audita/internal/core/config"
|
|
"gitea.maximumdirect.net/eric/audita/internal/core/schema"
|
|
"gitea.maximumdirect.net/eric/audita/internal/framework/proposals"
|
|
)
|
|
|
|
const (
|
|
ReasonApproved = "approved"
|
|
ReasonLowConfidence = "low_confidence"
|
|
ReasonMissingOriginalText = "missing_original_text"
|
|
ReasonMissingTargetSegment = "missing_target_segment"
|
|
ReasonEmptyCorrectedText = "empty_corrected_text"
|
|
ReasonNoEffect = "no_effect"
|
|
ReasonProtectedGlossaryTerm = "protected_glossary_term"
|
|
)
|
|
|
|
// Request is the runtime input shared by deterministic validators.
|
|
type Request struct {
|
|
WorkingTranscript *schema.Transcript `json:"-"`
|
|
CandidateProposal []proposals.EnrichedCorrectionProposal `json:"candidate_proposals"`
|
|
ModuleKey string `json:"module_key"`
|
|
ModuleInstance string `json:"module_instance"`
|
|
ReplacementPolicy proposals.ReplacementPolicy `json:"replacement_policy"`
|
|
Glossary *schema.Glossary `json:"-"`
|
|
Config *config.Config `json:"-"`
|
|
}
|
|
|
|
// Decision is one validator decision for one proposal index.
|
|
type Decision struct {
|
|
ProposalIndex int `json:"proposal_index"`
|
|
Approved bool `json:"approved"`
|
|
ReasonCode string `json:"reason_code"`
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
// Result is one validator output containing exactly one decision per proposal index.
|
|
type Result struct {
|
|
ValidatorName string `json:"validator_name"`
|
|
Decisions []Decision `json:"decisions"`
|
|
}
|
|
|
|
// Validator is the deterministic runtime validator interface.
|
|
type Validator interface {
|
|
Name() string
|
|
Validate(ctx context.Context, req Request) (Result, error)
|
|
}
|
|
|
|
// EnforceDecisionCardinality verifies every candidate proposal index receives exactly one decision.
|
|
func EnforceDecisionCardinality(candidate []proposals.EnrichedCorrectionProposal, decisions []Decision) error {
|
|
expected := make(map[int]struct{}, len(candidate))
|
|
for _, c := range candidate {
|
|
expected[c.ProposalIndex] = struct{}{}
|
|
}
|
|
|
|
seen := make(map[int]int, len(decisions))
|
|
for _, d := range decisions {
|
|
if _, ok := expected[d.ProposalIndex]; !ok {
|
|
return fmt.Errorf("unknown decision proposal index %d", d.ProposalIndex)
|
|
}
|
|
seen[d.ProposalIndex]++
|
|
if seen[d.ProposalIndex] > 1 {
|
|
return fmt.Errorf("duplicate decision proposal index %d", d.ProposalIndex)
|
|
}
|
|
}
|
|
|
|
for idx := range expected {
|
|
if seen[idx] == 0 {
|
|
return fmt.Errorf("missing decision proposal index %d", idx)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func approval(index int) Decision {
|
|
return Decision{ProposalIndex: index, Approved: true, ReasonCode: ReasonApproved, Message: "approved"}
|
|
}
|
|
|
|
func rejection(index int, reasonCode string, msg string) Decision {
|
|
return Decision{ProposalIndex: index, Approved: false, ReasonCode: reasonCode, Message: strings.TrimSpace(msg)}
|
|
}
|
|
|
|
func confidenceThresholdForModule(moduleKey string, cfg *config.Config) float64 {
|
|
if cfg == nil {
|
|
return 0.0
|
|
}
|
|
switch moduleKey {
|
|
case "glossary":
|
|
return cfg.Thresholds.Glossary
|
|
case "grammar":
|
|
return cfg.Thresholds.Grammar
|
|
case "homophones":
|
|
return cfg.Thresholds.Homophones
|
|
case "spoken_word":
|
|
return cfg.Thresholds.SpokenWord
|
|
default:
|
|
return 0.0
|
|
}
|
|
}
|