|
@@ -29,31 +29,46 @@ set_loggers(wikidata, 'wikidata')
|
|
# * https://www.wikidata.org/wiki/Help:Ranking
|
|
# * https://www.wikidata.org/wiki/Help:Ranking
|
|
# * https://www.mediawiki.org/wiki/Wikibase/Indexing/RDF_Dump_Format ("Statement representation" section)
|
|
# * https://www.mediawiki.org/wiki/Wikibase/Indexing/RDF_Dump_Format ("Statement representation" section)
|
|
# * https://w.wiki/32BT
|
|
# * https://w.wiki/32BT
|
|
|
|
+# * https://en.wikibooks.org/wiki/SPARQL/WIKIDATA_Precision,_Units_and_Coordinates#Quantities
|
|
# see the result for https://www.wikidata.org/wiki/Q11582
|
|
# see the result for https://www.wikidata.org/wiki/Q11582
|
|
# there are multiple symbols the same rank
|
|
# there are multiple symbols the same rank
|
|
SARQL_REQUEST = """
|
|
SARQL_REQUEST = """
|
|
-SELECT DISTINCT ?item ?symbol
|
|
|
|
|
|
+SELECT DISTINCT ?item ?symbol ?tosi ?tosiUnit
|
|
WHERE
|
|
WHERE
|
|
{
|
|
{
|
|
?item wdt:P31/wdt:P279 wd:Q47574 .
|
|
?item wdt:P31/wdt:P279 wd:Q47574 .
|
|
?item p:P5061 ?symbolP .
|
|
?item p:P5061 ?symbolP .
|
|
?symbolP ps:P5061 ?symbol ;
|
|
?symbolP ps:P5061 ?symbol ;
|
|
wikibase:rank ?rank .
|
|
wikibase:rank ?rank .
|
|
|
|
+ OPTIONAL {
|
|
|
|
+ ?item p:P2370 ?tosistmt .
|
|
|
|
+ ?tosistmt psv:P2370 ?tosinode .
|
|
|
|
+ ?tosinode wikibase:quantityAmount ?tosi .
|
|
|
|
+ ?tosinode wikibase:quantityUnit ?tosiUnit .
|
|
|
|
+ }
|
|
FILTER(LANG(?symbol) = "en").
|
|
FILTER(LANG(?symbol) = "en").
|
|
}
|
|
}
|
|
ORDER BY ?item DESC(?rank) ?symbol
|
|
ORDER BY ?item DESC(?rank) ?symbol
|
|
"""
|
|
"""
|
|
|
|
|
|
|
|
+_wikidata_url = "https://www.wikidata.org/entity/"
|
|
|
|
+
|
|
|
|
|
|
def get_data():
|
|
def get_data():
|
|
results = collections.OrderedDict()
|
|
results = collections.OrderedDict()
|
|
response = wikidata.send_wikidata_query(SARQL_REQUEST)
|
|
response = wikidata.send_wikidata_query(SARQL_REQUEST)
|
|
for unit in response['results']['bindings']:
|
|
for unit in response['results']['bindings']:
|
|
- name = unit['item']['value'].replace('http://www.wikidata.org/entity/', '')
|
|
|
|
- unit = unit['symbol']['value']
|
|
|
|
|
|
+ name = unit['item']['value'].replace(_wikidata_url, '')
|
|
|
|
+ symbol = unit['symbol']['value']
|
|
|
|
+ si_name = unit.get('tosiUnit', {}).get('value', '').replace(_wikidata_url, '')
|
|
|
|
+ to_si_factor = unit.get('tosi', {}).get('value', '')
|
|
if name not in results:
|
|
if name not in results:
|
|
# ignore duplicate: always use the first one
|
|
# ignore duplicate: always use the first one
|
|
- results[name] = unit
|
|
|
|
|
|
+ results[name] = {
|
|
|
|
+ 'symbol': symbol,
|
|
|
|
+ 'si_name': si_name if si_name else None,
|
|
|
|
+ 'to_si_factor': float(to_si_factor) if to_si_factor else None,
|
|
|
|
+ }
|
|
return results
|
|
return results
|
|
|
|
|
|
|
|
|