コード例 #1
0
ファイル: parser_demo.py プロジェクト: Shar-pei-bear/SLURP
def interactive_mode(window, first_input):
    """Interactively get input from the user and parse it."""
    input_frame, input_win, parse_win, semantic_win = setup_windows(window)

    # Initialize pipeline and knowledge base
    pipeline = PipelineClient()
    kb = KnowledgeBase()

    # Send some data through the pipeline
    result = pipeline.parse("This is a test.")
    input_frame.addstr(1, 1, 'Enter your input, then press Ctrl+G. '
                       'Enter "quit" or press Ctrl+C to exit.')
    input_frame.refresh()

    # Until the input is q/quit, process data
    last_input = first_input
    while True:
        # Display the first input if needed
        input_win.erase()
        input_win.refresh()
        if last_input:
            input_win.addstr(0, 0, last_input)

        # Get text from the input box, removing any embedded newlines
        if first_input:
            text = first_input
            first_input = None
        else:
            text = get_input(input_win).replace("\n", "").strip()
        last_input = text

        # Quit if needed
        if text == "q" or text == "quit":
            return

        # Get input again if it was empty
        if not text:
            continue

        # Echo input and display status, clearing both windows
        parse_win.clear()
        parse_win.addstr(text)
        parse_win.addstr('\nParsing and restoring null elements...')
        parse_win.refresh()
        semantic_win.clear()
        semantic_win.refresh()

        # Run the parse pipeline
        result = pipeline.parse(text)
        result_tree = Tree(result)

        # Output the longest parse that will fit. We try to draw the
        # possible output in order of decreasing length.
        parse_max_width = parse_win.getmaxyx()[1]
        possible_formats = (result_tree.pprint(margin=parse_max_width, force_multiline=True),
                            result_tree.pprint(margin=parse_max_width),
                            result)

        for formatted_result in possible_formats:
            parse_win.clear()
            try:
                parse_win.addstr(text + '\n')
                parse_win.addstr(formatted_result)
            except _curses.error:
                continue
            else:
                # We've successfully printed, stop trying formats
                break
        else:
            parse_win.clear()
            parse_win.addstr("Parse too large to show.\n")
        parse_win.refresh()

        # Do the same for semantics
        # Echo input and display status, after clearing the window
        semantic_win.clear()
        semantic_win.addstr(text)
        semantic_win.addstr('\nPerforming semantic analysis...')
        semantic_win.refresh()

        frames, new_commands, kb_response = process_parse_tree(result, text, kb)
        semantic_win.clear()
        try:
            if frames:
                semantic_win.addstr("Frames matched:\n")
                for frame in frames:
                    semantic_win.addstr("\t" + str(frame) + "\n")
            if new_commands:
                semantic_win.addstr("New commands:\n")
                for command in new_commands:
                    semantic_win.addstr(str(command) + "\n")
            if kb_response:
                semantic_win.addstr("KB response:\n")
                semantic_win.addstr(str(kb_response) + "\n")
            if not any((frames, new_commands, kb_response)):
                semantic_win.addstr("No frames matched.\n")
        except _curses.error:
            semantic_win.clear()
            semantic_win.addstr("Semantic representation too large to show.")
        semantic_win.refresh()

    return