dbpedia SPARQL query to get certain value's fo

2019-05-15 03:25发布

I am sure what I want to do is very easy, yet I cannot seem to get the query right. I have records in dataset which have values such as city name e.g. 'New York' and it's corresponding country code e.g 'US'. I also have access to the full country name and country ISO codes.

I would like to get the population and abstract value's for these cities off dbpedia, by using a where clause such as:

Get population where name = "New York" and isoCountryCode = "US"

I've searched for help on this to no avail.

so far I have been kindly helped by @rohk with this query, which does not fully work for all locations:

SELECT DISTINCT ?city ?abstract ?pop
WHERE {
   ?city rdf:type schema:City ;
     rdfs:label ?label ;
     dbpedia-owl:abstract ?abstract ;
     dbpedia-owl:country ?country ;
     dbpedia-owl:populationTotal ?pop .
   ?country dbpprop:countryCode "USA"@en .
   FILTER ( lang(?abstract) = 'en' and regex(?label, "New York City"))
 }

The above works for New York, however when I change it to:

SELECT DISTINCT ?city ?abstract ?pop
WHERE {
   ?city rdf:type schema:City ;
         rdfs:label ?label ;
         dbpedia-owl:abstract ?abstract ;
         dbpedia-owl:country ?country ;
         dbpedia-owl:populationTotal ?pop .
   ?country dbpprop:countryCode "THA"@en .
   FILTER ( lang(?abstract) = 'en' and regex(?label, "Bangkok"))
}

It returns no results for Bangkok, Thailand.

I just cant seem to get the SPARQL query correct, I'm sure I am being silly with my query. If any guru's could provide me with help I'd appreciate it. Thanks!

2条回答
劳资没心,怎么记你
2楼-- · 2019-05-15 03:47

I guess you want something like this:

SELECT * WHERE {
  ?x rdfs:label "New York City"@en.
  ?x dbpedia-owl:populationTotal ?pop.
  ?x dbpedia-owl:abstract ?abstract.
}

To get only the English abstract, add a FILTER:

SELECT * WHERE {
  ?x rdfs:label "New York City"@en.
  ?x dbpedia-owl:populationTotal ?pop.
  ?x dbpedia-owl:abstract ?abstract.
  FILTER (LANG(?abstract) = 'en')
}

“New York” is the state and it doesn't have a populationTotal figure attached. “New York City” is the city.

查看更多
戒情不戒烟
3楼-- · 2019-05-15 04:03

This query is working

SELECT DISTINCT *
WHERE {
   ?city rdf:type schema:City ;
         rdfs:label ?label ;
         dbpedia-owl:abstract ?abstract ;
         dbpedia-owl:country ?country ;
         dbpprop:website ?website ;
         dbpedia-owl:populationTotal ?pop .
   ?country dbpprop:countryCode "USA"@en .
   FILTER ( lang(?abstract) = 'en' and regex(?label, "New York City"))
}

EDIT : For Bangkok, there are 2 problems :

  • No country code for Thailand : you can use rdfs:label "Thailand"@en instead.
  • rdf:type of Bangkok is not schema:City but dbpedia-owl:Settlement

Here is a working query for Bangkok

SELECT DISTINCT *
WHERE {
   ?city rdf:type dbpedia-owl:Settlement ;
         rdfs:label "Bangkok"@en ;
         dbpedia-owl:abstract ?abstract ;
         dbpedia-owl:populationTotal ?pop ;
         dbpedia-owl:country ?country ;
         dbpprop:website ?website .
   ?country rdfs:label "Thailand"@en .
   FILTER ( lang(?abstract) = 'en' )
}
查看更多
登录 后发表回答