#NEWLINE# def apply_coreferences( document , coreferences ) : #NEWLINE# #INDENT# '\n    Makes all entity ocurrences listed in `coreferences` have the same\n    entity.\n    It uses coreference information to merge entity ocurrence\'s\n    entities into a single entity.\n    `correferences` is a list of tuples (i, j, head) where:\n     - `i` is the offset in tokens where the occurrence starts.\n     - `j` is the offset in tokens where the occurrence ends.\n     - `head` is the index in tokens of the head of the occurrence (the "most\n        important word").\n\n    Every entity occurrence in `coreference` might already exist or not in\n    `document`. If no occurrence exists in `document` then nothing is done.\n    If at least one ocurrence exists in `document` then all other ocurrences\n    named in `coreference` are automatically created.\n\n    This function can raise CofererenceError in case a merge is attempted on\n    entities of different kinds.\n    ' #NEWLINE# occurrences = defaultdict ( list ) #NEWLINE# for occurrence in document . entity_occurrences . all ( ) : #NEWLINE# #INDENT# for i in range ( occurrence . offset , occurrence . offset_end ) : #NEWLINE# #INDENT# occurrences [ i ] . append ( occurrence ) #NEWLINE# #UNINDENT# #UNINDENT# entities = [ ] #NEWLINE# pickable_as_representant = [ ] #NEWLINE# missing = [ ] #NEWLINE# for ( i , j , head ) in sorted ( coreferences ) : #NEWLINE# #INDENT# if occurrences [ head ] : #NEWLINE# #INDENT# for x in occurrences [ head ] : #NEWLINE# #INDENT# entities . append ( x . entity ) #NEWLINE# if ( not x . anaphora ) : #NEWLINE# #INDENT# pickable_as_representant . append ( x . entity ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# missing . append ( ( i , j , head ) ) #NEWLINE# #UNINDENT# #UNINDENT# if ( not pickable_as_representant ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# issues = issues_merging_entities ( document , entities ) #NEWLINE# if issues : #NEWLINE# #INDENT# raise CoreferenceError ( issues ) #NEWLINE# #UNINDENT# from_ner = [ r for r in pickable_as_representant if ( not r . gazette ) ] #NEWLINE# if from_ner : #NEWLINE# #INDENT# canonical = from_ner [ 0 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# canonical = pickable_as_representant [ 0 ] #NEWLINE# #UNINDENT# for ( i , j , head ) in missing : #NEWLINE# #INDENT# if ( ( j - i ) >= 5 ) : #NEWLINE# #INDENT# i = head #NEWLINE# j = ( head + 1 ) #NEWLINE# #UNINDENT# EntityOccurrence . objects . get_or_create ( document = document , entity = canonical , offset = i , offset_end = j , alias = ' ' . join ( document . tokens [ i : j ] ) , defaults = { #NEWLINE# #INDENT# 'anaphora' : True , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# for entity in set ( ( x for x in entities if ( x != canonical ) ) ) : #NEWLINE# #INDENT# for occurrence in EntityOccurrence . objects . filter ( entity = entity , document = document ) : #NEWLINE# #INDENT# occurrence . entity = canonical #NEWLINE# occurrence . save ( )