Questions Re: AnzoGraph Tutorial SPARQL Queries - sparql

I'm going through the AnzoGraph tutorial. I have 2 questions: 1) They often use desc, e.g. desc(?sum) in the Order By or other parts of the query. I'm not familiar with this and didn't find much help in the SPARQL documentation. What does desc do and why is it needed? 2) Sometimes, when I would expect to see a number in the output I see an alphanumeric string. E.g., in the query:
SELECT ?location ?kind ?name ?list_date (((?selldate - ?list_date)) as ?sale_age)
FROM <tickit>
WHERE {
?sale <saletime> ?selldate .
?sale <eventid> ?event .
?listing <eventid> ?event .
?listing <listtime> ?list_date .
?event <eventname> ?name .
?event <venueid> ?venue .
?event <catid> ?cat .
?cat <catname> ?kind .
?venue <venuename> ?location .
}
ORDER BY desc(?selldate) desc(?list_date) ?location ?kind ?name
I expect to see ?sale_age as an an integer but it is displayed as alphanumerics such as: P1DT1H50M.

Related

SPARQL query - multiple OPTIONALS

I want to retrieve the entities of the classes along with some information - if exists (e.g. label, comment, image)
I run the following query:
PREFIX schema: <http://schema.org/>
SELECT DISTINCT ?entity ?label ?comment ?image
WHERE {
?entity a ?class .
OPTIONAL {?entity rdfs:label ?label } .
OPTIONAL {?entity schema:image ?image} .
OPTIONAL {?entity rdfs:comment ?comment} .
OPTIONAL {?entity rdfs:label ?label} .
FILTER (langMatches(lang(?comment), "EN") && langMatches(lang(?label), "EN")) .
}
Although, some triples contain this extra information I do not receive them
(even if I re-write and include all the optional statements in one statement)
How could I compose such a query?

i want to get the names of similar types using sparql queries from dbpedia

I need to find the names of similar types from DBpedia so I'm trying to figure out a query which can return me the names of entities which have same subject type in its dct:subject (example I want to find similar types of white house so i want to write a query for same . I'm considering the dct:subject to find them ). If there is any other approach please mention it
Previously I tried it for rdf:type but the result are not so good and some time it shows time out
I have done my problem by the query mentioned below and now i want to consider dct:subject instead of rdf:type
select distinct ?label ?resource count(distinct ?type) as ?score where {
values ?type { dbo:Thing dbo:Organization yago:WikicatIslam-relatedControversies yago:WikicatIslamistGroups yago:WikicatRussianFederalSecurityServiceDesignatedTerroristOrganizations yago:Abstraction100002137 yago:Act100030358 yago:Cabal108241798 yago:Group100031264 yago:Movement108464601 yago:PoliticalMovement108472335
}
?resource rdfs:label ?label ;
foaf:name ?name ;
a ?type .
FILTER (lang(?label) = 'en').
}
ORDER BY DESC(?score)

how to remove duplicates in sparql query

I wrote this query and return list of couples and particular condition. ( in http://live.dbpedia.org/sparql)
SELECT DISTINCT ?actor ?person2 ?cnt
WHERE
{
{
select DISTINCT ?actor ?person2 (count (?film) as ?cnt)
where {
?film dbo:starring ?actor .
?actor dbo:spouse ?person2.
?film dbo:starring ?person2.
}
order by ?actor
}
FILTER (?cnt >9)
}
Problem is that some rows is duplicate.
example:
http://dbpedia.org/resource/George_Burns http://dbpedia.org/resource/Gracie_Allen 12
http://dbpedia.org/resource/Gracie_Allen http://dbpedia.org/resource/George_Burns 12
how to remove these duplications?
I added gender to ?actor but it damage current result.
Natan Cox's answer shows the typical way to exclude these kind of pseudo-duplicates. The results aren't actually duplicates, because in one, e.g., George Burns is the ?actor, and in the other he is the ?person2. In many cases, you can add a filter to require that the two things are ordered, and that will remove the duplicate cases. E.g., when you have data like:
:a :likes :b .
:a :likes :c .
and you search for
select ?x ?y where {
:a :likes ?x, ?y .
}
you can add filter(?x < ?y) to enforce an ordering between the between ?x and ?y which will remove these pseudo-duplicates. However, in this case, it's a bit trickier, since ?actor and ?person2 aren't found using the same critera. If DBpedia contains
:PersonB dbo:spouse :PersonA
but not
:PersonA dbo:spouse :PersonB
then the simple filter won't work, because you'll never find the triple where the subject PersonA is less than the object PersonB. So in this case, you also need to modify your query a bit to make the criteria symmetric:
select distinct ?actor ?spouse (count(?film) as ?count) {
?film dbo:starring ?actor, ?spouse .
?actor dbo:spouse|^dbo:spouse ?spouse .
filter(?actor < ?spouse)
}
group by ?actor ?spouse
having (count(?film) > 9)
order by ?actor
(This query also shows that you don't need a subquery here, you can use having to "filter" on aggregate values.) But the important part is using the property path dbo:spouse|^dbo:spouse to find a value for ?spouse such that either ?actor dbo:spouse ?spouse or ?spouse dbo:spouse ?actor. This makes the relationship symmetric, so that you're guaranteed to get all the pairs, even if the relationship is only declared in one direction.
It is not actual duplicates of course since you can look at it from both ways. The way to fix it if you want to is to add a filter. It is a bit of a dirty hack but it only takes on of the 2 rows that are the "same".
SELECT DISTINCT ?actor ?person2 ?cnt
WHERE
{
{
select DISTINCT ?actor ?person2 (count (?film) as ?cnt)
where {
?film dbo:starring ?actor .
?actor dbo:spouse ?person2.
?film dbo:starring ?person2.
FILTER (?actor < ?person2)
}
order by ?actor
}
FILTER (?cnt >9)
}

Dbpedia/sparql: get population & lat/lng of all cities/towns/villages in UK

I'm entering the following query at http://dbpedia.org/sparql:
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
SELECT ?s ?name ?value ?lat ?lng
WHERE {
?s a <http://dbpedia.org/ontology/PopulatedPlace> .
?s <http://dbpedia.org/property/name> ?name .
?s <http://dbpedia.org/property/populationTotal> ?value .
FILTER (?lng > -8.64 AND ?lng < 2.1 AND ?lat < 61.1 AND ?lat > 49.35 )
?s geo:lat ?lat .
?s geo:long ?lng .
}
(The bounding box is intended to be for the UK, the other option is to add <http://dbpedia.org/ontology/country> <http://dbpedia.org/resource/United_Kingdom> ., but there's a possibility that some places might not have been tagged with UK as the country).
The problem is that it doesn't seem to be pulling back many places (around 290).
Swapping population for populationTotal gives 1588 places, and I can't figure out (semantically) which one should be used.
Is this a limitation with the underlying data, or is there something that could be improved in the way I'm formulating the query?
Note: this question is mainly academic now as I got the info from http://download.geonames.org/export/dump/GB.zip, but I'd much prefer to use open data and the semantic web, so posting up this question to see if there was something I was missing, or to find out if there is a shortcoming in how the data is being scraped from Wikipedia and whether I can muck in.
Your query is only returning locations that have a value for populationTotal. For example, if Town A has "10,000" for populationTotal in the database, and Town B has NULL, only Town A will be returned.
If you want to return all locations in the UK, then you need to specify population as an optional parameter. This query will show you all the locations, as well as the populations for the ones that have that data.
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
SELECT ?s ?name ?value ?lat ?lng
WHERE {
?s a <http://dbpedia.org/ontology/PopulatedPlace> .
?s <http://dbpedia.org/property/name> ?name .
OPTIONAL { ?s <http://dbpedia.org/property/populationTotal> ?value . }
FILTER (?lng > -8.64 AND ?lng < 2.1 AND ?lat < 61.1 AND ?lat > 49.35 )
?s geo:lat ?lat .
?s geo:long ?lng .
}

Creating geometry object from two lat/long doubles in SPARQL

I would like to get some places from DBPedia in certain area, and that would be easy if those points would had geometry property. However, all they have is georss:point. I have converted this to two doubles, but I cannot convert them to geo:geometry object that can be supplied to location filter.
The code I have thus far:
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?name
?abstract
?ns
?ew
WHERE
{ ?m foaf:name ?name
. ?m georss:point ?coord
. ?m <http://dbpedia.org/ontology/abstract> ?abstract
. BIND( xsd:double(strbefore( ?coord, " " )) AS ?ns )
BIND( xsd:double(strafter( ?coord, " " )) AS ?ew )
BIND( geo:Point(?ew, ?ns) AS ?geo ) # <-- I have problem with this
FILTER (bif:st_intersects (?geo, bif:st_point(?geo), 10))
FILTER (LANG(?abstract) = "en")
}
If you know any way to convert those or other filtering method, please enlighten me. :)
There is not enough information here to give you a simple and clear answer.
However, I think you may be able to figure it out from the examples in the GEOSPARQL documentation for Virtuoso, the DBMS engine which hosts DBpedia. There's more here.