Exemplo n.º 1
0
    def matchFrame(self, clause):
        # which frame slots to use?
        slots = self.active_frame_slots if not self.isMatchVerbPassive() else self.passive_frame_slots
        failed = False
        i = 0
        # try to find matches for frame slots
        while not failed and i < len(slots):
            phrase_matches = False
            if isinstance(slots[i], VerbSlot):  # for now, just set match item for verb slot
                slots[i].match_item = self.vp_match_item
                phrase_matches = True
            else:  # find matching phrase for common slot
                for phrase in self.getCandidatePhrases(clause, slots[i]):  # search in candidate phrases based on dependency of given slot
                    if not self.isMatched(phrase) and not phrase_matches and slots[i].form.matchPhrase(phrase) and slots[i].testAttributes(phrase):
                        slots[i].match_item = phrase
                        phrase_matches = True
            # fail if match was not found and slot is obligatory and can't be ellipsed
            failed = (not phrase_matches) and slots[i].isObligatory() and not slots[i].canBeEllipsed()
            i += 1

        # generate semantic relation and occupy given roles
        # relation = SemanticRelation() if not failed else None
        # newer version - if clause already has relation, use this
        if not failed:
            if clause.containing_relation == None:
                clause.containing_relation = SemanticRelation()
                clause.containing_relation.containing_clause = clause
            self.matched_slots = slots
            for slot in self.matched_slots:
                # create role for every occupied slot and for slot, which is ellipsable
                # check if given phrase already has given role
                if isinstance(slot, CommonSlot) and (slot.match_item != None or slot.canBeEllipsed()) and ((slot.match_item == None and slot.canBeEllipsed()) or (slot.match_item != None and not slot.match_item.hasRole(slot.second_level_role))):
                    role = SemanticRole(slot.first_level_role, slot.second_level_role)
                    if slot.match_item != None:
                        # set role phrase
                        role.setPhrase(slot.match_item)
                        # set link to role into phrase
                        slot.match_item.semantic_roles.append(role)
                    # ellipsed WE agens
                    elif self.vp_match_item.hasTag('p1') and 'actor' in slot.second_level_role and 'AG' in slot.first_level_role:
                        we_phrase = NPhrase('k1gInPc1', '0', '0', False)
                        # we_phrase.tokens.append(Token('Fio_kA', 'Fio_kA', 'k1gInPc1'))
                        we_phrase.tokens.append(Token('Fio_ACTOR', 'Fio', 'k1gInPc1'))
                        clause.phrases.append(we_phrase)
                        role.setPhrase(we_phrase)
                        we_phrase.semantic_roles.append(role)
                    # don't add new ellipsed role
                    if role.phrase != None or (role.phrase == None and not clause.containing_relation.hasEllipsedRole(slot.second_level_role)):
                        clause.containing_relation.addNewRole(role)
Exemplo n.º 2
0
    def matchClauseTokens(self, clause):
        # relations = []
        for phrase in clause.phrases:

            # does any frame noun match some phrase in clause?
            # takes coordination phrases as one
            if isinstance(phrase, NPhrase) and not phrase.isInCoordination() and self.matchesPhrase(phrase) and not phrase.roleConflict(self.role):
                # relation = None
                roles = []

                # if clause doesn't have containing relation, create one
                if clause.containing_relation == None:
                    clause.containing_relation = SemanticRelation()
                    clause.containing_relation.containing_clause = clause

                # add semantic role to matching phrase
                # if role does not exist, create new one
                # if exists, use this one
                # and at same time tries to upgrade existing base role to specific role
                if not phrase.findRoleAndUpgrade(self.role):
                    role = SemanticRole('OBJ', self.role)
                    role.setPhrase(phrase)
                    phrase.addSemanticRole(role)
                    roles.append(role)

                # match complements
                for complement in self.complements:
                    if not complement.matched:
                        for phr in self.getCandidatePhrases(clause, phrase):
                            # does complement match?
                            if complement.matchPhrase(phr):
                                # does given phrase already have given role?
                                if not phr.findRoleAndUpgrade(complement.second_level_role):
                                    role = SemanticRole(complement.first_level_role, complement.second_level_role)
                                    role.setPhrase(phr)
                                    phr.addSemanticRole(role)
                                    roles.append(role)
                                # elif phr.findRoleAndUpgrade(complement.second_level_role) and relation == None:  # if given phrase already has given role, use this relation
                                #     relation = phr.findRoleAndUpgrade(complement.second_level_role).getRelation()
                                complement.matched = True
                        # if complement is obligatory, then later it has to be resolved, for now is ellipsed
                        if not complement.matched and complement.isObligatory():
                            role = SemanticRole(complement.first_level_role, complement.second_level_role)
                            roles.append(role)

                # add roles to relation
                # remove unnecessary ellipsed roles -  not in current version
                for r in roles:
                    r.relation = clause.containing_relation
                    # do not add unnecessary ellipsed role
                    if r.phrase != None or (r.phrase == None and not clause.containing_relation.hasEllipsedRole(r.second_level_role)):
                        clause.containing_relation.addNewRole(r)