def reduce(alist: Alist, children: List[Alist], G: InferenceGraph): # do comparisons vars_to_compare = alist.get(tt.OPVAR).split(' ') # propagate projection vars to parent5 propagate.projections(alist, tuple(children)) response_var = "?_lte_" if len(vars_to_compare) == 0: alist.set(response_var, "false") return alist result = True if len(vars_to_compare) > 1 and utils.is_numeric( alist.instantiation_value(vars_to_compare[0])): for x in vars_to_compare[1:]: if utils.is_numeric( alist.instantiation_value(x)) and utils.is_numeric( alist.instantiation_value(x)): result = (utils.get_number( alist.instantiation_value( vars_to_compare[0]), 0) <= utils.get_number( alist.instantiation_value(x), 0)) and result else: result = False break else: result = False alist.set(response_var, str(result).lower()) # alist.instantiate_variable(tt.COV, estimate_uncertainty( # children, True, alist.get(tt.OP), len(children) # )) return alist
def _search_cache(tx, alist_to_instantiate: Alist, attribute_to_instantiate, search_attributes) -> (bool, list): alists = [] conditions = "" resultStatus = False if not attribute_to_instantiate: return (resultStatus, [alist_to_instantiate]) conditions = " AND ".join([ f"n.{x}='{alist_to_instantiate.instantiation_value(x)}'" for x in search_attributes ]) results = tx.run("MATCH (n:Alist)" f"WHERE {conditions}" "RETURN n") for node in results: if node[0]: try: alist = Alist(**node[0]._properties) if alist.is_instantiated(attribute_to_instantiate): alist.set( attribute_to_instantiate, alist.instantiation_value(attribute_to_instantiate)) alist.attributes.pop('sessionId', None) alist.attributes.pop('parentId', None) alist.attributes.pop('src', None) alists.append(alist) resultStatus = True except: print("Error during alist creation from search result") return (resultStatus, alists)
def test_getInstantiationValue(self): alist = Alist( **{ tt.ID: '1', tt.SUBJECT: 'Africa', tt.PROPERTY: 'P1082', tt.OBJECT: '?x', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 1 }) alist.set('#d', '') alist.set('?x', '#d') alist.set('$y', '') alist.instantiate_variable('#d', 99) results = (alist.instantiation_value('#d'), alist.instantiation_value('?x'), alist.instantiation_value(tt.OBJECT), alist.instantiation_value(tt.TIME), alist.instantiation_value('$y')) self.assertEqual(results, (99, 99, 99, '2010', ''))
def reduce(alist: Alist, children: List[Alist], G: InferenceGraph): # do comparisons print(children) vars_to_compare = alist.get(tt.OPVAR).split(' ') # # copy projection vars of min alist to parent # for c in children: # if c.get(tt.OP) != 'comp': # for vc in vars_to_compare: # if vc in c.attributes: # projVars = c.projection_variables() # alist.instantiate_variable(vc, c.instantiation_value(vc), insert_missing=True) # for x in vars_to_compare: # if alist.is_instantiated(x) == False: # # return None # pass # propagate projection vars to parent propagate.projections(alist, tuple(children)) response_var = "?_eq_" if len(vars_to_compare) == 0: alist.set(response_var, "false") return alist result = True for x in vars_to_compare: result = (alist.instantiation_value(x) == alist.instantiation_value(vars_to_compare[0])) and result alist.instantiate_variable(response_var, str( result).lower(), insert_missing=True) # alist.instantiate_variable(tt.COV, estimate_uncertainty( # children, True, alist.get(tt.OP), len(children) # )) return alist
def find_property_values(self, alist: Alist, search_element: str): if search_element == tt.OBJECT: subject = alist.instantiation_value(tt.SUBJECT) nodes = self._get_nodes(subject) results = [] for node in nodes: try: data_alist = alist.copy() data_alist.set(tt.OBJECT, node[alist.get(tt.PROPERTY)]) data_alist.data_sources = list( set(data_alist.data_sources + [self.name])) results.append(data_alist) except: pass return results
def find_property_subject(alist: Alist): entity_id = find_entity(alist.instantiation_value(tt.OBJECT), alist.get(tt.PROPERTY)) if not entity_id: return [] # compose wikidata query query = "" if alist.get(tt.TIME): query = """ SELECT DISTINCT ?sLabel (YEAR(?date) as ?year) WHERE{{ ?s wdt:{property_id} wd:{entity_id}. OPTIONAL {{wd:{entity_id} pq:P585 ?date .}} SERVICE wikibase:label {{ bd:serviceParam wikibase:language "en".}} } } """.format(entity_id=entity_id, property_id=alist.get(tt.PROPERTY)) else: query = """ SELECT DISTINCT ?s ?sLabel WHERE {{ OPTIONAL {{ ?s wdt:{property_id} wd:{entity_id} . }} OPTIONAL {{ wd:{entity_id} wdt:{property_id} ?s . }} # hack to find inverse triple SERVICE wikibase:label {{ bd:serviceParam wikibase:language "en".}} }} """.format(entity_id=entity_id, property_id=alist.get(tt.PROPERTY)) params = {'format': 'json', 'query': query} response = requests.get(url='https://query.wikidata.org/sparql', params=params) alist_arr = [] try: data = response.json() for d in data['results']['bindings']: data_alist = alist.copy() data_alist.set(tt.SUBJECT, d['sLabel']['value']) if 'year' in d: data_alist.set(tt.TIME, d['year']['value']) data_alist.data_sources = list( set(data_alist.data_sources + ['wikidata'])) alist_arr.append(data_alist) except Exception as e: print("wikidata query response error: " + str(e)) return alist_arr
def find_property_object(alist: Alist): results = [] subj_instantiation = alist.instantiation_value(tt.SUBJECT) if isinstance(subj_instantiation, str): country_id = getCountryPropertyDb(subj_instantiation.replace("_", " "), "id") else: return results if not country_id: return results try: params = { 'date': str(alist.get(tt.TIME)).replace(".0", ""), 'format': 'json', 'per_page': 1000 } response = requests.get( url= f'http://api.worldbank.org/v2/countries/{country_id}/indicators/{alist.get(tt.PROPERTY)}', params=params) try: data = response.json() if len(data) > 1 and data[1]: for d in data[1]: if d['value']: data_alist = alist.copy() data_alist.set(tt.OBJECT, d['value']) data_alist.data_sources = list( set(data_alist.data_sources + ['worldbank'])) results.append(data_alist) except Exception as ex: print("worldbank query response error: " + str(ex)) except Exception as ex: print("worldbank query error: " + str(ex)) return results
def find_property_object(alist: Alist): entity_id = None wikidata_base_uri = 'http://www.wikidata.org/entity/' if 'http://www.wikidata.org/entity/' in alist.instantiation_value( tt.SUBJECT): entity_id = alist.instantiation_value( tt.SUBJECT)[len(wikidata_base_uri):] else: entity_id = find_entity(alist.instantiation_value(tt.SUBJECT), alist.get(tt.PROPERTY)) if not entity_id: return [] # compose wikidata query query = """ SELECT DISTINCT ?oLabel (YEAR(?date) as ?year) WHERE {{ wd:{entity_id} p:{property_id} ?ob . ?ob ps:{property_id} ?o . OPTIONAL {{ ?ob pq:P585 ?date . }} OPTIONAL {{ ?ob pq:P580 ?date . }} OPTIONAL {{ FILTER (YEAR(?date) = {time}) . }} SERVICE wikibase:label {{ bd:serviceParam wikibase:language "en" . }} }} ORDER By DESC(?year) """.format(entity_id=entity_id, property_id=alist.get(tt.PROPERTY), time=alist.get(tt.TIME).replace(".0", "") if alist.get(tt.TIME) else '0') params = {'format': 'json', 'query': query} response = requests.get(url='https://query.wikidata.org/sparql', params=params) alist_arr = [] try: data = response.json() ctx = {} if data['results']['bindings']: ctx = alist.get(tt.CONTEXT) ctx = {**ctx[0], **ctx[1], **ctx[2]} if ctx else {} result_with_year = False for d in data['results']['bindings']: if 'year' in d: result_with_year = True break for d in data['results']['bindings']: # if alist has explicit time and no context, # or has explicit time not from context # then result must include time if (alist.get(tt.TIME) and tt.TIME not in ctx): if ('year' in d) and (d['year']['value'] == alist.get( tt.TIME)): data_alist = alist.copy() data_alist.set(tt.OBJECT, d['oLabel']['value']) data_alist.set(tt.TIME, d['year']['value']) data_alist.data_sources = list( set(data_alist.data_sources + ['wikidata'])) alist_arr.append(data_alist) # else if time is injected from context # then only append results that have no time only if the dataset is empty.. # wikidata returns bindings with the time attribute first so this works elif alist.get(tt.TIME) and tt.TIME in ctx: current_year = str(datetime.now().year) if (('year' in d) and (d['year']['value'] == alist.get(tt.TIME))) or \ ((((('year' in d) and (d['year']['value'] != alist.get(tt.TIME))) and len(alist_arr) == 0) or (('year' not in d) and len(alist_arr) == 0)) and ( (alist.get(tt.TIME) == current_year and (not frank.util.utils.is_numeric(d['oLabel']['value']))) or (alist.get(tt.TIME) == ctx[tt.TIME] and result_with_year == False) )): # last condition: append this value only if time (i.e the context time) is the current year and the data value is not numeric. data_alist = alist.copy() data_alist.set(tt.OBJECT, d['oLabel']['value']) # if 'year' in d: # use time in dataset optionally # data_alist.set(tt.TIME, d['year']['value']) data_alist.data_sources = list( set(data_alist.data_sources + ['wikidata'])) alist_arr.append(data_alist) else: data_alist = alist.copy() data_alist.set(tt.OBJECT, d['oLabel']['value']) # if 'year' in d: # use time in dataset optionally # data_alist.set(tt.TIME, d['year']['value']) data_alist.data_sources = list( set(data_alist.data_sources + ['wikidata'])) alist_arr.append(data_alist) except Exception as ex: print("wikidata query response error: " + str(ex)) return alist_arr
def what(self, G: InferenceGraph, alist: Alist, is_reduced: bool, in_place=True): ''' Explain a reduction of this alist. ''' what = '' how = '' time = "" if alist.get(tt.TIME): time = f" in {alist.get(tt.TIME)}" if not is_reduced: if alist.get(tt.OP) in ['eq', 'gt', 'gte', 'lt', 'lte']: what = f"Failed to compare the values since the values of all items being compare are not known. " elif alist.get(tt.OP) in ['comp']: what = f"Failed to solve the sub-problem. " elif alist.get(tt.OP) in ['value', 'values']: what = f"Failed to determine the {self.ops_text[alist.get(tt.OP)]} of {alist.get(tt.PROPERTY)}{time}." else: what = f"Failed to calculate the {self.ops_text[alist.get(tt.OP)]} of {alist.get(tt.PROPERTY)}{time}." else: if alist.get(tt.OP) in ['eq', 'gt', 'gte', 'lt', 'lte']: vars_compared = alist.get(tt.OPVAR).split(' ') if len(vars_compared) > 1: what = f"Inferred value is '{alist.instantiation_value('?'+ alist.get(tt.OP))}'." how = f"Did a comparison to determine if {alist.instantiation_value(vars_compared[0])} is " + \ f"{self.ops_text[alist.get(tt.OP)]} {alist.instantiation_value(vars_compared[1])}." elif alist.get(tt.OP) in ['comp']: listed_str = '' listed = alist.instantiation_value(alist.get(tt.OPVAR)) if listed: listed = listed.split(',') if len(listed) > 8: listed_str += f"{', '.join(listed[0:8])}, etc" else: listed_str += ', '.join(listed) if listed_str: what = f"Solved the sub-query and found the following values: {listed_str}." else: inferred_value = '' projected = alist.projection_variables() if projected: inferred_value = list(projected.values())[0] if not inferred_value: inferred_value = alist.instantiation_value( alist.get(tt.OPVAR)) if inferred_value: if ':' in alist.get(tt.PROPERTY): listed_str = '' listed = alist.instantiation_value(alist.get( tt.OPVAR)).split(',') if len(listed) > 8: listed_str += f"{', '.join(listed[0:8])}, etc" else: listed_str += ', '.join(listed) what = f"The {alist.get(tt.PROPERTY).split(':')[1]} values found for the sub-query include: {listed_str}." elif (projected or inferred_value) and not alist.get(tt.PROPERTY): # for alists with just a projected value but no property what = f"An input value for operation is {inferred_value}." elif projected and alist.get( tt.OPVAR) not in projected and alist.get( tt.OP) in ['max', 'min']: what = f"The entity whose {alist.get(tt.PROPERTY)}{time} has the {self.ops_text[alist.get(tt.OP)]} of {alist.instantiation_value(alist.get(tt.OPVAR))} is {inferred_value}." elif projected and alist.get( tt.OPVAR) not in projected and alist.get( tt.OP) not in ['max', 'min']: what = f"The {self.ops_text[alist.get(tt.OP)]} of the {alist.get(tt.PROPERTY)}{time} of {inferred_value} is {alist.instantiation_value(alist.get(tt.OPVAR))}." else: what = f"The {self.ops_text[alist.get(tt.OP)]} of the {alist.get(tt.PROPERTY)} of {alist.instantiation_value(tt.SUBJECT)}{time} is {inferred_value}." if alist.get(tt.OP) in [ 'regress', 'nnpredict', 'linregress', 'gpregress', 'nnregress' ]: decomp_items = [] children = G.child_alists(alist.id) # for c in alist.children[0].children: for c in G.child_alists(children[0].id): decomp_items.append(c.get(tt.TIME)) if len(decomp_items) > 0: how = f"Generated a regression function from times between {min(decomp_items)} and {max(decomp_items)}." if in_place: alist.set("what", what) alist.set("how", how) G.add_alist(alist)