예제 #1
0
def create_semantic_structures(frame_semantic_list):
    """Take in the list of VerbNet frames and generate the semantic
    representation structures from them."""
    semantic_representation_list = []
    conditional = False
    # Frame semantic list is a list of conjunction strings and tuples, where the
    # first element of the tuple is the frame semantics, the second element is
    # the original tree branch it came from, and the third is a list of tags.

    for frame in frame_semantic_list:
        # Check that this is a VerbNet frame and not a conjunction
        try:
            frame_items = frame[0].items()
        except:
            if 'if' in str(frame):
                conditional = True
            frame_items = None
            semantic_representation_list.append(frame)
            continue

        verb = frame[3].split('-')[0]

        location_predicates = defaultdict(list)
        location_resolved = False

        entity_class_dict = {}

        for key, value in frame_items:
            entity_class_dict[key] = extract_entity_class(value, key)        
        
        wh_question_type = frames.get_wh_question_type(str(frame[1]))
        
        # If it's a WH-question, find the type of question it is and add the object
        if wh_question_type is not None and 'Theme' in entity_class_dict:
            semantic_representation_list.append(\
                WhQuery(entity_class_dict['Theme'], wh_question_type))
        # If it's a yes-no question, add the theme of the question
        elif frames.is_yn_question(str(frame[1])):
            if 'Theme' in entity_class_dict and 'Location' in entity_class_dict:
                entity_class_dict['Theme'].predicates = \
                                        dict(entity_class_dict['Theme'].predicates.items() +
                                                entity_class_dict['Location'].predicates.items() )
                semantic_representation_list.append(\
                    YNQuery(entity_class_dict['Theme']))
        # If there is a specific location involved, it's a command (this may need to be changed)
        elif location_resolved is True:
            semantic_representation_list.append(
                Command(str(location_predicates['Location'][0]),'go'))
            # If there was a conditional statement, this command ends it
            conditional = False
        # If it's a conditional statement, the first statement is an event
        elif conditional is True and 'Theme' in entity_class_dict:
            semantic_representation_list.append(Event(entity_class_dict['Theme'], verb))
        # It's a regular command
        elif verb is not None and verb != 'be':
            command_predicate_dict = {}
            for key, value in entity_class_dict.items():  
                # Only add semantic roles, not prepositions and verbs
                if key == string.capitalize(key):
                    command_predicate_dict[key] = value.predicates[key]
            
            for key, value in entity_class_dict.items():
                if key == 'Theme' or key == 'Agent' or key == 'Patient':
                    semantic_representation_list.append(Command(
                                                            EntityClass(
                                                                value.quantifier,
                                                                command_predicate_dict),
                                                            verb))
                    break
        # It's an assertion
        else:
            semantic_representation_list.append(\
                Assertion(entity_class_dict['Theme'], entity_class_dict['Location'].predicates,\
                          ('ex' in frame[2])))

    return semantic_representation_list
예제 #2
0
def create_semantic_structures(frame_semantic_list):
    """Take in the list of VerbNet frames and generate the semantic
    representation structures from them."""
    semantic_representation_list = []
    conditional = False
    # Frame semantic list is a list of conjunction strings and tuples, where the
    # first element of the tuple is the frame semantics, the second element is
    # the original tree branch it came from, the third is a list of tags, and
    # the fourth is the sense.

    for frame in frame_semantic_list:
        # Check that this is a VerbNet frame and not a conjunction
        try:
            frame_items = frame[0].items()
        except AttributeError:
            if 'if' in str(frame):
                conditional = True
            frame_items = None
            #semantic_representation_list.append(frame)
            continue

        sense = frame[3].split('-')[0]
        # Get the action associated with the sense
        # If such a mapping does not exist, use the original verb
        action = ACTION_ALIASES.get(sense, frame[4])

        item_to_entity = {key:extract_entity(value, key) for key, value in frame_items}

        wh_question_type = frames.get_wh_question_type(str(frame[1]))

        # If it's a WH-question, find the type of question it is and add the object
        if wh_question_type is not None:
            if wh_question_type == 'Status':
                semantic_representation_list.append(StatusQuery())
            elif 'Theme' in item_to_entity:
                if wh_question_type == 'Location':
                    semantic_representation_list.append(LocationQuery(item_to_entity['Theme']))
                elif wh_question_type in ('People', 'Entity'):
                    semantic_representation_list.append(EntityQuery(item_to_entity['Location']))

        # If it's a yes-no question, add the theme and location of the question
        elif frames.is_yn_question(str(frame[1])):
            if 'Theme' in item_to_entity and 'Location' in item_to_entity:
                semantic_representation_list.append(YNQuery(item_to_entity['Theme'], item_to_entity['Location']))
        # If it's a conditional statement, the first statement is an event or an assertion
        elif conditional:
            if 'Stimulus' in item_to_entity:
                condition = Event(item_to_entity['Stimulus'], action)
            else:
                condition = Assertion(item_to_entity.get('Theme', None),\
                                          item_to_entity.get('Location', None),\
                                          'ex' in frame[2])
            if len(semantic_representation_list) > 0 and isinstance(semantic_representation_list[-1], Command):
                semantic_representation_list[-1].condition = condition
            else:
                # Dangling condition (shouldn't happen)
                semantic_representation_list.append(condition)
        # It's a regular command
        elif action is not None and action not in  ('is', 'are', 'be'):
            theme = item_to_entity.get('Theme', None)
            agent = item_to_entity.get('Agent', None)
            patient = item_to_entity.get('Patient', None)
            if patient is None:
                patient = item_to_entity.get('Recipient', None)
            location = item_to_entity.get('Location', None)
            source = item_to_entity.get('Source', None)
            destination = item_to_entity.get('Destination', None)
            current_command = Command(agent, theme, patient, location, source, destination, action, negation=frame[5])
            semantic_representation_list.append(current_command)
        # It's an assertion
        else:
            semantic_representation_list.append(Assertion(item_to_entity.get('Theme', None),
                                                          item_to_entity.get('Location', None),
                                                          'ex' in frame[2]))
    if EXTRACT_DEBUG:
        print 'Semantic representation list:'
        print semantic_representation_list
    return semantic_representation_list