基于 Neo4j 中节点属性的 Jaccard 相似性创建节点之间的关系?

Create relationship between nodes based on Jaccard similarity of the nodes attributes in Neo4j?

我的 Neo4j 图中有多个节点。 我想在任何 2 个节点之间创建关系,当且仅当它们在属性上的 Jaccard 相似性高于某个阈值 alpha。

考虑 2 个节点:

Node 1: {id:1, abc: 1.1, eww: -9.4, ssv: "likj"}
Node 2: {id:2, we2: 1, eww: 900}
Node 3: {id:3, kuku: -91, lulu: 383, ssv: "bubu"}

因此节点 1 和节点 2 Jaccard 在属性上的相似性为: (交集 =) 2/ (并集 =) 5 = 0.4

我如何在 Neo4j 中执行此操作?我知道有一个 Jaccard 相似函数,但如何配置它以处理节点的属性?

假设您指的是属性存在的 Jaccard 相似性,那么您可以这样做

MATCH (a:Node)
MATCH (b:Node) WHERE id(b) > id(a)
WITH a, b, [prop IN keys(a) WHERE prop IN keys(b)] AS shared_properties // Find the properties that exist on both nodes using the IN operator
WITH a, b, size(shared_properties) AS shared_property_count // Get the number of shared properties 
WITH 1.0*shared_property_count / size(apoc.coll.union(keys(a), keys(b))) AS jaccard_similarity, a, b // Compute the Jaccard similarity as the intersection over the union
WHERE jaccard_similarity > $threshold // Make sure the similarity is higher than some threshold
CREATE (a)-[:SIMILAR_TO {jaccard: jaccard_similarity}]->(b) 

WITH 语句找到两个节点上都存在的属性并对它们进行计数,最后我们找到 Jaccard 相似度。