retrieving most specific classes of instances - sparql

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)

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

Related

i want to get the names of similar types using sparql queries from dbpedia

I need to find the names of similar types from DBpedia so I'm trying to figure out a query which can return me the names of entities which have same subject type in its dct:subject (example I want to find similar types of white house so i want to write a query for same . I'm considering the dct:subject to find them ). If there is any other approach please mention it
Previously I tried it for rdf:type but the result are not so good and some time it shows time out
I have done my problem by the query mentioned below and now i want to consider dct:subject instead of rdf:type
select distinct ?label ?resource count(distinct ?type) as ?score where {
values ?type { dbo:Thing dbo:Organization yago:WikicatIslam-relatedControversies yago:WikicatIslamistGroups yago:WikicatRussianFederalSecurityServiceDesignatedTerroristOrganizations yago:Abstraction100002137 yago:Act100030358 yago:Cabal108241798 yago:Group100031264 yago:Movement108464601 yago:PoliticalMovement108472335
}
?resource rdfs:label ?label ;
foaf:name ?name ;
a ?type .
FILTER (lang(?label) = 'en').
}
ORDER BY DESC(?score)

What does "^a" mean in this SPARQL query?

I have found this query, but i'm not able what does it do.
I don't know what the "^a" means, particularly.
select distinct ?type where {
dbpedia:Stephen_King a ?type .
filter not exists {
?subtype ^a dbpedia:Stephen_King ;
rdfs:subClassOf ?type .
filter ( ?subtype != ?type )
}
}
It's a SPARQL 1.1 property path which describes a route through a graph between two graph nodes, in your case it denotes the inverse path, i.e. from object to subject, thus, it's equivalent to
dbpedia:Stephen_King a ?subtype .
with a being just a shortcut for rdf:type
It's just used here to be able to use the more compact Turtle syntax, i.e. instead of writing
dbpedia:Stephen_King a ?subtype .
?subtype rdfs:subClassOf ?type .
you can write
?subtype ^a dbpedia:Stephen_King
?subtype rdfs:subClassOf ?type .
and therefore since subjects are the same
?subtype ^a dbpedia:Stephen_King ;
rdfs:subClassOf ?type .

How to exclude nodes from path?

I want to get all mathematicians from DBpedia, so I wrote this query for DBpedia's SPARQL service:
SELECT DISTINCT ?person
{
?person dct:subject ?category.
?category skos:broader* dbc:Mathematicians.
}
The problem with this is that the category Mathematicians is polluted, due to categories like dbc:Euclid, which then includes all of Euclidean geometry. I believe it's categories like these which cause the query to fail:
Virtuoso 42000 Error TN...: Exceeded 1000000000 bytes in transitive temp memory. use t_distinct, t_max or more T_MAX_memory options to limit the search or increase the pool
A lot of the problematic categories are in dbc:Wikipedia_categories_named_after_mathematicians.
Is there some way to ignore these categories in the skos:broader* path that would make the error go away?
You can list the categories that you don't want to include by filtering them out:
SELECT DISTINCT ?person
{
?person dct:subject ?category.
?category skos:broader* dbc:Mathematicians.
FILTER (?category NOT IN (dbc:Euclid))
}
But that won't remove the error because Virtuoso still needs to traverse the skos:broader hierarchy, exhausting 'transitive heap memory'. Other approaches include selecting specific categories or traversing part of the hierarchy.
The specific category could use UNION statements, but the VALUES shortcut is a simpler syntax:
SELECT DISTINCT ?person
{
VALUES ?category {dbc:Mathematicians dbc:Mental_calculators dbc:Lists_of_mathematicians}
?person dct:subject ?category.
}
For querying part of the hierarchy, you can use some property path expressions. This one will get parents and grandparents:
SELECT DISTINCT ?person
{
?person dct:subject ?category.
?category skos:broader | (skos:broader/skos:broader) dbc:Mathematicians.
# filter as desired - FILTER (?category NOT IN (dbc:Euclid))
}

How to get individuals data property value in sparql

Hi i am in new to ontologies. I have developed a restaurant ontology by Protege software. But I badly need a SPARQL query that finds property values for all individuals of a class.
I want to get a result like:
Angels_n_Gypsies
House #30, Rd No. 19/A, Dhaka 1213, Bangladesh
Just follow the property values...
SELECT ?rname ?lname
WHERE {
?inst a :Popular_restaurant .
?inst :restaurant_name ?rname .
?inst :Location_name ?lname .
}
But that's just the end result. A way to understand SPARQL is to start pedantically. For example:
SELECT ?inst
WHERE {
?inst a :Popular_restaurant .
}
That gets you all members of the class :Popular_restaurant. Then find what properties are defined for each member:
SELECT ?inst ?p ?o
WHERE {
?inst a :Popular_restaurant .
?inst ?p ?o .
}
And the bindings for p will tell you what properties are defined for members of this class. So use those values to continuously refine the query.

How to traverse skos:broader property to list tourist places in various cities

Any help in my learning will be highly appreciated.
Problem statement:
Need to print list of tourist places in various cities
I have http://dbpedia.org/page/Category:Tourism_by_city category which i have to explore. The problem is this category is a skos:broader of various other categories like http://dbpedia.org/page/Category:Tourism_in_Bratislava which in itself is a skos:broader of http://dbpedia.org/page/Category:Visitor_attractions_in_Bratislava which contains dcterms:subject property and which is the list of tourist places.
I have to explore all cities starting from Tourism_by_city category.
What I have done
SELECT DISTINCT ?places
WHERE {
?entity skos:broader* <http://dbpedia.org/resource/Category:Tourism_by_city> .
?places dcterms:subject ?entity
}
Problem:
skos:broader is further exploring the graph, i want to restrict it till Visitor_attractions level. Also skos:broader is exploring all the categories but i want it to explore for just Visitor_attractions category.
Level 1 : Tourism_by_city - > explore all skos:broader of
Level 2 : Tourism_by_xxxcity -> explore only category:Vistors_attractions_by_xxxcity
Level 3 : Do not explore further.
Is this achievable ?
Please do let me know if question is unclear. Thanks
Finally solved the question, I don't know whether this is the only viable option:
SELECT DISTINCT (str(?city) as ?City) (str(?label) as ?Attractions)
WHERE {
?entity skos:broader <http://dbpedia.org/resource/Category:Tourism_by_city> .
?places skos:broader ?entity .
?places rdfs:label ?city .
FILTER langMatches(lang(?city),"en") .
?attractions dcterms:subject ?places .
?attractions rdfs:label ?label .
FILTER langMatches(lang(?label),"en").
FILTER (if (isliteral(?city ), contains(str(?city ), "Visitor"), false))
}
ORDER BY ASC(?city)