#NEWLINE# def set_segmentation_result( self , value , increment = True , override = False ) : #NEWLINE# #INDENT# if override : #NEWLINE# #INDENT# self . segments . all ( ) . delete ( ) #NEWLINE# logger . info ( 'Previous segments removed' ) #NEWLINE# #UNINDENT# get_offsets = attrgetter ( 'offset' , 'offset_end' ) #NEWLINE# value = sorted ( value , key = get_offsets ) #NEWLINE# logger . info ( 'About to set %s segments for current doc' , len ( value ) ) #NEWLINE# doc_ent_occurrences = list ( self . entity_occurrences . all ( ) ) #NEWLINE# currents = set ( self . segments . all ( ) . values_list ( 'offset' , 'offset_end' ) ) #NEWLINE# new_segs = [ ] #NEWLINE# for ( i , raw_segment ) in enumerate ( value ) : #NEWLINE# #INDENT# if ( ( raw_segment . offset , raw_segment . offset_end ) in currents ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# _segm = TextSegment ( document = self , offset = raw_segment . offset , offset_end = raw_segment . offset_end ) #NEWLINE# new_segs . append ( ( _segm , raw_segment ) ) #NEWLINE# #UNINDENT# if new_segs : #NEWLINE# #INDENT# TextSegment . objects . bulk_create ( list ( zip ( * new_segs ) ) [ 0 ] ) #NEWLINE# logger . info ( 'New %s segments created' , len ( new_segs ) ) #NEWLINE# #UNINDENT# doc_segments = dict ( ( ( get_offsets ( s ) , s ) for s in self . segments . all ( ) ) ) #NEWLINE# for ( _segm , raw_segment ) in new_segs : #NEWLINE# #INDENT# segm = doc_segments [ get_offsets ( _segm ) ] #NEWLINE# if ( raw_segment . entity_occurrences is None ) : #NEWLINE# #INDENT# segm . entity_occurrences = [ eo for eo in doc_ent_occurrences if ( ( eo . offset >= segm . offset ) and ( eo . offset_end <= segm . offset_end ) ) ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# segm . entity_occurrences = currents . entity_occurrences #NEWLINE# #UNINDENT# #UNINDENT# self . segmentation_done_at = datetime . now ( ) #NEWLINE# return self