Search in sources :

Example 6 with TypeSystemDescription

use of org.apache.uima.resource.metadata.TypeSystemDescription in project webanno by webanno.

the class ComplexTypeTest method testProfType.

@Test
public void testProfType() throws Exception {
    TypeSystemDescription tsd = TypeSystemDescriptionFactory.createTypeSystemDescription("desc.types.TestTypeSystemDescriptor");
    CAS cas = CasCreationUtils.createCas(tsd, null, null);
    cas.setDocumentText("I listen to lectures by Prof. Gurevych sometimes.");
    TypeSystem ts = cas.getTypeSystem();
    Type profType = ts.getType("de.tud.Prof");
    Feature profNameFeature = profType.getFeatureByBaseName("fullName");
    Feature profBossFeature = profType.getFeatureByBaseName("boss");
    AnnotationFS proemel = cas.createAnnotation(profType, 0, 0);
    proemel.setStringValue(profNameFeature, "Hans Juergen Proeml");
    cas.addFsToIndexes(proemel);
    AnnotationFS gurevych = cas.createAnnotation(profType, 24, 38);
    gurevych.setStringValue(profNameFeature, "Iryna Gurevych");
    gurevych.setFeatureValue(profBossFeature, proemel);
    cas.addFsToIndexes(gurevych);
    /*
         * for (String feature : Arrays.asList("fullName", "boss")) { Feature someFeature =
         * gurevych.getType().getFeatureByBaseName(feature); if
         * (someFeature.getRange().isPrimitive()) { String value =
         * gurevych.getFeatureValueAsString(someFeature); System.out.println(value); } else {
         * FeatureStructure value = gurevych.getFeatureValue(someFeature);
         * System.out.printf("%s (%s)%n", value.getFeatureValueAsString(profNameFeature),
         * value.getType()); } }
         */
    ConstraintsGrammar parser = new ConstraintsGrammar(new FileInputStream("src/test/resources/rules/prof.rules"));
    Parse p = parser.Parse();
    ParsedConstraints constraints = p.accept(new ParserVisitor());
    Evaluator constraintsEvaluator = new ValuesGenerator();
    List<PossibleValue> possibleValues = constraintsEvaluator.generatePossibleValues(gurevych, "professorName", constraints);
    List<PossibleValue> exValues = new LinkedList<>();
    exValues.add(new PossibleValue("Iryna Gurevych", false));
    assertEquals(possibleValues, exValues);
}
Also used : TypeSystem(org.apache.uima.cas.TypeSystem) TypeSystemDescription(org.apache.uima.resource.metadata.TypeSystemDescription) Parse(de.tudarmstadt.ukp.clarin.webanno.constraints.grammar.syntaxtree.Parse) ParserVisitor(de.tudarmstadt.ukp.clarin.webanno.constraints.visitor.ParserVisitor) ParsedConstraints(de.tudarmstadt.ukp.clarin.webanno.constraints.model.ParsedConstraints) ValuesGenerator(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.ValuesGenerator) Evaluator(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.Evaluator) Feature(org.apache.uima.cas.Feature) FileInputStream(java.io.FileInputStream) LinkedList(java.util.LinkedList) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) CAS(org.apache.uima.cas.CAS) PossibleValue(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.PossibleValue) ConstraintsGrammar(de.tudarmstadt.ukp.clarin.webanno.constraints.grammar.ConstraintsGrammar) Test(org.junit.Test)

Example 7 with TypeSystemDescription

use of org.apache.uima.resource.metadata.TypeSystemDescription in project webanno by webanno.

the class AnnotationSchemaServiceImpl method getProjectTypes.

@Override
public TypeSystemDescription getProjectTypes(Project aProject) {
    // Create a new type system from scratch
    TypeSystemDescription tsd = new TypeSystemDescription_impl();
    for (AnnotationLayer type : listAnnotationLayer(aProject)) {
        if (type.getType().equals(SPAN_TYPE) && !type.isBuiltIn()) {
            TypeDescription td = tsd.addType(type.getName(), "", CAS.TYPE_NAME_ANNOTATION);
            generateFeatures(tsd, td, type);
        } else if (type.getType().equals(RELATION_TYPE) && !type.isBuiltIn()) {
            TypeDescription td = tsd.addType(type.getName(), "", CAS.TYPE_NAME_ANNOTATION);
            AnnotationLayer attachType = type.getAttachType();
            td.addFeature(WebAnnoConst.FEAT_REL_TARGET, "", attachType.getName());
            td.addFeature(WebAnnoConst.FEAT_REL_SOURCE, "", attachType.getName());
            generateFeatures(tsd, td, type);
        } else if (type.getType().equals(CHAIN_TYPE) && !type.isBuiltIn()) {
            TypeDescription tdChains = tsd.addType(type.getName() + "Chain", "", CAS.TYPE_NAME_ANNOTATION_BASE);
            tdChains.addFeature("first", "", type.getName() + "Link");
            // Custom features on chain layers are currently not supported
            // generateFeatures(tsd, tdChains, type);
            TypeDescription tdLink = tsd.addType(type.getName() + "Link", "", CAS.TYPE_NAME_ANNOTATION);
            tdLink.addFeature("next", "", type.getName() + "Link");
            tdLink.addFeature("referenceType", "", CAS.TYPE_NAME_STRING);
            tdLink.addFeature("referenceRelation", "", CAS.TYPE_NAME_STRING);
        }
    }
    return tsd;
}
Also used : TypeSystemDescription_impl(org.apache.uima.resource.metadata.impl.TypeSystemDescription_impl) TypeSystemDescription(org.apache.uima.resource.metadata.TypeSystemDescription) TypeDescription(org.apache.uima.resource.metadata.TypeDescription) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)

Example 8 with TypeSystemDescription

use of org.apache.uima.resource.metadata.TypeSystemDescription in project webanno by webanno.

the class AnnotationSchemaServiceImpl method upgradeCas.

@Override
public void upgradeCas(CAS aCas, SourceDocument aSourceDocument, String aUser) throws UIMAException, IOException {
    TypeSystemDescription builtInTypes = TypeSystemDescriptionFactory.createTypeSystemDescription();
    TypeSystemDescription projectTypes = getProjectTypes(aSourceDocument.getProject());
    TypeSystemDescription allTypes = CasCreationUtils.mergeTypeSystems(asList(projectTypes, builtInTypes));
    // Prepare template for new CAS
    CAS newCas = JCasFactory.createJCas(allTypes).getCas();
    CASCompleteSerializer serializer = Serialization.serializeCASComplete((CASImpl) newCas);
    // Save old type system
    TypeSystem oldTypeSystem = aCas.getTypeSystem();
    // Save old CAS contents
    ByteArrayOutputStream os2 = new ByteArrayOutputStream();
    Serialization.serializeWithCompression(aCas, os2, oldTypeSystem);
    // Prepare CAS with new type system
    Serialization.deserializeCASComplete(serializer, (CASImpl) aCas);
    // Restore CAS data to new type system
    Serialization.deserializeCAS(aCas, new ByteArrayInputStream(os2.toByteArray()), oldTypeSystem, null);
    // Make sure JCas is properly initialized too
    aCas.getJCas();
    try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID, String.valueOf(aSourceDocument.getProject().getId()))) {
        Project project = aSourceDocument.getProject();
        log.info("Upgraded CAS of user [{}] for " + "document [{}]({}) in project [{}]({})", aUser, aSourceDocument.getName(), aSourceDocument.getId(), project.getName(), project.getId());
    }
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) TypeSystem(org.apache.uima.cas.TypeSystem) TypeSystemDescription(org.apache.uima.resource.metadata.TypeSystemDescription) CAS(org.apache.uima.cas.CAS) ByteArrayInputStream(java.io.ByteArrayInputStream) CASCompleteSerializer(org.apache.uima.cas.impl.CASCompleteSerializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MDC(org.slf4j.MDC)

Example 9 with TypeSystemDescription

use of org.apache.uima.resource.metadata.TypeSystemDescription in project webanno by webanno.

the class WebAnnoTsv3WriterTestBase method testTwoSentencesWithNoSpaceInBetween.

@Test
public void testTwoSentencesWithNoSpaceInBetween() throws Exception {
    TypeSystemDescription global = TypeSystemDescriptionFactory.createTypeSystemDescription();
    TypeSystemDescription local = TypeSystemDescriptionFactory.createTypeSystemDescriptionFromPath("src/test/resources/desc/type/webannoTestTypes.xml");
    TypeSystemDescription merged = CasCreationUtils.mergeTypeSystems(asList(global, local));
    JCas jcas = JCasFactory.createJCas(merged);
    DocumentMetaData.create(jcas).setDocumentId("doc");
    jcas.setDocumentText("onetwo");
    new Token(jcas, 0, 3).addToIndexes();
    new Sentence(jcas, 0, 3).addToIndexes();
    new Token(jcas, 3, 6).addToIndexes();
    new Sentence(jcas, 3, 6).addToIndexes();
    writeAndAssertEquals(jcas);
}
Also used : TypeSystemDescription(org.apache.uima.resource.metadata.TypeSystemDescription) JCas(org.apache.uima.jcas.JCas) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence) Test(org.junit.Test)

Example 10 with TypeSystemDescription

use of org.apache.uima.resource.metadata.TypeSystemDescription in project webanno by webanno.

the class WebAnnoTsv3XReaderWriterRoundTripTest method runTest.

@Test
public void runTest() throws Exception {
    TypeSystemDescription global = TypeSystemDescriptionFactory.createTypeSystemDescription();
    TypeSystemDescription local;
    if (new File(referenceFolder, "typesystem.xml").exists()) {
        local = TypeSystemDescriptionFactory.createTypeSystemDescriptionFromPath(new File(referenceFolder, "typesystem.xml").toString());
    } else {
        local = TypeSystemDescriptionFactory.createTypeSystemDescriptionFromPath("src/test/resources/desc/type/webannoTestTypes.xml");
    }
    TypeSystemDescription merged = CasCreationUtils.mergeTypeSystems(asList(global, local));
    String targetFolder = "target/test-output/WebAnnoTsv3XReaderWriterRoundTripTest/" + referenceFolder.getName();
    CollectionReaderDescription reader = createReaderDescription(WebannoTsv3XReader.class, merged, WebannoTsv3XReader.PARAM_SOURCE_LOCATION, referenceFolder, WebannoTsv3XReader.PARAM_PATTERNS, "reference.tsv");
    AnalysisEngineDescription checker = createEngineDescription(DKProCoreConventionsChecker.class);
    AnalysisEngineDescription tsvWriter = createEngineDescription(WebannoTsv3XWriter.class, merged, WebannoTsv3XWriter.PARAM_TARGET_LOCATION, targetFolder, WebannoTsv3XWriter.PARAM_STRIP_EXTENSION, true);
    AnalysisEngineDescription xmiWriter = createEngineDescription(XmiWriter.class, merged, XmiWriter.PARAM_TARGET_LOCATION, targetFolder, XmiWriter.PARAM_STRIP_EXTENSION, true);
    SimplePipeline.runPipeline(reader, checker, tsvWriter, xmiWriter);
    String referenceTsv = FileUtils.readFileToString(new File(referenceFolder, "reference.tsv"), "UTF-8");
    String actualTsv = FileUtils.readFileToString(new File(targetFolder, "reference.tsv"), "UTF-8");
    // 
    // The XMI files here are not compared semantically but using their serialization which
    // is subject to minor variations depending e.g. on the order in which annotation are
    // created in the CAS. Thus, this code is commented out and should only be used on a
    // case-by-case base to compare XMIs during development.
    // 
    // String referenceXmi = FileUtils.readFileToString(new File(referenceFolder,
    // "reference.xmi"),
    // "UTF-8");
    // 
    // String actualXmi = FileUtils.readFileToString(new File(targetFolder, "reference.xmi"),
    // "UTF-8");
    assertEquals(referenceTsv, actualTsv);
// assertEquals(referenceXmi, actualXmi);
}
Also used : CollectionReaderDescription(org.apache.uima.collection.CollectionReaderDescription) TypeSystemDescription(org.apache.uima.resource.metadata.TypeSystemDescription) AnalysisEngineDescription(org.apache.uima.analysis_engine.AnalysisEngineDescription) File(java.io.File) Test(org.junit.Test)

Aggregations

TypeSystemDescription (org.apache.uima.resource.metadata.TypeSystemDescription)34 Test (org.junit.Test)23 JCas (org.apache.uima.jcas.JCas)13 ArrayList (java.util.ArrayList)11 TypeSystemDescriptionFactory.createTypeSystemDescription (org.apache.uima.fit.factory.TypeSystemDescriptionFactory.createTypeSystemDescription)10 SoftAssertions (org.assertj.core.api.SoftAssertions)9 CAS (org.apache.uima.cas.CAS)8 Type (org.apache.uima.cas.Type)7 Token (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token)6 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)6 DiffResult (de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.DiffResult)5 AnnotationLayer (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)5 Arrays.asList (java.util.Arrays.asList)5 List (java.util.List)5 SpanDiffAdapter (de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.SpanDiffAdapter)4 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)4 Sentence (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)4 TypeSystem (org.apache.uima.cas.TypeSystem)4 TypeDescription (org.apache.uima.resource.metadata.TypeDescription)4 ArcDiffAdapter (de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.ArcDiffAdapter)3