infer_reversible

infer_reversible(mappings: Iterable[Mapping], *, progress: bool = True) list[Mapping][source]

Extend the mapping list with flipped mappings.

Parameters:
  • mappings – An iterable of mappings

  • progress – Should a progress bar be shown? Defaults to true.

Returns:

A list where if a mapping can be flipped (i.e., flip()), a flipped mapping is added. Flipped mappings contain reasoned evidence ReasonedEvidence objects that point to the mapping from which the evidence was derived.

Flipping a mapping means switching the subject and object, then modifying the predicate as follows:

  1. Broad becomes narrow

  2. Narrow becomes broad

  3. Exact and close mappings remain the same, since they’re reflexive

This is configured in the semra.rules.FLIP dictionary.

>>> from semra import Mapping, Reference, EXACT_MATCH, SimpleEvidence
>>> from semra.api import get_test_evidence, get_test_reference
>>> r1, r2 = get_test_reference(2)
>>> e1 = get_test_evidence()
>>> m1 = Mapping(subject=r1, predicate=EXACT_MATCH, object=r2, evidence=[e1])
>>> mappings = infer_reversible([m1], progress=False)
>>> len(mappings)
2
>>> assert mappings[0] == m1

Warning

This operation does not “assemble”, meaning if you had existing evidence for an inverse mapping, they will be seperate. Therefore, you can chain it with the semra.api.assemble_evidences() operation:

>>> from semra import Mapping, Reference, EXACT_MATCH
>>> from semra.api import get_test_evidence
>>> from semra.api import get_test_evidence, get_test_reference
>>> r1, r2 = get_test_reference(2)
>>> e1, e2 = get_test_evidence(2)
>>> m1 = Mapping(subject=r1, predicate=EXACT_MATCH, object=r2, evidence=[e1])
>>> m2 = Mapping(subject=r2, predicate=EXACT_MATCH, object=r1, evidence=[e2])
>>> mappings = infer_reversible([m1, m2])
>>> len(mappings)
4
>>> mappings = assemble_evidences(mappings)
>>> len(mappings)
2