feat: add NgBvnRecognizer (Nigerian BVN) and KeNationalIdRecognizer (Kenyan national ID)#2169
Open
kingztech2019 wants to merge 11 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds two new country-specific Presidio Analyzer recognizers to cover currently-missing African identifiers: Nigerian BVN (NG_BVN) and Kenyan National ID (KE_NATIONAL_ID). This extends the predefined recognizer catalog and makes the new recognizers configurable via the default registry YAML (disabled by default).
Changes:
- Added
NgBvnRecognizer(Nigeria) with a very-weak 11-digit pattern and context-driven scoring (no checksum). - Added
KeNationalIdRecognizer(Kenya) with a very-weak 7–8 digit pattern and context-driven scoring (no checksum). - Registered both recognizers in
presidio_analyzer/conf/default_recognizers.yamland exported them viapresidio_analyzer/predefined_recognizers/__init__.py.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| presidio-analyzer/tests/test_ng_bvn_recognizer.py | Adds unit tests for BVN detection and validation behavior. |
| presidio-analyzer/tests/test_ke_national_id_recognizer.py | Adds unit tests for Kenyan National ID detection and validation behavior. |
| presidio-analyzer/presidio_analyzer/predefined_recognizers/country_specific/nigeria/ng_bvn_recognizer.py | Introduces the new Nigerian BVN recognizer implementation. |
| presidio-analyzer/presidio_analyzer/predefined_recognizers/country_specific/nigeria/init.py | Exposes NgBvnRecognizer from the Nigeria recognizers package. |
| presidio-analyzer/presidio_analyzer/predefined_recognizers/country_specific/kenya/ke_national_id_recognizer.py | Introduces the new Kenyan National ID recognizer implementation. |
| presidio-analyzer/presidio_analyzer/predefined_recognizers/country_specific/kenya/init.py | Creates the Kenya recognizers package and exports KeNationalIdRecognizer. |
| presidio-analyzer/presidio_analyzer/predefined_recognizers/init.py | Exports the two new recognizers via the predefined recognizers module. |
| presidio-analyzer/presidio_analyzer/conf/default_recognizers.yaml | Registers the new recognizers (disabled by default) with country metadata. |
Comment on lines
+1
to
+4
| import pytest | ||
|
|
||
| from presidio_analyzer.predefined_recognizers import NgBvnRecognizer | ||
| from tests.assertions import assert_result_within_score_range |
Comment on lines
+109
to
+125
| def test_when_bvn_in_text_then_expected_results( | ||
| text, | ||
| expected_len, | ||
| expected_positions, | ||
| expected_score_ranges, | ||
| recognizer, | ||
| entities, | ||
| ): | ||
| results = recognizer.analyze(text, entities) | ||
| assert len(results) == expected_len | ||
|
|
||
| for res, (st_pos, fn_pos), (st_score, fn_score) in zip( | ||
| results, expected_positions, expected_score_ranges | ||
| ): | ||
| assert_result_within_score_range( | ||
| res, entities[0], st_pos, fn_pos, st_score, fn_score | ||
| ) |
Comment on lines
+55
to
+60
| ( | ||
| f"bank verification number: {VALID_BVN_2}", | ||
| 1, | ||
| ((26, 37),), | ||
| ((0.3, 1.0),), | ||
| ), |
Comment on lines
+40
to
+51
| CONTEXT = [ | ||
| "national id", | ||
| "national identity", | ||
| "national identity card", | ||
| "id number", | ||
| "nid", | ||
| "kenyan id", | ||
| "kenya national id", | ||
| "registration number", | ||
| "id card", | ||
| "national registration", | ||
| ] |
Comment on lines
+1
to
+4
| import pytest | ||
|
|
||
| from presidio_analyzer.predefined_recognizers import KeNationalIdRecognizer | ||
| from tests.assertions import assert_result_within_score_range |
Comment on lines
+107
to
+124
| def test_when_ke_national_id_in_text_then_expected_results( | ||
| text, | ||
| expected_len, | ||
| expected_positions, | ||
| expected_score_ranges, | ||
| recognizer, | ||
| entities, | ||
| ): | ||
| results = recognizer.analyze(text, entities) | ||
| assert len(results) == expected_len | ||
|
|
||
| for res, (st_pos, fn_pos), (st_score, fn_score) in zip( | ||
| results, expected_positions, expected_score_ranges | ||
| ): | ||
| assert_result_within_score_range( | ||
| res, entities[0], st_pos, fn_pos, st_score, fn_score | ||
| ) | ||
|
|
Comment on lines
+43
to
+48
| ( | ||
| f"national id: {VALID_ID_8}", | ||
| 1, | ||
| ((13, 21),), | ||
| ((0.3, 1.0),), | ||
| ), |
Comment on lines
+61
to
+66
| ( | ||
| f"national identity card: {VALID_ID_8}", | ||
| 1, | ||
| ((23, 31),), | ||
| ((0.3, 1.0),), | ||
| ), |
Comment on lines
+184
to
+189
| - name: NgBvnRecognizer | ||
| supported_languages: | ||
| - en | ||
| type: predefined | ||
| enabled: false | ||
| country_code: ng |
…tack#2169 - Split both test files into separate "without context" and "with context" parametrized test functions; context-boosted score assertions now run LemmaContextAwareEnhancer with real spaCy NlpArtifacts via the spacy_nlp_engine fixture instead of calling analyze() directly - Fix BVN "bank verification number" context test: add "BVN" token to the text so the single-token "bvn" context entry fires deterministically (LemmaContextAwareEnhancer matches against individual token lemmas, not multi-word phrases) - Add single-token context variants to KeNationalIdRecognizer.CONTEXT: "kenya", "kenyan", "national", "registration" — ensures LemmaContextAwareEnhancer can trigger a boost in typical texts containing those individual tokens - Add NG_BVN and KE_NATIONAL_ID entries to docs/supported_entities.md - Add CHANGELOG.md entries under [unreleased] → Analyzer → Added
…ts import
- BVN: "Please provide your BVN for KYC. Your BVN is {BVN}." prefix is
45 chars, not 46, so match position is (45,56) not (46,57)
- KE: "national identity card: {ID}" prefix is 24 chars, not 23, so
match position is (24,32) not (23,31)
- Remove unused `from presidio_analyzer.nlp_engine import NlpArtifacts`
from both test files; the real spaCy engine is used instead of a
manually constructed NlpArtifacts instance, making the import dead code
that would trigger flake8/ruff linting failures in CI
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Change Description
Adds two new country-specific PII recognizers for African identifiers currently absent from Presidio:
NgBvnRecognizer— Nigerian Bank Verification Number (NG_BVN)The BVN is an 11-digit identifier issued by the Central Bank of Nigeria (CBN) and managed by NIBSS (Nigerian Interbank Settlement System). It links every bank account held by an individual to a single biometric record. Unauthorized disclosure is a criminal offence under the CBN BVN Regulatory Framework (Circular FPR/DIR/GEN/CIR/01/009, 2014).
Design notes:
\b\d{11}\bat score 0.01 — same footprint as the existingNgNinRecognizervalidate_resultreturnsNone(notTrue) for structurally valid matches so context-boosted score is preserved rather than unconditionally promoting toMAX_SCOREbvn,bank verification number,bank verification no,nibss,bank identity numberKeNationalIdRecognizer— Kenyan National Identity Card (KE_NATIONAL_ID)The Kenyan national ID card number is a 7–8-digit sequential identifier issued by the National Registration Bureau under the Registration of Persons Act (Cap. 107). Kenya currently has no recognizers in Presidio — this is the first.
Design notes:
\b\d{7,8}\bat score 0.01 — covers 7-digit (older cards) and 8-digit (current cards)validate_resultreturnsNonefor structurally valid matchesnational id,national identity,nid,kenyan id,kenya national id,id card,national registrationBoth recognizers are registered in
default_recognizers.yamlwithenabled: false.Issue reference
N/A — new recognizer addition for uncovered African PII entities.
Checklist