コード例 #1
0
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
コード例 #2
0
def _helper2(semantic_forest, path_nodes, v_formula):
    if len(path_nodes) == 3:
        return v_formula
    else:
        basic_ontology = semantic_forest.basic_ontology
        function = path_nodes[2]
        new_path_nodes = path_nodes[2:]
        children = [_helper2(semantic_forest, new_path_nodes, v_formula)]
        return Formula(basic_ontology, function, children)
コード例 #3
0
def tree_graph_to_formula(semantic_forest, tree_graph, node_key):
    basic_ontology = semantic_forest.basic_ontology
    ground = semantic_forest.graph_nodes[node_key].ground
    if isinstance(ground, Constant):
        return Formula(basic_ontology, ground, [])
    children = range(ground.valence)
    from_index = [x for x, data in tree_graph.nodes(data=True) if data['key'] == node_key][0]
    for _, to_index, data in tree_graph.edges(from_index, data=True):
        u = tree_graph.node[from_index]['key']
        v = tree_graph.node[to_index]['key']
        data = semantic_forest.forest_graph[u][v][data['key']]
        v_formula = tree_graph_to_formula(semantic_forest, tree_graph, v)
        arg_idx = data['arg_idx']
        if 'ontology_path' in data:
            ontology_path = data['ontology_path']
            path_formula = _helper(semantic_forest, ontology_path, v_formula)
        else:
            path_formula = v_formula
        children[arg_idx] = path_formula
    formula = Formula(basic_ontology, ground, children)
    return formula
コード例 #4
0
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
コード例 #5
0
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