Пример #1
0
    def visit_group(self, node, children) -> Any:
        params = {
            'label':
            f"group{children[1]['label'] if len(children) > 1 else ''}",
            'font': Font.ITALIC,
            'shape': Shape.ELLIPSE,
        }
        if len(children) > 1:
            for key, value in children[1].items():
                if key not in ('label', 'line') and value is not None:
                    params[key] = value
        graph = add_node(**params)
        if len(children) > 1 and 'line' in children[1]:
            params = {
                k: v
                for k, v in children[1].items()
                if k != 'style' and v is not None
            }
            graph = add_edge(graph['top'], graph['top'], graph, **params)

        source, target = graph['top'], children[0]['top']
        graph = merge(graph, children[0])
        graph = add_edge(
            source,
            target,
            graph,
        )

        return graph
Пример #2
0
    def visit_sequence(self, node, children) -> Any:
        if len(children) == 1:
            return children[0]

        graph = add_node('sequence',
                         font=Font.ITALIC,
                         shape=Shape.BOX,
                         style=Style.ROUNDED)
        source = graph['top']

        label, style = None, None
        for child in children:
            target = child['top']
            root = child['nodes'][target]

            if root['label'] == 'atom':
                ident = next((k for k in child['edges'][target]), None)
                atom = child['nodes'][ident]
                if label is None:
                    label = atom['label']
                    style = Style.FILLED if 'style' in atom else None
                else:
                    label = label + atom['label']
                    style = style and (Style.FILLED
                                       if 'style' in atom else None)

            else:
                if label is not None:
                    node = add_node(label, shape=Shape.BOX, style=style)
                    tgt = node['top']
                    node = merge(
                        node,
                        add_node('atom', font=Font.ITALIC,
                                 shape=Shape.ELLIPSE))
                    src = node['top']
                    node = add_edge(src, tgt, node)

                    graph = merge(graph, node)
                    graph = add_edge(source, src, graph)
                    label, style = None, None

                graph = merge(graph, child)
                graph = add_edge(source, target, graph)

        if label is not None:
            node = add_node(label, shape=Shape.BOX, style=style)
            tgt = node['top']
            node = merge(
                node, add_node('atom', font=Font.ITALIC, shape=Shape.ELLIPSE))
            src = node['top']
            node = add_edge(src, tgt, node)

            graph = merge(graph, node)
            graph = add_edge(source, src, graph)

            # graph = merge(graph, child)
            # graph = add_edge(source, target, graph)

        return graph
Пример #3
0
def marginal_cep_ed (x_v, A, e):
    ue = node_out(x_v, e)
    e_sign = A[e]
    A = delete_edge(A, e)
    try:
        cep_ue = find_cep(A, ue, x_v)
    except:
        cep_ue = -1
    A = add_edge (A, e, e_sign)
    return cep_ue
Пример #4
0
    def visit_alternative(self, node, children) -> Any:
        if len(children) == 1:
            return children[0]

        graph = add_node('alternative',
                         font=Font.ITALIC,
                         shape=Shape.DIAMOND,
                         style=Style.ROUNDED)
        for child in children:
            graph = merge(graph, child)
            graph = add_edge(graph['top'], child['top'], graph)

        return graph
Пример #5
0
def marginal_gain_ed(x_v, A, e):
    """
    Calculating f_{S_{i-1}}(e) for each iteration 
    """
    ue = node_out(x_v, e)
    e_sign = A[e]
    A = delete_edge(A, e)
    try:
        label_ue = find_label(A, ue, x_v)
    except:
        # Edge making graph unconnected
        A = add_edge(A, e, e_sign)
        return -1
    if (label_ue == 0):
        A = add_edge(A, e, e_sign)
        return 0
    else:
        x_v[ue] = label_ue
        cc = count_compatible(ue, A, x_v.copy())
        A = add_edge(A, e, e_sign)
        x_v[ue] = 0
        # x_v = unset_all (ue, x_v, A, S)
        return (cc)
Пример #6
0
    def visit_character_set(self, node, children) -> Any:
        negated = children[0] == '^'
        if negated:
            children = children[1:]

        classes, values = set(), set()
        if children[0] in ['-', ']']:
            values.add(ord(children[0]))
            children = children[1:]
        for child in children:
            if isinstance(child, dict):
                ident = child['top']
                label = child['nodes'][ident]['label']
                try:
                    values.update(order(label))
                except TypeError:
                    classes.add(label)
            else:
                values.update(child)

        graph = add_node(
            f"{'^' if negated else ''}charset",
            font=Font.ITALIC,
            shape=Shape.TRAPEZIUM,
            color=NEGATED if negated else None,
        )
        source = graph['top']

        for class_ in sorted(classes):
            child = add_node(class_, shape=Shape.BOX, style=Style.FILLED)
            graph = merge(graph, child)
            graph = add_edge(source, child['top'], graph)

        for group, symbol in [
            (BUT_SPACE, '\\S'),
            (BUT_DIGIT, '\\D'),
            (BUT_WORD, '\\W'),
            (WORD, '\\w'),
            (DIGIT, '\\d'),
            (SPACE, '\\s'),
        ]:
            if group in values:
                child = add_node(symbol, shape=Shape.BOX, style=Style.FILLED)
                graph = merge(graph, child)
                graph = add_edge(source, child['top'], graph)
                values -= group

        start, last = None, None
        for value in sorted(values):
            if last is None:
                start, last = value, value
            elif value == last + 1:
                last = value
            else:
                label = normal(
                    start
                ) if last == start else f"{normal(start)}-{normal(last)}"
                child = add_node(label, shape=Shape.BOX)
                graph = merge(graph, child)
                graph = add_edge(source, child['top'], graph)
                start, last = None, None

        if last is not None:
            label = normal(
                start) if last == start else f"{normal(start)}-{normal(last)}"
            child = add_node(label, shape=Shape.BOX)
            graph = merge(graph, child)
            graph = add_edge(source, child['top'], graph)

        return graph