Example #1
0
    def process_text(self, text):
        """Parse text and pass it to semantics."""
        if text:
            # Clean unicode first if needed
            if isinstance(text, unicode):
                text = text.encode('ascii', 'ignore')

            print "Parsing:", repr(text)
            parse = parse_text(text)
            result = WORLD_KNOWLEDGE.process_parse_tree(parse, text)
            answer = result[0]
            new_commands = result[3]
            # Use the answer if there was one and it was a string
            response = answer if answer and isinstance(answer, str) else ""

            # Also, echo back any new commands
            command_echo = make_response(new_commands)

            # Combine the responses as needed.
            if not response:
                response = command_echo
            elif new_commands:
                response += " " + command_echo

            if response:
                self.send_response(response)

            print "Completed:", repr(text)
Example #2
0
 def parse_text(self, request):
     """Return a parse response."""
     text = request.in_
     rospy.loginfo("NLP pipeline request: %r" % text)
     response = parse_text(text)
     rospy.loginfo("NLP pipeline response: %r" % response)
     return response
Example #3
0
    def process_text(self, text):
        """Parse text and pass it to semantics."""
        if text:
            # Clean unicode first if needed
            if isinstance(text, unicode):
                text = text.encode('ascii', 'ignore')
            
            print "Parsing:", repr(text)
            parse = parse_text(text)
            result = WORLD_KNOWLEDGE.process_parse_tree(parse, text)
            answer = result[0]
            new_commands = result[3]
            # Use the answer if there was one and it was a string
            response = answer if answer and isinstance(answer, str) else ""
            
            # Also, echo back any new commands
            command_echo = make_response(new_commands)
                
            # Combine the responses as needed.
            if not response:
                response = command_echo
            elif new_commands:
                response += " " + command_echo

            if response:
                self.send_response(response)
            
            print "Completed:", repr(text)
Example #4
0
 def parse_text(self, message):
     """Send a parse request to the pipline."""
     data = json.loads(message)
     print "Message:", repr(data)
     print "Parsing..."
     response = parse_text(**data)
     print "Sending:", repr(response)
     self.send(response)
Example #5
0
 def parse_text(self, message):
     """Receive a parse request for the pipeline."""
     try:
         data = json.loads(message)
     except ValueError:
         # Make a default set of arguments
         data = {'text': message}
     print "Message:", repr(data)
     print "Parsing..."
     response = parse_text(**data)
     print "Sending:", repr(response)
     self.send(response)
Example #6
0
def interactive_mode(window):
    """Interactively get input from the user and parse it."""
    input_frame, input_win, parse_win, semantic_win = setup_windows(window)

    # Set up semantics module
    world_knowledge = knowledge.Knowledge()
    
    # Send some data through the pipeline
    result = parse_text("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
    while True:
        # Get text from the input box, removing any embedded newlines
        text = get_input(input_win).strip()

        # 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 = parse_text(text)

        # Clear the status and output the result, it's easiest to just
        # clear and echo the input again 
        parse_win.clear()
        try:
            parse_win.addstr(text + '\n')
            parse_win.addstr(result)
        except _curses.error:
            parse_win.clear()
            semantic_win.addstr("Parse too large to show.""")
        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()

        process_result = world_knowledge.process_parse_tree(result, text)
        semantic_answer, frame_trees = process_result[0], process_result[1]
        if frame_trees is not None:
            modified_trees = [str(modified_parse_tree[1]) for modified_parse_tree in frame_trees 
                              if (len(modified_parse_tree) > 1 and 
                                  isinstance(modified_parse_tree[1], tree.Tree))]
            # TODO: Remove after dupes stop coming in
            modified_trees = list(set(modified_trees))
            frames = [str(frame_dict) for frame_dict in [frame[0] for frame in frame_trees \
                                                         if not isinstance(frame, str)]] 
            semantics = "Modified trees: " + "\n".join(modified_trees) + \
                        "\nFrames: " + "\n".join(frames) + "\nResponse: " + str(semantic_answer)
        else:
            semantics = str(semantic_answer)

        # Clear the status and output the result, it's easiest to just
        # clear and echo the input again 
        semantic_win.clear()
        try:
            semantic_win.addstr(text + '\n')
            semantic_win.addstr(semantics)
        except _curses.error:
            semantic_win.clear()
            semantic_win.addstr("Semantics representation too large to show.""")
        semantic_win.refresh()     

    return
Example #7
0
    def _process_text(self, msg):
        """Run a string through the NLP pipeline."""
        # Remove backlashes since they are sometimes in the input for no good reason
        text = msg.strip().replace('\\', '')

        if text.startswith(SECRET_CODE):
            knowledge_demo = True
            text = text[len(SECRET_CODE):]
        else:
            knowledge_demo = False
        
        # Return nothing if there was not text
        if not text:
            return {}

        if knowledge_demo:
            print "Secret demo mode!"
            global _WORLD_KNOWLEDGE
            if not _WORLD_KNOWLEDGE or text == "reset":                
                _WORLD_KNOWLEDGE = knowledge.Knowledge()

            world_knowledge = _WORLD_KNOWLEDGE
        else:
            world_knowledge = knowledge.Knowledge()
        
        response = {}

        # Run the parse pipeline
        parse = parse_text(text)
        response['parse'] = parse.replace('(', '[').replace(')', ']')
        
        # Get the results from semantics
        results = world_knowledge.process_parse_tree(parse, text)
        answer, frame_trees, new_commands = results[0], results[1], results[3]

        # Use the answer if there was one and it was a string
        user_response = answer if answer and isinstance(answer, str) else ""
            
        # Also, echo back any new commands
        command_echo = make_response(new_commands)
                
        # Combine the responses as needed.
        if not user_response:
            user_response = command_echo
        elif new_commands:
            user_response += " " + command_echo

        response['response'] = user_response

        if frame_trees is not None:
            modified_trees = [str(modified_parse_tree[1]).replace('(', '[').replace(')', ']')
                              for modified_parse_tree in frame_trees 
                              if (len(modified_parse_tree) > 1 and 
                                  isinstance(modified_parse_tree[1], tree.Tree))]
            response['trees'] = list(set(modified_trees))
            
            frames = [frame_dict for frame_dict in [frame[0] for frame in frame_trees
                                                    if isinstance(frame[0], dict)]]
            response['frames'] = frames
        else:
            response['trees'] = []
            response['frames'] = []

        # Extract the command queue and add it to the response.
        # This is turned off for now because better talkback makes it
        # unnecessary.
        #command_queue = world_knowledge.command_queue
        #if command_queue:
        #    response['response'] += " Commands: " + str(command_queue) 

        return response