def get_class_label(wikidata_class): query = """select ?item ?itemLabel { bind(wd:%s as ?item) SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } } """ % wikidata_class from resolver.resolver import ENDPOINT_URL query_results = get_results(ENDPOINT_URL, query) bindings = query_results["results"]["bindings"] return bindings[0]["itemLabel"]["value"]
def resolve_get_filter_suggestions_result(entity_id, filled_properties): suggestions = [] filters = "" if filled_properties != "" and filled_properties is not None: filled_property = filled_properties.split(",") for elem in filled_property: filters += " FILTER(?p != wdt:%s) " % elem query = """ SELECT ?pFull ?pFullLabel ?cnt { ?pFull wikibase:directClaim ?p . MINUS {?pFull <http://wikiba.se/ontology#propertyType> <http://wikiba.se/ontology#ExternalId>} { SELECT ?p (COUNT(?s) AS ?cnt) { SELECT DISTINCT ?s ?p WHERE { {SELECT DISTINCT ?s { { SELECT ?s WHERE { ?s wdt:P31 wd:%s. } LIMIT 1000 } }} OPTIONAL { ?s ?p ?o . FILTER(STRSTARTS(STR(?p),"http://www.wikidata.org/prop/direct/")) # only select direct statements } FILTER(?p != wdt:P31) FILTER(?p != wdt:P373) %s } } GROUP BY ?p } SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } # get labels } ORDER BY DESC(?cnt) limit 10 """ % (entity_id, filters) from resolver.resolver import ENDPOINT_URL query_results = get_results(ENDPOINT_URL, query) results = query_results["results"]["bindings"] for elem in results: prop_link = elem["pFull"]["value"] prop_id = prop_link.split("/")[-1] prop_label = elem["pFullLabel"]["value"] prop_count = elem["cnt"]["value"] prop_obj = { "propertyLink": prop_link, "propertyID": prop_id, "propertyLabel": prop_label, "propertyCount": prop_count } suggestions.append(prop_obj) result = {"amount": len(suggestions), "suggestions": suggestions} return result
def get_wiikidata_classes(limit): from resolver.resolver import ENDPOINT_URL query = """SELECT DISTINCT ?o WHERE {{ ?s wdt:P31 ?o . }} limit {limit} """.format(limit=limit) query_results = get_results(ENDPOINT_URL, query) bindings = query_results["results"]["bindings"] classes = [] for elem in bindings: class_link = elem["o"]["value"] class_id = class_link.split("/")[-1] classes.append(class_id) return classes
def resolve_get_entities_same_as_profile(single_dashboard): from resolver.resolver import ENDPOINT_URL, LIMITS entity = single_dashboard.entity filters = eval(single_dashboard.filters) filter_query = "" for elem in filters: for elem_filter in elem.keys(): filter_query += "?item wdt:%s wd:%s . " % (elem_filter, elem[elem_filter]) query = """ SELECT (count( ?item ) as ?cnt) WHERE { ?item wdt:P31 wd:%s . %s } LIMIT %d """ % (entity, filter_query, 10000) query_results = get_results(ENDPOINT_URL, query) query_results = query_results["results"]["bindings"][0] count = query_results["cnt"]["value"] result = {"entityCount": count} return result
def resolve_get_comparison_properties_result(single_dashboard, item_number): entity_id = single_dashboard.entity entity_filters = eval(single_dashboard.filters) properties = eval(single_dashboard.properties) compare_filters = eval(single_dashboard.compare_filters) properties_result = [] filter_query_top = "" filter_query_bottom = "" for elem in entity_filters: for elem_filter in elem.keys(): filter_query_top += "?s wdt:%s wd:%s . " % (elem_filter, elem[elem_filter]) filter_query_bottom += "FILTER(?p != wdt:%s) " % elem_filter for elem in compare_filters: elem_filter = elem["propertyID"] elem_value = elem["value"][item_number] filter_query_top += "?item wdt:%s wd:%s . " % (elem_filter, elem_value) filter_query_bottom += "FILTER(?p != wdt:%s) " % elem_filter filter_property = "" for elem in properties: filter_property += "FILTER(?p = wdt:%s)" % elem query = """ SELECT ?pFull ?pFullLabel ?pDescription ?cnt { ?pFull wikibase:directClaim ?p . MINUS {?pFull <http://wikiba.se/ontology#propertyType> <http://wikiba.se/ontology#ExternalId>} { SELECT ?p (COUNT(?s) AS ?cnt) { SELECT DISTINCT ?s ?p WHERE { {SELECT DISTINCT ?s { { SELECT ?s WHERE { ?s wdt:P31 wd:%s. %s } LIMIT 10000 } }} OPTIONAL { ?s ?p ?o . FILTER(STRSTARTS(STR(?p),"http://www.wikidata.org/prop/direct/")) # only select direct statements } FILTER(?p != wdt:P31) FILTER(?p != wdt:P373) %s %s } } GROUP BY ?p } ?pFull schema:description ?pDescription. FILTER(LANG(?pDescription)="en") SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } # get labels } ORDER BY DESC(?cnt) """ % (entity_id, filter_query_top, filter_query_bottom, filter_property) from resolver.resolver import ENDPOINT_URL query_results = get_results(ENDPOINT_URL, query) properties_bindings = query_results["results"]["bindings"] for prop in properties_bindings: property_link = prop["pFull"]["value"] property_id = property_link.split("/")[-1] property_label = prop["pFullLabel"]["value"] property_description = prop["pDescription"]["value"] property_entities_count = prop["cnt"]["value"] property_obj = { "propertyID": property_id, "propertyLabel": property_label, "propertyDescription": property_description, "propertyLink": property_link, "entitiesCount": property_entities_count } properties_result.append(property_obj) result = {"properties": properties_result} return result