-
Notifications
You must be signed in to change notification settings - Fork 3
feat(bench): add Presidio and compromise comparison runs #195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feat/bench
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| presidio-analyzer==2.2.359 | ||
| spacy==3.8.13 | ||
| # Models (installed via `python -m spacy download <name>`): | ||
| # en_core_web_lg | ||
| # de_core_news_lg | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,112 @@ | ||||||||||||||||||||||||||
| """Runs Microsoft Presidio over the bench contract corpus and writes | ||||||||||||||||||||||||||
| predictions in the bench interchange format (packages/bench/README.md). | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Czech fixtures are skipped: Presidio has no Czech language support | ||||||||||||||||||||||||||
| (no spaCy model and no Czech recognizers); that absence is reported | ||||||||||||||||||||||||||
| in the results rather than scored as zero. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Offsets are converted from Python code-point indices to UTF-16 code | ||||||||||||||||||||||||||
| units to match the reference annotations. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Usage: | ||||||||||||||||||||||||||
| python run.py [--out ../../results/predictions.presidio.json] | ||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| import argparse | ||||||||||||||||||||||||||
| import json | ||||||||||||||||||||||||||
| from pathlib import Path | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| from presidio_analyzer import AnalyzerEngine | ||||||||||||||||||||||||||
| from presidio_analyzer.nlp_engine import NlpEngineProvider | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| LANGUAGE_MODELS = {"en": "en_core_web_lg", "de": "de_core_news_lg"} | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| LABEL_MAP = { | ||||||||||||||||||||||||||
| "PERSON": "person", | ||||||||||||||||||||||||||
| "ORGANIZATION": "organization", | ||||||||||||||||||||||||||
| "EMAIL_ADDRESS": "email address", | ||||||||||||||||||||||||||
| "PHONE_NUMBER": "phone number", | ||||||||||||||||||||||||||
| "DATE_TIME": "date", | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| FIXTURES_DIR = ( | ||||||||||||||||||||||||||
| Path(__file__).resolve().parents[3] | ||||||||||||||||||||||||||
| / "anonymize" | ||||||||||||||||||||||||||
| / "src" | ||||||||||||||||||||||||||
| / "__test__" | ||||||||||||||||||||||||||
| / "fixtures" | ||||||||||||||||||||||||||
| / "contracts" | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| DEFAULT_OUT = ( | ||||||||||||||||||||||||||
| Path(__file__).resolve().parents[2] / "results" / "predictions.presidio.json" | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def utf16_offsets(text: str) -> list[int]: | ||||||||||||||||||||||||||
| """Cumulative UTF-16 code-unit offset for each code-point index.""" | ||||||||||||||||||||||||||
| offsets = [0] * (len(text) + 1) | ||||||||||||||||||||||||||
| for index, char in enumerate(text): | ||||||||||||||||||||||||||
| offsets[index + 1] = offsets[index] + (2 if ord(char) > 0xFFFF else 1) | ||||||||||||||||||||||||||
| return offsets | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def build_analyzer() -> AnalyzerEngine: | ||||||||||||||||||||||||||
| configuration = { | ||||||||||||||||||||||||||
| "nlp_engine_name": "spacy", | ||||||||||||||||||||||||||
| "models": [ | ||||||||||||||||||||||||||
| {"lang_code": lang, "model_name": model} | ||||||||||||||||||||||||||
| for lang, model in LANGUAGE_MODELS.items() | ||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||
| # Default Presidio config ignores ORG spans from spaCy; the | ||||||||||||||||||||||||||
| # comparison needs organizations, so keep only the truly | ||||||||||||||||||||||||||
| # non-PII tags ignored. | ||||||||||||||||||||||||||
| "ner_model_configuration": { | ||||||||||||||||||||||||||
| "labels_to_ignore": ["CARDINAL", "ORDINAL", "QUANTITY", "PERCENT"], | ||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| provider = NlpEngineProvider(nlp_configuration=configuration) | ||||||||||||||||||||||||||
| return AnalyzerEngine( | ||||||||||||||||||||||||||
| nlp_engine=provider.create_engine(), | ||||||||||||||||||||||||||
| supported_languages=list(LANGUAGE_MODELS), | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def main() -> None: | ||||||||||||||||||||||||||
| parser = argparse.ArgumentParser() | ||||||||||||||||||||||||||
| parser.add_argument("--out", type=Path, default=DEFAULT_OUT) | ||||||||||||||||||||||||||
| args = parser.parse_args() | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| analyzer = build_analyzer() | ||||||||||||||||||||||||||
| docs = [] | ||||||||||||||||||||||||||
| for language_dir in sorted(FIXTURES_DIR.iterdir()): | ||||||||||||||||||||||||||
| language = language_dir.name | ||||||||||||||||||||||||||
| if language not in LANGUAGE_MODELS: | ||||||||||||||||||||||||||
| print(f"skipping {language}: no Presidio language support") | ||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||
|
Comment on lines
+81
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To prevent noisy console output or potential errors when encountering non-directory files (such as
Suggested change
|
||||||||||||||||||||||||||
| for fixture in sorted(language_dir.glob("*.txt")): | ||||||||||||||||||||||||||
| text = fixture.read_text(encoding="utf-8").replace("\r\n", "\n") | ||||||||||||||||||||||||||
| offsets = utf16_offsets(text) | ||||||||||||||||||||||||||
| results = analyzer.analyze( | ||||||||||||||||||||||||||
| text=text, language=language, entities=list(LABEL_MAP) | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| entities = [ | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| "start": offsets[result.start], | ||||||||||||||||||||||||||
| "end": offsets[result.end], | ||||||||||||||||||||||||||
| "label": LABEL_MAP[result.entity_type], | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| for result in results | ||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||
| docs.append({"id": f"{language}/{fixture.name}", "entities": entities}) | ||||||||||||||||||||||||||
| print(f"{language}/{fixture.name}: {len(entities)} entities") | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| args.out.parent.mkdir(parents=True, exist_ok=True) | ||||||||||||||||||||||||||
| args.out.write_text( | ||||||||||||||||||||||||||
| json.dumps({"tool": "presidio", "docs": docs}, indent=2) + "\n", | ||||||||||||||||||||||||||
| encoding="utf-8", | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| print(f"written: {args.out}") | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if __name__ == "__main__": | ||||||||||||||||||||||||||
| main() | ||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Presidio comparison is meant to be reproducible, but these model installs are left as bare names, so
python -m spacy download en_core_web_lg/de_core_news_lgwill install the best compatible model available at rerun time rather than the exact model used for the committed numbers. If spaCy publishes a new compatible model, the same pinnedrequirements.txtcan produce different entities and benchmark results; pin the model wheel versions or direct download names alongside the Python deps.Useful? React with 👍 / 👎.