Skip to content

Commit caf4ad3

Browse files
committed
Add negate field to triple hash
1 parent b57a4ff commit caf4ad3

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

src/curies/api.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2657,10 +2657,11 @@ def get_subconverter(self, prefixes: Iterable[str]) -> Converter:
26572657
]
26582658
return Converter(records)
26592659

2660-
def hash_triple(self, triple: Triple) -> str:
2660+
def hash_triple(self, triple: Triple, *, negate: bool = False) -> str:
26612661
"""Hash a triple using :func:`curies.triples.hash_triple`, implementing https://ts4nfdi.github.io/mapping-sameness-identifier.
26622662
26632663
:param triple: A subject-predicate-object triple
2664+
:param negate: If true, considers the triple as "negative" and postpends a ``~`` to the hash
26642665
:return: A hexadecimal digest of the SHA-256 hash of the space-joined expanded URI triple
26652666
26662667
>>> import curies
@@ -2679,6 +2680,8 @@ def hash_triple(self, triple: Triple) -> str:
26792680
... )
26802681
>>> converter.hash_triple(triple)
26812682
'36a1f9244ea7641a90987c82f33c25c0c13712ee8f48207b2a0825f8a4e4e26a'
2683+
>>> converter.hash_triple(triple, negate=True)
2684+
'36a1f9244ea7641a90987c82f33c25c0c13712ee8f48207b2a0825f8a4e4e26a~'
26822685
"""
26832686
from .triples import hash_triple
26842687

src/curies/triples/hash_utils.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
]
1414

1515

16-
def hash_triple(converter: Converter, triple: Triple) -> str:
16+
def hash_triple(converter: Converter, triple: Triple, *, negate: bool = False) -> str:
1717
"""Encode a triple with URL-safe base64 encoding.
1818
1919
:param converter: A converter
2020
:param triple: A triple of CURIE objects
21+
:param negate: If true, considers the triple as "negative" and postpends a ``~`` to
22+
the hash
2123
2224
:returns: An encoded triple of URIs
2325
@@ -37,30 +39,37 @@ def hash_triple(converter: Converter, triple: Triple) -> str:
3739
>>> triple = Triple(subject="mesh:C000089", predicate="skos:exactMatch", object="CHEBI:28646")
3840
>>> hash_triple(converter, triple)
3941
'36a1f9244ea7641a90987c82f33c25c0c13712ee8f48207b2a0825f8a4e4e26a'
42+
>>> hash_triple(converter, triple, negate=True)
43+
'36a1f9244ea7641a90987c82f33c25c0c13712ee8f48207b2a0825f8a4e4e26a~'
4044
"""
41-
return encode_uri_triple(triple.as_uri_triple(converter))
45+
return encode_uri_triple(triple.as_uri_triple(converter), negate=negate)
4246

4347

44-
def encode_uri_triple(uri_triple: tuple[str, str, str]) -> str:
48+
def encode_uri_triple(uri_triple: tuple[str, str, str], *, negate: bool = False) -> str:
4549
"""Encode a subject-predicate-object triple.
4650
4751
:param uri_triple: A triple of URIs represented as strings
52+
:param negate: If true, considers the triple as "negative" and postpends a ``~`` to
53+
the hash
4854
4955
:returns: An encoded triple of URIs
5056
5157
.. seealso::
5258
5359
https://ts4nfdi.github.io/mapping-sameness-identifier/
5460
55-
>>> encode_uri_triple(
56-
... (
57-
... "http://id.nlm.nih.gov/mesh/C000089",
58-
... "http://www.w3.org/2004/02/skos/core#exactMatch",
59-
... "http://purl.obolibrary.org/obo/CHEBI_28646",
60-
... )
61+
>>> triple = (
62+
... "http://id.nlm.nih.gov/mesh/C000089",
63+
... "http://www.w3.org/2004/02/skos/core#exactMatch",
64+
... "http://purl.obolibrary.org/obo/CHEBI_28646",
6165
... )
66+
>>> encode_uri_triple(triple)
6267
'36a1f9244ea7641a90987c82f33c25c0c13712ee8f48207b2a0825f8a4e4e26a'
68+
>>> encode_uri_triple(triple, negate=True)
69+
'36a1f9244ea7641a90987c82f33c25c0c13712ee8f48207b2a0825f8a4e4e26a~'
6370
"""
6471
delimited_uris = " ".join(uri_triple)
6572
digest = hashlib.sha256(delimited_uris.encode("utf-8")).hexdigest()
73+
if negate:
74+
digest += "~"
6675
return digest

0 commit comments

Comments
 (0)