Using a string as an URI works using:
SELECT DISTINCT ?db_uri ?uri ?db_uri_var ?prop ?val
{
BIND(URI("http://dbpedia.org/resource/Delta_Air_Lines") as ?db_uri)
SERVICE <http://DBpedia.org/sparql>
{
?db_uri ?prop ?val .
}
}
However, as the value for the string comes "from somewhere else", I would need to use a variable inside the URI-call. This seems not to work.
SELECT DISTINCT ?db_uri ?uri ?db_uri_var ?prop ?val
{
BIND("http://dbpedia.org/resource/Delta_Air_Lines" as ?uri)
BIND(URI(?uri) as ?db_uri)
SERVICE <http://DBpedia.org/sparql>
{
?db_uri ?prop ?val .
}
}
Related
I'm trying to get the details of an organization based on the official website of the company using the below query. It constantly gets timed out.
I require all the below fields. Is there a way to optimize it? I understand that OPTIONAL is equivalent to an INNER JOIN and is the cause of the timeouts but is there any other way of getting these fields?
I'm using the python api and setting a timeout of 5mins doesn't help either. The timeout value doesn't get set.
SELECT distinct
(GROUP_CONCAT( DISTINCT ?official_name; separator=";") AS ?official_name)
(GROUP_CONCAT( DISTINCT ?isin; separator=";") AS ?isin)
?item
?itemLabel
?stock_exchange
?stock_exchangeLabel
(GROUP_CONCAT( DISTINCT ?ticker; separator=";") AS ?ticker)
(GROUP_CONCAT( DISTINCT ?other_name; separator=";") AS ?other_name)
(GROUP_CONCAT(DISTINCT ?parent_orgLabel; SEPARATOR = ";") AS ?parent_orgLabel)
(GROUP_CONCAT(DISTINCT ?owned_byLabel; SEPARATOR = ";") AS ?owned_byLabel)
(GROUP_CONCAT(DISTINCT ?instance_of; SEPARATOR = ";") AS ?instance_of)
(GROUP_CONCAT(DISTINCT ?instance_ofLabel; SEPARATOR = ";") AS ?instance_ofLabel)
(GROUP_CONCAT(DISTINCT ?domains; SEPARATOR = ";") AS ?domains)
(GROUP_CONCAT(DISTINCT ?subsidiaryLabel; SEPARATOR = ";") AS ?subsidiaryLabel)
(GROUP_CONCAT(DISTINCT ?owner_ofLabel; SEPARATOR = ";") AS ?owner_ofLabel)
(GROUP_CONCAT(DISTINCT ?part_ofLabel; SEPARATOR = ";") AS ?part_ofLabel)
WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
{
{ ?item p:P856 [ ps:P856 <https://www.amazon.com> ]}}
OPTIONAL {
?item p:P856 ?web_domains.
?web_domains ps:P856 ?domains .
}
OPTIONAL { ?item wdt:P1448 ?official_name. }
OPTIONAL { ?item wdt:P946 ?isin. }
OPTIONAL {
?item p:P414 ?SE .
?SE ps:P414 ?stock_exchange .
?SE pq:P249 ?ticker .
}
OPTIONAL { ?item skos:altLabel ?other_name. FILTER (LANG (?other_name) = "en") }
OPTIONAL {
?item wdt:P361 ?part_of.
?part_of rdfs:label ?part_ofLabel.
filter(lang(?part_ofLabel)="en")
}
OPTIONAL {
?item wdt:P749 ?parent_org.
?parent_org rdfs:label ?parent_orgLabel.
filter(lang(?parent_orgLabel)="en")
}
OPTIONAL {
?item wdt:P127 ?owned_by.
?owned_by rdfs:label ?owned_byLabel.
filter(lang(?owned_byLabel)="en")
}
OPTIONAL {
?item wdt:P31 ?instance_of.
?instance_of rdfs:label ?instance_ofLabel.
filter(lang(?instance_ofLabel)="en")
}
OPTIONAL {
?item wdt:P355 ?subsidiary.
?subsidiary rdfs:label ?subsidiaryLabel.
filter(lang(?subsidiaryLabel)="en")
}
OPTIONAL {
?item wdt:P1830 ?owner_of.
?owner_of rdfs:label ?owner_ofLabel.
filter(lang(?owner_ofLabel)="en")
}
}
GROUP BY ?item ?itemLabel ?stock_exchange ?stock_exchangeLabel
Is there an RDF data/other format that allow me to get all the properties that can exist in a category e.g. Person, then I should be returned properties like sex, date of birth.
How to query this information at https://query.wikidata.org/ ?
What I want is this https://www.wikidata.org/wiki/Wikidata:List_of_properties/Summary_table
But is there a better format for this? I want to access programmatically.
UPDATE
This query is too heavy, causes timeout.
SELECT ?p ?attName WHERE {
?q wdt:P31 wd:Q5.
?q ?p ?statement.
?realAtt wikibase:claim ?p.
?realAtt rdfs:label ?attName.
FILTER(((LANG(?attName)) = "en") || ((LANG(?attName)) = ""))
}
GROUP BY ?p ?attName
I must specify the entity, e.g. to Barrack Obama then it works, but this does not give me the all possible properties.
SELECT ?p ?attName WHERE {
BIND(wd:Q76 AS ?q)
?q wdt:P31 wd:Q5.
?q ?p ?statement.
?realAtt wikibase:claim ?p.
?realAtt rdfs:label ?attName.
FILTER(((LANG(?attName)) = "en") || ((LANG(?attName)) = ""))
}
GROUP BY ?p ?attName
1
The page you have linked to is created by a bot. Contact the BetaBot operator, if you need to know how the bot works.
2
Perhaps the bot relies on the wd:P1963 property:
SELECT ?property ?propertyLabel {
VALUES (?class) {(wd:Q5)}
?class wdt:P1963 ?property
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
} ORDER BY ASC(xsd:integer(strafter(str(?property), concat(str(wd:), "P"))))
The above query returns 49 results.
3
I'd suggest you rely on type constraints from property pages:
SELECT ?property ?propertyLabel {
VALUES (?class) {(wd:Q5)}
?property a wikibase:Property .
?property p:P2302 [ ps:P2302 wd:Q21503250 ;
pq:P2309 wd:Q21503252 ;
pq:P2308 ?class ] .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
} ORDER BY ASC(xsd:integer(strafter(str(?property), concat(str(wd:), "P"))))
The above query returns 700 results.
4
The first query from your question works fine for relatively small classes, e. g. wd:Q6256 ('country'). On the public endpoint, it is not possible to make the query work for large classes.
However, you could split the query into small parts. In Python:
from wdqs import Client
from time import sleep
client = Client()
result = client.query("SELECT (count(?p) AS ?c) {?p a wikibase:Property}")
count = int(result[0]["c"])
offset = 0
limit = 50
possible = []
while offset <= count:
props = client.query("""
SELECT ?property WHERE {
hint:Query hint:optimizer "None" .
{
SELECT ?property {
?property a wikibase:Property .
} ORDER BY ?property OFFSET %s LIMIT %s
}
?property wikibase:directClaim ?wdt.
FILTER EXISTS {
?human ?wdt [] ; wdt:P31 wd:Q5 .
hint:Group hint:maxParallel 501 .
}
hint:Query hint:filterExists "SubQueryLimitOne" .
# SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
""" % (offset, limit))
for prop in props:
possible.append(prop['property'])
offset += limit
print (len(possible), min(offset, count))
sleep(0.25)
The last line of the output is:
2156 5154
For a given property like 'occupation (P106)', I want to retrieve all its aliases like: profession, job, work, career, employment, craft. All of this is present on the properties wikidata page, under 'Also known as'. How can I go about retrieving this using SPARQL?
I tried using the following query.
SELECT ?predicate ?object WHERE {
wdt:P106 wdt:P1449 ?predicate . //Nickname
wdt:P106 wdt:P734 ?predicate . //Family Name
wdt:P106 wdt:P735 ?predicate . //Given Name
wdt:P106 skos:altLabel ?predicate .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
SELECT ?altLabel
{
VALUES (?wd) {(wd:P106)}
?wd skos:altLabel ?altLabel .
FILTER (lang(?altLabel) = "en")
}
or
SELECT ?altLabel
{
VALUES (?wdt) {(wdt:P106)}
?wd wikibase:directClaim ?wdt .
?wd skos:altLabel ?altLabel .
FILTER (lang(?altLabel) = "en")
}
These paragraphs provide some explanation:
Truthy statements
Properties
Predicates
Update
You still could use the label service:
SELECT ?wdAltLabel
{
VALUES (?wdt) {(wdt:P106)}
?wd wikibase:directClaim ?wdt .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Trying to execute some queries but when searching for foaf:name resultset is empty. Here's my code:
SELECT DISTINCT ?uri ?string
WHERE {
?uri rdf:type ?x.
?uri foaf:name 'Cavallo domestico'#it .
OPTIONAL { ?uri rdfs:label ?string . FILTER (lang(?string) = 'it') }
}
page exist http://it.dbpedia.org/resource/Equus_caballus/html
Apparently seems it's not related with languages different than english but with foaf:name request. If I execute following, retrieving generic foaf:givenName, it works:
SELECT DISTINCT ?uri ?string
WHERE {
?uri rdf:type ?x.
?uri foaf:givenName 'Jimmy'#en .
OPTIONAL { ?uri rdfs:label ?string . FILTER (lang(?string) = 'en') }
}
I think this wasn't working when I first mentioned that it didn't in a comment, but, as AKSW points out, this seems to be working now. The rdfs:label property has the article titles in various languages, not the foaf:name, so you can do this to get the types of Horse:
select ?x ?type {
?x a ?type ;
rdfs:label "Equus caballus"#it
}
SPARQL results
Have the following working SPARQL query that selects items from DBpedia that include the string "fish" in their name.
SELECT ?name, ?kingdom, ?phylum, ?class, ?order, ?family, ?genus, ?species, ?subspecies, ?img, ?abstract
WHERE {
?s dbpedia2:regnum ?hasValue;
rdfs:label ?name
FILTER regex( ?name, "fish", "i" )
FILTER ( langMatches( lang( ?name ), "EN" ))
?animal dbpedia2:name ?name;
foaf:depiction ?img;
dbpedia2:regnum ?kingdom
OPTIONAL { ?animal dbpedia2:ordo ?order . }
OPTIONAL { ?animal dbpedia2:phylum ?phylum . }
OPTIONAL { ?animal dbpedia2:classis ?class . }
OPTIONAL { ?animal dbpedia2:familia ?family . }
OPTIONAL { ?animal dbpedia2:genus ?genus . }
OPTIONAL { ?animal dbpedia2:species ?species . }
OPTIONAL { ?animal dbpedia2:subspecies ?subspecies . }
OPTIONAL {
FILTER ( langMatches( lang( ?abstract ), "EN" ))
}
}
GROUP BY ?name
LIMIT 500
Here is the result on SNORQL.
This approach finds animals with the word "fish" in their name (example: "starfish" which is not a fish but member of the phylum Echinoderm).
Would like a more precise query that selects DBpedia items by phylum, or by class, or by order, etc.
How to change the query to search only on dbpedia2:phylum (Chordata); on dbpedia2:classis (Actinopterygii); on dbpedia2:familia; etc. ?
Looking at Tuna, I see that there is a rdf:type assertion for the class
http://umbel.org/umbel/rc/Fish
that looks useful. E.g.,
select ?fish { ?fish a <http://umbel.org/umbel/rc/Fish> }
SPARQL results (10,000)
There's also the dbpedia-owl:Fish class, which gets more results:
select (count(*) as ?nFish) where {
?fish a dbpedia-owl:Fish .
}
SPARQL results (17,420)
While Wikipedia has lots of scientific classification information, I don't see much of it reflected in DBpedia. E.g,. while the Wikipedia article for Tuna has kingdom, phylum, class, order, etc., I don't see that data in the corresponding DBpedia resource.
Notes
Note that your query, as written, isn't actually legal SPARQL (even if Virtuoso, the SPARQL endpoint that DBpedia uses, accepts it). You can't have commas between the projection variables. Also, once you group by one variable, the non-group variables can't appear in the variable list. You could sample the other values though. E.g., you should end up with something like:
SELECT
?name
(sample(?kingdom) as ?kingdom_)
(sample(?phylum) as ?phylum_)
#-- ...
(sample(?img) as ?img_)
(sample(?abstract) as ?abstract_)
WHERE {
?s dbpedia2:regnum ?hasValue;
rdfs:label ?name
FILTER regex( ?name, "fish", "i" )
FILTER ( langMatches( lang( ?name ), "EN" ))
?animal dbpedia2:name ?name;
foaf:depiction ?img;
dbpedia2:regnum ?kingdom
OPTIONAL { ?animal dbpedia2:ordo ?order . }
OPTIONAL { ?animal dbpedia2:phylum ?phylum . }
OPTIONAL { ?animal dbpedia2:classis ?class . }
OPTIONAL { ?animal dbpedia2:familia ?family . }
OPTIONAL { ?animal dbpedia2:genus ?genus . }
OPTIONAL { ?animal dbpedia2:species ?species . }
OPTIONAL { ?animal dbpedia2:subspecies ?subspecies . }
OPTIONAL {
FILTER ( langMatches( lang( ?abstract ), "EN" ))
}
}
GROUP BY ?name
LIMIT 500