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 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
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
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"
Exemplo n.º 5
0
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"
Exemplo n.º 6
0
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
Exemplo n.º 7
0
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
Exemplo n.º 8
0
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"
Exemplo n.º 9
0
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"
        )
Exemplo n.º 10
0
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"
Exemplo n.º 11
0
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")
Exemplo n.º 12
0
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")
Exemplo n.º 13
0
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)
Exemplo n.º 14
0
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")
Exemplo n.º 15
0
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"
Exemplo n.º 16
0
def test_is_primitive_when_parent_is_primitive():
    typesystem = TypeSystem()
    typesystem.create_type("test.string", "uima.cas.String")

    assert typesystem.is_primitive("test.string")