def _get_specific(match_parse, basic_ontology, type_, constant): assert isinstance(match_parse, MatchParse) assert isinstance(constant, Constant) packs = [] if type_.name == 'line': label_a = constant.content[0] label_b = constant.content[-1] keys_a = match_parse.match_graph[label_a].keys() keys_b = match_parse.match_graph[label_b].keys() for key_a, key_b in itertools.product(keys_a, keys_b): point_a = match_parse.formulas[key_a] point_b = match_parse.formulas[key_b] if not isinstance(point_a, instantiators['point']) or not isinstance( point_b, instantiators['point']): continue a_key = _get_point_key(match_parse, point_a) b_key = _get_point_key(match_parse, point_b) lines = get_instances(match_parse.general_graph_parse, 'line', a_key, b_key).values() for line in lines: constant = Constant(line, basic_ontology.types['line']) formula = Formula(basic_ontology, constant, []) variables = {} cost = 0 packs.append(FormulaPack(formula, variables, cost)) elif type_.name == 'circle': if len(constant.content) == 1: label = constant.content[0] keys = match_parse.match_graph[label].keys() for key in keys: point = match_parse.formulas[key] if not isinstance(point, instantiators['point']): continue key = _get_point_key(match_parse, point) circles = get_instances(match_parse.general_graph_parse, 'circle', key).values() for circle in circles: constant = Constant(circle, basic_ontology.types['circle']) formula = Formula(basic_ontology, constant, []) variables = {} cost = 0 packs.append(FormulaPack(formula, variables, cost)) # TODO : Add other things as well return packs
def identify_constants(basic_ontology, ontology_semantics, word): """ Returns a dictionary of constant-score pairs that the word might refer to, in the domain of number, variable and reference. For now, ontology semantics is not used, but later, this needs to be used to instantiate new functions, as the names have to make sense in the semantics. :param BasicOntology basic_ontology: :param OntologySemantics ontology_semantics: :param word: :return: """ assert isinstance(basic_ontology, BasicOntology) number_score = _get_number_score(word) reference_score = _get_reference_score(word) variable_score = _get_variable_score(word) pairs = {} if number_score > 0 and reference_score > 0: raise Exception("?") elif number_score > 0: constant = Constant(word, basic_ontology.types['number']) pair = ConstantScorePair(constant, number_score) pairs[word] = pair elif variable_score > 0: constant = Constant(word, basic_ontology.types['number']) pair = ConstantScorePair(constant, variable_score) pairs[word] = pair elif reference_score > 0: constant = Constant(word, basic_ontology.types['reference']) pair = ConstantScorePair(constant, reference_score) pairs[word] = pair return pairs
def _get_all(match_parse, basic_ontology, type_): assert isinstance(match_parse, MatchParse) general_graph_parse = match_parse.general_graph_parse instances = get_all_instances(general_graph_parse, type_.name) assert isinstance(instances, dict) packs = [] for key, d in instances.iteritems(): instance = d['instance'] constant = Constant(instance, type_) formula = Formula(basic_ontology, constant, []) variables = {} cost = 0 packs.append(FormulaPack(formula, variables, cost)) return packs
def _ground_formula(match_parse, text_formula): """ Helper function for ground_semantic_tree returns a tuple (grounded_formula, variables, cost) :param match_parse: :param formula: :return: """ basic_ontology = text_formula.basic_ontolgy # Base cases: entity | number and constant | function | error current = text_formula.current # current is either Constant or FormulaNode packs = [] if isinstance(current, Constant) and basic_ontology.isinstance( current.type, basic_ontology.types['number']): # Either variable ('x', 'y', etc) or number ('1', '555', etc.) assert isinstance(current.content, str) number_cost = 1 - _get_number_score(current.content) variable_cost = 1 - _get_variable_score(current.content) if number_cost < 1: new_current = Constant(float(current.content), basic_ontology.types['number']) grounded_formula = Formula(basic_ontology, new_current, []) packs.append(FormulaPack(grounded_formula, {}, number_cost)) if variable_cost < 1: variable = sympy.symbols(current.content) variables = {current.content: variable} new_current = Constant(variable, basic_ontology.types['number']) grounded_formula = Formula(basic_ontology, new_current, []) packs.append( FormulaPack(grounded_formula, variables, variable_cost)) elif basic_ontology.isinstance(current.type, basic_ontology.types['entity']): # only single child packs.extend(_ground_entity(match_parse, text_formula)) elif isinstance(current, Function): # Recursive case pack_list_of_list = [ _ground_formula(match_parse, child) for child in text_formula.children ] for pack_list in itertools.product(*pack_list_of_list): variables = {} cost = 0 children = [] for child, local_variables, local_cost in pack_list: children.append(child) cost += local_cost for key, value in local_variables.iteritems(): variables[key] = value grounded_formula = Formula(basic_ontology, current, children) packs.append(FormulaPack(grounded_formula, variables, cost)) else: raise Exception("Something is wrong...") return packs