def brand(): '''return products meet the industry& brand''' if request.method=='GET': idsty = dget(mapping, '%s.data'%industry) idsty_sort = dget(mapping, '%s.sort'%industry) brand_name = request.args.get('brand', '') products_db = db.webpages.find({'transformed.data.brand_name': brand_name}) #for easy to get, write back will be a mess .. so we need a map products_key = list(idsty.keys()) products = [ [dget(p, idsty[k], '') for k, v in idsty.items()] for p in products_db ] #sort the data, to the convinent of displaying.. for fs in idsty_sort: try: products = sorted(products, key=lambda x: x[products_key.index(fs)]) except: print(fs) return render_template('row_template.html', products = products, titles = list(idsty.keys()))
def get_value(data): """Gets Python type value from WikiData response field""" data_type = dget(data, 'type.value') value = dget(data, 'valLabel.value') if data_type == WikiDataAnswer.TIME_VALUE: dt = parser.parse(value) dt = dt.replace(tzinfo=None) return dt elif data_type == WikiDataAnswer.QUANTITY_VALUE: if value.isdigit(): return float(value) return value else: return value
def _get_property(self, subject, prop, prop_id=None): """Queries Wikidata to get property""" self.debug('{0}, {1}', subject, prop) subject_id = self._get_id(subject, 'item') if not prop_id: prop_id = self._get_id(prop, 'property') if not prop_id or not subject_id: return None query = """ SELECT ?valLabel ?type WHERE { """ sub_queries = [] for pid in prop_id.split(','): sub_query = """{ wd:%s p:%s ?prop . ?prop ps:%s ?val . OPTIONAL { ?prop psv:%s ?propVal . ?propVal rdf:type ?type . } }""" % (subject_id, pid, pid, pid) sub_queries.append(sub_query) query += ' UNION '.join(sub_queries) query += """ SERVICE wikibase:label { bd:serviceParam wikibase:language "en"} } """ result = self._query_wdsparql(query) bindings = dget(result, 'results.bindings') return WikiDataAnswer(sparql_query=query, bindings=bindings)
def _get_aliases(self, subject): """Get all aliases of an entity""" self.debug('Get alias {0}'.format(subject)) subject_id = self._get_id(subject, 'item') query = """ SELECT ?valLabel WHERE { { wd:%s skos:altLabel ?val FILTER (LANG (?val) = "en") } UNION { wd:%s rdfs:label ?val FILTER (LANG (?val) = "en") } SERVICE wikibase:label { bd:serviceParam wikibase:language "en"} }""" % (subject_id, subject_id) result = self._query_wdsparql(query) bindings = dget(result, 'results.bindings') return WikiDataAnswer(sparql_query=query, bindings=bindings)
def _get_id(self, name, _type='item'): """Get WikiData ID of a name""" item = self._search_entity(name, _type) return dget(item, 'search.0.id')
def _get_desc(self, subject): """Get WikiData description of subject""" data = self._search_entity(subject) desc = dget(data, 'search.0.description') return Answer(data=desc)
def _find_entity(self, qtype, inst, params): """Count number of things instance/subclass of inst with props""" self.info('Get instances of {0} that are {1}'.format(inst, params)) inst_id = self._get_id(inst) if not inst_id: self.info('Cannot find id of: {0}'.format(inst)) return None if qtype == 'how many': select = '(count(*) as ?count)' elif qtype in ['which', 'who']: select = '?valLabel' else: self.warn('Qtype {0} not known'.format(qtype)) return None query = """ SELECT %s WHERE { { ?val p:P39 ?pos . # position held ?pos ps:P39 wd:%s . # pos = inst ?val wdt:P31 wd:Q5 . # as a human } UNION { ?val wdt:P31 wd:%s . # instance of } """ % (select, inst_id, inst_id) for prop, prop_val, op in params: if op in ['>', '<']: prop_id = self._get_id(prop, 'property') self.info('Count number of {0} where {1} {2} {3}'.format( inst, prop_id, op, prop_val)) query += """ ?val wdt:%s ?value FILTER(?value %s %s) . # Filter by value """ % (prop_id, op, prop_val) elif op in ['in', 'by', 'of', 'from']: if op == 'in' and prop_val.isdigit(): iso_time = parser.parse(prop_val).isoformat() query += """ ?pos pq:P580 ?startDate . # pos.startDate ?pos pq:P582 ?endDate . # pos.endDate FILTER (?startDate < "%s"^^xsd:dateTime && ?endDate > "%s"^^xsd:dateTime) """ % (iso_time, iso_time) elif op == 'of' and prop_val: prop_val_id = self._get_id(prop_val) query += """ ?pos pq:P108 wd:%s . # pos.employer """ % (prop_val_id) else: # Get value entity prop_val_id = self._get_id(prop_val) if prop: # Get property id if prop in ['died', 'killed' ] and op in ['from', 'by', 'of']: # slight hack because lookup for died defaults to place of death # cause of death prop_id = 'P509' else: prop_id = self._get_id(prop, 'property') query += '?val wdt:%s wd:%s .\n' % (prop_id, prop_val_id) else: # Infer property from value (e.g. How many countries are in China?) # e.g. infer: How many countries with continent China? prop_id = '*' query += """ wd:%s wdt:P31 ?instance . # Get entities that value is an instance of. Ex: ?instance = wd:Q5107 (continent) ?instance wdt:P1687 ?propEntity . # instance of Entity to property. Ex: ?propEntity = wd:P30 (continent) ?propEntity wikibase:directClaim ?prop . # wd to wdt. Ex: ?prop = wdt:P30 (continent) ?val ?prop wd:%s . """ % (prop_val_id, prop_val_id) self.info('Count number of {0} where {1}={2}'.format( inst, prop_id, prop_val_id)) query += 'SERVICE wikibase:label { bd:serviceParam wikibase:language "en"} }' result = { 'sparql_query': query, } try: data = self._query_wdsparql(query) except ValueError: self.error('Error parsing data') return WikiDataAnswer(**result) if qtype == 'how many': result['data'] = dget(data, 'results.bindings.0.count.value') elif qtype in ['which', 'who']: result['bindings'] = dget(data, 'results.bindings') return WikiDataAnswer(**result)
def _get_label(self, subject): """Get WikiData label of subject""" data = self._search_entity(subject) label = dget(data, 'search.0.label') return Answer(data=label)