dewet
dewet

Reputation: 31

Common super category between two wikipedia categories using DBpedia

I have the following SPARQL query that supposed to retrieve the common categroies between Category:Computer_science category and Category:German_scientists

SELECT DISTINCT ?subject WHERE {
?subject <http://purl.org/dc/terms/subject> ?cat1, ?cat2 .
?cat1 skos:broader? / skos:broader? / skos:broader?         
  <http://dbpedia.org/resource/Category:Computer_science> .     
?cat2 skos:broader? / skos:broader? / skos:broader?
  <http://dbpedia.org/resource/Category:German_scientists>.
} LIMIT 10

I need to get the closest category to them or who has the shortest path to both categories , How Can I do that ? How to get the lenght between each common categroy between (computer_science) and (German_scientists) for example ?

Upvotes: 1

Views: 308

Answers (2)

Rituraj Singh
Rituraj Singh

Reputation: 639

For the clarification:: The query is the PREFIX version of the below answer which follows current norms :

PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX dbc: <http://dbpedia.org/resource/Category:>
select distinct ?super where {
  ?super (^skos:broader){0,4} dbc:Computer_science, dbc:German_scientists
} 

SPARQL RESULTS

super
--------------------------------------------
http://dbpedia.org/resource/Category:Science_and_technology

Upvotes: 0

Joshua Taylor
Joshua Taylor

Reputation: 85883

Virtuoso, the endpoint that serves the DBpedia data supports a non-standard SPARQL extension to property paths: instead of just p*, p+, and p?, to mean a path of length "zero or more", "one or more", and "zero or one", respectively, Virtuoso also supports p{m,n}, meaning a path of length "at least m and at most n". By trying the following query with n=0 and increasing values of m, I started getting results with m=4:

prefix category: <http://dbpedia.org/resource/Category:>

select distinct ?super where {
  ?super (^skos:broader){0,4} category:Computer_science, category:German_scientists
}
super
-----------------------------------------------
http://dbpedia.org/resource/Category:Technology
http://dbpedia.org/resource/Category:Science

Upvotes: 3

Related Questions