-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add Indian UPI ID recognizer for NPCI payment compliance #2036
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
shauryaraghav
wants to merge
10
commits into
data-privacy-stack:main
Choose a base branch
from
shauryaraghav:main
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
10 commits
Select commit
Hold shift + click to select a range
6b561cc
Add Indian UPI ID recognizer for NPCI payment compliance
shauryaraghav e55bc51
Remove accidentally committed file
shauryaraghav 4989a1b
Potential fix for pull request finding
shauryaraghav 3b5bb1d
Merge branch 'main' into main
shauryaraghav d8f742a
Merge branch 'main' into main
shauryaraghav 6830345
Add InUpiRecognizer to default recognizers
shauryaraghav 6d1655a
Enhance UPI recognizer tests with invalid cases
shauryaraghav e3d0cac
Add UPI ID support to supported entities documentation
shauryaraghav e06dbb8
Update CHANGELOG with new features and fixes
shauryaraghav 417c1ab
Merge branch 'main' into main
SharonHart 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
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
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
65 changes: 65 additions & 0 deletions
65
...yzer/presidio_analyzer/predefined_recognizers/country_specific/india/in_upi_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,65 @@ | ||
| from typing import List, Optional | ||
| from presidio_analyzer import Pattern, PatternRecognizer | ||
|
|
||
|
|
||
| class InUpiRecognizer(PatternRecognizer): | ||
| """ | ||
| Recognizes Indian UPI (Unified Payments Interface) IDs. | ||
|
|
||
| UPI IDs are used for digital payments in India and follow the format: | ||
| username@bankhandle (e.g., shaurya@okicici, 9876543210@paytm) | ||
|
|
||
| Common UPI handles include: okicici, okhdfcbank, okaxis, paytm, | ||
| ybl, upi, apl, ibl, axl, timecosmos, waicici, wahdfcbank | ||
|
|
||
| This recognizer identifies UPI IDs using regex and context words. | ||
| Reference: https://www.npci.org.in/what-we-do/upi/product-overview | ||
|
|
||
| :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 | ||
| """ | ||
|
shauryaraghav marked this conversation as resolved.
|
||
|
|
||
| COUNTRY_CODE = "in" | ||
|
|
||
| PATTERNS = [ | ||
| Pattern( | ||
| "UPI ID (High)", | ||
| r"\b([a-zA-Z0-9.\-_]{2,256}@(okicici|okhdfcbank|okaxis|oksbi|paytm|ybl|upi|apl|ibl|axl|waicici|wahdfcbank|timecosmos|rapl|mbk|ikwik|freecharge))(?![\w.-])", | ||
| 0.7, | ||
| ), | ||
| Pattern( | ||
| "UPI ID (Medium)", | ||
| r"\b([a-zA-Z0-9.\-_]{2,256}@[a-zA-Z]{2,64})(?![\w.-])", | ||
| 0.2, | ||
| ), | ||
| ] | ||
|
|
||
| CONTEXT = [ | ||
| "upi", | ||
| "upi id", | ||
| "payment", | ||
| "gpay", | ||
| "phonepe", | ||
| "paytm", | ||
| "bhim", | ||
| "transfer", | ||
| "pay", | ||
| ] | ||
|
|
||
| def __init__( | ||
| self, | ||
| patterns: Optional[List[Pattern]] = None, | ||
| context: Optional[List[str]] = None, | ||
| supported_language: str = "en", | ||
| supported_entity: str = "IN_UPI", | ||
| ): | ||
| 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, | ||
| ) | ||
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,59 @@ | ||
| import pytest | ||
| from tests import assert_result | ||
| from presidio_analyzer.predefined_recognizers import InUpiRecognizer | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def recognizer(): | ||
| return InUpiRecognizer() | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def entities(): | ||
| return ["IN_UPI"] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "text, expected_len, expected_position, expected_score", | ||
| [ | ||
| # fmt: off | ||
| # Valid UPI IDs with known handles (High confidence) | ||
| ("shaurya@okicici", 1, (0, 15), 0.7), | ||
| ("9876543210@paytm", 1, (0, 16), 0.7), | ||
| ("john.doe@okhdfcbank", 1, (0, 19), 0.7), | ||
| ("user123@ybl", 1, (0, 11), 0.7), | ||
| # Invalid UPI IDs | ||
| ("notaupiid", 0, (), ()), | ||
| ("@okicici", 0, (), ()), | ||
| ("john.doe@gmail.com", 0, (), ()), | ||
| ("admin@localhost", 0, (), ()), | ||
| ("user@company.com", 0, (), ()), | ||
|
|
||
| # Valid UPI IDs with unknown handles (Medium confidence) | ||
| ("myname@somebank", 1, (0, 15), 0.4), | ||
|
|
||
|
|
||
|
|
||
| # UPI in sentence | ||
| ("Please pay to shaurya@okicici for the order", 1, (14, 29), 0.7), | ||
| # fmt: on | ||
| ], | ||
| ) | ||
| def test_when_upi_in_text_then_all_upis_found( | ||
| text, | ||
| expected_len, | ||
| expected_position, | ||
| expected_score, | ||
| recognizer, | ||
| entities, | ||
| ): | ||
| results = recognizer.analyze(text, entities) | ||
| assert len(results) == expected_len | ||
| if results: | ||
| assert_result( | ||
| results[0], | ||
| entities[0], | ||
| expected_position[0], | ||
| expected_position[1], | ||
| expected_score, | ||
| ) |
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.