Rank results of a sparql query -


i have hierarchy of skos concepts , individuals indexed concepts (for example ex:regina dct:subject ex:pizza).

i want find every individuals indexed concepts (or more specific ones). query below retrieves every meal pizza or homemade. (i'm using concat , group_concat concepts of meals in 1 row)

select ?ind ?name (group_concat(concat(str(?concept),':',?conceptname)) ?concepts){   ?ind ex:individual.   ?ind skos:preflabel ?name.   ?ind dct:subject ?concept.   ?concept skos:preflabel ?conceptname.   optional{?ind dct:subject [skos:broadertransitive* ex:pizza].}   optional{?ind dct:subject [skos:broadertransitive* ex:homemade].} } group ?ind ?name 

this great, rank results according numbers of concepts have in common want. in example homemade pizzas first, pizzas or homemade meals.
rid of results having none of concepts i'm interested in.
ideally, meals directly indexed desired concept have higher score 1 being indexed more specific concepts.

i postprocess results of course, i'm pretty sure there clever , efficient way in sparql. idea ?

edit : sample data per request

@prefix : <http://www.semanticweb.org/owl/owlapi/turtle#> . @prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix xml: <http://www.w3.org/xml/1998/namespace> . @prefix xsd: <http://www.w3.org/2001/xmlschema#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix skos: <http://www.w3.org/2004/02/skos/core#> . @prefix dct: <http://purl.org/dc/terms/>  . @prefix ex: <http://example.com#> .  ex:foodscheme skos:conceptscheme ;     skos:preflabel "the food schema"@en ;     skos:hastopconcept ex:food .  ex:food skos:concept ;     skos:preflabel "food"@en ;     skos:inscheme ex:foodscheme .      ex:italian skos:concept ;         skos:preflabel "italian food"@en ;         skos:broadertransitive ex:food ;         skos:inscheme ex:foodscheme .          ex:pizza skos:concept ;             skos:preflabel "pizza"@en ;             skos:broadertransitive ex:italian ;             skos:inscheme ex:foodscheme .          ex:pasta skos:concept ;             skos:preflabel "pasta"@en ;             skos:broadertransitive ex:italian ;             skos:inscheme ex:foodscheme .      ex:homemade skos:concept ;         skos:preflabel "homemade food"@en ;         skos:broadertransitive ex:food ;         skos:inscheme ex:foodscheme .  ex:regina ex:meal ;     skos:preflabel "regina"@en ;     dct:subject ex:homemade ;     dct:subject ex:pizza.  ex:peppypaneer ex:meal ;     skos:preflabel "from domino's"@en ;     dct:subject ex:pizza.  ex:carbonara ex:meal ;     skos:preflabel "pasta la carbonara"@en ;     dct:subject ex:homemade ;     dct:subject ex:pasta.  ex:lasagna ex:meal ;     skos:preflabel "lasagna"@en ;     dct:subject ex:italian. 

expected results homemade pizza (regina first, because it's homemade , pizza, carbonara not included beacause it's neither homemade nor pizza. order of peppypaneer , carbonara not matter, should have same "score"):

ex:regina ex:peppypaneer ex:carbonara 

expected results italian (lasagna first because directly indexed italian while others not):

ex:lasagna ex:carbonara ex:peppypaneer ex:regina 

given set of relevant subjects { ex:pizza ex:homemade }, can do:

query

prefix  ex: <http://example.com#>  prefix  dct:  <http://purl.org/dc/terms/> prefix  skos: <http://www.w3.org/2004/02/skos/core#>  select  ?ind ?name (group_concat(distinct ?conceptstr) ?concepts) (count(distinct ?cls) ?score)   { ?ind      skos:preflabel  ?name ;               dct:subject     ?concept .     ?concept  skos:preflabel  ?conceptname     bind(concat(str(?concept), ":", ?conceptname) ?conceptstr)     optional       { values ?cls { ex:pizza ex:homemade }         ?ind dct:subject/(skos:broadertransitive)* ?cls       }   } group ?ind ?name having ( ?score > 0 ) order desc(?score) 

output:

+-----------------+-------------------------+--------------------------------------------------------------------------+-------+ |       ind       |          name           |                                 concepts                                 | score | +-----------------+-------------------------+--------------------------------------------------------------------------+-------+ |  ex:regina      | regina en               | http://example.com#pizza:pizza http://example.com#homemade:homemade food |     2 | |  ex:carbonara   | pasta la carbonara en | http://example.com#pasta:pasta http://example.com#homemade:homemade food |     1 | |  ex:peppypaneer | domino's en        | http://example.com#pizza:pizza                                           |     1 | +-----------------+-------------------------+--------------------------------------------------------------------------+-------+ 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -