def test_select_covering_also_returns_parent_instances(small_typesystem_xml, tokens, sentences): typesystem = load_typesystem(small_typesystem_xml) SubSentenceType = typesystem.create_type("cassis.SubSentence", supertypeName="cassis.Sentence") cas = Cas(typesystem=typesystem) first_sentence, second_sentence = sentences annotations = tokens + sentences subsentence1 = SubSentenceType(begin=first_sentence.begin, end=first_sentence.end) subsentence2 = SubSentenceType(begin=second_sentence.begin, end=second_sentence.end) annotations.append(subsentence1) annotations.append(subsentence2) cas.add_annotations(annotations) tokens_in_first_sentence = tokens[:6] tokens_in_second_sentence = tokens[6:] for token in tokens_in_first_sentence: result = set(cas.select_covering("cassis.Sentence", token)) assert result == {first_sentence, subsentence1} for token in tokens_in_second_sentence: result = set(cas.select_covering("cassis.Sentence", token)) assert result == {second_sentence, subsentence2}
def test_select_covered_also_returns_parent_instances(small_typesystem_xml, tokens, sentences): typesystem = load_typesystem(small_typesystem_xml) SubTokenType = typesystem.create_type("cassis.SubToken", supertypeName="cassis.Token") annotations = tokens + sentences subtoken1 = SubTokenType(begin=tokens[2].begin, end=tokens[3].end) subtoken2 = SubTokenType(begin=tokens[8].begin, end=tokens[8].end) annotations.append(subtoken1) annotations.append(subtoken2) cas = Cas(typesystem=typesystem) cas.add_annotations(annotations) first_sentence, second_sentence = sentences tokens_in_first_sentence = tokens[:6] tokens_in_second_sentence = tokens[6:] actual_tokens_in_first_sentence = list( cas.select_covered("cassis.Token", first_sentence)) actual_tokens_in_second_sentence = list( cas.select_covered("cassis.Token", second_sentence)) assert set(actual_tokens_in_first_sentence) == set( tokens_in_first_sentence + [subtoken1]) assert set(actual_tokens_in_second_sentence) == set( tokens_in_second_sentence + [subtoken2])
def test_select_also_returns_parent_instances(small_typesystem_xml, tokens, sentences): annotations = tokens + sentences cas = Cas(typesystem=load_typesystem(small_typesystem_xml)) cas.add_annotations(annotations) actual_annotations = list(cas.select("uima.tcas.Annotation")) assert set(actual_annotations) == set(annotations)
def test_select(tokens, sentences): cas = Cas() cas.add_annotations(tokens + sentences) actual_tokens = list(cas.select("cassis.Token")) actual_sentences = list(cas.select("cassis.Sentence")) assert actual_tokens == tokens assert actual_sentences == sentences
def test_removing_throws_if_fs_in_other_view(small_typesystem_xml, tokens, sentences): cas = Cas(typesystem=load_typesystem(small_typesystem_xml)) cas.add_annotations(tokens) view = cas.create_view("testView") with pytest.raises(ValueError): view.remove_annotation(tokens[0])
def test_select(small_typesystem_xml, tokens, sentences): cas = Cas(typesystem=load_typesystem(small_typesystem_xml)) cas.add_annotations(tokens + sentences) actual_tokens = list(cas.select("cassis.Token")) actual_sentences = list(cas.select("cassis.Sentence")) assert actual_tokens == tokens assert actual_sentences == sentences
def test_select_only_returns_annotations_of_current_view(tokens, sentences, small_typesystem_xml): cas = Cas(typesystem=load_typesystem(small_typesystem_xml)) cas.add_annotations(tokens) view = cas.create_view("testView") view.add_annotations(sentences) actual_annotations_in_initial_view = list(cas.get_view("_InitialView").select_all()) actual_annotations_in_test_view = list(cas.get_view("testView").select_all()) assert tokens == actual_annotations_in_initial_view assert sentences == actual_annotations_in_test_view
def test_select_covered(small_typesystem_xml, tokens, sentences): cas = Cas(typesystem=load_typesystem(small_typesystem_xml)) cas.add_annotations(tokens + sentences) first_sentence, second_sentence = sentences tokens_in_first_sentence = tokens[:6] tokens_in_second_sentence = tokens[6:] actual_tokens_in_first_sentence = list(cas.select_covered("cassis.Token", first_sentence)) actual_tokens_in_second_sentence = list(cas.select_covered("cassis.Token", second_sentence)) assert actual_tokens_in_first_sentence == tokens_in_first_sentence assert actual_tokens_in_second_sentence == tokens_in_second_sentence
def test_removing_removes_from_view(small_typesystem_xml, tokens, sentences): annotations = tokens + sentences cas = Cas(typesystem=load_typesystem(small_typesystem_xml)) view = cas.create_view("testView") cas.add_annotations(annotations) view.add_annotations(annotations) for annotation in annotations: cas.remove_annotation(annotation) assert set(cas.select("uima.tcas.Annotation")) == set() assert set(view.select("uima.tcas.Annotation")) == set(annotations)
def test_select_covered(tokens, sentences): cas = Cas() cas.add_annotations(tokens + sentences) first_sentence, second_sentence = sentences tokens_in_first_sentence = tokens[:6] tokens_in_second_sentence = tokens[6:] actual_tokens_in_first_sentence = list( cas.select_covered("cassis.Token", first_sentence)) actual_tokens_in_second_sentence = list( cas.select_covered("cassis.Token", second_sentence)) assert actual_tokens_in_first_sentence == tokens_in_first_sentence assert actual_tokens_in_second_sentence == tokens_in_second_sentence
def test_removing_of_existing_fs_works(small_typesystem_xml, tokens, sentences): annotations = tokens + sentences cas = Cas(typesystem=load_typesystem(small_typesystem_xml)) cas.add_annotations(annotations) for token in tokens: cas.remove_annotation(token) actual_annotations = list(cas.select("uima.tcas.Annotation")) assert set(actual_annotations) == set(sentences) for sentence in sentences: cas.remove_annotation(sentence) actual_annotations = list(cas.select("uima.tcas.Annotation")) assert set(actual_annotations) == set()
def test_select_covered_overlapping(small_typesystem_xml, tokens, sentences): cas = Cas(typesystem=load_typesystem(small_typesystem_xml)) AnnotationType = cas.typesystem.create_type("test.Annotation") SentenceType = cas.typesystem.get_type("cassis.Sentence") sentence = SentenceType(begin=0, end=10) annotations = [ AnnotationType(begin=0, end=5), AnnotationType(begin=0, end=5) ] cas.add_annotation(sentence) cas.add_annotations(annotations) actual_annotations = list(cas.select_covered("test.Annotation", sentence)) assert actual_annotations == annotations
def test_select_covering(small_typesystem_xml, tokens, sentences): cas = Cas(typesystem=load_typesystem(small_typesystem_xml)) cas.add_annotations(tokens + sentences) actual_first_sentence, actual_second_sentence = sentences tokens_in_first_sentence = tokens[:6] tokens_in_second_sentence = tokens[6:] for token in tokens_in_first_sentence: result = list(cas.select_covering("cassis.Sentence", token)) first_sentence = result[0] assert len(result) == 1 assert actual_first_sentence == first_sentence for token in tokens_in_second_sentence: result = list(cas.select_covering("cassis.Sentence", token)) second_sentence = result[0] assert len(result) == 1 assert actual_second_sentence == second_sentence