principles

package
v1.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package principles holds the built-in detectors of the values layer: a curated lexicon for the safety principle (destructive infrastructure commands, the shell ones taken from instinct/detection) and the toxicity principle (abuse and threats aimed at people), and the score that turns pattern matches into a violation score.

The patterns are written for folded text (see detection.Fold): lower case, one space between words. The engine scores the folded text.

Lexicon entries are ASCII case-insensitive. Score counts their matches on a lower-cased copy of the text with a literal-prefix search, so a lexicon costs one fast pass per entry instead of a full regexp scan, and stays linear on large inputs. A shell command whose repetition is unbounded runs as one regexp pass over the lower-cased text instead, which is linear too. Score counts exactly what the returned regexps find.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Lexicon

func Lexicon(p values.Principle, categories []string) []*regexp.Regexp

Lexicon returns the built-in patterns of principle p in the given categories, or in every category when categories is empty. It returns nil for a principle without a built-in lexicon: only safety and toxicity have one. The patterns are ASCII case-insensitive.

func Score

func Score(text string, patterns []*regexp.Regexp) (score float64, count int)

Score counts the matches of patterns in text and combines them as a noisy-or with 0.5 per match: one match scores 0.5, two 0.75, three 0.875. It returns the score and the match count. Matches of one pattern do not overlap; matches of different patterns may.

Types

type Scorer

type Scorer struct {
	// contains filtered or unexported fields
}

Scorer counts the matches of a fixed set of patterns, as Score does. It groups the built-in lexicon once, so counting many short texts against the same patterns stays cheap.

func NewScorer

func NewScorer(patterns []*regexp.Regexp) *Scorer

NewScorer prepares patterns for Count. Nil patterns are skipped.

func (*Scorer) Count

func (s *Scorer) Count(text string) int

Count returns the number of matches of the scorer's patterns in text.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL