mmax2conll/mmax2conll/mmax_document_readers.py
def extract_coref_sets(self, xml):
        """
        Extract the sets of markables that refer to the same entity.

        !! NB !! Returns a list because dicts are not hashable
        """
        all_markables = {m['id']: m for m in self.extract_all_items(xml)}
        markables = {
            ID: m
            for ID, m in filter(
                lambda tup: self.item_filter(tup[1]),
                all_markables.items()
            )
        }

        # Create a ID -> [markables referring to ID]
        forward_refs = {}

        for markable in markables.values():
            ref = markable.get('ref', None)
            # If ref is None, this markable does not refer to anything
            if ref is not None:
                if self.validate:
                    if ref not in all_markables:
                        raise ValidationError(
                            f"Reference to unknown markable ({ref!r}):"
                            f" {markable}"
                        )
                forward_refs.setdefault(ref, []).append(markable['id'])

        sets = []
        for root in markables.values():
            if root.get('ref', None) is None or root['ref'] not in markables:
                refset = []
                stack = [root['id']]
                while stack:
                    ID = stack.pop()
                    refset.append(markables[ID])
                    if ID in forward_refs:
                        stack.extend(forward_refs[ID])
                sets.append(refset)
        if markables and not any(sets):
            raise ValueError("You lost all your markables!")
        if self.validate:
            input_total = len(markables)
            output_total = sum(map(len, sets))
            if output_total != input_total:
                out_ids = set(m['id'] for refset in sets for m in refset)
                in_ids = set(markables)
                missing = in_ids - out_ids
                raise ValidationError(
                    f"Expected {input_total} markables in output, found:"
                    f" {output_total}. Missing markable IDs: {sorted(missing)}"
                )
        return sets

        Extract the sets of markables that refer to the same entity.

        !! NB !! Returns a list because dicts are not hashable
        
Extract the sets of markables that refer to the same entity.
NB !. Returns a list because dicts are not hashable
Extract the sets of markables that refer to the same entity . NB ! . Returns a list because dicts are not hashable
def extract_coref_sets(self, xml):
        all_markables = {m['id']: m for m in self.extract_all_items(xml)}
        markables = {
            ID: m
            for ID, m in filter(
                lambda tup: self.item_filter(tup[1]),
                all_markables.items()
            )
        }
        forward_refs = {}
        for markable in markables.values():
            ref = markable.get('ref', None)
            if ref is not None:
                if self.validate:
                    if ref not in all_markables:
                        raise ValidationError(
                            f"Reference to unknown markable ({ref!r}):"
                            f" {markable}"
                        )
                forward_refs.setdefault(ref, []).append(markable['id'])
        sets = []
        for root in markables.values():
            if root.get('ref', None) is None or root['ref'] not in markables:
                refset = []
                stack = [root['id']]
                while stack:
                    ID = stack.pop()
                    refset.append(markables[ID])
                    if ID in forward_refs:
                        stack.extend(forward_refs[ID])
                sets.append(refset)
        if markables and not any(sets):
            raise ValueError("You lost all your markables!")
        if self.validate:
            input_total = len(markables)
            output_total = sum(map(len, sets))
            if output_total != input_total:
                out_ids = set(m['id'] for refset in sets for m in refset)
                in_ids = set(markables)
                missing = in_ids - out_ids
                raise ValidationError(
                    f"Expected {input_total} markables in output, found:"
                    f" {output_total}. Missing markable IDs: {sorted(missing)}"
                )
        return sets
def extract_coref_sets ( self , xml ) : all_markables = { m [ 'id' ] : m for m in self . extract_all_items ( xml ) } markables = { ID : m for ID , m in filter ( lambda tup : self . item_filter ( tup [ 1 ] ) , all_markables . items ( ) ) } forward_refs = { } for markable in markables . values ( ) : ref = markable . get ( 'ref' , None ) if ref is not None : if self . validate : if ref not in all_markables : raise ValidationError ( f"Reference to unknown markable ({ref!r}):" f" {markable}" ) forward_refs . setdefault ( ref , [ ] ) . append ( markable [ 'id' ] ) sets = [ ] for root in markables . values ( ) : if root . get ( 'ref' , None ) is None or root [ 'ref' ] not in markables : refset = [ ] stack = [ root [ 'id' ] ] while stack : ID = stack . pop ( ) refset . append ( markables [ ID ] ) if ID in forward_refs : stack . extend ( forward_refs [ ID ] ) sets . append ( refset ) if markables and not any ( sets ) : raise ValueError ( "You lost all your markables!" ) if self . validate : input_total = len ( markables ) output_total = sum ( map ( len , sets ) ) if output_total != input_total : out_ids = set ( m [ 'id' ] for refset in sets for m in refset ) in_ids = set ( markables ) missing = in_ids - out_ids raise ValidationError ( f"Expected {input_total} markables in output, found:" f" {output_total}. Missing markable IDs: {sorted(missing)}" ) return sets
Extract the sets of markables that refer to the same entity.
Extract the sets of markables that refer to the same entity .
"""
        Extract the sets of markables that refer to the same entity.

        !! NB !! Returns a list because dicts are not hashable
        """ # Create a ID -> [markables referring to ID] # If ref is None, this markable does not refer to anything
self
xml