-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(analyzer): add Taiwan-specific recognizers for national id and phone #2073
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
Open
matheme-justyn
wants to merge
17
commits into
data-privacy-stack:main
Choose a base branch
from
matheme-justyn:codex/issue-2065
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8852e85
test(analyzer): add TW national id recognizer tests
matheme-justyn c61c005
feat(analyzer): add TW national id recognizer
matheme-justyn 4c1db61
docs: document TW national id recognizer
matheme-justyn c65d5b5
test(analyzer): add TW phone recognizer tests
matheme-justyn dac8df7
feat(analyzer): add TW phone recognizer
matheme-justyn 1dff54d
docs: document TW phone recognizer
matheme-justyn 2aefeec
refactor(analyzer): tighten TW recognizer docs and tests
matheme-justyn 87ec90c
fix(analyzer): export TW recognizers
matheme-justyn fb97168
test(analyzer): align TW expectations with matcher behavior
matheme-justyn e78d940
feat(analyzer): load TW recognizers by default config
matheme-justyn 06230b8
test(analyzer): verify TW default recognizer config
matheme-justyn 6333603
fix(test): import recognizer list loader
matheme-justyn 3289310
style(analyzer): format TW recognizer changes
matheme-justyn a86ce6f
Merge upstream/main into codex/issue-2065
matheme-justyn f636dfd
fix(analyzer): address Taiwan recognizer review comments
matheme-justyn 788f4dc
style(analyzer): wrap Taiwan recognizer docstring
matheme-justyn 109e17a
Merge branch 'main' into codex/issue-2065
omri374 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...dio-analyzer/presidio_analyzer/predefined_recognizers/country_specific/taiwan/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| """Taiwan-specific predefined recognizers.""" | ||
|
|
||
| from .tw_national_id_recognizer import TwNationalIdRecognizer | ||
| from .tw_phone_number_recognizer import TwPhoneNumberRecognizer | ||
|
|
||
| __all__ = [ | ||
| "TwNationalIdRecognizer", | ||
| "TwPhoneNumberRecognizer", | ||
| ] |
131 changes: 131 additions & 0 deletions
131
...idio_analyzer/predefined_recognizers/country_specific/taiwan/tw_national_id_recognizer.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| from typing import List, Optional, Tuple, Union | ||
|
|
||
| from presidio_analyzer import EntityRecognizer, Pattern, PatternRecognizer | ||
|
|
||
|
|
||
| class TwNationalIdRecognizer(PatternRecognizer): | ||
| """ | ||
| Recognize Taiwan National Identification Numbers. | ||
|
|
||
| Taiwan national IDs use one leading letter followed by 9 digits. | ||
| The second digit is typically 1 or 2, and the full value follows | ||
| a public checksum rule defined by Taiwan's National Identification Card | ||
| numbering scheme. | ||
|
|
||
| Public reference for format and checksum background: | ||
| https://www.ris.gov.tw/app/portal/3053 | ||
|
|
||
| :param patterns: List of patterns to be used by this recognizer | ||
| :param context: List of context words to increase confidence in detection | ||
| :param supported_language: Language this recognizer supports | ||
| :param supported_entity: The entity this recognizer can detect | ||
| :param replacement_pairs: List of tuples with potential replacement values | ||
| for different strings to be used during pattern matching. | ||
| """ | ||
|
|
||
| COUNTRY_CODE = "tw" | ||
|
|
||
| PATTERNS = [ | ||
| Pattern( | ||
| "TW_NATIONAL_ID", | ||
| r"\b[A-Z][12]\d{8}\b", | ||
| 0.3, | ||
| ), | ||
|
Comment on lines
+29
to
+33
|
||
| ] | ||
|
|
||
| CONTEXT = [ | ||
| "身分證", | ||
| "身分證字號", | ||
| "身份證", | ||
| "身份證字號", | ||
| "國民身分證", | ||
| "national id", | ||
| "id number", | ||
| ] | ||
|
|
||
| LETTER_MAPPING = { | ||
| "A": 10, | ||
| "B": 11, | ||
| "C": 12, | ||
| "D": 13, | ||
| "E": 14, | ||
| "F": 15, | ||
| "G": 16, | ||
| "H": 17, | ||
| "I": 34, | ||
| "J": 18, | ||
| "K": 19, | ||
| "L": 20, | ||
| "M": 21, | ||
| "N": 22, | ||
| "O": 35, | ||
| "P": 23, | ||
| "Q": 24, | ||
| "R": 25, | ||
| "S": 26, | ||
| "T": 27, | ||
| "U": 28, | ||
| "V": 29, | ||
| "W": 32, | ||
| "X": 30, | ||
| "Y": 31, | ||
| "Z": 33, | ||
| } | ||
|
|
||
| WEIGHTS = [1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1] | ||
|
|
||
| def __init__( | ||
| self, | ||
| patterns: Optional[List[Pattern]] = None, | ||
| context: Optional[List[str]] = None, | ||
| supported_language: str = "zh", | ||
| supported_entity: str = "TW_NATIONAL_ID", | ||
| replacement_pairs: Optional[List[Tuple[str, str]]] = None, | ||
| name: Optional[str] = None, | ||
| ): | ||
| self.replacement_pairs = replacement_pairs if replacement_pairs else [] | ||
|
|
||
| patterns = patterns if patterns else self.PATTERNS | ||
| context = context if context else self.CONTEXT | ||
| super().__init__( | ||
| supported_entity=supported_entity, | ||
| patterns=patterns, | ||
| context=context, | ||
| supported_language=supported_language, | ||
| name=name, | ||
| ) | ||
|
|
||
| def validate_result(self, pattern_text: str) -> Union[bool, None]: | ||
| """ | ||
| Validate the pattern logic by running Taiwan ID checksum verification. | ||
|
|
||
| :param pattern_text: The text to validate | ||
| (only the substring detected by the regex engine). | ||
| :return: A bool or None, indicating whether the validation was successful. | ||
| """ | ||
| sanitized_value = EntityRecognizer.sanitize_value( | ||
| pattern_text, self.replacement_pairs | ||
| ) | ||
|
|
||
| if len(sanitized_value) != 10: | ||
| return False | ||
|
|
||
| if not sanitized_value[0].isalpha() or not sanitized_value[1:].isdigit(): | ||
| return False | ||
|
|
||
| sanitized_value = sanitized_value.upper() | ||
|
|
||
| if sanitized_value[0] not in self.LETTER_MAPPING: | ||
| return False | ||
|
|
||
| if sanitized_value[1] not in {"1", "2"}: | ||
| return False | ||
|
|
||
| return self._validate_checksum(sanitized_value) | ||
|
|
||
| def _validate_checksum(self, national_id: str) -> bool: | ||
| """Validate Taiwan National ID using the public checksum algorithm.""" | ||
| mapped_prefix = str(self.LETTER_MAPPING[national_id[0]]) | ||
| digits = [int(digit) for digit in mapped_prefix + national_id[1:]] | ||
| checksum = sum(digit * weight for digit, weight in zip(digits, self.WEIGHTS)) | ||
| return checksum % 10 == 0 | ||
56 changes: 56 additions & 0 deletions
56
...dio_analyzer/predefined_recognizers/country_specific/taiwan/tw_phone_number_recognizer.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| from typing import List, Optional | ||
|
|
||
| from presidio_analyzer.predefined_recognizers.generic.phone_recognizer import ( | ||
| PhoneRecognizer, | ||
| ) | ||
|
|
||
|
|
||
| class TwPhoneNumberRecognizer(PhoneRecognizer): | ||
|
Collaborator
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. Why not define a region in the existing phone number recognizer? any gaps that made it not work for you? |
||
| """ | ||
| Recognize Taiwan phone numbers using python-phonenumbers with TW region hints. | ||
|
|
||
| This recognizer intentionally delegates parsing and validation to the | ||
| generic PhoneRecognizer with the supported region restricted to Taiwan. | ||
|
|
||
| Public reference for numbering background: | ||
| https://www.itu.int/itudoc/itu-t/ob-lists/icc/e164_886.html | ||
|
|
||
| :param context: Base context words for enhancing the assurance scores. | ||
| :param supported_language: Language this recognizer supports | ||
| :param supported_entity: The entity this recognizer can detect | ||
| :param name: Optional recognizer name override | ||
| """ | ||
|
|
||
| COUNTRY_CODE = "tw" | ||
|
|
||
| CONTEXT = [ | ||
| "電話", | ||
| "電話號碼", | ||
| "手機", | ||
| "手機號碼", | ||
| "行動電話", | ||
| "聯絡電話", | ||
| "市話", | ||
| "office phone", | ||
| "phone", | ||
| "phone number", | ||
| "mobile", | ||
| "cell", | ||
| "call", | ||
| "contact", | ||
| ] | ||
|
|
||
| def __init__( | ||
| self, | ||
| context: Optional[List[str]] = None, | ||
| supported_language: str = "zh", | ||
| supported_entity: str = "TW_PHONE_NUMBER", | ||
| name: Optional[str] = None, | ||
| ): | ||
| super().__init__( | ||
| context=context if context else self.CONTEXT, | ||
| supported_language=supported_language, | ||
| supported_entity=supported_entity, | ||
| supported_regions=["TW"], | ||
| name=name, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.