feat(analyzer): add Taiwan-specific recognizers for national id and phone#2073
feat(analyzer): add Taiwan-specific recognizers for national id and phone#2073matheme-justyn wants to merge 17 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Adds Taiwan (TW) country-specific predefined recognizers to presidio-analyzer for detecting Taiwan National ID numbers and Taiwan phone numbers, including default-registry configuration, unit tests, and public documentation updates.
Changes:
- Introduce
TwNationalIdRecognizer(checksum-validated) andTwPhoneNumberRecognizer(TW-region wrapper over genericPhoneRecognizer). - Register both recognizers in
default_recognizers.yaml(disabled by default) and expose them viapresidio_analyzer.predefined_recognizers. - Add unit tests and update supported-entities docs and changelog entries.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| presidio-analyzer/presidio_analyzer/predefined_recognizers/country_specific/taiwan/tw_national_id_recognizer.py | New TW national ID recognizer with checksum validation. |
| presidio-analyzer/presidio_analyzer/predefined_recognizers/country_specific/taiwan/tw_phone_number_recognizer.py | New TW phone recognizer wrapper restricting validation to region TW. |
| presidio-analyzer/presidio_analyzer/predefined_recognizers/init.py | Exposes Taiwan recognizers from the predefined recognizers package. |
| presidio-analyzer/presidio_analyzer/conf/default_recognizers.yaml | Adds disabled-by-default YAML entries for the two TW recognizers with country_code: tw. |
| presidio-analyzer/tests/test_tw_national_id_recognizer.py | Adds unit coverage for TW national ID detection and checksum validation. |
| presidio-analyzer/tests/test_tw_phone_number_recognizer.py | Adds unit coverage for TW phone number detection and metadata defaults. |
| presidio-analyzer/tests/test_recognizer_registry.py | Adds registry-level test ensuring YAML entries exist and classes resolve via loader. |
| docs/supported_entities.md | Documents the new TW entity types (needs section placement/content correction per review). |
| CHANGELOG.md | Notes the addition of the two Taiwan recognizers under Unreleased. |
|
@microsoft-github-policy-service agree |
| PATTERNS = [ | ||
| Pattern( | ||
| "TW_NATIONAL_ID", | ||
| r"\b[A-Z][12]\d{8}\b", |
There was a problem hiding this comment.
@matheme-justyn consider using a different boundary.
| ), | ||
| ( | ||
| "National ID A100000001 belongs to the applicant.", | ||
| 1, |
omri374
left a comment
There was a problem hiding this comment.
Thanks! left a couple of minor comments.
| PATTERNS = [ | ||
| Pattern( | ||
| "TW_NATIONAL_ID", | ||
| r"\b[A-Z][12]\d{8}\b", |
There was a problem hiding this comment.
@matheme-justyn consider using a different boundary.
| ) | ||
|
|
||
|
|
||
| class TwPhoneNumberRecognizer(PhoneRecognizer): |
There was a problem hiding this comment.
Why not define a region in the existing phone number recognizer? any gaps that made it not work for you?
| Pattern( | ||
| "TW_NATIONAL_ID", | ||
| r"\b[A-Z][12]\d{8}\b", | ||
| 0.3, | ||
| ), |
| ((6, 16),), | ||
| ((0.5, 1.0),), | ||
| ), | ||
| ( |
Change Description
This PR adds Taiwan-specific predefined recognizers in
presidio-analyzer:TW_NATIONAL_IDTW_PHONE_NUMBERThis iteration intentionally does not include Taiwan Unified Business Number.
Issue reference
Fixes #2065
Public references
Taiwan National ID
Taiwan phone numbers
What is included
TW_NATIONAL_IDTW_NATIONAL_IDrecognizer with checksum validationTW_NATIONAL_IDTW_PHONE_NUMBERTW_PHONE_NUMBERrecognizer using Taiwan (TW) regional phone validationTW_PHONE_NUMBERImplementation notes
TW_NATIONAL_IDfirst matches one leading letter followed by 9 digits, restricts the second digit to1or2, and then validates the candidate with the Taiwan ID checksum calculation implemented locally in Presidio.TW_PHONE_NUMBERfollows the upstream Taiwan phone-validation flow: the input is normalized,+886or regionTWis used to determine the country code, the national trunk prefix0is stripped, and the resulting national number is checked against Taiwan metadata by length and full^(?:pattern)$regular-expression match across the supported number types (for example mobile, fixed line, toll-free, and others).Notes
TW_PHONE_NUMBERis intentionally implemented as a thin Taiwan-specific wrapper around the existing genericPhoneRecognizer, restricting validation to theTWregion instead of duplicating parsing logic.default_recognizers.yamlwithenabled: false, matching the existing convention for many country-specific recognizers.Tests
Executed locally:
../.venv/bin/python -m pytest tests/test_tw_national_id_recognizer.py tests/test_tw_phone_number_recognizer.py../.venv/bin/python -m pytest tests/test_tw_national_id_recognizer.py tests/test_tw_phone_number_recognizer.py tests/test_recognizer_registry.py -k 'tw or country or default_yaml'../.venv/bin/python -m ruff check tests/test_tw_national_id_recognizer.py tests/test_tw_phone_number_recognizer.py presidio_analyzer/predefined_recognizers/country_specific/taiwan/tw_national_id_recognizer.py presidio_analyzer/predefined_recognizers/country_specific/taiwan/tw_phone_number_recognizer.py presidio_analyzer/predefined_recognizers/__init__.pyResult:
63 passed, 19 deselectedruff checkpassed for the touched Python filesChecklist