def test_location(self):
     """Test talkback for a location command."""
     location = ObjectEntity("hallway")
     command = Command(None, ObjectEntity(DEFUSE_ACTION), None, location,
                       None, None, ACTIVATE_ACTION)
     response = CommandResponse(command, None)
     self.assertEqual(
         self.interpreter.interpret(response),
         ResponseInterpreter.GOTIT_LONG.format(
             "", repr(DEFUSE_ACTION),
             ResponseInterpreter.PREP_REGION.format(
                 "in", "", repr(location.name))) + '.')
    def command_from_dict(self, d):
        #agents and patients
        if d["Patient"] != "":
            patient_object = d["Patient"]["Object"]
            patient = ObjectEntity.from_dict(patient_object)
        else:
            patient = None
        if d["Agent"] != "":
            agent_object = d["Agent"]["Object"]
            agent = ObjectEntity.from_dict(agent_object)
        else:
            agent = None
        #Locations
        if d["Location"] != "":
            location_location = d["Location"]["Location"]
            location = Location.from_dict(location_location)
        else:
            location = None
        if d["Destination"] != "":
            destination_location = d["Destination"]["Location"]
            destination = Location.from_dict(destination_location)
        else:
            destination = None
        if d["Source"] != "":
            source_location = d["Source"]["Location"]
            source = Location.from_dict(source_location)
        else:
            source = None
        #Themes
        if d["Theme"] != "":
            theme_object = d["Theme"]["Object"]
            theme = ObjectEntity.from_dict(theme_object)
        else:
            theme = None
        #Conditions
        if d["Condition"] != "":
            if "Command" in d["Condition"]:
                condition_command = d["Condition"]["Command"]
                condition = self.command_from_dict(condition_command)
            elif "Assertion" in d["Condition"]:
                #Not training on assertions, skip for now
                condition = None
            else:
                condition = None
        else:
            condition = None

        action = d["Action"]
        if action in ACTION_ALIASES:
            action = ACTION_ALIASES[action]

        return Command(agent, theme, patient, location, source, destination,
                       action, condition, d["Negation"])
 def test_defuse(self):
     """Test talkback for a defuse command."""
     bomb = "bomb"
     command = Command(
         None, ObjectEntity(DEFUSE_ACTION), None, None, None, None,
         ACTIVATE_ACTION,
         Command(None, ObjectEntity(bomb), None, None, None, None,
                 SEE_ACTION))
     response = CommandResponse(command, None)
     self.assertEqual(
         self.interpreter.interpret(response),
         ResponseInterpreter.CONDITION.format(bomb) + ", " +
         ResponseInterpreter.GOTIT.format("", DEFUSE_ACTION) + '.')
 def test_bad_region(self):
     """Test talkback for a command with a bad region."""
     location = ObjectEntity("aslkjal")
     response = CommandResponse(
         Command(None, None, None, location, None, None, GO_ACTION),
         NoSuchLocationError(location.name))
     self.assertEqual(self.interpreter.interpret(response),
                      ResponseInterpreter.NO_LOCATION.format(location.name))
Esempio n. 5
0
def extract_entity(parse_tree, semantic_role=''):
    """Creates an entity object given a snippet of a parse tree."""
    entity = Location() if semantic_role in (
        'Location', 'Source', 'Destination') else ObjectEntity()

    # print 'Extracting from:'
    # print str(parse_tree)

    # Ignore rescursed trees and added descriptions
    ignore_positions = []
    previous_node = None
    previous_leaves = None
    for position in parse_tree.treepositions():
        if not isinstance(parse_tree[position], Tree):
            continue
        if position in ignore_positions:
            continue
        subtree = parse_tree[position]
        node = subtree.node

        leaves = ' '.join(subtree.leaves()).lower()
        # A noun phrase might have sub-parts that we need to parse recursively
        # Recurse while there are NP's below the current node
        if subtree is not parse_tree and 'NP' in node:
            entity.merge(extract_entity(subtree))
            # ignore_positions should be relative to parse_tree
            ignore_positions.extend(position + subposition
                                    for subposition in subtree.treepositions())
        # A determiner cardinal node adds some information for the quantifier
        if 'DT' in node:
            entity.quantifier.fill_determiner(leaves)
        # Cardinal number sets the quantifier number
        elif node == 'CD':
            entity.quantifier.fill_cardinal(leaves)
            if entity.quantifier.number is None:
                # Not actually a number
                entity.name = leaves
        elif node == 'PRP':
            entity.name = 'Commander' if leaves in ('i', 'me') else leaves
        elif ('PP' in node and entity.name) or node in ('SBAR', 'JJ'):
            entity.description.append(leaves)
            # ignore_positions should be relative to parse_tree
            ignore_positions.extend(position + subposition
                                    for subposition in subtree.treepositions())
        elif 'NN' in node and previous_node and 'NN' in previous_node and entity.name == previous_leaves:
            entity.description.append(previous_leaves)
            entity.name = leaves
        elif 'NN' in node or node == '-NONE-':
            entity.name = morphy(leaves, 'n')
            if entity.name is None:
                entity.name = leaves
        elif node == 'RB' and leaves == 'there':
            entity.name = 'there'
        previous_node = node
        previous_leaves = leaves
    return entity
 def test_go(self):
     """Test talkback for a go command."""
     location = ObjectEntity("hallway")
     response = CommandResponse(
         Command(None, None, None, location, None, None, GO_ACTION), None)
     self.assertEqual(
         self.interpreter.interpret(response),
         ResponseInterpreter.GOTIT_LONG.format(
             "", GO_ACTION,
             ResponseInterpreter.PREP_REGION.format(
                 "to", "", repr(location.name))) + '.')
 def test_quantification(self):
     """Test talkback for a quantified command."""
     location = ObjectEntity("rooms")
     response = CommandResponse(
         Command(None, None, None, location, None, None, GO_ACTION), None)
     rooms = ['r1', 'r2', 'r3']
     response.command.additional_data[ADDITIONAL_DATA_QUANTIFIER] = rooms
     self.assertEqual(
         self.interpreter.interpret(response),
         ResponseInterpreter.GOTIT_LONG.format(
             "", GO_ACTION,
             ResponseInterpreter.PREP_REGION.format(
                 "to", "s", _and_join(rooms))) + '.')