Skip to content

Canada - Add a recognizer for Quebec health insurance numbers (RAMQ / NAM) - #2483

Closed
mdeloupy wants to merge 1 commit into
data-privacy-stack:mainfrom
mdeloupy:feat/ca-ramq-recognizer
Closed

mdeloupy wants to merge 1 commit into
data-privacy-stack:mainfrom
mdeloupy:feat/ca-ramq-recognizer

Conversation

@mdeloupy

Copy link
Copy Markdown

Change Description

Adds CaRamqRecognizer, a pattern-based recognizer for Quebec health insurance numbers (RAMQ, also called "NAM"), entity CA_RAMQ.

  • Format: 4 letters followed by 8 digits, with an optional space or dash between any two groups (ABCD 1234 5678, ABCD-12-34-56-78, ABCD12345678). Matching is case-insensitive, like the other Canadian recognizers.
  • Scores: two mutually exclusive patterns, so each number matches once.
    • CA RAMQ (strong), 0.65: the embedded birth date is plausible (month 01-12 for men or 51-62 for women, day 01-31).
    • CA RAMQ (medium), 0.5: any other 4 letters + 8 digits.
    • The date only ranks a match and never drops it, so a number with an odd date still matches at 0.5 and the score threshold decides.
  • No checksum: no check digit is published for this number, so there is no validate_result and no match is promoted to 1.0.
  • Context words (+0.35, capped at 1.0): ramq, assurance maladie, health insurance, carte soleil, health card, medicare card. Bare nam and card are left out on purpose: substring matching makes them fire on "dynamic", "naming" and similar words.
  • Languages and defaults: en and fr. Country-specific, so COUNTRY_CODE = "ca" and enabled: false by default.
  • Registered in: the three __init__.py exports, conf/default_recognizers.yaml, and docs/supported_entities.md.

Existing behavior: no change. The PR adds new files and additive lines only (exports, a YAML entry that ships disabled, a docs row). No existing recognizer's patterns, scores or context are touched.

Impact on detection accuracy: none for existing users, because the recognizer is disabled by default. When enabled, it has an expected false-positive surface: with no check digit, any token shaped like 4 letters + 8 digits matches (for example an order reference such as ABCD12345678 scores 0.5, or 0.65 if its digits happen to look like a date). Context words raise real matches, and a score threshold filters the rest. The format (surname letters, birth year, month with 50 added for women, day, two administrative digits) comes from secondary sources : https://tools.tnaado.ca/canadian/ramq-number-validator/

Impact on security: no PII is logged. The patterns use bounded quantifiers only, and I timed them on 10k-24k character adversarial inputs (long runs of letters, digits, and repeated ABCD 85 ): at most about 4 ms, so no catastrophic backtracking.

How it was tested: unit tests with synthetic values only (no real RAMQ numbers), and no evaluation on a dataset.

Issue reference

Fixes #2482

Tests

presidio-analyzer/tests/test_ca_ramq_recognizer.py, 61 tests, 100% coverage of the new module:

  • format and separator variants with exact start/end positions, including values embedded in text
  • lookalike negatives (3 or 5 letters, 7 digits, SKU- and invoice-style strings)
  • birth-date boundaries (months 00/01/12/13/50/51/62/63, days 00/01/31/32): each number matches exactly once, at the expected score
  • exact scores with and without context, covering prefix versus suffix placement and the excluded short words
  • the constructor accepts the loader's kwargs
  • configuration path: loaded through RecognizerRegistryProvider from default_recognizers.yaml in en and fr (the fr case sets the top-level supported_languages), and from a temporary YAML with enabled: true, asserting detection

Checklist

  • I have reviewed the contribution guidelines
  • I agree to follow this project's Code of Conduct
  • I confirm that I have the right to submit this contribution and that it does not knowingly contain proprietary or confidential code.
  • My code includes unit tests
  • All unit tests and lint checks pass locally
  • My PR contains documentation updates / additions if required
  • My PR description states any change to existing behavior and any possible impact on detection accuracy or security
  • If I added or changed a recognizer, it has a test that enables it in a YAML configuration and loads it through RecognizerRegistryProvider

Pattern-based recognizer for RAMQ (NAM) numbers: 4 letters followed by
8 digits, with optional space or dash separators. No check digit is
published, so none is checked and no score is promoted to 1.0.

Two mutually exclusive patterns rank a match by its embedded birth date:
strong (0.65) when the month is 01-12 or 51-62 and the day is 01-31,
medium (0.5) otherwise. The date never drops a match.

Disabled by default; registered in the YAML, the exports and the
supported entities docs, with a configuration-path test.
@mdeloupy

Copy link
Copy Markdown
Author

Closing this in favour of canpii (https://github.com/ArthurJZL/canpii), which already provides a RAMQ recognizer with the official sources, to avoid two overlapping efforts. Thanks to @ArthurJZL for the sources and the review offer. Discussion continues on #2482.

@mdeloupy mdeloupy closed this Nov 17, 2026
@omri374

omri374 commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

@mdeloupy how about adding canpii to the community list in Presidio? This would help users find the Canadian recognizers and models more easily.

@ArthurJZL ArthurJZL mentioned this pull request Nov 19, 2026
5 of 8 tasks
@ArthurJZL

Copy link
Copy Markdown
Contributor

Thanks @omri374, good idea. As the canpii maintainer I opened #2516 to add it to the community list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants