or “Excluding results from SPARQL by their relation to a tainted set”.
So, in the bar last night my old friend Faith was telling me about an SPARQL problem she had. She has a dataset describing the statements of a Shakespear play, and she wanted to test it against the Bechdel Test.
The challenge she had was that should wanted to exclude results if they had a single dcterms:subject relationship to an entity of type “Man”. It’s really easy to do the opposite; to include items which have such a relation, but excluding is a more confusing patter.
What she had (roughly):
?conversation ns:hasParticipant ?char1 . ?conversation ns:hasParticipant ?char2 . ?char1 a ns:Woman . ?char2 a ns:Woman . FILTER( ?char1 != ?char2 ) . ?conversation ns:hasTopic ?topic . ?topic a ?type . FILTER( ?type != ns:Man )
Now this last bit wasn’t doing the right thing. What it actually does is
1. get a list of all combinations of ?char1, ?char2, ?conversation, ?topic, ?type
2. Filter out the unwanted ones. But if a conversation has two topics, and one of them is about a man, then only that row will be filtered, and other topics in the same conversation don’t get filtered.
It took me and Dave Challis (who was at the next table in the same bar, luckily) a while to work out the correct syntax to exclude all results when a relationship exists to X, even if there’s other relations of the same type to different things. That last bit should have been:
OPTIONAL { ?conversation ns:hasTopic ?topic . ?topic a ns:Man . } FILTER( !bound( ?topic ) )
This feels a bit backwards, but what it does is return all the ?char1 ?char2 and ?conversation as normal, and for each row returns a topic about a man if available. Then, if it was available, we didn’t want this row so we filter it.
It’s a useful pattern and was not obvious to a bunch of smart, motivated people, so I figured it was worth writing about.
Are there any other useful patterns like this in SPARQL?
One Response
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.
Continuing the Discussion