Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use DBPedia to load all People along with some data

I am using DBpedia for the first time. I would like to download all the people in the dataset of people along with properties for commonName, nationality, birthDate, and knownFor (I will eventually stick it into an excel spread sheet using some sort of scripting language I think).

This was my first attempt at a query to do this job, however it does not work. I tried piecing it together from other bits of code I've seen on the internet. Does anyone know how to fix this? Thanks

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>        
PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/> 

SELECT ?person ?commonName ?nationality ?knownFor ? birthDate
WHERE {
    ?person a type:Person .
    ?person prop:commonName ?commonNameFilter(lang(?commonName) = 'en') .
    ?person prop:nationality ?nationality(lang(?nationality) = 'en') .
    ?person prop:knownFor ?knownFor(lang(?knownFor) = 'en') .
    ?person prop:birthDate ?birthDate .
}

EDIT: NEW VERSION OF CODE: RETURNS COMMONNAME (WITH NON-ENGLISH DUPLICATES). STILL MISSING OTHER PROPERTIES.

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>        
PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/ontology/>

SELECT DISTINCT * WHERE {
    ?person a dbpedia-owl:Person ;  
              dbpedia-owl:commonName ?commonName . FILTER(lang(?commonName) = 'en')
}
LIMIT 30
like image 778
Alex Chumbley Avatar asked Feb 15 '23 22:02

Alex Chumbley


1 Answers

First, your query has a bunch of syntax issues:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>        
PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/> 
            ^ you probably want to use the dbpedia-owl properties which are 
            # in <http://dbpedia.org/ontology/>
SELECT ?person ?commonName ?nationality ?knownFor ? birthDate
                                                   ^ space between ? and varname
WHERE {
    ?person a type:Person .
    ?person prop:commonName ?commonNameFilter(lang(?commonName) = 'en') .
                                       ^ This needs to be "?commonName . FILTER(..."
                                       # and the same thing applies to your other 
                                       # filters
    ?person prop:nationality ?nationality(lang(?nationality) = 'en') .
    ?person prop:knownFor ?knownFor(lang(?knownFor) = 'en') .
    ?person prop:birthDate ?birthDate .
}

It's easier to build some of these queries incrementally, because then you can find out what properties some of the resources actually have, and then you can extend your query some more. The public endpoint has a number of predefined namespaces, and using those will make it easier for others to read your query. So, you can start by asking for people:

SELECT * WHERE {
    ?person a dbpedia-owl:Person .
}
LIMIT 10

SPARQL results

Seeing that that's working, you can look at some of the returned instances and see that they have dbpedia-owl:commonName properties, and then extend the query:

SELECT * WHERE {
    ?person a dbpedia-owl:Person ;  
              dbpedia-owl:commonName ?commonName .
}
LIMIT 10

SPARQL results

It's easy enough to extend this with the dbpedia-owl:birthDate property. I don't see a nationality predicate on the instances that I've looked at, so I'm not sure what you were basing the nationality query on. While I saw some use of the knownFor property, I didn't see it on many instances, so if you make it a required property, you're going to exclude lots of people. This sort of incremental approach will probably help you out in the long run, though.

Finding Properties

While the browseable ontology provides a nice way to find classes, I'm not sure whether there's such a nice way of finding properties. However, you can do something in a brute force manner. E.g., to find all the properties that have actually been used for Persons, you can run a query like the following. (Note: this query takes a while to execute, so if you use it, you should probably download the results.)

select distinct ?p where {
  [] a dbpedia-owl:Person ;
     ?p [] .
}

SPARQL results

I'll note that dbpedia-owl:nationality does appear in that list.

To get all the properties for everything, you can download the ontology, and run a query like:

prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix owl: <http://www.w3.org/2002/07/owl#>
select * where { 
  { ?p a owl:ObjectProperty }
  UNION 
  { ?p a owl:DatatypeProperty }
}

I ran this locally using Jena's ARQ:

$ arq --query properties.sparql --data dbpedia_3.8.owl
----------------------------------------------------------------------------
| p                                                                        |
============================================================================
| <http://dbpedia.org/ontology/regionServed>                               |
| <http://dbpedia.org/ontology/coachedTeam>                                |
| <http://dbpedia.org/ontology/legalForm>                                  |
| <http://dbpedia.org/ontology/goldenCalfAward>                            |
| <http://dbpedia.org/ontology/composer>                                   |
| <http://dbpedia.org/ontology/owningOrganisation>                         |
| <http://dbpedia.org/ontology/branchFrom>                                 |
| <http://dbpedia.org/ontology/iso6393Code>                                |
...
| <http://dbpedia.org/ontology/classification>                             |
| <http://dbpedia.org/ontology/bgafdId>                                    |
| <http://dbpedia.org/ontology/currencyCode>                               |
| <http://dbpedia.org/ontology/onChromosome>                               |
| <http://dbpedia.org/ontology/course>                                     |
| <http://dbpedia.org/ontology/frequentlyUpdated>                          |
| <http://dbpedia.org/ontology/distance>                                   |
| <http://dbpedia.org/ontology/volume>                                     |
| <http://dbpedia.org/ontology/description>                                |
----------------------------------------------------------------------------

That won't provide the rdfs:domain and rdfs:range, but you could also ask for these, or for just those properties with rdfs:range dbpedia-owl:Person (but note that this won't get all the properties that could be used Person, since the range could be more or less specific):

prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix dbpedia-owl: <http://dbpedia.org/ontology/>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>

select ?p ?range where { 
  { ?p a owl:ObjectProperty }
  UNION 
  { ?p a owl:DatatypeProperty }

  ?p rdfs:domain dbpedia-owl:Person ; rdfs:range ?range .
}


$ arq --query properties.sparql --data dbpedia_3.8.owl | head
--------------------------------------------------------------------------------------------------------
| p                                           | range                                                  |
========================================================================================================
| dbpedia-owl:restingPlacePosition            | <http://www.w3.org/2003/01/geo/wgs84_pos#SpatialThing> |
| dbpedia-owl:opponent                        | dbpedia-owl:Person                                     |
| dbpedia-owl:employer                        | dbpedia-owl:Organisation                               |
| dbpedia-owl:hometown                        | dbpedia-owl:Settlement                                 |
| dbpedia-owl:militaryBranch                  | dbpedia-owl:MilitaryUnit                               |
| dbpedia-owl:school                          | dbpedia-owl:EducationalInstitution                     |
| dbpedia-owl:ethnicity                       | dbpedia-owl:EthnicGroup                                |
...
| dbpedia-owl:sex                             | xsd:string                                             |
| dbpedia-owl:hipSize                         | xsd:double                                             |
| dbpedia-owl:individualisedPnd               | xsd:nonNegativeInteger                                 |
| dbpedia-owl:weddingParentsDate              | xsd:date                                               |
| dbpedia-owl:birthName                       | xsd:string                                             |
| dbpedia-owl:networth                        | xsd:double                                             |
| dbpedia-owl:birthYear                       | xsd:gYear                                              |
| dbpedia-owl:bustSize                        | xsd:double                                             |
| dbpedia-owl:description                     | xsd:string                                             |
--------------------------------------------------------------------------------------------------------
like image 86
Joshua Taylor Avatar answered Mar 08 '23 18:03

Joshua Taylor