def test_intersect_progress(self): """Does intersect track progress?""" def progress_callback(): """Mock progress callback""" self.count += 1 Segmenter.intersect( source=self.letter_seg, filtering=self.third_letter_seg, progress_callback=progress_callback, ) self.assertEqual(self.count, len(self.letter_seg), msg="intersect doesn't track progress!")
def test_intersect_autonumber(self): """Does intersect autonumber input segments?""" segmentation, _ = Segmenter.intersect(source=self.letter_seg, filtering=self.third_letter_seg, auto_number_as='num') self.assertEqual([s.annotations['num'] for s in segmentation], [1, 2], msg="intersect doesn't autonumber input segments!")
def test_intersect_progress(self): """Does intersect track progress?""" def progress_callback(): """Mock progress callback""" self.count += 1 Segmenter.intersect( source=self.letter_seg, filtering=self.third_letter_seg, progress_callback=progress_callback, ) self.assertEqual( self.count, len(self.letter_seg), msg="intersect doesn't track progress!" )
def test_intersect_mode(self): """Does intersect respect mode setting?""" segmentation, _ = Segmenter.intersect( source=self.letter_seg, filtering=self.third_letter_seg, mode="exclude", ) self.assertEqual(''.join(s.get_content() for s in segmentation), 'ace', msg="intersect doesn't respect mode setting!")
def test_intersect_neg(self): """Does intersect output complementary segmentation?""" _, segmentation = Segmenter.intersect( source=self.letter_seg, filtering=self.third_letter_seg, ) self.assertEqual( ''.join(s.get_content() for s in segmentation), 'ace', msg="intersect doesn't output complementary segmentation!")
def test_intersect_content_content(self): """Does intersect filter segments (content content)?""" segmentation, _ = Segmenter.intersect( source=self.letter_seg, filtering=self.third_letter_seg, ) self.assertEqual( ''.join(s.get_content() for s in segmentation), 'bd', msg="intersect doesn't filter segments (content content)!")
def test_intersect_import_annotations_false(self): """Does intersect skip importing annotations?""" segmentation, _ = Segmenter.intersect( source=self.letter_seg, filtering=self.third_letter_seg, source_annotation_key='a', copy_annotations=False, ) self.assertFalse('a' in segmentation[0].annotations, msg="intersect doesn't skip importing annotations!")
def test_intersect_neg(self): """Does intersect output complementary segmentation?""" _, segmentation = Segmenter.intersect( source=self.letter_seg, filtering=self.third_letter_seg, ) self.assertEqual( ''.join(s.get_content() for s in segmentation), 'ace', msg="intersect doesn't output complementary segmentation!" )
def test_intersect_content_content(self): """Does intersect filter segments (content content)?""" segmentation, _ = Segmenter.intersect( source=self.letter_seg, filtering=self.third_letter_seg, ) self.assertEqual( ''.join(s.get_content() for s in segmentation), 'bd', msg="intersect doesn't filter segments (content content)!" )
def test_intersect_import_annotations(self): """Does intersect import annotations?""" segmentation, _ = Segmenter.intersect( source=self.letter_seg, filtering=self.third_letter_seg, source_annotation_key='a', copy_annotations=True, ) self.assertEqual(segmentation[0].annotations['a'], '1', msg="intersect doesn't import annotations!")
def test_intersect_annotation_annotation(self): """Does intersect filter segments (annotation annotation)?""" segmentation, _ = Segmenter.intersect( source=self.letter_seg, filtering=self.third_letter_seg, source_annotation_key='b', filtering_annotation_key='a', ) self.assertEqual( ''.join(s.get_content() for s in segmentation), 'd', msg="intersect doesn't filter segments (annotation annotation)!")
def test_intersect_import_annotations_false(self): """Does intersect skip importing annotations?""" segmentation, _ = Segmenter.intersect( source=self.letter_seg, filtering=self.third_letter_seg, source_annotation_key='a', copy_annotations=False, ) self.assertFalse( 'a' in segmentation[0].annotations, msg="intersect doesn't skip importing annotations!" )
def test_intersect_mode(self): """Does intersect respect mode setting?""" segmentation, _ = Segmenter.intersect( source=self.letter_seg, filtering=self.third_letter_seg, mode="exclude", ) self.assertEqual( ''.join(s.get_content() for s in segmentation), 'ace', msg="intersect doesn't respect mode setting!" )
def test_intersect_autonumber(self): """Does intersect autonumber input segments?""" segmentation, _ = Segmenter.intersect( source=self.letter_seg, filtering=self.third_letter_seg, auto_number_as='num' ) self.assertEqual( [s.annotations['num'] for s in segmentation], [1, 2], msg="intersect doesn't autonumber input segments!" )
def test_intersect_annotation_annotation(self): """Does intersect filter segments (annotation annotation)?""" segmentation, _ = Segmenter.intersect( source=self.letter_seg, filtering=self.third_letter_seg, source_annotation_key='b', filtering_annotation_key='a', ) self.assertEqual( ''.join(s.get_content() for s in segmentation), 'd', msg="intersect doesn't filter segments (annotation annotation)!" )
def test_intersect_import_annotations(self): """Does intersect import annotations?""" segmentation, _ = Segmenter.intersect( source=self.letter_seg, filtering=self.third_letter_seg, source_annotation_key='a', copy_annotations=True, ) self.assertEqual( segmentation[0].annotations['a'], '1', msg="intersect doesn't import annotations!" )
def sendData(self): """(Have LTTL.Segmenter) perform the actual filtering""" # Check that there's something on input... if len(self.segmentations) == 0: self.infoBox.setText(u'Widget needs input.', 'warning') self.send('Selected data', None, self) self.send('Discarded data', None, self) return assert self.source >= 0 assert self.filtering >= 0 # TODO: remove message 'No label was provided.' from docs # Source and filtering parameter... source = self.segmentations[self.source][1] filtering = self.segmentations[self.filtering][1] if self.displayAdvancedSettings: source_annotation_key = self.sourceAnnotationKey or None if self.sourceAnnotationKey == u'(none)': source_annotation_key = None filtering_annotation_key = self.filteringAnnotationKey or None if filtering_annotation_key == u'(none)': filtering_annotation_key = None else: source_annotation_key = None filtering_annotation_key = None # Check that autoNumberKey is not empty (if necessary)... if self.displayAdvancedSettings and self.autoNumber: if self.autoNumberKey: autoNumberKey = self.autoNumberKey num_iterations = 2 * len(source['segmentation']) else: self.infoBox.setText( u'Please enter an annotation key for auto-numbering.', 'warning' ) self.send('Selected data', None, self) self.send('Discarded data', None, self) return else: autoNumberKey = None num_iterations = len(source) # Basic settings... if self.displayAdvancedSettings: copyAnnotations = self.copyAnnotations else: copyAnnotations = True # Perform filtering... self.infoBox.setText(u"Processing, please wait...", "warning") self.controlArea.setDisabled(True) progressBar = ProgressBar( self, iterations=num_iterations ) (filtered_data, discarded_data) = Segmenter.intersect( source=source, source_annotation_key=source_annotation_key, filtering=filtering, filtering_annotation_key=filtering_annotation_key, mode=self.mode.lower(), label=self.captionTitle, copy_annotations=self.copyAnnotations, auto_number_as=autoNumberKey, progress_callback=progressBar.advance, ) progressBar.finish() self.controlArea.setDisabled(False) message = u'%i segment@p sent to output.' % len(filtered_data) message = pluralize(message, len(filtered_data)) self.infoBox.setText(message) self.send('Selected data', filtered_data, self) self.send('Discarded data', discarded_data, self) self.sendButton.resetSettingsChangedFlag()