예제 #1
0
def _print_semexes_header(ctx):
    """
    Pretty prints the semexes
    """
    pp("Path Sets: ")
    for name, semex in ctx.semexes().iteritems():
        pp("  %s %s" % (name, semex.semex))
예제 #2
0
def _print_most_likely_paths(ctx, node):
    """
    Calculates and prints the most likely endpoints for all semexes
    in the context provided.
    """
    indent()
    graph = ctx.graph
    num_nodes = ctx.graph_number_of_nodes()
    for name, val in ctx.semexes().iteritems():
        if not val.active:
            continue
        # Calculate semex starting at this neighbor node
        semex = val.semex
        this_semex = make_semex_starting_here(ctx.transition,
                                              ctx.transition_op,
                                              graph,
                                              num_nodes,
                                              semex,
                                              node)
        most_likely = most_likely_endpoints(this_semex, num_nodes)
        nodes = graph.nodes() # cache this lookup
        pp("%s"%name)
        indent()
        for (idx, val) in most_likely:
            endpoint = nodes[idx]
            # Skip current
            if endpoint == ctx.posn or endpoint == node:
                continue
            pp("%s [%e]" % (str(endpoint)[:80], val))
        deindent()
    deindent()
예제 #3
0
def semex_add(ctx, args):
    """
    Adds a semex to the context's list of semexes.

    Args:
        ctx (context.Context)
        args (kct.argparse.Namepsace)
        args.name (str) - used to refer to the semex
        args.semex-str (str) - the semex in string form. A valid argument to
            semex.parser.compile_regex

    Returns: None
    """
    from vinge.semex.parser import compile_regex, RegexParseException
    from vinge.semex.ast_to_semex import ast_to_semex
    name = args.name
    # argparse gives us an array of strings as the semex-str. We want a string
    semex_str = ' '.join(getattr(args, 'semex-str'))
    try:
        semex_ast = compile_regex(semex_str)
        semex = ast_to_semex(ctx.graph, ctx.transition, ctx.transition_op, semex_ast)
        # Add to the context
        ctx.add_semex(name, semex)
        pp('Successfully added path set')
    except RegexParseException, rpe:
        error("Error parsing path set (semex) '%s': %s"%(semex_str, rpe.message))
예제 #4
0
def repl(ctx):
    parser = _setup_command_parser()
    while True:
        output.pp("> ", newline=False)
        str_args = sys.stdin.readline().rstrip()
        parser_args = str_args.split()
        try:
            args = parser.parse_args(parser_args)
        except _CommandParsingError, cpe:
            print "Error %s" % cpe.message
            continue
        args.func(ctx, args)
예제 #5
0
def semex_peek(ctx, args):
    name = args.name
    node_ref_str = getattr(args, 'node-ref')
    node_ref = parse_node_ref(node_ref_str)
    if node_ref is None:
        error("Error %s is not a valid node-ref" % node_ref_str)
        return
    node = ctx.node_by_node_ref(node_ref)

    active_semex = ctx.semexes().get(name)
    if active_semex is None:
        error("Error unknown path set '%s'" % name)
    semex = active_semex.semex

    new_semex = make_semex_starting_here(ctx.transition, ctx.transition_op,
                                         ctx.graph, ctx.graph_number_of_nodes(),
                                         semex, node)
    most_likely = most_likely_endpoints(new_semex, ctx.graph_number_of_nodes())
    for (idx, val) in most_likely:
        endpoint = ctx.graph.nodes()[idx]
        pp("%s [%e]" % (str(endpoint)[:80], val))
예제 #6
0
def semex_list(ctx, args):
    """
    Prints the current semexes.
    """
    for name, semex in ctx.semexes().iteritems():
        pp("  %s: %s" % (name, str(semex.semex)))
예제 #7
0
def do_help(ctx, args):
    topic = args.topic
    # General help message
    if not topic:
        pp("help <topic>")
        for key in sorted(_HELP.iterkeys()):
            pp(key, indent=1)
    # Top level (we're only going one level if this gets much more complex
    # I'm going back to trying to make argparse work)
    elif _HELP.get(topic[0]):
        if len(topic) == 1:
            pp('%s ' % color.bold(topic[0]), newline=False)
            pp(_HELP[topic[0]]['_msg'])
            # Check if there's sub topics
            subtopics = filter(lambda s: s != '_msg', _HELP[topic[0]].keys())
            if len(subtopics) > 0:
                pp('  Subtopics')
                for key in sorted(subtopics):
                    pp(key, indent=2)
        elif _HELP[topic[0]].get(topic[1]):
            pp('%s %s ' % (color.bold(topic[0]), topic[1]), newline=False)
            pp(_HELP[topic[0]][topic[1]])
        else:
            pp("Unknown action: %s" % ' '.join(topic))
    # Nothing
    else:
        pp("Unknown action: %s" % ' '.join(topic))