I am new to SPARQLR query and need some help.
I have 2 classes named "Platform" and "Video_game".
The "Platform" class has 3 instances: Windows, Playstation_4 and Xbox_One.
The "Video_game" class also has 3 instances: Just_Cause_3, Battlefield_4 and Grand_Theft_Auto_5
I also have object property named "availableOn" to link "Platform" and "Video_game" class together.
Now, if I want to retrieve all video games and their respective platform, I would run this query:
SELECT ?Name ?Platform
WHERE { ?Name rdf:type vg:Video_game ;
vg:availableOn ?Platform . }
That query can be run successfully. But now, I would like to use "FILTER" command to only show all the game but only on a particular platform. For example, only on "Windows".
How would I be able to FILTER the result? Do I need to add some data properties to use the FILTER command?
Thanks in advanced.
Let <P> be the URI for the windows platform. Get the right value from your data.
Something like:
SELECT ?Name
WHERE { ?Name rdf:type vg:Video_game ;
vg:availableOn <P> . }
You don't need a FILTER.
Or
SELECT ?Name ?Platform
WHERE { ?Name rdf:type vg:Video_game ;
vg:availableOn ?Platform .
FILTER(?Platform = <P>)
}
if you still want ?Platform in the results.
Related
I'm trying to get available information about certain resources from DBPedia.
However, I want to filter out the label, comment and abstract to be only in english.
My original query is:
SELECT ?property ?value (lang(?value ) as ?lang) { <http://dbpedia.org/resource/England> ?property ?value . }
To filter the english results I have modified it to:
SELECT ?property ?value (lang(?value ) as ?lang) { <http://dbpedia.org/resource/England> ?property ?value . FILTER(LANG(?value) = "en") }
However, doing this I lose a lot of entries and information such as population, density, position, latitude, longitude and many many more.
I was wondering, if there is a way to get all the available entries but filter the abstract, label and comment to be only in English.
I have tried to modify my Query to the following:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?property ?value (lang(?value ) as ?lang) { <http://dbpedia.org/resource/England> ?property ?value . FILTER(LANG(?rdfs:label) = "en")}
Unfortunately, I did not get any results since the query is false.
Any help will be greatly appreciated.
I have answered my own question after a thorough research.
You can use the FILTER function to get all the values with or without any #en or language part available. For this you can use the following part:
FILTER(LANG(?value) = "" || LANGMATCHES(LANG(?value), "en")
However, this leads to losing all the available uri and other links.
If one also wants to get all the links besides the English filtered results, one will also need a similar function to the startsWith function. Its equivalent in the SPARQL language is called strstarts. I have added the following part to the FILTER in my query and I have gotten all originally needed results.
strstarts(str(?value), 'http'))
So this is the resulting query. Hint I have removed the PREFIXES since I do not need them in my query
SELECT ?property ?value { <http://dbpedia.org/resource/England> ?property ?value .
FILTER(LANG(?value) = "" || LANGMATCHES(LANG(?value), "en") || strstarts(str(?value), 'http'))}
I'm trying to fetch people who are still alive from Wikipedia API but I haven't figured out how to do it.
I found this question which is the same as mine and as far as I understood the only way is to search for people who only have the birth_date parameter, how do I actually do that?
For instance, if I wanted to search for "Ronaldo", I should get a list of all people called Ronaldo who are still alive. The only data I care about are date of birth, name and surname, and eventually date of death if they die.
The only thing I came up with is this:
https://en.wikipedia.org/w/api.php?action=opensearch&search=%22Ronaldo%22&format=json
But it doesn't do the job that I'd like it to do because it only gets the biography and link.
You can execute the following query here: https://query.wikidata.org/ in order to fetch the details you are interested in.
SELECT DISTINCT ?id ?idLabel (SAMPLE(?birth) as ?birth_date)
(SAMPLE(?death_date) as ?dateOfDeath) WHERE {
?id wdt:P31 wd:Q5.
?id wdt:P569 ?birth .
OPTIONAL{?id wdt:P570 ?death_date .}
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en".
?id rdfs:label ?idLabel .
}?id ?label "Ronaldo"#en.
}
GROUP BY ?id ?idLabel
Please look at the following query:
SELECT distinct ?item ?itemLabel ?itemDescription (SAMPLE(?DR) as ?DR) (SAMPLE(?RIP) as ?RIP) (SAMPLE(?image)as ?image) (SAMPLE(?article)as ?article) WHERE {
?item wdt:P31 wd:Q5.
?item ?label "Ronaldo"#en.
?article schema:about ?item .
?article schema:inLanguage "en" .
?article schema:isPartOf <https://en.wikipedia.org/>.
OPTIONAL{?item wdt:P569 ?DR .} # P569 : Date of birth
OPTIONAL{?item wdt:P570 ?RIP .} # P570 : Date of death
OPTIONAL{?item wdt:P18 ?image .} # P18 : image
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
GROUP BY ?item ?itemLabel ?itemDescription
Query on Wikidata.
You can add Filter on death date to see only alive people.
Use the structured data query service. The generic API does not provide much in the way of filtering.
I'm using the following query to retrieve all cast members for "Star Wars - The Force Awakens" using its IMDb identifier (tt2488496).
The problem is that the results are not sorted accordingly, for example, I would like to see some main characters like John Boyega, Adam Driver, Daisy Ridley, etc. appear on the top most section of the result, but the results just seem random.
For example, IMDb sorts by top-billed cast, and I don't know if wikidata has that kind of info, but if we look into the wikidata page for the Star Wars - The Force Awakens title (https://www.wikidata.org/wiki/Q6074), in the cast member section, you will see that they are sorted properly.
Is there any way to achieve the same sorting that wikidata has on their title page?
SPARQL query (link):
SELECT ?titleLabel ?castLabel ?castImdb WHERE {
VALUES ?imdbIds { "tt2488496" }
?title wdt:P345 ?imdbIds .
?cast wdt:P345 ?castImdb .
?title wdt:P161 ?cast .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
I am having problems with a live.dbpedia SPARQL request, for it returns some entries twice (once as an utf8 URI, once as a non-utf8 URI : Here are the results.
Is it something that needs to be fixed inside of dbpedia (where should it be reported)?
Is there a way to keep only one version of these duplicated urls? (I do not want to ignore a non-utf8 URI if there is no utf8 counterpart)
P.S.: The actual request
select distinct ?name where {
?name <http://purl.org/dc/terms/subject><http://dbpedia.org/resource/Category:Individual_graphs>.
} ORDER BY desc(?name) LIMIT 2
Even though there are multiple URIs that can identify the article, they all have the same article title, so you can extract the title (it's the value of the rdfs:label property), group by that, and then sample the URIs. Doing that, along with using the built-in DBpedia namespaces, I end up with this query:
select distinct (sample(?name_) as ?name) where {
?name_ dcterms:subject category:Individual_graphs ;
rdfs:label ?label
}
group by ?label
order by desc(?name)
SPARQL results
I am trying to integrate SPARQL queries into Prolog program (I am using SWI-Prolog)
To test it I am doing the following operations:
Into SWI-Prolog shell I execute this command: use_module(library(semweb/sparql_client)). that load the sparql client library
Then, in the SWI-Prolog shell, I execute the following SPARQL query:
?- sparql_query('select count(*) where { ?person a http://dbpedia.org/ontology/Person .?person http://it.dbpedia.org/property/nome ?name.filter regex(?name,"Leonardo"). }', Row, [ host('dbpedia.org'), path('/sparql/')]).
Row = row(literal(type('http://www.w3.org/2001/XMLSchema#integer', '0'))).
I don't know well SPARQL but I think that this is pretty simple and that work in this way:
This query ask about the instances number of objects that are considered personal names on a RDF ontology called dbpedia, passing the input parameter "Leonardo".
As you can see the problem seems that don't find any instance of this type (I have tried also with others personal name)
Why? What are am I missing?
Your query does not return what you want it to return. You can use SPARQL interactively with the DBpedia SPARQL endpoint. I suggest that you use that to debug your query and make sure it returns what you would like first, and then copy it into your Prolog program. You can build it up in pieces to make sure it's giving suitable results. For instance, start with something like the following:
select * where {
?person a <http://dbpedia.org/ontology/Person> .
}
LIMIT 10
SPARQL results
Then expand it piece by piece:
select * where {
?person a <http://dbpedia.org/ontology/Person> .
?person <http://it.dbpedia.org/property/nome> ?name .
}
LIMIT 10
SPARQL results
This query doesn't return anything, so it would be best to debug it before continuing on the the FILTER or COUNT.
After playing with the endpoint for a while, it seems that using REGEX is going to be kind of expensive, as are other string operations. If you can query for the strings you want directly, you might be better off. Unfortunately, you'll need to start working with language tags, too. For instance, here is a query that counts people with the given name or surname "Leonardo".
select COUNT(?person) where {
?person a dbpedia-owl:Person .
{ ?person foaf:givenName "Leonardo" }
UNION
{ ?person foaf:surname "Leonardo" }
}
SPARQL results
It returns 0. However, if we add (English) language tags to those strings, we get different results (133):
select COUNT(?person) where {
?person a dbpedia-owl:Person .
{ ?person foaf:givenName "Leonardo"#en }
UNION
{ ?person foaf:surname "Leonardo"#en }
}
SPARQL results