def test_type_inherits_from_annotation_base(): typesystem = TypeSystem() test_type = typesystem.create_type(name="test.Type", supertypeName="uima.cas.AnnotationBase") annotation = test_type(sofa=42) assert annotation.sofa == 42
def test_is_collection(type_name: str, feature_name: str, expected: bool): typesystem = TypeSystem() t = typesystem.get_type(type_name) feature = Feature("test_feature", rangeTypeName=feature_name) t.add_feature(feature) assert typesystem.is_collection(type_name, feature) == expected
def test_type_can_be_created(): typesystem = TypeSystem() test_type = typesystem.create_type(name="test.Type") assert test_type.name == "test.Type" assert test_type.supertypeName == "uima.tcas.Annotation"
def test_type_inherits_from_annotation(): typesystem = TypeSystem() test_type = typesystem.create_type(name="test.Type") annotation = test_type(begin=0, end=42, sofa=1337) assert annotation.begin == 0 assert annotation.end == 42 assert annotation.sofa == 1337
def test_type_can_create_instances(): typesystem = TypeSystem() test_type = typesystem.create_type(name="test.Type") typesystem.add_feature(type_=test_type, name="testFeature", rangeTypeName="String", description="A test feature") annotation = test_type(begin=0, end=42, testFeature="testValue") assert annotation.begin == 0 assert annotation.end == 42 assert annotation.testFeature == "testValue"
def build_typesystem() -> TypeSystem: typesystem = TypeSystem() SentenceType = typesystem.create_type(SENTENCE_TYPE) PredictedType = typesystem.create_type(PREDICTED_TYPE) typesystem.add_feature(PredictedType, PREDICTED_FEATURE, "uima.cas.String") typesystem.add_feature(PredictedType, IS_PREDICTION, "uima.cas.Boolean") return typesystem
def test_that_merging_incompatible_typesystem_throws( name, rangeTypeName, elementType, multipleReferencesAllowed): with open(typesystem_merge_base_path(), "r") as f: base = load_typesystem(f.read()) ts = TypeSystem() t = ts.create_type("test.ArraysAndListsWithElementTypes", supertypeName="uima.cas.TOP") ts.add_feature( type_=t, name=name, rangeTypeName=rangeTypeName, elementType=elementType, multipleReferencesAllowed=multipleReferencesAllowed, ) with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=UserWarning) with pytest.raises(ValueError, match=r".*\[{0}\].*".format(name)): merge_typesystems(base, ts)
def test_that_merging_types_with_different_incompatible_supertypes_throws(): ts1 = TypeSystem() ts1.create_type("test.Sub", description="Example type.", supertypeName="uima.cas.EmptyIntegerList") ts2 = TypeSystem() ts2.create_type("test.Sub", description="Example type.", supertypeName="uima.cas.NonEmptyStringList") with pytest.raises(ValueError, match=r".*incompatible super types.*"): merge_typesystems(ts1, ts2)
def test_type_can_create_instance_with_inherited_fields(): typesystem = TypeSystem() parent_type = typesystem.create_type(name="test.ParentType") typesystem.add_feature(type_=parent_type, name="parentFeature", rangeTypeName="String") child_type = typesystem.create_type(name="test.ChildType", supertypeName=parent_type.name) typesystem.add_feature(type_=child_type, name="childFeature", rangeTypeName="Integer") annotation = child_type(parentFeature="parent", childFeature="child") assert annotation.parentFeature == "parent" assert annotation.childFeature == "child"
def test_feature_adding_throws_if_already_existing(): typesystem = TypeSystem() test_type = typesystem.create_type(name="test.Type") typesystem.add_feature(type_=test_type, name="testFeature", rangeTypeName="String", description="A test feature") with pytest.raises(ValueError): typesystem.add_feature( type_=test_type, name="testFeature", rangeTypeName="String", description="A test feature" )
def write_sentence_documents(sentences: List[str], labels: List[str], path: Path, labeled=True): typesystem = TypeSystem() cas = Cas(typesystem=typesystem) SentenceType = typesystem.create_type( "de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence") SentimentType = typesystem.create_type("webanno.custom.Sentiment") typesystem.add_feature(type_=SentimentType, name="value", rangeTypeName="uima.cas.String") cas.sofa_string = " ".join(sentences) begin = 0 for sentence, label in zip(sentences, labels): end = begin + len(sentence) cas_sentence = SentenceType(begin=begin, end=end) sentiment_annotation = SentimentType(begin=begin, end=end, value=label) begin = end + 1 cas.add_annotation(cas_sentence) if labeled: cas.add_annotation(sentiment_annotation) cas.to_xmi(path, pretty_print=True) for sentence in cas.select(SENTENCE_TYPE): print(cas.get_covered_text(sentence))
def test_that_merging_compatible_typesystem_works(name, rangeTypeName, elementType, multipleReferencesAllowed): with open(typesystem_merge_base_path(), "r") as f: base = load_typesystem(f.read()) ts = TypeSystem() t = ts.create_type("test.ArraysAndListsWithElementTypes", supertypeName="uima.cas.TOP") ts.add_feature( type_=t, name=name, rangeTypeName=rangeTypeName, elementType=elementType, multipleReferencesAllowed=multipleReferencesAllowed, ) with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=UserWarning) result = merge_typesystems(base, ts) assert result.contains_type("test.ArraysAndListsWithElementTypes")
def test_that_merging_types_with_different_compatible_supertypes_works(): ts1 = TypeSystem() ts1.create_type("test.Sub", description="Example type.", supertypeName="uima.tcas.Annotation") ts2 = TypeSystem() ts2.create_type("test.Super", description="Example type.", supertypeName="uima.tcas.Annotation") ts2.create_type("test.Sub", description="Example type.", supertypeName="test.Super") result = merge_typesystems(ts1, ts2) sub = result.get_type("test.Sub") assert sub.supertypeName == "test.Super" # Also check the other order result = merge_typesystems(ts2, ts1) sub = result.get_type("test.Sub") assert sub.supertypeName == "test.Super"
def test_feature_adding_warns_if_redefined_identically(): typesystem = TypeSystem() test_type = typesystem.create_type(name="test.Type") typesystem.add_feature(type_=test_type, name="testFeature", rangeTypeName="String", description="A test feature") with pytest.warns(UserWarning): typesystem.add_feature(type_=test_type, name="testFeature", rangeTypeName="String", description="A test feature")
def test_feature_adding_throws_if_redefined_differently(): typesystem = TypeSystem() test_type = typesystem.create_type(name="test.Type") typesystem.add_feature(type_=test_type, name="testFeature", rangeTypeName="String", description="A test feature") with pytest.raises(ValueError): typesystem.add_feature(type_=test_type, name="testFeature", rangeTypeName="Boolean", description="A test feature")
def test_feature_can_be_added(): typesystem = TypeSystem() test_type = typesystem.create_type(name="test.Type") typesystem.add_feature(type_=test_type, name="testFeature", rangeTypeName="String", description="A test feature") actual_type = typesystem.get_type("test.Type") actual_feature = actual_type.get_feature("testFeature") assert actual_feature.name == "testFeature" assert actual_feature.rangeTypeName == "String" assert actual_feature.description == "A test feature"
def test_import_cas(document_collection, requests_mock): requests_mock.post( f"{API_BASE}/importer/projects/{PROJECT_NAME}/documentCollections/test-collection/documents", json={ "payload": { "original_document_name": "text1.xmi", "document_name": "text1.xmi" }, "errorMessages": [], }, ) cas = Cas(typesystem=TypeSystem()) result = document_collection.import_documents(cas, filename="text1.xmi") assert result[0]["document_name"] == "text1.xmi"
def test_is_collection_for_builtin_collections_with_elements(type_name: str): typesystem = TypeSystem() t = typesystem.get_type(type_name) feature = Feature("elements", rangeTypeName="uima.cas.TOP") assert typesystem.is_collection(type_name, feature) is True
def test_is_primitive(type_name: str, expected: bool): typesystem = TypeSystem() assert typesystem.is_primitive(type_name) is expected
def test_is_primitive_when_parent_is_primitive(): typesystem = TypeSystem() typesystem.create_type("test.string", "uima.cas.String") assert typesystem.is_primitive("test.string")
def test_is_primitive_collection(type_name: str, expected: bool): typesystem = TypeSystem() assert typesystem.is_primitive_collection(type_name) == expected
def test_subsumes(parent_name: str, child_name: str, expected: bool): ts = TypeSystem() assert ts.subsumes(parent_name, child_name) == expected