perception

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 perception holds the deterministic awareness detectors: personal data, secrets and topics, plus the placeholders that redact what they find.

Detectors run on the text with its invisible runes removed (see textnorm.Strip), so a zero-width space cannot split a token, and report original byte offsets that cover any such runes. A Match never holds the text it matched: it carries the detector, a placeholder type, offsets, a score and an opaque fingerprint that lets Placeholders give a repeated value the same number.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(text string, spans []Span) string

Apply replaces every span of text with its placeholder. Offsets are clamped to text and empty spans are skipped. Overlapping spans merge into one, and the earliest-starting span's placeholder wins (the first given on a tie).

func BuiltinPII

func BuiltinPII() []string

BuiltinPII returns the built-in PII detector names.

func BuiltinSecrets

func BuiltinSecrets() []string

BuiltinSecrets returns the built-in secret detector names.

func Entropy

func Entropy(s string) float64

Entropy returns the Shannon entropy of s in bits per character, counting runes. It is 0 for an empty string or one repeated character.

func Topic

func Topic(text string, patterns []*regexp.Regexp) bool

Topic reports whether any pattern matches text, as given or in its folded form (see textnorm.Fold: lower case, NFKC, invisible runes removed), so a lower-case pattern still sees "RELEASE" and a word split by a zero-width space. Nil patterns are skipped.

Types

type Match

type Match struct {
	Detector string  `json:"detector"`
	Type     string  `json:"type"`
	Start    int     `json:"start"`
	End      int     `json:"end"`
	Score    float64 `json:"score"`
	// contains filtered or unexported fields
}

Match is one detection. Start and End are original byte offsets; for a pattern with a named group "secret" they cover that group only. Type is the upper-case placeholder stem, such as "EMAIL" or "GITHUB_TOKEN".

func Custom

func Custom(text, name string, patterns []*regexp.Regexp) []Match

Custom runs caller patterns over text and reports every non-empty match as detector name, type name upper-cased with other runes as '_', score 1. A pattern with a named group "secret" reports that group only.

func PII

func PII(text string, detectors []string) []Match

PII runs the named built-in PII detectors over text. Empty detectors runs every built-in except ip_address; unknown names are ignored. Every match scores 1, and overlapping matches keep the longest.

func Secrets

func Secrets(text string, detectors []string) []Match

Secrets runs the named built-in secret detectors over text. Empty detectors runs them all; unknown names are ignored. Known formats score 1, generic_assignment 0.8 and high_entropy 0.6. Where matches overlap only the highest-scoring one is kept, so one secret is one match.

func (Match) GoString

func (m Match) GoString() string

GoString formats the exported fields only, for %#v.

func (Match) String

func (m Match) String() string

String formats the exported fields only, so the value fingerprint never reaches a log.

type Span

type Span struct {
	Start       int    `json:"start"`
	End         int    `json:"end"`
	Placeholder string `json:"placeholder"`
}

Span is a byte range of the original text and the placeholder that replaces it.

func Placeholders

func Placeholders(matches []Match) []Span

Placeholders turns matches into redaction spans "[TYPE_n]", sorted by Start. n counts per Type from 1 in reading order, and every match of the same value and Type within one call shares its n. A Match built by hand carries no value and gets its own n.

Jump to

Keyboard shortcuts

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