Define a PII detector
Usage
new_detector(
id,
class,
label,
pattern,
capture = NULL,
validate = NULL,
checks = NULL,
base = 0.8,
base_without_context = NULL,
context = NULL,
negative = NULL,
context_required = FALSE,
canonical = NULL,
describe = NULL,
ignore_case = FALSE,
column_label = NULL,
column_pattern = NULL
)Arguments
- id
Unique identifier, e.g.
"aadhaar".- class
Data class used by policies, e.g.
"identity_strong".- label
Human-readable name.
- pattern
ICU regular expression for candidates, or several (their matches are combined). Wrap it with
bounded()so it does not match inside a longer token.- capture
Name of a capture group whose span is reported instead of the whole match, or
NULL. Lets a pattern match a cue such as"Name:"without reporting it; much faster than a lookbehind.- validate
Function taking the matched strings (normalised text) and returning a logical vector; candidates returning
FALSEare dropped.- checks
Short description of what
validatechecks, reported as a reason for each match (e.g."Verhoeff checksum valid").- base
Confidence (0-1) of a validated match without supporting context, or a function taking the matched strings and returning one confidence per match (e.g. higher for a mobile number written with
+91).- base_without_context
Confidence (0-1) used instead of
basewhen no context keyword is found, orNULL. For patterns that are strong evidence with context but weak without it (a voter ID format alone could be a product code).- context
Regex (case-insensitive) of keywords that raise confidence when they appear near the match, or
NULL.- negative
Regex (case-insensitive) of keywords that lower confidence, or
NULL.- context_required
If
TRUE, matches without supporting context are dropped. Use for patterns too generic to trust on their own.- canonical
Function mapping matched strings to a canonical form (used for consistent tokenisation). Defaults to removing spaces and hyphens and upper-casing.
- describe
Optional function returning extra reasons per match (e.g. the PAN holder type).
- ignore_case
Match the pattern case-insensitively.
- column_label
Regex (case-insensitive) that, when it matches the caller's extra context (a column name), makes that context the label for
column_pattern, orNULL. For detectors whosepatternneeds a label in the text, so that a column namedcard_cvvlabels its values.- column_pattern
Patterns used, reporting the whole match, only when
column_labelmatches the extra context.
Examples
employee_id <- new_detector(
id = "employee_id", class = "internal", label = "Employee ID",
pattern = bounded("EMP[0-9]{6}"),
context = "employee|staff"
)
employee_id$label
#> [1] "Employee ID"