Documentation
¶
Overview ¶
Package ner provides ML-based named entity recognition for PII redaction. It uses ONNX Runtime (via purego) and a multilingual DistilBERT NER model to detect person names in freeform text.
The model bundle is downloaded separately via `hs pii-model install` and cached in the user's OS-specific cache directory. When the bundle is not present, the NER detector cannot be created and freeform text is hidden instead.
Index ¶
- Constants
- func CacheDir() (string, error)
- func DecodeLogits(logits [][]float32, labels []string, seqLen int) []tokenTag
- func IsModelReady() bool
- func LoadLabels(configPath string) ([]string, error)
- func MergePersonSpans(tags []tokenTag, offsets [][2]int, text string) []pii.NameSpan
- func RemoveModel() error
- type Detector
- type Encoding
- type Paths
- type ProgressFunc
- type Runtime
- type Tokenizer
Constants ¶
const MinConfidence float32 = 0.7
MinConfidence is the default threshold for accepting a name span.
const ModelVersion = "0.2.0"
ModelVersion is the version tag used for bundle download URLs.
Variables ¶
This section is empty.
Functions ¶
func DecodeLogits ¶
DecodeLogits converts raw logits to BIO tags via argmax.
func IsModelReady ¶
func IsModelReady() bool
IsModelReady checks whether the model bundle is present and matches the expected version without downloading anything.
func LoadLabels ¶
LoadLabels reads the id2label mapping from config.json and returns an ordered slice where index = label ID.
func MergePersonSpans ¶
MergePersonSpans groups B-PER + I-PER sequences into NameSpans using offsets. Special tokens (offset 0,0) and non-PER labels are skipped.
Types ¶
type Detector ¶
type Detector struct {
// contains filtered or unexported fields
}
Detector performs named entity recognition on text.
func NewDetector ¶
NewDetector loads the ONNX Runtime and model from the cache directory. Returns an error if the model is not installed.
type Encoding ¶
type Encoding struct {
IDs []int64
AttentionMask []int64
Offsets [][2]int // (start, end) byte offsets into original text per token
Tokens []string
}
Encoding holds tokenizer output.
type Paths ¶
type Paths struct {
RuntimeLib string // platform-specific ONNX Runtime shared lib
ModelONNX string // model_quantized.onnx
TokenizerJSON string // tokenizer.json
ConfigJSON string // config.json
}
Paths holds resolved file paths for the model bundle.
func EnsureModel ¶
func EnsureModel(progress ProgressFunc) (*Paths, error)
EnsureModel downloads and extracts the model bundle if not present.
func ModelPaths ¶
ModelPaths returns resolved paths if the model is installed, or an error.
type ProgressFunc ¶
type ProgressFunc func(bytesRead, totalBytes int64)
ProgressFunc reports download progress (bytesRead, totalBytes). totalBytes may be -1 if unknown.
type Runtime ¶
type Runtime struct {
// contains filtered or unexported fields
}
Runtime wraps an ONNX Runtime session for NER inference.
func NewRuntime ¶
NewRuntime loads the ONNX Runtime shared library and model.
type Tokenizer ¶
type Tokenizer struct {
// contains filtered or unexported fields
}
Tokenizer wraps a HuggingFace tokenizer loaded from tokenizer.json.
func NewTokenizer ¶
NewTokenizer loads a tokenizer from a tokenizer.json file.