retrieving most specific classes of instances
Asked Answered
P

1

12

Is it possible to have a definition of an resource (from DBpedia) with a SPARQL query? I want to have something like the TBox and ABox that are shown in (Conceptual) Clustering methods for the Semantic Web: issues and applications (slides 10–11). For example, for DBpedia resource Stephen King, I would like to have:

Stephen_King : Person ⊓ Writer ⊓ Male ⊓ … (most specific classes)

Primordium answered 28/10, 2013 at 11:1 Comment(1)
Is "Male" really a more specific class than "Writer"? (This question is meant both to prompt you to consider what results you're really trying to get, and to suggest that DBpedia/Wikipedia classes might need some repairs.)Monarchism
T
14

You can use a query like the following to ask for the classes of which Stephen King is an instance which have no subclasses of which Stephen King is also an instance. This seems to align well with the idea of “most specific classes.” However, since (as far as I know) there's no reasoner attached to the DBpedia SPARQL endpoint, there may be subclass relationships that could be inferred but which aren't explicitly present in the data.

select distinct ?type where { 
   dbr:Stephen_King a ?type .
  filter not exists { 
    ?subtype ^a  dbr:Stephen_King ;
             rdfs:subClassOf ?type .
  }
}

SPARQL results

Actually, since every class is an rdfs:subClassOf itself, you might want to add another line to that query to exclude the case where ?subtype and ?type are the same:

select distinct ?type where { 
   dbr:Stephen_King a ?type .
  filter not exists { 
    ?subtype ^a  dbr:Stephen_King ;
             rdfs:subClassOf ?type .
    filter ( ?subtype != ?type )
  }
}

SPARQL results

If you actually want a result string like the one shown in those slides, you could use values to bind a variable to dbr:Stephen_King, and then use some grouping and string concatenation to get something nicer looking (sort of):

select
  (concat( ?person, " =\n", group_concat(?type; separator=" AND\n")) as ?sentence)
where { 
  values ?person {  dbr:Stephen_King }
  ?type ^a ?person .
  filter not exists { 
    ?subtype ^a ?person ;
             rdfs:subClassOf ?type .
    filter ( ?subtype != ?type )
  }
}
group by ?person

SPARQL results

http://dbpedia.org/resource/Stephen_King =
http://dbpedia.org/class/yago/AuthorsOfBooksAboutWritingFiction AND
http://dbpedia.org/ontology/Writer AND
http://schema.org/Person AND
http://xmlns.com/foaf/0.1/Person AND
http://dbpedia.org/class/yago/AmericanSchoolteachers AND
http://dbpedia.org/class/yago/LivingPeople AND
http://dbpedia.org/class/yago/PeopleFromBangor,Maine AND
http://dbpedia.org/class/yago/PeopleFromPortland,Maine AND
http://dbpedia.org/class/yago/PeopleFromSarasota,Florida AND
http://dbpedia.org/class/yago/PeopleSelf-identifyingAsAlcoholics AND
http://umbel.org/umbel/rc/Artist AND
http://umbel.org/umbel/rc/Writer AND
http://dbpedia.org/class/yago/20th-centuryNovelists AND
http://dbpedia.org/class/yago/21st-centuryNovelists AND
http://dbpedia.org/class/yago/AmericanHorrorWriters AND
http://dbpedia.org/class/yago/AmericanNovelists AND
http://dbpedia.org/class/yago/AmericanShortStoryWriters AND
http://dbpedia.org/class/yago/CthulhuMythosWriters AND
http://dbpedia.org/class/yago/HorrorWriters AND
http://dbpedia.org/class/yago/WritersFromMaine AND
http://dbpedia.org/class/yago/PeopleFromDurham,Maine AND
http://dbpedia.org/class/yago/PeopleFromLisbon,Maine AND
http://dbpedia.org/class/yago/PostmodernWriters
Teena answered 28/10, 2013 at 17:33 Comment(1)
@BlockER Yes, DBpedia changed the namespaces that are predefined in their public endpoint. I've answered so many SPARQL queries based on DBpedia that I can't really go back and fix them up. I think that for these ones, you'd only need to replace dbpedia: with dbr:. Or define the dbpedia: prefix.Teena

© 2022 - 2024 — McMap. All rights reserved.