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