Reputation: 93
We have a quite big amount of medieval recipes and their structured data stored as RDF in a triple store (Blazegraph). The ingredients are Wikidata items and each recipe is part of a recipe collection:
<https://example.com/b2.1> schema:recipeIngredient <http://www.wikidata.org/entity/Q10987>,
<http://www.wikidata.org/entity/Q15046077>,
<http://www.wikidata.org/entity/Q15622897>,
<http://www.wikidata.org/entity/Q42527>.
<https://example.com/b2.1> ex:isPartOfCollection <https://example.com/b2>.
Now I would like to query for recipes with the exact same ingredients and get the amount of recipes in each collection that are intersecting (i.e. which have the exact same ingredients) and the amount of recipes that are unique (i.e. no recipes with the same ingredients found in any other collection). I did this quite expensive query:
prefix corema: <https://gams.uni-graz.at/o:corema.ontology#>
prefix schema: <http://schema.org/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT (?coll as ?collectionURI) (count(distinct ?r2) as ?NoOfrecipesWithExcatSameIngredients) (group_concat(?r1;separator=", ") as ?UriOfrecipesWithExcatSameIngredients)
WHERE {
{
SELECT (group_concat(?ing1;
separator=", ") as ?ingr1) ?r1 WHERE
{
?r1 a schema:Recipe;
schema:recipeIngredient ?ing1.
}
GROUP BY ?r1
ORDER BY ?r1
}
{
SELECT (group_concat(?ing2;
separator=", ") as ?ingr2) ?r2 ?coll WHERE
{
?r2 a schema:Recipe;
schema:recipeIngredient ?ing2;
corema:isPartOfCollection ?coll.
}
GROUP BY ?r2 ?coll
ORDER BY ?r2
}
FILTER(?ingr1=?ingr2)
FILTER(?r1!=?r2)
}
GROUP BY ?coll
ORDER BY ?collectionURI
LIMIT 10000
It works but 1) I don't know how to get the unique recipes and 2) the problem of unsecure ordering in group_concat (graphDB seems to stay stable while Blazegraphs gives me different numbers every time I run the query)
The output should be (as an example):
Collection | Amount of intersecting recipes | Amount of unique recipes | Intersecting with (Uris of recipes) | Unique recipe URIS |
---|---|---|---|---|
b2 | 12 | 4 | ex:b4.2, ex:b5.6, ex:gr1.7 | ex:b2.2, ex:b2.6, ex:b2.1 |
Any help appreciated!
Upvotes: 1
Views: 122