phone_numbers
phone_numbers
¶
Phone number helpers backed by libphonenumber, shared by PII replacement and its evaluation.
Functions:
| Name | Description |
|---|---|
phone_region |
Return the libphonenumber region of |
phone_identity |
Return the E.164 form of the phone number |
phone_in_layout |
Write |
country_code_prefix |
Return the start of |
kept_phone_digit_count |
Return how many leading digits of |
phone_region(locale)
¶
Return the libphonenumber region of locale's country, or None if it names no supported one.
The region is used to read phone numbers written without a country code.
Example
phone_region("en_US") is "US", phone_region("fr_CA") is "CA", and phone_region("en")
is None.
Source code in src/nemo_safe_synthesizer/pii_replacer/phone_numbers.py
phone_identity(text, region)
¶
Return the E.164 form of the phone number text, with its extension, or None if it is not one.
Two ways of writing one number give the same identity. region reads numbers written without a
country code; with None, only numbers that start with + have an identity.
Example
With region "US", "(555) 123-4567", "555.123.4567", and "+1 555 123 4567" all give
"+15551234567", and "555-123-4567 x89" gives "+15551234567;ext=89".
Source code in src/nemo_safe_synthesizer/pii_replacer/phone_numbers.py
phone_in_layout(occurrence, replacement)
¶
Write replacement's digits in occurrence's layout, or return replacement if they do not fit.
One original number written in several ways shares a replacement, and each occurrence keeps its own
layout, including its country code or the lack of one. The digits after any country code must be as
many in both, so a national trunk prefix such as the UK's leading 0 makes them not fit.
Example
phone_in_layout("555.123.4567", "(812) 904-3321") is "812.904.3321",
phone_in_layout("+1 555 123 4567", "(812) 904-3321") is "+1 812 904 3321", and
phone_in_layout("020 7946 0958", "+44 81 2604 9135") is "+44 81 2604 9135".
Source code in src/nemo_safe_synthesizer/pii_replacer/phone_numbers.py
country_code_prefix(phone)
¶
Return the start of phone through its country code, or "" if it has none.
The code is read with libphonenumber, so it is found even without a separator after it, as in
"+442079460958". Only the code needs to be known; the rest of the number need not be valid.
Example
country_code_prefix("+442079460958") is "+44", country_code_prefix("001-555") is
"001", and country_code_prefix("+999 123") is "", since 999 is not a country code.
Source code in src/nemo_safe_synthesizer/pii_replacer/phone_numbers.py
kept_phone_digit_count(phone, region)
¶
Return how many leading digits of phone a replacement keeps: its country code and area code.
The area code, or a mobile number's network prefix, is libphonenumber's national destination code,
so where it ends depends on the country. A national trunk prefix written before it, such as the UK's
leading 0, is kept with it. At most half of the national number is kept, so countries without
area codes, such as Singapore, where libphonenumber reports the first group instead, still get most
of their digits replaced. region reads a number written without a country code; when the number
cannot be read, or it has no area code, only a country code is kept.
Example
kept_phone_digit_count("(415) 555-2671", "US") is 3, for 415;
kept_phone_digit_count("+44 20 7946 0958", None) is 4, for 44 and 20; and
kept_phone_digit_count("12345", "US") is 0.