dbpedia SPARQL query to get certain value's for a given city

♀尐吖头ヾ 提交于 2019-12-07 17:53:44

问题


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!


回答1:


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' )
}



回答2:


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.



来源:https://stackoverflow.com/questions/9904938/dbpedia-sparql-query-to-get-certain-values-for-a-given-city

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!