-
Notifications
You must be signed in to change notification settings - Fork 437
Add min_candidate_length parameter to PhoneNumberMatcher #319
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
Merged
+41
−7
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -456,7 +456,8 @@ class PhoneNumberMatcher(object): | |
| _DONE = 2 | ||
|
|
||
| def __init__(self, text, region, | ||
| leniency=Leniency.VALID, max_tries=65535): | ||
| leniency=Leniency.VALID, max_tries=65535, | ||
| min_candidate_length=1): | ||
| """Creates a new instance. | ||
|
|
||
| Arguments: | ||
|
|
@@ -471,6 +472,9 @@ def __init__(self, text, region, | |
| max_tries -- The maximum number of invalid numbers to try before | ||
| giving up on the text. This is to cover degenerate cases where | ||
| the text has a lot of false positives in it. Must be >= 0. | ||
| min_candidate_length -- The minimum length of a candidate phone number. | ||
| Can be used to quickly skip candidates that are too short to be valid, | ||
| depending on your use-case needs. | ||
| """ | ||
| if leniency is None: | ||
| raise ValueError("Need a leniency value") | ||
|
|
@@ -487,6 +491,8 @@ def __init__(self, text, region, | |
| self.leniency = leniency | ||
| # The maximum number of retries after matching an invalid number. | ||
| self._max_tries = int(max_tries) | ||
| # The minimum length of a candidate phone number. | ||
| self._min_candidate_length = int(min_candidate_length) | ||
| # The iteration tristate. | ||
| self._state = PhoneNumberMatcher._NOT_READY | ||
| # The last successful match, None unless in state _READY | ||
|
|
@@ -513,13 +519,18 @@ def _find(self, index): | |
| # 123 45 67 / 68). | ||
| candidate = self._trim_after_first_match(_SECOND_NUMBER_START_PATTERN, | ||
| candidate) | ||
| candidate_len = len(candidate) | ||
|
|
||
| # UPSTREAM DIVERGENCE: The min_candidate_length is Python-specific | ||
| # feature, not present in the upstream Java version. | ||
| if candidate_len >= self._min_candidate_length: | ||
|
Owner
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. Please could we add a comment here to indicate that the Python code is diverging from the upstream code. |
||
| match = self._extract_match(candidate, start) | ||
| if match is not None: | ||
| return match | ||
| self._max_tries -= 1 | ||
|
|
||
| match = self._extract_match(candidate, start) | ||
| if match is not None: | ||
| return match | ||
| # Move along | ||
| index = start + len(candidate) | ||
| self._max_tries -= 1 | ||
| index = start + candidate_len | ||
| match = _PATTERN.search(self.text, index) | ||
| return None | ||
|
|
||
|
|
||
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
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.
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.
I'm not normally keen on the Python port having divergences from the upstream Java code, but this looks useful and the default value for the argument means that it's back-compatible with any existing client use.
Please could you add some unit tests for it, and mark them as Python-specific?