Search in sources :

Example 1 with TypeSystem

use of org.apache.uima.cas.TypeSystem in project webanno by webanno.

the class Tsv3XDeserializer method parseColumnDeclaration.

private TsvColumn parseColumnDeclaration(JCas aJCas, LayerType aLayerType, Type aUimaType, int aIndex, String aColDecl, TsvColumn aPrevCol) throws IOException {
    TypeSystem ts = aJCas.getTypeSystem();
    TsvColumn column;
    // SLOT_ROLE - starts with "ROLE_"
    if (SPAN.equals(aLayerType) && startsWith(aColDecl, HEADER_PREFIX_ROLE)) {
        String[] subFields = splitPreserveAllTokens(aColDecl, '_');
        String featureName = substringAfter(subFields[1], ":");
        Feature feat = aUimaType.getFeatureByBaseName(featureName);
        if (feat == null) {
            throw new IOException("CAS type [" + aUimaType.getName() + "] does not have a feature called [" + featureName + "]");
        }
        column = new TsvColumn(aIndex, aUimaType, aLayerType, featureName, SLOT_ROLE);
        String typeName = subFields[2];
        Type type = ts.getType(typeName);
        if (type == null) {
            throw new IOException("CAS does not contain a type called [" + typeName + "]");
        }
        column.setTargetTypeHint(type);
    } else // RELATION_REF - starts with "BT_
    if (RELATION.equals(aLayerType) && startsWith(aColDecl, HEADER_PREFIX_BASE_TYPE)) {
        column = new TsvColumn(aIndex, aUimaType, aLayerType, FEAT_REL_SOURCE, RELATION_REF);
        String typeName = substringAfter(aColDecl, HEADER_PREFIX_BASE_TYPE);
        Type type = ts.getType(typeName);
        if (type == null) {
            throw new IOException("CAS does not contain a type called [" + typeName + "]");
        }
        column.setTargetTypeHint(type);
    } else // CHAIN_ELEMENT_TYPE - "referenceType"
    if (CHAIN.equals(aLayerType) && COREFERENCE_TYPE_FEATURE.equals(aColDecl)) {
        column = new TsvColumn(aIndex, aUimaType, aLayerType, COREFERENCE_TYPE_FEATURE, CHAIN_ELEMENT_TYPE);
    } else // CHAIN_LINK_TYPE - "referenceRelation"
    if (CHAIN.equals(aLayerType) && COREFERENCE_RELATION_FEATURE.equals(aColDecl)) {
        column = new TsvColumn(aIndex, aUimaType, aLayerType, COREFERENCE_RELATION_FEATURE, CHAIN_LINK_TYPE);
    } else // SLOT_TARGET - name of the link target type
    if (SPAN.equals(aLayerType) && aColDecl.contains(".") || ts.getType(aColDecl) != null) {
        // the type name really exists in the target CAS.
        if (ts.getType(aColDecl) == null) {
            throw new IOException("CAS type system does not contain a type named [" + aColDecl + "]");
        }
        // name from it.
        if (aPrevCol == null || !SLOT_ROLE.equals(aPrevCol.featureType)) {
            throw new IOException("Slot target column declaration must follow slot role column declaration");
        }
        column = new TsvColumn(aIndex, aUimaType, aLayerType, aPrevCol.uimaFeature.getShortName(), SLOT_TARGET);
        Type type = ts.getType(aColDecl);
        if (type == null) {
            throw new IOException("CAS does not contain a type called [" + aColDecl + "]");
        }
        column.setTargetTypeHint(type);
    } else // PRIMITIVE - feature name
    if (aUimaType.getFeatureByBaseName(aColDecl) != null) {
        column = new TsvColumn(aIndex, aUimaType, aLayerType, aColDecl, PRIMITIVE);
    } else {
        throw new IOException("Type [" + aUimaType.getName() + "] does not contain a feature called [" + aColDecl + "]");
    }
    return column;
}
Also used : TypeSystem(org.apache.uima.cas.TypeSystem) Type(org.apache.uima.cas.Type) LayerType(de.tudarmstadt.ukp.clarin.webanno.tsv.internal.tsv3x.model.LayerType) TsvColumn(de.tudarmstadt.ukp.clarin.webanno.tsv.internal.tsv3x.model.TsvColumn) IOException(java.io.IOException) FSUtil.setFeature(org.apache.uima.fit.util.FSUtil.setFeature) FSUtil.getFeature(org.apache.uima.fit.util.FSUtil.getFeature) Feature(org.apache.uima.cas.Feature)

Example 2 with TypeSystem

use of org.apache.uima.cas.TypeSystem in project webanno by webanno.

the class CasDoctorUtils method getNonIndexedFSes.

public static Set<FeatureStructure> getNonIndexedFSes(CAS aCas) {
    TypeSystem ts = aCas.getTypeSystem();
    Set<FeatureStructure> allIndexedFS = collectIndexed(aCas);
    Set<FeatureStructure> allReachableFS = collectReachable(aCas);
    // Remove all that are indexed
    allReachableFS.removeAll(allIndexedFS);
    // Remove all that are not annotations
    allReachableFS.removeIf(fs -> !ts.subsumes(aCas.getAnnotationType(), fs.getType()));
    // All that is left are non-index annotations
    return allReachableFS;
}
Also used : FeatureStructure(org.apache.uima.cas.FeatureStructure) TypeSystem(org.apache.uima.cas.TypeSystem)

Example 3 with TypeSystem

use of org.apache.uima.cas.TypeSystem in project webanno by webanno.

the class ComplexTypeTest method testCountryType.

@Test
public void testCountryType() throws Exception {
    TypeSystemDescription tsd = TypeSystemDescriptionFactory.createTypeSystemDescription("desc.types.TestTypeSystemDescriptor");
    CAS cas = CasCreationUtils.createCas(tsd, null, null);
    cas.setDocumentText("Asia is the largest continent on Earth. Asia is subdivided into 48 countries, two of them (Russia and Turkey) having part of their land in Europe. The most active place on Earth for tropical cyclone activity lies northeast of the Philippines and south of Japan. The Gobi Desert is in Mongolia and the Arabian Desert stretches across much of the Middle East. The Yangtze River in China is the longest river in the continent. The Himalayas between Nepal and China is the tallest mountain range in the world. Tropical rainforests stretch across much of southern Asia and coniferous and deciduous forests lie farther north.");
    TypeSystem ts = cas.getTypeSystem();
    Type continentType = ts.getType("de.Continent");
    Feature continentName = continentType.getFeatureByBaseName("name");
    AnnotationFS asiaContinent = cas.createAnnotation(continentType, 0, 4);
    asiaContinent.setStringValue(continentName, "Asia");
    cas.addFsToIndexes(asiaContinent);
    Type countryType = ts.getType("de.Country");
    Feature countryName = countryType.getFeatureByBaseName("name");
    AnnotationFS russia = cas.createAnnotation(countryType, 56, 62);
    russia.setStringValue(countryName, "Russian Federation");
    Feature continentFeature = countryType.getFeatureByBaseName("continent");
    russia.setFeatureValue(continentFeature, asiaContinent);
    cas.addFsToIndexes(russia);
    ConstraintsGrammar parser = new ConstraintsGrammar(new FileInputStream("src/test/resources/rules/region.rules"));
    Parse p = parser.Parse();
    ParsedConstraints constraints = p.accept(new ParserVisitor());
    Evaluator constraintsEvaluator = new ValuesGenerator();
    List<PossibleValue> possibleValues = constraintsEvaluator.generatePossibleValues(russia, "regionType", constraints);
    List<PossibleValue> exValues = new LinkedList<>();
    exValues.add(new PossibleValue("cold", true));
    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 4 with TypeSystem

use of org.apache.uima.cas.TypeSystem 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 5 with TypeSystem

use of org.apache.uima.cas.TypeSystem 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)

Aggregations

TypeSystem (org.apache.uima.cas.TypeSystem)12 CAS (org.apache.uima.cas.CAS)7 Feature (org.apache.uima.cas.Feature)7 Type (org.apache.uima.cas.Type)7 TypeSystemDescription (org.apache.uima.resource.metadata.TypeSystemDescription)6 ArrayList (java.util.ArrayList)4 FeatureStructure (org.apache.uima.cas.FeatureStructure)4 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)4 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)3 Arrays.asList (java.util.Arrays.asList)3 List (java.util.List)3 Map (java.util.Map)3 WebAnnoConst (de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst)2 CHAIN_TYPE (de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.CHAIN_TYPE)2 FEAT_REL_SOURCE (de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.FEAT_REL_SOURCE)2 FEAT_REL_TARGET (de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.FEAT_REL_TARGET)2 RELATION_TYPE (de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.RELATION_TYPE)2 SPAN_TYPE (de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.SPAN_TYPE)2 Evaluator (de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.Evaluator)2 PossibleValue (de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.PossibleValue)2