Printing matching properties in DBpedia query - properties

The following query will search for matching subjects based on a list of properties and within a given distance. They are ranked by the number of matching properties (?numProperties), which is output as a number. How can I also print each of the properties that are matches?
Run query
select ?subject (count ( distinct ?property) as ?numProperties) ?label ?lat ?long where {
values ?property { dbpedia-owl:crosses dbpedia-owl:vehicle dbpedia-owl:reopened dbpedia-owl:years dbpedia-owl:access dbpedia-owl:third dbpedia-owl:time dbpedia-owl:construction dbpedia-owl:anniversary dbpedia-owl:series dbpedia-owl:length dbpprop:suspension dbpprop:bridge dbpprop:crosses dbpprop:City dbpprop:connecting dbpprop:last dbpprop:three dbpprop:suspension dbpprop:bridges dbpprop:built }
?subject ?property ?object .
?subject rdfs:label ?label .
?subject geo:lat ?lat .
?subject geo:long ?long .
FILTER (?long > -74.490898 && ?long < -73.490898 && ?lat > 40.207222 && ?lat < 41.207222 ) .
FILTER(langMatches(lang(?label),"EN")) .
}
group by ?subject ?label ?lat ?long
order by desc(?numProperties)
limit 15

#-- I took the liberty of tidying up your query a bit.
#-- The key is to use the GROUP_CONCAT aggregate function.
select
?subject
(count(distinct ?property) as ?numProperties)
?label
?lat
?long
#-- concatenate distinct properties into a ', ' separated string
(group_concat(distinct ?property;separator=', ') as ?properties)
where {
values ?property {
dbpedia-owl:crosses dbpedia-owl:vehicle dbpedia-owl:reopened
dbpedia-owl:years dbpedia-owl:access dbpedia-owl:third
dbpedia-owl:time dbpedia-owl:construction dbpedia-owl:anniversary
dbpedia-owl:series dbpedia-owl:length dbpprop:suspension
dbpprop:bridge dbpprop:crosses dbpprop:City dbpprop:connecting
dbpprop:last dbpprop:three dbpprop:suspension
dbpprop:bridges dbpprop:built
}
?subject ?property ?object ;
rdfs:label ?label ;
geo:lat ?lat ;geo:long ?long .
FILTER ( -74.490898 < ?long && ?long < -73.490898 &&
40.207222 < ?lat && ?lat < 41.207222 )
FILTER(langMatches(lang(?label),"EN"))
}
group by ?subject ?label ?lat ?long
order by desc(?numProperties)
limit 15
SPARQL results

Related

transaction time out in sparql query

select distinct ?label ?resource count(distinct ?type) as ?score
where
{
values ?type
{ <http://dbpedia.org/class/yago/Abstraction100002137>
<http://dbpedia.org/class/yago/Company108058098>
<http://dbpedia.org/class/yago/ElectronicsCompany108003035>
<http://dbpedia.org/class/yago/Group100031264>
<http://dbpedia.org/class/yago/Institution108053576>
<http://dbpedia.org/class/yago/Organization108008335>
}
?resource rdfs:label ?label ;
foaf:name ?name ;
a ?type .
FILTER (lang(?label) = 'en').
}
ORDER BY DESC(?score)
limit 10
I am trying to run this query but it shows transaction timeout, I cut out many of these links from this query, but still it shows same issue

Duplicated results from Wikidata

I created the following SPARQL query to Wikidata. And the result of this query are records related to states in Germany. But as you can see, results are occurring four times in a row (you can test it here: https://query.wikidata.org/). I supposed that there is a problem with geo coordinates and languages but I can't resolve it anyway. What is wrong with this query and how can I fix it to receive a result without repetition?
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX schema: <http://schema.org/>
PREFIX psv: <http://www.wikidata.org/prop/statement/value/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wd: <http://www.wikidata.org/entity/>
SELECT DISTINCT ?subject ?featureCode ?countryCode ?name ?latitude ?longitude ?description ?iso31662
WHERE
{ ?subject wdt:P31 wd:Q1221156 ;
rdfs:label ?name ;
wdt:P17 ?countryClass .
?countryClass
wdt:P297 ?countryCode .
?subject wdt:P31/(wdt:P279)* ?adminArea .
?adminArea wdt:P2452 "A.ADM1" ;
wdt:P2452 ?featureCode .
?subject wdt:P300 ?iso31662
OPTIONAL
{ ?subject schema:description ?description
FILTER ( lang(?description) = "en" )
?subject p:P625 ?coordinate .
?coordinate psv:P625 ?coordinateNode .
?coordinateNode
wikibase:geoLatitude ?latitude ;
wikibase:geoLongitude ?longitude
}
FILTER ( lang(?name) = "en" )
FILTER EXISTS { ?subject wdt:P300 ?iso31662 }
}
ORDER BY lcase(?name)
OFFSET 0
LIMIT 200
In short, "9.0411111111111"^^xsd:double and "9.0411111111111"^^xsd:decimal are distinct, though they might be equal in some sense.
Check this:
SELECT DISTINCT ?subject ?featureCode ?countryCode ?name ?description ?iso31662
(datatype(?latitude) AS ?lat)
(datatype(?longitude) AS ?long)
and this:
SELECT DISTINCT ?subject ?featureCode ?countryCode ?name ?description ?iso31662
(xsd:decimal(?latitude) AS ?lat)
(xsd:decimal(?longitude) AS ?long)

Aggregate properties

I'm developing my own Fuseki endpoint from some DBpedia data.
I'm in doubt on how to aggregate properties related to a single resource.
SELECT ?name ?website ?abstract ?genre ?image
WHERE{
VALUES ?s {<http://dbpedia.org/resource/Attack_Attack!>}
?s foaf:name ?name ;
dbo:abstract ?abstract .
OPTIONAL { ?s dbo:genre ?genre } .
OPTIONAL { ?s dbp:website ?website } .
OPTIONAL { ?s dbo:image ?image } .
FILTER LANGMATCHES(LANG(?abstract ), "en")
}
SPARQL endpoint: http://dbpedia.org/sparql/
This query returns 2 matching results. They are different just for the dbo:genre value. There is a way I can query the knowledge base and retrieving a single result with a list of genres?
#chrisis's query works well on the DBpedia SPARQL Endpoint, which is based on Virtuoso.
However, if you are using Jena Fuseki, you should use more conformant syntax:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
SELECT
?name
(SAMPLE(?website) AS ?sample_website)
(SAMPLE(?abstract) AS ?sample_abstract)
(SAMPLE(?image) AS ?sample_image)
(GROUP_CONCAT(?genre; separator=', ') AS ?genres)
WHERE {
VALUES (?s) {(<http://dbpedia.org/resource/Attack_Attack!>)}
?s foaf:name ?name ;
dbo:abstract ?abstract .
OPTIONAL { ?s dbo:genre ?genre } .
OPTIONAL { ?s dbp:website ?website } .
OPTIONAL { ?s dbo:image ?image} .
FILTER LANGMATCHES(LANG(?abstract ), "en")
} GROUP BY ?name
The differences from the #chrisis's query are:
Since GROUP_CONCAT is an aggregation function, it might be used with GROUP BY only;
Since GROUP BY is used, all non-grouping variables should be aggregated (e.g. via SAMPLE);
GROUP_CONCAT syntax is slightly different.
In Fuseki, these AS in the projection are in fact superfluous: see this question and comments.
Yes, the GROUP_CONCAT() function is what you want.
SELECT ?name ?website ?abstract (GROUP_CONCAT(?genre,',') AS ?genres) ?image
WHERE{
<http://dbpedia.org/resource/Attack_Attack!> a dbo:Band ;
foaf:name ?name;
dbo:abstract ?abstract .
OPTIONAL{ <http://dbpedia.org/resource/Attack_Attack!> dbo:genre ?genre } .
OPTIONAL{ <http://dbpedia.org/resource/Attack_Attack!> dbp:website ?website} .
OPTIONAL{ <http://dbpedia.org/resource/Attack_Attack!> dbo:image ?image} .
FILTER LANGMATCHES(LANG(?abstract ), "en")
}

How to use Sparql Contains to match similar String?

I'm trying to grab some definition in dbpedia inside my thesaurus.
Although can find country that have a label that match my country, i don't get all of them. So i try to match similar label with contains but it does not work.
Any idea why.
SELECT distinct ?idbcountry ?label ?labelDb ?def
WHERE {
?idbcountry a skos:Concept .
?idbcountry rdfs:label ?label .
?idbcountry skos:inScheme iadb:IdBCountries .
FILTER(lang(?label) = "en")
Service <http://dbpedia.org/sparql> {
?s a <http://dbpedia.org/ontology/Country> .
?s rdfs:label ?labelDb .
FILTER(CONTAINS (?labelDb, ?label)).
?s rdfs:comment ?def .
FILTER(lang(?def) = "en") .
FILTER(lang(?labelDb) = "en") .
}}
The exact matching query that works is as follows:
SELECT distinct ?idbcountry ?label ?def
WHERE {
?idbcountry a skos:Concept .
?idbcountry rdfs:label ?label .
?idbcountry skos:inScheme iadb:IdBCountries .
FILTER(lang(?label) = "en")
Service <http://dbpedia.org/sparql> {
?s a <http://dbpedia.org/ontology/Country> .
?s rdfs:label ?label .
?s rdfs:comment ?def
FILTER(lang(?def) = "en")
}
}
EDIT1
Data Samples:
<http://thesaurus.iadb.org/publicthesauri/10157002136735779158437>
rdf:type skos:Concept ;
dct:created "2015-03-27T16:43:48.052-04:00"^^xsd:dateTime ;
rdfs:label "BO"#en ;
rdfs:label "Bolivia"#en ;
rdfs:label "Bolivia"#es ;
rdfs:label "Bolivie"#fr ;
rdfs:label "Bolívia"#pt ;
skos:altLabel "BO"#en ;
skos:definition "Bolivia (/bəˈlɪviə/, Spanish: [boˈliβja], Quechua: Buliwya, Aymara: Wuliwya), officially known as the Plurinational State of Bolivia (Spanish: Estado Plurinacional de Bolivia locally: [esˈtaðo pluɾinasjoˈnal de βoˈliβja]), is a landlocked country located in western-central South America."#en ;
skos:inScheme :IdBCountries ;
skos:prefLabel "Bolivia"#en ;
skos:prefLabel "Bolivia"#es ;
skos:prefLabel "Bolivie"#fr ;
skos:prefLabel "Bolívia"#pt ;
skos:topConceptOf :IdBCountries ;
<http://xmlns.com/foaf/0.1/focus> <http://dbpedia.org/resource/Bolivia> ;
Without seeing your data, we can't know why your query isn't working. However, using contains is pretty straightforward. It's just a matter of contains(string,substring). As Jeen said, we can't reproduce your problem without knowing what your data looks like, but here's an example of contains in action:
select distinct ?country ?label {
?country a dbpedia-owl:Country ; #-- select countries
rdfs:label ?label . #-- and get labels
filter langMatches(lang(?label),"en") #-- but only English labels
filter contains(?label,"land") #-- containing "land"
}
SPARQL results

Query for banks in DBpedia

Using http://dbpedia.org/sparql, I want to receive the geographic coordinates of all bank buildings. The list of classes tells me that I should query for Bank.
Yet, the following code yields nothing:
SELECT DISTINCT ?label ?lat ?long
WHERE {
[]
rdf:type dbpedia-owl:Bank ;
geo:lat ?lat ;
geo:long ?long ;
rdfs:label ?label.
FILTER (LANGMATCHES(LANG(?label), 'en'))
}
If instead I query for any sibling to Bank, (e.g. Brewery or LawFirm), I see at least some results. What's wrong with above code?
If you look into a dbpedia page for a bank, you can see that instead on rdf:type, banks have a property dbpedia-owl:industry that has dbpedia:Bank (Refah bank) or dbpedia:Financial_services (Cyprus bank) as a value. So if you rewrite your query as the following, you will get some results:
SELECT DISTINCT ?label ?lat ?long
WHERE {
[] dbpedia-owl:industry dbpedia:Bank ;
geo:lat ?lat ;
geo:long ?long ;
rdfs:label ?label.
FILTER (LANGMATCHES(LANG(?label), 'en'))
}
If you add dbpedia:Financial_services, other organisations such as London stock exchange will also appear:
SELECT DISTINCT ?bank
WHERE {
?bank dbpedia-owl:industry ?place ;
geo:lat ?lat ;
geo:long ?long ;
rdfs:label ?label.
FILTER (?place in (dbpedia:Financial_services, dbpedia:Bank) &&
LANGMATCHES(LANG(?label), 'en'))
}
Again, by examining the London stock exchange, you can see that there is a product property that separates these financial institutions. So this will give you banks, but it might not cover all the banks available:
SELECT DISTINCT ?label ?lat ?long
WHERE {
[] dbpedia-owl:industry ?place ;
geo:lat ?lat ;
geo:long ?long ;
rdfs:label ?label;
dbpedia-owl:product ?product.
FILTER ( ?place in (dbpedia:Financial_services, dbpedia:Bank)
&& ?product in (dbpedia:Bank, dbpedia:Private_banking, dbpedia:Professional_Banking, dbpedia:Retail_banking, dbpedia:Investment_banking, dbpedia:Commercial_bank)
&& LANGMATCHES(LANG(?label), "en"))
} ORDER BY DESC(COUNT(DISTINCT ?product))