hippietrail
hippietrail

Reputation: 17004

Using DBpedia to find which Wikipedia categories are common to several given pages?

I've forgotten all I once new about DBpedia and SPARQL and find all the examples too complex and hard to understand when I Google for them.

What I wish to do is pass in two or three Wikipedia pages and get back the set of Wikipedia categories that all of the pages are members of.

This seems that it should be utterly simple in SPARQL so I would appreciate a very minimal example to get me started.

Upvotes: 3

Views: 1999

Answers (2)

Jeen Broekstra
Jeen Broekstra

Reputation: 22052

This is actually a variation of your earlier question about getting all pages belonging to two categories. The only difference is that this time, you want two/three subjects rather than objects, so you cannot use a comma-separated enumeration of values, but instead have to write out the triple pattern that you want to match.

For example, to get back all categories that both Spain and Portugal belong to, you could simply do a query like this:

SELECT ?cat   
WHERE { 
 <http://dbpedia.org/resource/Spain> dcterms:subject ?cat .
 <http://dbpedia.org/resource/Portugal> dcterms:subject ?cat .
} 

what this query does is select all triple patterns that have the same value of ?cat for the dcterms:subject relation for the subjects 'Spain' and 'Portugal'. In other words, it retrieves precisely those categories that both resources are a member of.

The trick is to think in terms of a graph, or triples with connected subjects and objects. It's a bit of a mental shift but once you've got that, query writing becomes a lot easier.

Upvotes: 4

William Greenly
William Greenly

Reputation: 3989

The mapping between wikipedia and dbpedia URI's is as follows:

For

http://en.wikipedia.org/wiki/Spain

DBPedia uri is:

http://dbpedia.org/resource/Spain

So to find out the categories for the above

PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?categoryUri ?categoryName
WHERE {
  <http://dbpedia.org/resource/Spain> dcterms:subject ?categoryUri.
  ?categoryUri rdfs:label ?categoryName.
  FILTER (lang(?categoryName) = "en")
}

Upvotes: 1

Related Questions