I am facing a problem while formating the SPARQL query using python code. There error shown is:
'SELECT Distinct ?article ?item ?itemLabel ?itemDescription (GROUP_CONCAT(DISTINCT(?entity_type); separator = ", ") AS ?entity_type_list) ?main_category ?wikipediaLabel (GROUP_CONCAT(DISTINCT(?altLabel); separator = ", ") AS ?altLabel_list) WHERE'
^
SyntaxError: f-string: single '}' is not allowed
I don't know what I am missing, can anyone help?
def search_wikidata_label(label,lang='ar'):
sparql_query = (
'SELECT Distinct ?article ?item ?itemLabel ?itemDescription (GROUP_CONCAT(DISTINCT(?entity_type); separator = ", ") AS ?entity_type_list) ?main_category ?wikipediaLabel (GROUP_CONCAT(DISTINCT(?altLabel); separator = ", ") AS ?altLabel_list) WHERE'
'{SERVICE wikibase:mwapi'
'{ bd:serviceParam wikibase:api "EntitySearch". bd:serviceParam wikibase:endpoint "www.wikidata.org".'
f'bd:serviceParam mwapi:search "{label}".'
f'bd:serviceParam mwapi:language "{lang}" .'
'?item wikibase:apiOutputItem mwapi:item .'
'?num wikibase:apiOrdinal true .}'
'?item wdt:P31 ?entity_type .'
'MINUS { ?item wdt:P31 wd:Q4167410}'
'OPTIONAL{ ?item wdt:P910 ?main_category}'
'OPTIONAL { ?item skos:altLabel ?altLabel .'
f'FILTER (lang(?altLabel) = "{lang}") }'
'OPTIONAL{ ?article schema:about ?item;schema:isPartOf <https://ar.wikipedia.org/>;schema:name ?wikipediaLabel}'
'OPTIONAL{ ?article schema:about ?item;schema:isPartOf <https://ar.wikipedia.org/>;schema:name ?wikipediaLabel}'
'SERVICE wikibase:label {'
f'bd:serviceParam wikibase:language "{lang}" .}'
'}'
'GROUP BY ?article ?item ?itemLabel ?itemDescription ?main_category ?wikipediaLabel'
)
#to query another endpoint, change the URL for the service and the query
sparql_service_url = "https://query.wikidata.org/sparql"
result_table = query_wikidata(sparql_query, sparql_service_url)
return result_table