wikidata fetching entities id containing a literal - sparql

I am trying to obtain the wikidata entity_id containing a given literal, for example "crypto".
I'm using:
SELECT ?item WHERE {
?o rdfs:label ?ol
FILTER (lang(?ol) = 'en')
FILTER(contains(str(?ol),'crypto' ))
}
Of course, the wrong approach :-/
Any help, more than welcome. Thx in advance.

Related

Assigning specific values to multiple variable in SPARQL

I am trying to query the WikiData database using their SPARQL endpoint. Here I want to assign specific values to certain variables independent from each other. I know that the VALUES keyword can be used for this, however, is there a way of doing this for multiple variables without having to specify each possible combination? For example, the query looks like this:
SELECT ?relation1 ?item1?
WHERE {wd:Q31 ?relation1 ?item1 .}
I would like to specify the values for both ?relation1 and ?item1 without having to go over each possible combination.
Thank you.
Edited answer:
Yes, you can use multiple VALUES statements.
This can be written like this:
SELECT *
WHERE {
VALUES ?person {wd:Q35332 wd:Q23844} #Brad Pitt and Geroge Clooney
VALUES ?property {wdt:P27 wdt:P19} #Country of nationality and place of birth
?person ?property ?o ;
rdfs:label ?personLabel .
?o rdfs:label ?oLabel .
FILTER(LANG(?personLabel) = 'en' && LANG(?oLabel) = 'en')
}
You will then see these results:
This link will contain more information.

How to get all property from an item based on its Q number?

I want to gather all properties of a item from wikidata.
All queries I see so far assume you know properties you are looking for, but in my case, I'm not.
For example, when querying for Q1798740, I would like a returned value that looks like
[{"item": "Q1798740",
"P31": ["Q1146"],
"P17": ["Q70972"],
...
"P2043":"70 metres"}
]
and that contains all statements from the wikidata page
What query should I perform?
You need only to ask for {wd:Q1798740 ?p ?value} but it would be useful also to get the labels of the properties, which is a bit trickier:
SELECT DISTINCT ?p ?property_label ?value
WHERE
{
wd:Q1798740 ?p ?value .
?property wikibase:directClaim ?p ;
rdfs:label ?property_label .
FILTER(LANG(?property_label)="en")
}

SPARQL search query

I have some RDF data structured like this:
[ pref:ip_address "127.0.0.1" ;
pref:time "1459630844.482" ;
pref:url "https://google.com" ;
pref:user "johndoe"
] .
I need query that will return all results matching given IP, time frame (between from time and end time), url (even partial match) and user (also even partial match).
What I have now is simple query to get results based on single value, like this:
PREFIX pref: <http://something> SELECT DISTINCT * WHERE { ?u pref:user USER_VALUE . ?u ?p ?o . }
This returns all results with given user but only if given username is full match. Meaning it will return all results for johndoe if USER_VALUE is johndoe but not if it is john.
My knowledge of SPARQL is extremely limited and I appreciate any help. Thank you.
To do anything more than exact match, you need to use a FILTER and use operations like CONTAINS or REGEX.
Example:
{ ?u pref:user ?user .
?u ?p ?o .
FILTER( CONTAINS(?user, "john") )
}
There are a number of FILTER functions that may be useful including REGEX. See the spec for details.

How do I restrict my search in dbpedia using SPARQL to "Persons" only

I am using a query to output the influences of all people listed in wikipedia (where possible). I am using http://dbpedia.org/snorql/. My code so far is:
SELECT *
WHERE {
?p a
<http://dbpedia.org/ontology/Person> .
?p <http://dbpedia.org/ontology/influenced> ?influenced.
}
The problem is that the influenced output includes things like genres and political ideologies. I want to restrict it to only output "people" and "people who were influenced by those people". Thanks in advanced.
Try:
SELECT *
WHERE {
?p a
<http://dbpedia.org/ontology/Person> .
?p <dbpedia-owl:birthYear> ?birthYear.
?p <http://dbpedia.org/ontology/influenced> ?influenced.
?influenced a <http://dbpedia.org/ontology/Person>.
}
EDITED TO ADD BIRTH YEAR

How to resolve disambiguation in Yago/DBpedia?

Assuming I am searching for an entity such as "Wizard of Oz" and know I am specifically interested in the book rather then movie or musical.
Which query/method will return correct results on most cases?
You can do that with a query like:
SELECT * WHERE {
?s <http://dbpedia.org/property/name> ?name .
?s a <http://dbpedia.org/ontology/Book> .
FILTER(regex(STR(?name), "wizard of oz", "i"))
}
You can also do that with DBpedia Lookup:
http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=book&QueryString=wizard+of+oz
Or with DBpedia Spotlight:
http://spotlight.dbpedia.org/rest/candidates?text=wizard+of+oz+book
As every book, and only books, have an ISBN, I guess you can take Steve Harris' query and, instead of asking if it is a book, you can ask if it has an ISBN.
SELECT * WHERE {
?s <http://dbpedia.org/property/name> ?name .
?s <http://dbpedia.org/ontology/isbn> ?isbn .
FILTER(regex(STR(?name), "wizard of oz", "i"))
}