You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+2-5Lines changed: 2 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Presidio-research
2
2
3
-
This package provides evaluation and data-science capabilities for
3
+
This package provides evaluation and data-science capabilities for
4
4
[Presidio](https://github.com/microsoft/presidio) and PII detection models in general.
5
5
6
6
It also includes a fake data generator that creates synthetic sentences based on templates and fake PII.
@@ -20,16 +20,13 @@ The easiest way to get started is by reviewing the notebooks.
20
20
-[Notebook 3](notebooks/3_Split_by_pattern_number.ipynb): Provides tools to split the dataset into train/test/validation sets while avoiding leakage due to the same pattern appearing in multiple folds (only applicable for synthetically generated data).
21
21
-[Notebook 4](notebooks/4_Evaluate_Presidio_Analyzer.ipynb): Shows how to use the evaluation tools to evaluate how well Presidio detects PII. Note that this is using the vanilla Presidio, and the results aren't very accurate.
22
22
-[Notebook 5](notebooks/5_Evaluate_Custom_Presidio_Analyzer.ipynb): Shows how one can configure Presidio to detect PII much more accurately, and boost the f score in ~30%.
23
+
-[Notebook 6](notebooks/6_Interactive_Entity_Mapping.ipynb): Explains the entity mapping process, which is crucial when evaluating multiple models each returning a different set of entities.
23
24
24
25
### Installation
25
26
26
-
>Note: Presidio evaluator requires Python version 3.11 or higher.
27
-
28
27
#### From PyPI
29
28
30
29
```sh
31
-
conda create --name presidio python=3.12
32
-
conda activate presidio
33
30
pip install presidio-evaluator
34
31
python -m spacy download en_core_web_sm # for tokenization
-**`HIERARCHY`** — a single nested Python dict that is the authoritative taxonomy.
16
-
-**`EntityHierarchy`** — a class that wraps the taxonomy and exposes canonicalization, branch lookup, and a mutation API.
17
-
-**Module-level shortcuts** — `canonicalize()`, `get_branch()`, and `print_hierarchy()` delegate to a shared default
18
-
instance so most callers never need to instantiate the class directly.
16
+
-**`EntityHierarchy`** — a class that wraps the taxonomy and exposes canonicalization, branch lookup, BIO prefix stripping, and alias extension.
17
+
-**`CanonicalMapper`** — a workflow class that resolves a full set of raw model/dataset labels through auto-resolution, fuzzy matching, and manual override.
19
18
20
19
---
21
20
@@ -32,8 +31,8 @@ The default canonical depth is **3** (passed as `canonical_depth=3` to `EntityHi
Rather than listing every `URUGUAY_TAX_ID`, `AUSTRALIA_DRIVERS_LICENSE`, etc. explicitly, the module keeps two tables:
90
90
91
-
-**`COUNTRIES`** — all 249 ISO 3166-1 alpha-2 codes plus full English country name tokens(e.g. `AUSTRALIA`, `GERMANY`).
92
-
-**`country_prefixed_doc_types`** — an instance attribute on `EntityHierarchy`; a suffix keyword → canonical entity mapping (e.g. `"DRIVER"` → `"DRIVER_LICENSE"`). Add entries via `h.add_country_doc_type()`.
91
+
-**`COUNTRIES`** — all 249 ISO 3166-1 alpha-2 codes plus full English country name tokens, demonyms, and adjectival forms (e.g. `AUSTRALIA`, `GERMANY`, `BRITISH`, `FRENCH`).
92
+
-**`country_prefixed_doc_types`** — an instance attribute on `EntityHierarchy`; a suffix keyword → canonical entity mapping (e.g. `"DRIVER"` → `"DRIVER_LICENSE"`). Mutate directly: `h.country_prefixed_doc_types["MY_SUFFIX"] = "MY_CANONICAL"`.
93
93
94
94
Any `<COUNTRY>_<SUFFIX>` label is resolved automatically. An unrecognized suffix with a known country prefix defaults
`EntityHierarchy.normalize()` is a static method that strips BIO prefixes/suffixes, uppercases, and removes `_` and `-`. It is the first step of every canonicalization:
`add_alias()` raises `KeyError` if `entity_name` is not found in the hierarchy. Each `add_alias()` call triggers a full rebuild of the internal lookup tables.
259
+
260
+
### Country-prefix customization
258
261
259
-
# Teach the engine that a new suffix maps to an existing canonical
`CanonicalMapper` resolves a full set of raw entity labels — as used by models or evaluation datasets — to canonical entities. Auto-resolution runs at construction time (exact alias → country-prefix → fuzzy); unresolvable labels land in `pending` and must be handled manually before `get_mapping()` will succeed.
298
+
299
+
```python
300
+
from presidio_evaluator.entity_mapping import CanonicalMapper, IncompleteMapping
When annotation and prediction labels are related but resolve to different canonical entities at the current depth, a `UserWarning` is emitted. Passing `hierarchy=2` (or another depth) rebuilds the underlying `EntityHierarchy` and re-resolves all previously seen labels.
346
+
288
347
---
289
348
290
349
## Quick reference
291
350
292
-
| Import | Type | Description |
351
+
### `EntityHierarchy`
352
+
353
+
| Symbol | Type | Description |
354
+
|--------|------|-------------|
355
+
|`EntityHierarchy()`|`EntityHierarchy`| Default instance at `canonical_depth=3`|
356
+
|`h.canonicalize(raw, threshold=0.80)`|`str`| Resolve raw label → canonical; fuzzy-enabled by default |
357
+
|`h.get_branch(raw)`|`list[str]`| Full ancestor path for a raw label |
358
+
|`EntityHierarchy.normalize(label)`|`str` (static) | Strip BIO prefix/suffix, uppercase, remove `_`/`-`|
359
+
|`h.add_alias(entity, alias)`|`None`| Add a raw alias to an existing entity |
360
+
|`h.raw_to_canonical`|`dict[str, str]`| Normalized raw → canonical |
361
+
|`h.all_canonical_entities`|`list[str]`| Every canonical-depth entity name |
362
+
|`h.canonical_to_branch`|`dict[str, list[str]]`| Canonical → full ancestor path |
363
+
|`h.country_prefixed_doc_types`|`dict[str, str]`| Suffix → canonical overrides for the country-prefix engine |
364
+
365
+
### `CanonicalMapper`
366
+
367
+
| Symbol | Type | Description |
368
+
|--------|------|-------------|
369
+
|`CanonicalMapper(labels)`|`CanonicalMapper`| Resolve a set of raw labels with auto + manual fallback |
0 commit comments