Collaborative filtering with jaccard index

I am working on collaborative filtering which has implemented jaccard index in it.
I have managed to get it working for user-user:

            MATCH (c1:user)-[r]->(u:user)<-[r]-(c2:user)
            WHERE c1 <> c2 AND c1.UserId = {UserId}
            AND not (c1)-[r]->(c2)
            WITH c1, c2, COUNT(DISTINCT u) as intersection

            MATCH (c:user)-[r]->(u:user)
            WHERE c in [c1, c2]
            WITH c1, c2, intersection, COUNT(DISTINCT u) as union

            // compute Jaccard index
            WITH c1, c2, intersection, union, (intersection * 1.0 / union) as jaccard_index

            ORDER BY jaccard_index DESC, c2.UserId
            WITH c1, COLLECT([c2.UserId, jaccard_index, intersection, union])[0..{k}] as neighbors

            WHERE SIZE(neighbors) = {k}   // return users with enough neighbors
            RETURN c1.UserId, neighbors```

But the problem is that when I want to change slightly this algorithm so it recommends an activity that most similar user to me has been involved: user-activity somehow its not working.

this is what I have now:
        MATCH (u1:user),(u:user)-[r]->(a:activity)       
        WHERE u1.UserId = {UserId}
          AND (u)-[r]->(a)
          AND not (u1)-[r]->(a)                        

        WITH u1, a, COUNT(DISTINCT u) as cnt                             
        ORDER BY u1.UserId, cnt DESC                                               
        RETURN u1.UserId as user, COLLECT([a._bldrId, cnt])[0..{n}] as recos , COLLECT([a.Title,cnt])[0..{n}] as recos_names
        ```

It works, but I'm struggling to implement jaccard index here.
Could anybody help with it ?

How about just using the jaccard similarity implementation from graph algorithms?

You find details for it in the book

and here:

https://neo4j.com/docs/graph-algorithms/current/algorithms/similarity/

1 Like