Skip to content

credencestack._internal

Shared utilities, with no stability promise. Each concern has exactly one module, and tests/unit/test_structure.py fails if a copy appears elsewhere:

Module What it holds
canonical Canonical JSON: the one encoding everything hashed or signed uses
hashing SHA-256 of bytes, text, canonical JSON, a file, a set of files; short hashes for confidential ids
io Reading YAML and JSON files (read_data, parse_data) and formatting validation errors
seeding Seed derivation (derive, derive_seed), private RNGs, fresh seeds
stats Wilson and Newcombe intervals, Cohen's kappa
fmt Number formatting for the console and reports: rates, intervals, money, durations
log The credencestack.* logger tree (get_logger)
ids, outputs Output identifiers, output directories and latest-<kind> pointers
matching Name matching (below)

Other modules import them; users should not.

API

Name Kind Signature Purpose Errors Effects

Tests

  • tests/unit/test_canonical.py
  • tests/unit/test_forge_generator.py
  • tests/unit/test_forge_mutators.py
  • tests/unit/test_inspect_runner.py
  • tests/unit/test_matching.py
  • tests/unit/test_otel_workflows.py
  • tests/unit/test_stats.py

Name matching: credencestack._internal.matching

In-house name matching, standard library only: Unicode normalisation, transliteration and spelling-variant tables, a homoglyph map, Levenshtein distance, Jaro and Jaro-Winkler similarity, and name tokenising, reordering, splitting and joining. Used by screening (the fincrime world and grader) and onboarding (PEP and adverse-media lookups).

Responsibilities

  • Normalise a name so variants compare equal: NFKC, look-alike letters, casefold, transliteration, diacritics, punctuation, optional spelling variants (normalise_name).
  • Score two names and say which view matched: exact, Jaro-Winkler, reordered words, joined or split words, or the word-set score (compare_names).

Non-goals

  • Phonetic matching, nickname tables, or a production screening engine.

Examples

Two names to a score

examples/dev/matching.py
"""Matching: two names -> the best score and the view that produced it."""

from __future__ import annotations

from credencestack._internal.matching import (
    compare_names,
    jaro_winkler,
    levenshtein,
    normalise_name,
)
from examples.dev._common import show

show("normalise_name('  José MÜLLER-Ørsted ')", normalise_name("  José MÜLLER-Ørsted "))
show("jaro_winkler('MARTHA', 'MARHTA')", round(jaro_winkler("MARTHA", "MARHTA"), 6))
show("levenshtein('kitten', 'sitting')", levenshtein("kitten", "sitting"))
for left, right in [
    ("KERROW Ivana", "Ivana Kerrow"),  # reordered
    ("Іvаna Kеrrоw", "Ivana Kerrow"),  # Cyrillic look-alike letters
    ("Kasimir Vandal Orne", "Kasimir Vandalorne"),  # split surname
    ("Yousouf Haldarov", "Yusuf Khaldarov"),  # romanisation variants
    ("Petra Lindqvist", "Petra Lindqvarn"),  # a near miss
]:
    m = compare_names(left, right)
    show(f"compare_names({left!r}, {right!r})", {"score": m.score, "method": m.method})
# normalise_name('  José MÜLLER-Ørsted ')
"jose muller orsted"
# jaro_winkler('MARTHA', 'MARHTA')
0.961111
# levenshtein('kitten', 'sitting')
3
# compare_names('KERROW Ivana', 'Ivana Kerrow')
{
  "score": 1.0,
  "method": "token_reorder"
}
# compare_names('Іvаna Kеrrоw', 'Ivana Kerrow')
{
  "score": 1.0,
  "method": "exact"
}
# compare_names('Kasimir Vandal Orne', 'Kasimir Vandalorne')
{
  "score": 1.0,
  "method": "split_join"
}
# compare_names('Yousouf Haldarov', 'Yusuf Khaldarov')
{
  "score": 1.0,
  "method": "exact"
}
# compare_names('Petra Lindqvist', 'Petra Lindqvarn')
{
  "score": 0.9333,
  "method": "token_set"
}

Invariants and gotchas

  • Pure and deterministic; no third-party dependency.
  • The homoglyph map also folds the digits 0 and 1 to o and l, so identifiers are compared after the same folding on both sides.
  • Spelling variants are folded longest first; a policy can replace the table (matching.spelling_variants) or extend transliteration.
  • Unit tests check Levenshtein and Jaro-Winkler against hand-computed values (the Winkler 1990 examples).