How to query Wikidata (SPARQL) by official website property P856? - sparql

How can you search the Wikidata SPARQL query service for a record whose official website (property P856) matches a specific value?
For example, fetch all records whose official website is "https://www.google.com". This should return Q95 and Q9366, but instead the following query yields no results:
SELECT ?entity
WHERE {
?entity wdt:P856 "https://www.google.com"
}
I can get it to work with a FILTER, but it's very slow:
SELECT ?entity
WHERE {
?entity wdt:P856 ?url FILTER(str(?url)="https://www.google.com")
}
Using FILTER seems very inefficient. Why doesn't the first query work?

Look at the values you get from this query --
SELECT ?entity ?value
WHERE
{
?entity wdt:P856 ?value
}
LIMIT 25
Note that they're all wrapped in <>, marking them as URIs. To compare a URI with a string literal, you must change the URI to a string, as you are doing in the second query.
In other words -- there are no entities with an official website of "https://www.google.com", but there are some entities with an official website of <https://www.google.com>.

Related

Get movie(s) based on book(s) from DBpedia

I am new to SPARQL and trying to fetch a movie adapted from specific book from dbpedia. This is what I have so far:
PREFIX onto: <http://dbpedia.org/ontology/>
SELECT *
WHERE
{
<http://dbpedia.org/page/2001:_A_Space_Odyssey> a ?type.
?type onto:basedOn ?book .
?book a onto:Book
}
I can't get any results. How can I do that?
When using any web resource, and in your case the property :basedOn, you need to make sure that you have declared the right prefix. If you are querying from the DBpedia SPARQL endpoint, then you can directly use dbo:basedOneven without declaring it, as it is among predefined. Alternatively, if you want to use your own, or if you are using another SPARQL client, make sure that whatever short name you choose for this property, you declare the prefix for http://dbpedia.org/ontology/.
Then, first, to get more result you may not restrict the type of the subject of this triple pattern, as there could be movies that actually not type as such. So, a query like this
select distinct *
{
?movie dbo:basedOn ?book .
?book a dbo:Book .
}
will give you lots of good results but not all. For example, the resource from your example will be missing. You can easily check test the available properties between these two resource with a query like this:
select ?p
{
{<http://dbpedia.org/resource/2001:_A_Space_Odyssey_(film)> ?p <http://dbpedia.org/resource/2001:_A_Space_Odyssey> }
UNION
{ <http://dbpedia.org/resource/2001:_A_Space_Odyssey> ?p <http://dbpedia.org/resource/2001:_A_Space_Odyssey_(film)>}
}
You'll get only one result:
http://www.w3.org/2000/01/rdf-schema#seeAlso
(note that the URI is with 'resource', not with 'page')
Then you may search for any path between the two resource, using the method described here, or find a combination of other patterns that would increase the number of results.

How to get 'is dct:subject of' property from dbpedia using SPARQL [duplicate]

I am using the following query :
select ?value where { <http://dbpedia.org/resource/Paris> dbpedia-owl:wikiPageRedirects* ?value }
in order to retrieve the wikiPageRedirects property of Paris.
Based on dbpedia Paris has more than 20 redirect links. Why am I only retrieving the first one?
Your direction was wrong.
select distinct *
where {
?x dbpedia-owl:wikiPageRedirects <http://dbpedia.org/resource/Paris>
}
Artemis's answer is right; the "direction" in the query is wrong. It's worth explaining that a bit more, though. On the DBpedia "page", you'll see lots of data like:
dbpedia-owl:area 105400000.000000 (xsd:double)
dbpedia-owl:country dbpedia:France
dbpedia-owl:inseeCode 75056 (xsd:integer)
dbpedia-owl:mayor dbpedia:Anne_Hidalgo
These mean that DBpedia contains triples where these are the predicates and objects. That is, DBpedia contains a triple:
dbpedia:Paris dbpedia-owl:country dbpedia:France
On other hand, you'll also see things like "is … of":
is dbpedia-owl:beatifiedPlace of dbpedia:Daniel_Brottier
is dbpedia-owl:billed of dbpedia:René_Duprée
These mean that dbpedia:Paris the object of triples with these subjects and predicates. E.g., DBpedia contains the triple
dbpedia:René_Duprée dbpedia-owl:billed dbpedia:Paris
The redirects properties that you're seeing are like this:
is dbpedia-owl:wikiPageRedirects of dbpedia:City_of_Love_(city)
dbpedia:Département_de_Paris
dbpedia:Departement_de_Paris
dbpedia:FRPAR
That means that there are a bunch of triples of the form:
?something dbpedia-owl:wikiPageRedirects dbpedia:Paris
and that means that your query needs to be
select ?resource where {
?resource dbpedia-owl:wikiPageRedirects dbpedia:Paris
}
SPARQL results

How to search for rdfs:labels in dbpedia which are partial matches to a given term using SPARQL?

I am using this query to search for all labels that contains the word "Medi"
select distinct ?label where
{
?concept rdfs:label ?label
filter contains(?label,"Medi")
filter(langMatches(lang(?label),"en"))
}
However, as soon as I change the term from "Medi" to "Medicare" it doesn't work and times out. How do I get it to work with longer words like Medicare i.e. extract all labels which has the word Medicare in it.
Your query has to iterate over all labels in DBpedia - which is quite a large number - and then apply String containment check. This is indeed expensive.
Even a count query leads to an "estimated timeout error":
select count(?label) where
{
?concept rdfs:label ?label
filter(regex(str(?label),"Medi"))
filter(langMatches(lang(?label),"en"))
}
Two options:
Virtuoso has some fulltext search support:
SELECT DISTINCT ?label WHERE {
?concept rdfs:label ?label .
?label bif:contains "Medicare"
FILTER(langMatches(lang(?label),"en"))
}
Since the public DBpedia endpoint is a shared endpoint, the solution is to load the DBpedia dataset into your own triple store, e.g. Virtuoso. There you can adjust the max. estimated execution timeout parameter.

How to get data property from dbpedia? [duplicate]

I am using the following query :
select ?value where { <http://dbpedia.org/resource/Paris> dbpedia-owl:wikiPageRedirects* ?value }
in order to retrieve the wikiPageRedirects property of Paris.
Based on dbpedia Paris has more than 20 redirect links. Why am I only retrieving the first one?
Your direction was wrong.
select distinct *
where {
?x dbpedia-owl:wikiPageRedirects <http://dbpedia.org/resource/Paris>
}
Artemis's answer is right; the "direction" in the query is wrong. It's worth explaining that a bit more, though. On the DBpedia "page", you'll see lots of data like:
dbpedia-owl:area 105400000.000000 (xsd:double)
dbpedia-owl:country dbpedia:France
dbpedia-owl:inseeCode 75056 (xsd:integer)
dbpedia-owl:mayor dbpedia:Anne_Hidalgo
These mean that DBpedia contains triples where these are the predicates and objects. That is, DBpedia contains a triple:
dbpedia:Paris dbpedia-owl:country dbpedia:France
On other hand, you'll also see things like "is … of":
is dbpedia-owl:beatifiedPlace of dbpedia:Daniel_Brottier
is dbpedia-owl:billed of dbpedia:René_Duprée
These mean that dbpedia:Paris the object of triples with these subjects and predicates. E.g., DBpedia contains the triple
dbpedia:René_Duprée dbpedia-owl:billed dbpedia:Paris
The redirects properties that you're seeing are like this:
is dbpedia-owl:wikiPageRedirects of dbpedia:City_of_Love_(city)
dbpedia:Département_de_Paris
dbpedia:Departement_de_Paris
dbpedia:FRPAR
That means that there are a bunch of triples of the form:
?something dbpedia-owl:wikiPageRedirects dbpedia:Paris
and that means that your query needs to be
select ?resource where {
?resource dbpedia-owl:wikiPageRedirects dbpedia:Paris
}
SPARQL results

How to retrieve abstract for a DBpedia resource?

I need to find all DBpedia categories and articles that their abstract include a specific word.
I know how to write a SPARQL query that queries the label like the following:
SELECT ?uri ?txt WHERE {
?uri rdfs:label ?txt .
?txt bif:contains "Machine" .
}
but I have not figured out yet how to search the abstract.
I've tried with the following but it seems not to be correct.
SELECT ?uri ?txt WHERE {
?uri owl:abstract ?txt .
?txt bif:contains "Machine" .
}
How can I retrieve the abstract in order to query its text?
Since you already know how to search a string for text content, this question is really about how to get the abstract. If you retrieve any DBpedia resource in a web browser, e.g., http://dbpedia.org/resource/Mount_Monadnock (which will redirect to http://dbpedia.org/page/Mount_Monadnock), you can see the triples of which it's a subject or predicate. In this case, you'll see that the property is dbpedia-owl:abstract. Thus you can do things like
select * where {
?s dbpedia-owl:abstract ?abstract .
?abstract bif:contains "Monadnock" .
filter langMatches(lang(?abstract),"en")
}
limit 10
SPARQL results
Instead of visiting the page for the resource, which not endpoints will support, you could have simply retrieved all the triples for the subject, and looked at which ones relate it to its abstract. Since you know the abstract is a literal, you could even restrict it to triples where the object is a literal, and perhaps with a language that you want. E.g.,
select ?p ?o where {
dbpedia:Mount_Monadnock ?p ?o .
filter ( isLiteral(?o) && langMatches(lang(?o),'en') )
}
SPARQL results
This also clearly shows that the property you want is http://dbpedia.org/ontology/abstract. When you have a live query interface that you can use to pull down arbitrary data, it's very easy to find out what parts of the data you want. Just pull down more than you want at first, and then refine to get just what you want.