There is a dataset of users ranking movies. Need to find the users with similar taste to user1. Similar taste defined as follows: consider the average rank for genre from user1 as avgr1 and for the same genre from user2 as avgr2, then user1 and user2 have similar taste is abs(avgr1-avgr2)<1 . So far I was able to get the names, the genre and the absolute value between averages, but the filtering for comparison is not working.
SELECT ?p ?p1 ?genre (abs (AVG(?rating)-AVG(?ratingp1)) AS ?RDiff)
WHERE{
?p movies:hasRated ?rate.
?p1 foaf:knows ?p.
?rate movies:ratedMovie ?mov.
?rate movies:hasRating ?rating.
?mov movies:hasGenre ?genre.
?p1 movies:hasRated ?ratep1.
?ratep1 movies:ratedMovie ?movp1.
?ratep1 movies:hasRating ?ratingp1.
?movp1 movies:hasGenre ?genre.
FILTER (xsd:float(?Rdiff)<1.0 && ?p=movies:user1)
}
GROUP BY ?p ?p1 ?genre
It's very hard to answer these kinds of questions without some sample data to work with. Here's some sample data that has two users who have similar rankings on comedy, but different rankings on romance:
Here's a query that computes the difference of their average rankings on genres:
Now, you can't filter on aggregate results, you have to use having, so to only take values where diff is less than some particular value, you'd do this:
If you don't care about the actual diff, except that it's below the threshold, you can put the expression in the having directly, and do: