How can I get the labels in English or any other language in Wikidata by ID using SPARQL endpoint?
Suppose wd:Q146190 is your wikidata entity ID
get the label in a specific language of your specific entity ID:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wd: <http://www.wikidata.org/entity/>
SELECT *
WHERE {
wd:Q146190 rdfs:label ?label .
FILTER (langMatches( lang(?label), "EN" ) )
}
LIMIT 1
live example in english
live example in german
get all the labels in any language of your specific entity ID:
SELECT * WHERE {
wd:Q146190 rdfs:label ?label
}
here the link to the live try press on play to run the query then you could download a full JSON and get such a rensponse ..only a trunk copied here:
{
"head": {
"vars": [
"label"
]
},
"results": {
"bindings": [
{
"label": {
"xml:lang": "ar",
"type": "literal",
"value": "دوار الشمس الدرني"
}
},
{
"label": {
"xml:lang": "az",
"type": "literal",
"value": "Kökyumrulu günəbaxan"
}
},
..etc,etc.
get labels for each entity ID in a response resultset with multiple id
in this case you should use the Label service
example without labels
example with labels for each entity in the specified language: english
SELECT ?p ?pLabel ?w ?wLabel WHERE {
wd:Q30 p:P6/ps:P6 ?p .
?p wdt:P26 ?w .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
to use this service add Label to the variable ( ie: for ?p label you must use ?pLabel then add
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
to the WHERE block
Related
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT (SAMPLE(?sport) AS ?sport) ?sportLabel (SAMPLE(?uses) AS ?uses) (SAMPLE(?usesLabel) AS ?usesLabel)
WHERE
{
# instance of sport
?sport wdt:P31 wd:Q31629.
FILTER (!isBlank(?uses))
OPTIONAL { ?sport wdt:P2283 ?uses } .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
GROUP BY ?sportLabel
Not able to get the label of uses. ?usesLabel returns blank strings but ?uses returns correct ids
There are (at least) two options.
(Note: I replaced (SAMPLE(?sport) AS ?sport) with ?sport, as you presumably want to list all sport items.)
Using a subquery
You could use a subquery to aggregate everything, and the outer query to display the labels:
SELECT ?sport ?sportLabel ?usesSample ?usesSampleLabel
WHERE {
{
SELECT ?sport (SAMPLE(?uses) AS ?usesSample)
WHERE {
# instance of sport
?sport wdt:P31 wd:Q31629 .
FILTER (!isBlank(?uses)) .
OPTIONAL { ?sport wdt:P2283 ?uses . }
} GROUP BY ?sport
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
Using Wikidata’s label service
You could keep your current query, and add the labels you need to the SERVICE:
SELECT ?sport ?sportLabel (SAMPLE(?usesLabel) AS ?usesSampleLabel)
WHERE
{
# instance of sport
?sport wdt:P31 wd:Q31629 .
FILTER (!isBlank(?uses)) .
OPTIONAL { ?sport wdt:P2283 ?uses . }
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
?sport rdfs:label ?sportLabel .
?uses rdfs:label ?usesLabel .
}
}
GROUP BY ?sport ?sportLabel
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
I am using Jena ARQ with this query:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX sc: <http://iiif.io/api/presentation/2#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX as: <http://www.w3.org/ns/activitystreams#>
CONSTRUCT {?collectionIRI rdf:type sc:Collection .
?collectionIRI as:items ?manifest .
?manifest rdf:type sc:Manifest}
WHERE {GRAPH ?g {?manifest rdf:type sc:Manifest .
BIND (URI(STR(CONCAT("https://api.repository.org/orgs/example.domain.org/collections/" , STRUUID()))) AS ?collectionIRI) .
FILTER(regex(str(?manifest),"example.domain.org"))
}}
The intention is to create a single ?collectionIRI by BINDing STRUUID() to a domain name (e.g. "example.domain.org" - a query parameter).
With this query every ?manifest in the result set is bound to a different ?collectionIRI and the result looks like this:
{
"#graph": [
{
"#id": "https://api.repository.org/orgs/example.domain.org/collections/0342aaf2-b772-48ea-be90-298a555ef5ab",
"#type": "sc:Collection",
"items": "http://example.domain.org/unescoCzechReformation/AIPDIG-NKCR__X_C_23______3CKR674-cs/"
},
{
"#id": "https://api.repository.org/orgs/example.domain.org/collections/04782c88-81cf-4d7b-8ac6-f15d83f094a5",
"#type": "sc:Collection",
"items": "http://example.domain.org/unescoCzechReformation/AIPDIG-NKCR__VIII_G_26___38E5KUD-cs/"
}
]
}
instead of the desired result:
{
"#graph": [
{
"#id": "https://api.repository.org/orgs/example.domain.org/collections/0342aaf2-b772-48ea-be90-298a555ef5ab",
"#type": "sc:Collection",
"items": [
"http://example.domain.org/unescoCzechReformation/AIPDIG-NKCR__VIII_B_6____30XYOX6-cs/",
"http://example.domain.org/unescoCzechReformation/AIPDIG-NKCR__I_E_45______2KDU8A5-cs/"
]
}
]
}
Is it possible to restrict BIND scope with CONSTRUCT?
I try to query Wikidata for a city by its GeoNames ID.
I can see the id of the property is P1556: https://www.wikidata.org/wiki/Property:P1566
So my query for Berlin (as an example) is:
SELECT * WHERE {
?id wdt:P1566 wd:2950157
}
But I don't get a match.
What am I doing wrong?
A simple trial and error on Wikidata results in the following: No need for wd:2950157, it is a literal, so use only "2950157" instead. Use the following (SERVICE is used to show the name of the city):
Edit: The straightforward method:
SELECT ?id ?idLabel WHERE {
?id wdt:P1566 "2950157".
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
End of EDIT
Using BIND or FILTER
SELECT ?id ?idLabel WHERE {
Bind ("2950157" as ?x)
?id wdt:P1566 ?x.
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
Or (a slower option) use FILTER
SELECT ?id ?idLabel WHERE {
?id wdt:P1566 ?x.
filter (?x = "2950157")
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
Here is my JSON-LD :
"locationCreated": {
"#type": "Place",
"#id": "http://test.fr/city/pacific-grove",
"geo": {
"#type": "GeoCoordinates",
"#id": "http://test.fr/city/pacific-grove/geo",
"latitude": "36.6177374",
"longitude": "-121.9166215"
},
"name": "Pacific Grove",
"alternateName": "Pacific Grove, CA, USA"
}
I want to get the latitude & longitude value with SPARQL. Actually, when i make my query, I just have access to the URI "http://test.fr/city/pacific-grove" of locationCreated.
SPARQL Query:
prefix schema: <http://schema.org/>
select ?locationCreated
where {
?x schema:locationCreated ?locationCreated
}
LIMIT 100
Thank you for your answer.
This should work given your sample data:
prefix schema: <http://schema.org/>
select ?locationCreated ?lat ?long
where {
?x schema:locationCreated ?locationCreated .
?locationCreated schema:geo ?geoData .
?geoData schema:latitude ?lat .
?geoData schema:longitude ?long .
}
LIMIT 100