SPARQL query that returns Wikidpedia labels from Wikidata itemLabel - sparql

I am new to SPARQL,
Is it possible to write a query that returns Wikipedia box information for a corresponding item label from the Wikipedia box for the Arabic Language that appears at the bottom of the Wikidata item page?
see the picture:
Instead of the Wikipedia URL in the following Query, I need to return the Wikipedia Label, in our case (الرامة (جنين))
Try Query on Wikidata Query Service
SELECT DISTINCT ?article ?item ?itemLabel ?itemDescription ?entity_type ?main_category (GROUP_CONCAT(DISTINCT(?altLabel); separator = ", ") AS ?altLabel_list) WHERE {
?item ?label "الرامة"#ar.
?item wdt:P31 ?entity_type .
MINUS { ?item wdt:P31 wd:Q4167410}
OPTIONAL{ ?item wdt:P910 ?main_category}
?article schema:about ?item;
schema:isPartOf <https://ar.wikipedia.org/>;
OPTIONAL { ?item skos:altLabel ?altLabel . FILTER (lang(?altLabel) = "ar") }
SERVICE wikibase:label { bd:serviceParam wikibase:language "ar" .}
}
GROUP BY ?article ?item ?itemLabel ?itemDescription ?entity_type ?main_category

This is the answer by the UninformedUser
> SELECT ?article ?wikipediaLabel WHERE
> { ?article schema:about wd:Q12187640 . ?article schema:isPartOf <https://ar.wikipedia.org/>; schema:name
> ?wikipediaLabel }

Related

Wikidata do not return me itemLabel sometimes

I am looking for people of french nationality born in 1900 (and still living). I do not well understand the behaviour of wikidata in response to my following request:
SELECT ?item ?itemLabel ?itemDescription
WHERE {
?item wdt:P31 wd:Q5.
?item wdt:P569 ?dateOfBirth.
?item wdt:P27 wd:Q142.
FILTER NOT EXISTS {?item wdt:P570|wdt:P509|wdt:P20 ?o}
FILTER("1900-00-00"^^xsd:dateTime <= ?dateOfBirth && ?dateOfBirth < "1901-00-00"^^xsd:dateTime)
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr". }
}
I do not understand why the folowing request do not return itemLabel for some rows; for example the itemlabel returned for https://www.wikidata.org/wiki/Q47508624 is its "id": Q47508624
By using the wikibase:language option, you're asking for Wikidata to provide you with the labels for each ?item in the ?itemLabel variable. You've requested that it provide you labels in either the language preferred by your browser ([AUTO_LANGUAGE]) or French (fr). I would guess that your browser's default language is French also. With a browser set with English as the default, I get "Hugues Esquerre" as the ?itemLabel value for wd:Q47508624 (this record has labels defined in English and Spanish).
You can add additional acceptable languages in the comma-separated list in the query to increase the liklihood of getting label values back:
SELECT ?item ?itemLabel ?itemDescription
WHERE {
?item wdt:P31 wd:Q5.
?item wdt:P569 ?dateOfBirth.
?item wdt:P27 wd:Q142.
FILTER NOT EXISTS {?item wdt:P570|wdt:P509|wdt:P20 ?o}
FILTER("1900-00-00"^^xsd:dateTime <= ?dateOfBirth && ?dateOfBirth < "1901-00-00"^^xsd:dateTime)
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr,en,es". }
}

Wikidata query for items with one Wikipedia page not in English

I want to find Wikidata items, with each referring to exactly one Wikipedia page which is not an en. Wikipedia page.
I came up with this query:
SELECT ?item WHERE {
?article schema:about ?item .
FILTER (SUBSTR(str(?article), 9, 2) != "en") .
{
SELECT ?item (COUNT(DISTINCT ?lang) AS ?count) WHERE {
?item wdt:P1367 ?yp_id . # BBC 'Your paintings' artist identifier
?article schema:about ?item .
FILTER (SUBSTR(str(?article), 11, 15) = ".wikipedia.org/") .
?article schema:inLanguage ?lang .
} GROUP BY ?item
HAVING (?count=1)
ORDER BY DESC (?count)
}
}
It executes. However, I always get a timeout.
Is there a better query to achieve what I am looking for?
Here's some tip:
Since you take only ?count=1, there is no reason to order by ?count.
Since for each article you can have only one ?lang, you can count by ?article without considering a redundant variable.
Instead of working on (sub)strings, just use the schema:isPartOf property for selecting the specific domain that you want to exclude.
Use FILTER NOT EXISTS instead of FILTER (... != ...)
The fourth optimiziation is the most important and it is sufficient per se.
SELECT ?item WHERE {
FILTER NOT EXISTS {
?article schema:about ?item ;
schema:isPartOf <https://en.wikipedia.org/> .
}
{
SELECT ?item (COUNT(DISTINCT ?article) AS ?count) WHERE {
?item wdt:P1367 ?yp_id . # BBC 'Your paintings' artist identifier
?article schema:about ?item .
FILTER (SUBSTR(str(?article), 11, 15) = ".wikipedia.org/") .
}
GROUP BY ?item
HAVING (?count=1)
}
}

How to query wikidata using SPARQL to find a place that matches certain criteria and is geographically near another city

I wrote the following SPARQL query to find the wikidata item with the label "San Leucio" in Italy.
SELECT DISTINCT * WHERE {
?location ?label 'San Leucio'#en .
?location wdt:P17 wd:Q38 .
?location rdfs:label ?locationName .
OPTIONAL {
?article schema:about ?location .
?article schema:isPartOf <https://en.wikivoyage.org/> .
}
?location wdt:P18 ?image .
FILTER(lang(?locationName) = "en")
}
The query returns these 3 results:
wd:Q55179410
wd:Q20009063
wd:Q846499
The result I want is wd:Q846499, which is outside of Naples, Italy. Is there any way I could further filter this query to return the result that is nearest to Naples? I know that I can get the geoCoordinates for each of these with ?location wdt:P625 ?coordinates, but I'm not sure how I could use that to compare to the geo-coordinates of Naples to get what I want.
SELECT DISTINCT * {
VALUES ?naples {wd:Q2634}
?Napfes wdt:P625 ?naples_coordinates.
?location rdfs:label 'San Leucio'#en .
?location wdt:P17 wd:Q38 .
?location wdt:P18 ?image .
?location wdt:P625 ?location_coordinates.
OPTIONAL {
?article schema:about ?location .
?article schema:isPartOf <https://en.wikivoyage.org/> .
}
BIND (geof:distance(?location_coordinates, ?naples_coordinates) AS ?distance)
} ORDER BY ?distance LIMIT 1

How To Convert To String Search In SPARQL

I want to use a string instead of the Wikidata ID#.
The code below works perfectly using wd:Q2263 (Tom Hanks) to show what films (Q11424) he was cast (P161) in.
SELECT DISTINCT ?item ?itemLabel ?itemDescription
WHERE {
?item wdt:P31 wd:Q11424; wdt:P161 wd:Q2263.
?article schema:about ?item;
schema:name ?title;
schema:inLanguage "en";
schema:isPartOf <https://en.wikipedia.org/>.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 10
The problem is that I want to change wd:Q2263 (Tom Hanks) to be a string like: item ?label "Tom Hanks"#en.
On my website, I am using a search box, so in the code I need the name of the person to be a string. I don't want to have to look it up in Wikipedia to get the Wikidata ID.

Wikidata SPARQL - get company entities and the location of their headquarters

I'm having trouble extracting location attributes of company HQ's.
My query: finds all companies or sub-classes, and returns some basic properties such as ISIN and URL, and the Headquarter location.
I have tried to use this example to extend the Headquarter part of the query to return location information such as city, country, and coordinate latitude and longitude. However I am getting stuck on pulling the values or labels through.
Thank you
SELECT
?item ?itemLabel ?web ?isin ?hq ?hqloc ?inception
# valueLabel is only useful for properties with item-datatype
WHERE
{
?item p:P31/ps:P31/wdt:P279* wd:Q783794.
OPTIONAL{?item wdt:P856 ?web.} # get item
OPTIONAL{?item wdt:P946 ?isin.} # get item
OPTIONAL{?item wdt:P571 ?inception.} # get item
OPTIONAL{?item wdt:P159 ?hq.}
OPTIONAL{?item p:P159 ?hqItem. # get property
?hqItem ps:P159 wd:Q515. # get property-statement wikidata-entity
?hqItem pq:P17 ?hqloc. # get country of city
}
?article schema:about ?item .
?article schema:inLanguage "en" .
?article schema:isPartOf <https://en.wikipedia.org/>.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 10
A more simplified query to select some of the values you mentioned:
SELECT
?company ?companyLabel ?isin ?web ?country ?countryLabel ?inception
WHERE
{
?article schema:inLanguage "en" .
?article schema:isPartOf <https://en.wikipedia.org/>.
?article schema:about ?company .
?company p:P31/ps:P31/wdt:P279* wd:Q783794.
?company wdt:P946 ?isin.
OPTIONAL {?company wdt:P856 ?web.}
OPTIONAL {?company wdt:P571 ?inception.}
OPTIONAL {?company wdt:P17 ?country.}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
} LIMIT 10
What I changed:
changed some labels to be more explicit (ex: "?item" -> "?company")
usage of P17 to directly select the country
I removed the OPTIONAL on ISIN to show that there exist some values. You did not get a result because it seems that many company instances on Wikidata lack that information.
From here, selecting the other values should be easy.