Skip to content

Use the phonenumbers library for phone number detection instead of the NANP-only regex #49

Description

@jzonthemtn

Summary

Replace the hand-rolled, NANP-only phone number regex in phileas-python with detection backed by the phonenumbers package (the Python port of Google's libphonenumber), matching what the Java Phileas already does. This closes the international-phone detection gap.

Background

phileas/filters/phone_number_filter.py detects with a set of North-American Numbering Plan regex patterns (optional +1, strict 3-3-4). Numbers in other formats ship unredacted: +44 20 7946 0958, +33 1 42 68 53 00, +91 98765 43210, +49 30 901820, and national-format foreign numbers all fail the patterns. phonenumbers is not currently a dependency.

This is the same fidelity gap that was found in the .NET runtime (its regex-only phone filter). The Java Phileas already avoids it: it detects via libphonenumber (PhoneNumberUtil.findNumbers(input, "US", Leniency.POSSIBLE, ...)). The fix is to bring phileas-python to the same footing.

Proposed change

  • Add phonenumbers as a dependency.
  • Detect phone numbers with phonenumbers.PhoneNumberMatcher(text, region, leniency=Leniency.POSSIBLE) (default region "US"), mirroring the Java filter: +-prefixed international numbers are found regardless of region, and bare national-format numbers are found for the default region.
  • This is not a regex swap: PhoneNumberMatcher is a scanner. Introduce a matcher-based phone filter rather than editing the pattern list. Retain the existing regex only if needed as a fallback for parity; otherwise remove it.
  • Keep behavior aligned with the Java filter (same default region and leniency) so the runtimes converge.

Relationship to #48

This is the prerequisite for #48 (support the phone number filter region property). Once detection runs through phonenumbers, #48 makes the default region configurable per policy (string or array). This issue closes the international-detection gap on its own, with the region still hardcoded to "US" (matching Java today).

Acceptance criteria

  • phileas-python depends on phonenumbers.
  • Phone detection uses phonenumbers (default region "US", leniency comparable to libphonenumber POSSIBLE).
  • The international examples (+44 20 7946 0958, +33 1 42 68 53 00, +91 98765 43210, +49 30 901820) are detected and redacted.
  • NANP formats that worked before still match ((555) 123-4567, +1 555 123 4567, 555-123-4567) - no regression.
  • Behavior matches the Java Phileas phone filter for the same inputs (parity check).
  • Tests cover both the international examples and the NANP regression set.

Unblocks #48.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions