Skip to main content

presidio-ru-recognizers

PyPI Python License CI

Russian PII recognizers for Microsoft Presidio. Распознаватели российских персональных данных для Microsoft Presidio.

Drop-in PatternRecognizer subclasses with strict checksum validation for seven Russian identifier types:

Entity What Checksum?
INN_RU ИНН — 10 (юрлицо) или 12 (физлицо/ИП) да (per-length)
SNILS СНИЛС — XXX-XXX-XXX YY или XXXXXXXXXXX да
OGRN ОГРН — 13 цифр (юрлицо) да
OGRNIP ОГРНИП — 15 цифр (ИП) да
PASSPORT_RF Паспорт РФ — 4 (серия) + 6 (номер) нет (нет в спеке)
PHONE_RF Российский телефон +7 / 8 формат + ровно 11 цифр
BANK_ACCOUNT_RF Расчётный счёт юрлица — 20 цифр regex-only*

* Полная БИК-зависимая КС счёта требует справочника ЦБ РФ — за scope этого пакета.


Why a separate package?

Microsoft Presidio's default recognizers cover English-locale PII (US SSN, US passport, UK NHS, etc.) — там нет ничего про РФ. Делать regex для ИНН/ОГРН без контрольной суммы — почти бесполезно: на любом log'е с длинными цифровыми ID получите false-positive rate под 90%. Этот пакет закрывает gap для compliance-задач (152-ФЗ, GDPR в части data-subject identifiers, audit-логи).

Англ.: Presidio ships English-locale recognizers only. Russian identifiers without their checksums produce 90%+ false positive rates on any log containing long numeric IDs (timestamps, hashes, internal PKs). This package wires the official Federal Tax Service / Pension Fund / state-registration checksums into Presidio recognizers so detections arrive validated.


Installation

pip install presidio-ru-recognizers

Requires presidio-analyzer >= 2.2. Pure stdlib for the checksums — no extra runtime dependencies.


Quick start / Быстрый старт

from presidio_analyzer import AnalyzerEngine, RecognizerRegistry
from presidio_analyzer.nlp_engine import NlpEngineProvider

from presidio_ru_recognizers import register_all_ru_recognizers

# Configure NLP — Presidio needs *some* spaCy model loaded even if our
# recognizers are pattern-based. Map 'ru' to the English small model
# as a stub if you don't have a Russian spaCy model set up.
nlp = NlpEngineProvider(nlp_configuration={
    "nlp_engine_name": "spacy",
    "models": [
        {"lang_code": "en", "model_name": "en_core_web_sm"},
        {"lang_code": "ru", "model_name": "en_core_web_sm"},
    ],
}).create_engine()

registry = RecognizerRegistry(supported_languages=["en", "ru"])
register_all_ru_recognizers(registry)

analyzer = AnalyzerEngine(
    registry=registry,
    nlp_engine=nlp,
    supported_languages=["en", "ru"],
)

text = (
    "ООО Сбербанк, ИНН 7707083893, ОГРН 1027700132195. "
    "Тел +7 (495) 500-55-50. Паспорт 4505 678901."
)

for r in analyzer.analyze(text=text, language="ru"):
    print(f"{r.entity_type:20s} score={r.score:.2f}  '{text[r.start:r.end]}'")

Output:

INN_RU               score=1.00  '7707083893'
OGRN                 score=1.00  '1027700132195'
PHONE_RF             score=0.60  '+7 (495) 500-55-50'
PASSPORT_RF          score=0.40  '4505 678901'

score=1.0 означает «прошло КС-валидацию». Паспорт без КС идёт с базовым 0.4 — поднимайте контекстными словами через ContextAwareEnhancer.

Регистрация одного recognizer'а

from presidio_ru_recognizers import InnRecognizer

analyzer.registry.add_recognizer(InnRecognizer())

Overlapping spans

Presidio runs every recognizer independently and returns all matches. Это значит, что 10-цифровой ИНН в тексте also совпадёт с PASSPORT_RF (поскольку 4+6 без сепаратора — те же 10 цифр). Сравнение очков снимает неоднозначность: КС-валидированный ИНН будет иметь score=1.0, паспорт — 0.4. Берите highest-score-wins:

from collections import defaultdict

results = analyzer.analyze(text=text, language="ru")
by_span: dict[tuple[int, int], list] = defaultdict(list)
for r in results:
    by_span[(r.start, r.end)].append(r)
final = [max(group, key=lambda r: r.score) for group in by_span.values()]

Или используйте decision_process=True и presidio-anonymizer — он применяет тот же tie-break автоматически.

Использование чистых КС-функций без Presidio

from presidio_ru_recognizers import inn10, snils, ogrn

inn10("7707083893")        # True (Sberbank)
inn10("7707083892")        # False (wrong checksum)
snils("112-233-445 95")    # True
ogrn("1027700132195")      # True

Entity reference

INN_RU — ИНН

  • 10 цифр (юрлицо): веса [2,4,10,3,5,9,4,6,8], КС = Σ % 11 % 10.
  • 12 цифр (физлицо/ИП): два каскадных КС.
from presidio_ru_recognizers import InnRecognizer
analyzer.registry.add_recognizer(InnRecognizer())

SNILS

  • XXX-XXX-XXX YY — каноничный формат.
  • XXXXXXXXXXX — bare 11 цифр (нижний score, чтобы не ловить телефоны).
  • Special-case для ранних выпусков (≤ 001-001-998): КС всегда 00.

OGRN / OGRNIP

  • ОГРН (13): int(first_12) % 11 % 10 == digit[12].
  • ОГРНИП (15): int(first_14) % 13 % 10 == digit[14].

PASSPORT_RF

\d{4}[\s\-]?\d{6} со word-boundary. КС не существует, поэтому базовый score 0.4 — повышайте через context.

PHONE_RF

+7 или 8 + 10 цифр; разделители -(). допускаются. После strip — ровно 11 цифр.

BANK_ACCOUNT_RF

20 цифр. КС зависит от БИК (справочник ЦБ РФ — out of scope здесь); держите низкий score и опирайтесь на контекстные слова (р/с, расчётный счёт).


Sponsored by Brikko

This package was extracted from the Brikko Gateway — the legal Russian gateway to OpenAI, Anthropic, Google, DeepSeek, YandexGPT and GigaChat from a single rouble account, with smart routing, failover, and full closing documents (договор, акт, чек).

If you build LLM apps for Russian companies, Brikko is the legal entry point. Try it at brikko.ru or follow development at github.com/brikkoAI.

This recognizer package will stay community-maintained and brand-neutral under MIT — Brikko sponsors it because we eat our own dog food (every LLM request through Brikko Gateway runs through these checksums before hitting a provider).


Development

git clone https://github.com/brikkoAI/presidio-ru-recognizers
cd presidio-ru-recognizers
pip install -e ".[dev]"
python -m spacy download en_core_web_sm
pytest                       # 61 tests
ruff check .
mypy presidio_ru_recognizers

Adding a new recognizer

  1. Add the algorithm to presidio_ru_recognizers/checksums.py (pure stdlib).
  2. Subclass PatternRecognizer in a new module — see inn.py as the canonical example.
  3. Register the class in _RECOGNIZER_CLASSES in __init__.py.
  4. Add fixtures + tests in tests/.
  5. PR welcome.

License

MIT. See LICENSE.

The checksum algorithms themselves are public mathematical procedures defined by Russian federal regulators (ФНС, ПФР, ЦБ РФ) — not subject to copyright. The Python implementations here are released under MIT for maximum compatibility with the upstream Microsoft Presidio license.

Metadata

Release files for presidio-ru-recognizers 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for presidio-ru-recognizers 0.1.0
File Size Uploaded Provenance Actions
presidio_ru_recognizers-0.1.0.tar.gz 19.0 kB Without provenance Details for presidio_ru_recognizers-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for presidio-ru-recognizers 0.1.0
File Interpreter ABI Platform Actions
presidio_ru_recognizers-0.1.0-py3-none-any.whl Python 3 none any Details for presidio_ru_recognizers-0.1.0-py3-none-any.whl

Total release size: 38.8 kB

Release files / presidio_ru_recognizers-0.1.0.tar.gz

Download URL presidio_ru_recognizers-0.1.0.tar.gz
Size 19.0 kB
Tags Source
SHA-256 checksum
How to use checksums
10a38e839ed7b26cffd3f6f1b9bda982212a3a6bad79afb2c4a2ff2aae89c6e7
BLAKE2b-256 checksum
How to use checksums
a6af5bb39c2b8bc89eb38af404ece8a73d48ddcd20bae49b16ca65bdb29168a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release files / presidio_ru_recognizers-0.1.0-py3-none-any.whl

Download URL presidio_ru_recognizers-0.1.0-py3-none-any.whl
Size 19.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a835f9012fc27478661de4fcae3a4c624cb8db1cdb33c46762bb44641c47fddc
BLAKE2b-256 checksum
How to use checksums
1afc41662ef4cebd5d14cd68c384727623ad85ee22433b148b4999558401d6ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Security for presidio-ru-recognizers 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page