Ricordisamoa has uploaded a new change for review. https://gerrit.wikimedia.org/r/315671
Change subject: Add and use SparqlElementProvider ...................................................................... Add and use SparqlElementProvider Bug: T122706 Change-Id: I19fd9f9c18cf17856413eb1753875c91ee591100 --- M app.py M chemistry.py 2 files changed, 46 insertions(+), 3 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/labs/tools/ptable refs/changes/71/315671/1 diff --git a/app.py b/app.py index dddd4cc..9cf9f0b 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- """ -Copyright © 2012-2015 Ricordisamoa +Copyright © 2012-2016 Ricordisamoa This file is part of the Wikidata periodic table. @@ -36,7 +36,7 @@ app.json_encoder = CustomJSONEncoder # May be set to chemistry.ApiElementProvider (slower, but more up-to-date) -element_provider_class = chemistry.WdqElementProvider +element_provider_class = chemistry.SparqlElementProvider nuclide_provider_class = nuclides.SparqlNuclideProvider fake_globals = {'isinstance': isinstance, 'sorted': sorted} diff --git a/chemistry.py b/chemistry.py index af58b82..cde8a9d 100644 --- a/chemistry.py +++ b/chemistry.py @@ -22,7 +22,7 @@ from collections import defaultdict import data -from base import BaseProvider, PropertyAlreadySetException, TableCell, WdqBase, get_json +from base import BaseProvider, PropertyAlreadySetException, SparqlBase, TableCell, WdqBase, get_json class ElementProvider(BaseProvider): @@ -133,6 +133,49 @@ } +class SparqlElementProvider(SparqlBase, ElementProvider): + """Load elements from Wikidata Sparql endpoint.""" + def __iter__(self): + query = 'SELECT ?item ?symbol ?number (group_concat(?subclass_of) as ?subclass_of) \ +WHERE {{ \ + ?item wdt:P{symbol_pid} ?symbol . \ + OPTIONAL {{ \ + ?item wdt:P{subclass_pid} ?subclass_of \ + }} \ + OPTIONAL {{ \ + ?item wdt:P{number_pid} ?number \ + }} \ +}} \ +GROUP BY ?item ?symbol ?number'.format(symbol_pid=Element.symbol_pid, subclass_pid=Element.subclass_pid, number_pid=Element.number_pid) + items = self.get_sparql(query) + ids = [item['item']['value'].replace('http://www.wikidata.org/entity/', '') for item in items] + entities = self.get_entities(ids, props='labels', + languages=self.language, languagefallback=1) + for item in items: + element = Element() + element.item_id = item['item']['value'].replace('http://www.wikidata.org/entity/', '') + if 'number' in item and item['number']['value'].isdigit(): + element.number = int(item['number']['value']) + else: + element.number = None + if 'symbol' in item: + element.symbol = item['symbol']['value'] + else: + element.symbol = None + if 'subclass_of' in item: + subclass_of = [int(subclass_id.replace('http://www.wikidata.org/entity/Q', '')) + for subclass_id in item['subclass_of']['value'].split()] + else: + subclass_of = [] + element.load_data_from_superclasses(subclass_of) + label = None + entity = entities.get(element.item_id) + if entity and 'labels' in entity and len(entity['labels']) == 1: + label = list(entity['labels'].values())[0]['value'] + element.label = label + yield element + + class ApiElementProvider(ElementProvider): """Load elements from the Wikidata API.""" def __iter__(self): -- To view, visit https://gerrit.wikimedia.org/r/315671 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I19fd9f9c18cf17856413eb1753875c91ee591100 Gerrit-PatchSet: 1 Gerrit-Project: labs/tools/ptable Gerrit-Branch: master Gerrit-Owner: Ricordisamoa <ricordisa...@openmailbox.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits