nyms.mapping#
The reversible mapping store: surrogate <-> original, per project, on a dol store.
A MappingStore holds one Record per distinct value: entity type, original,
normalised form, surrogate, aliases (other normalised forms that stand for the same value,
such as a surname alone), strategy name and creation time. It is keyed by surrogate (so
re-identification is one lookup) and indexes every normalised form in memory so the same
value gets the same surrogate on every occurrence and in every document of the project.
The backing store is any MutableMapping[str, dict]. The default,
local_mapping_store(), is dol.JsonFiles under ~/.local/share/nyms/<project>/mapping,
one JSON file per record (file names are a URL-safe encoding of the surrogate, so any
surrogate string is a legal file name on any platform), which a human can inspect and
delete. Encryption at rest is the codec seam: pass encrypted() (needs the optional
cryptography dependency) and every value is Fernet-encrypted under the project key before
it hits disk. The project directory carries a meta.json naming the codec, so opening an
encrypted store without the codec (or the reverse) fails with a clear message.
Normalisation decides what counts as “the same value”: digits only for numeric identifiers,
casefolded and whitespace-collapsed for names, lower case for e-mail. The Danish genitive is
handled in MappingStore.resolve(): Rasmus Jensens resolves to Rasmus Jensen’s
surrogate plus the suffix s when that person is already known, and the facade writes
[PERSON_1]s. A first-seen ...s is taken literally; if the base form turns up later,
the record is relinked to it.
>>> from nyms.surrogates import Counter
>>> m = MappingStore({}, strategy=Counter())
>>> m.resolve('PERSON', 'Rasmus Jensen')
('[PERSON_1]', '')
>>> m.resolve('PERSON', 'Rasmus Jensens')
('[PERSON_1]', 's')
>>> m.resolve('CPR', '010190 1234') == m.resolve('CPR', '010190-1234')
True
>>> m.original('[PERSON_1]')
'Rasmus Jensen'
Functions
|
The loose form of a surrogate used for re-identification and collision checks. |
|
A |
|
A |
|
The identity of a value for the purpose of "same surrogate". |
|
Where a project's data lives: |
Classes
|
Records keyed by surrogate, with an in-memory index by |
|
One distinct value and its surrogate. |
Exceptions
A mapping store could not be opened the way it was created (codec mismatch). |
- class nyms.mapping.MappingStore(store=None, *, strategy=None)[source]#
Bases:
objectRecords keyed by surrogate, with an in-memory index by
(entity_type, normalised).- add(entity_type, text, *, surrogate=None)[source]#
Create a record for a new value (with a given surrogate, or one from the strategy).
- Return type:
- add_alias_form(surrogate, entity_type, text)[source]#
Make text (a surname alone, say) resolve to an existing record’s surrogate.
- Return type:
- alias(entity_type, text, surrogate)[source]#
Choose the surrogate for a value yourself (before it is first seen).
- Return type:
- resolve(entity_type, text)[source]#
(surrogate, suffix)for text, creating the surrogate if the value is new.suffix is a genitive ending found on an already known value, to be re-attached after the surrogate (
[PERSON_1]+s). It is empty for a new value: nyms does not guess that an unknown...sis a genitive; if the base form appears later the record is relinked to it.
- class nyms.mapping.Record(surrogate, original, entity_type, normalised, strategy='', created=0.0, aliases=tuple())[source]#
Bases:
objectOne distinct value and its surrogate.
- exception nyms.mapping.StoreError[source]#
Bases:
RuntimeErrorA mapping store could not be opened the way it was created (codec mismatch).
- nyms.mapping.canon(surrogate)[source]#
The loose form of a surrogate used for re-identification and collision checks.
>>> canon('[ PERSON 1 ]'), canon('Alder Brook') ('person_1', 'alder_brook')
- Return type:
- nyms.mapping.encrypted(key)[source]#
A
(encode, decode)codec: Fernet under a key derived from key. Needscryptography.Fernet (AES-128-CBC with HMAC-SHA-256) is the simplest authenticated encryption the
cryptographypackage offers; AES-GCM orageare the replacements.
- nyms.mapping.local_mapping_store(project='default', *, rootdir=None, codec=None)[source]#
A
dolfile store of records under the project dir, optionally through codec.codec is a pair
(encode: bytes -> bytes, decode: bytes -> bytes)applied to the JSON bytes of each record; seeencrypted(). The project’smeta.jsonrecords which codec the store was created with; a mismatch raisesStoreError.- Return type: