org.apache.uima.fit.util.CasUtil Java Examples

The following examples show how to use org.apache.uima.fit.util.CasUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CasMetadataUtils.java    From webanno with Apache License 2.0 6 votes vote down vote up
public static void clearCasMetadata(CAS aCas) throws IllegalStateException
{
    // If the type system of the CAS does not yet support CASMetadata, then we do not add it
    // and wait for the next regular CAS upgrade before we include this data.
    if (aCas.getTypeSystem().getType(CASMetadata.class.getName()) == null) {
        return;
    }
    
    List<AnnotationFS> cmds = new ArrayList<>(
            CasUtil.select(aCas, getType(aCas, CASMetadata.class)));
    if (cmds.size() > 1) {
        throw new IllegalStateException("CAS contains more than one CASMetadata instance");
    }

    cmds.forEach(aCas::removeFsFromIndexes);
}
 
Example #2
Source File: CasMergeTest.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Test
public void simpleCopyToDiffExistingAnnoWithNoStackingTest()
    throws Exception
{
    CAS jcas = createJCas().getCas();
    Type type = jcas.getTypeSystem().getType(POS.class.getTypeName());
    AnnotationFS clickedFs = createPOSAnno(jcas, "NN", 0, 0);

    CAS mergeCAs = createJCas().getCas();
    AnnotationFS existingFs = mergeCAs.createAnnotation(type, 0, 0);
    Feature posValue = type.getFeatureByBaseName("PosValue");
    existingFs.setStringValue(posValue, "NE");
    mergeCAs.addFsToIndexes(existingFs);

    sut.mergeSpanAnnotation(null, null, posLayer, mergeCAs, clickedFs, false);

    assertEquals(1, CasUtil.selectCovered(mergeCAs, type, 0, 0).size());
}
 
Example #3
Source File: SuggestionBuilder.java    From webanno with Apache License 2.0 6 votes vote down vote up
/**
 * Puts CASes into a list and get a random annotation document that will be used as a base for
 * the diff.
 */
private void updateSegment(AnnotatorState aBratAnnotatorModel,
        Map<Integer, Integer> aIdxSentenceBeginEnd,
        Map<Integer, Integer> aIdxSentenceBeginNumber,
        Map<String, Map<Integer, Integer>> aSegmentAdress, CAS aCas, String aUsername,
        int aWindowStart, int aWindowEnd)
{
    diffRangeBegin = aWindowStart;
    diffRangeEnd = aWindowEnd;

    // Get the number of the first sentence - instead of fetching the number over and over
    // we can just increment this one.
    int sentenceNumber = WebAnnoCasUtil.getSentenceNumber(aCas, diffRangeBegin);

    aSegmentAdress.put(aUsername, new HashMap<>());
    Type sentenceType = CasUtil.getType(aCas, Sentence.class);
    for (AnnotationFS sentence : selectCovered(aCas, sentenceType, diffRangeBegin,
            diffRangeEnd)) {
        aIdxSentenceBeginEnd.put(sentence.getBegin(), sentence.getEnd());
        aIdxSentenceBeginNumber.put(sentence.getBegin(), sentenceNumber);
        aSegmentAdress.get(aUsername).put(sentence.getBegin(), getAddr(sentence));
        sentenceNumber += 1;
    }
}
 
Example #4
Source File: BratRenderer.java    From webanno with Apache License 2.0 6 votes vote down vote up
public static void renderTokens(CAS aCas, GetDocumentResponse aResponse, AnnotatorState aState)
{
    int winBegin = aState.getWindowBeginOffset();
    int winEnd = aState.getWindowEndOffset();
    Type tokenType = CasUtil.getType(aCas, Token.class);

    List<AnnotationFS> tokens = selectCovered(aCas, tokenType, winBegin, winEnd);
    for (AnnotationFS fs : tokens) {
        // attach type such as POS adds non-existing token element for ellipsis annotation
        if (fs.getBegin() == fs.getEnd()) {
            continue;
        }
        
        split(aResponse.getSentenceOffsets(), fs.getCoveredText(), fs.getBegin() - winBegin,
                fs.getEnd() - winBegin)                    
                .forEach(range -> {
                    aResponse.addToken(range.getBegin(), range.getEnd());
                    if (DEBUG) {
                        aResponse.addEntity(new Entity(new VID(fs), "Token",
                                new Offsets(range.getBegin(), range.getEnd()),
                                fs.getCoveredText(), "#d9d9d9",
                                "[" + fs.getBegin() + "-" + fs.getEnd() + "]"));
                    }
                });
    }
}
 
Example #5
Source File: AnnotationDetailEditorPanel.java    From webanno with Apache License 2.0 6 votes vote down vote up
private static Set<AnnotationFS> getAttachedSpans(AnnotationSchemaService aAS, AnnotationFS aFs,
        AnnotationLayer aLayer)
{
    CAS cas = aFs.getCAS();
    Set<AnnotationFS> attachedSpans = new HashSet<>();
    TypeAdapter adapter = aAS.getAdapter(aLayer);
    if (adapter instanceof SpanAdapter && aLayer.getAttachType() != null) {
        Type spanType = CasUtil.getType(cas, aLayer.getAttachType().getName());
        Feature attachFeature = spanType.getFeatureByBaseName(aLayer.getAttachFeature()
            .getName());
        final Type type = spanType;

        for (AnnotationFS attachedFs : selectAt(cas, type, aFs.getBegin(), aFs.getEnd())) {
            if (isSame(attachedFs.getFeatureValue(attachFeature), aFs)) {
                attachedSpans.add(attachedFs);
            }
        }
    }
    return attachedSpans;
}
 
Example #6
Source File: SpanAdapter.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Override
public void delete(SourceDocument aDocument, String aUsername, CAS aCas, VID aVid)
{
    AnnotationFS fs = selectByAddr(aCas, AnnotationFS.class, aVid.getId());
    aCas.removeFsFromIndexes(fs);

    // delete associated attachFeature
    if (getAttachTypeName() != null) {
        Type theType = CasUtil.getType(aCas, getAttachTypeName());
        Feature attachFeature = theType.getFeatureByBaseName(getAttachFeatureName());
        if (attachFeature != null) {
            CasUtil.selectCovered(aCas, theType, fs.getBegin(), fs.getEnd()).get(0)
                    .setFeatureValue(attachFeature, null);
        }
    }
    
    publishEvent(new SpanDeletedEvent(this, aDocument, aUsername, getLayer(), fs));
}
 
Example #7
Source File: CasAssert.java    From inception with Apache License 2.0 6 votes vote down vote up
public CasAssert containsNamedEntity(String text, String value)
{
    isNotNull();

    Type type = CasUtil.getType(actual, TYPE_NE);
    for (AnnotationFS annotation : CasUtil.select(actual, type)) {
        if (annotation.getCoveredText().equals(text) &&
            FSUtil.getFeature(annotation, "value", String.class).equals(value)) {
            return this;
        }
    }

    failWithMessage("No named entity with text <%s> and value <%s> found", text, value);

    return this;
}
 
Example #8
Source File: RemoteStringMatchingNerRecommender.java    From inception with Apache License 2.0 6 votes vote down vote up
public String predict(String aPredictionRequestJson) throws IOException, UIMAException,
    SAXException, RecommendationException
{
    PredictionRequest request = deserializePredictionRequest(aPredictionRequestJson);
    CAS cas = deserializeCas(request.getDocument().getXmi(), request.getTypeSystem());

    // Only work on real annotations, not on predictions
    Type predictedType = CasUtil.getType(cas, recommender.getLayer().getName());
    Feature feature = predictedType.getFeatureByBaseName(FEATURE_NAME_IS_PREDICTION);

    for (AnnotationFS fs : CasUtil.select(cas, predictedType)) {
        if (fs.getBooleanValue(feature)) {
            cas.removeFsFromIndexes(fs);
        }
    }

    recommendationEngine.predict(context, cas);

    return buildPredictionResponse(cas);
}
 
Example #9
Source File: ChainAdapter.java    From webanno with Apache License 2.0 6 votes vote down vote up
/**
 * Find the chain head for the given link.
 *
 * @param aCas the CAS.
 * @param aLink the link to search the chain for.
 * @return the chain.
 */
private FeatureStructure getChainForLink(CAS aCas, AnnotationFS aLink)
{
    Type chainType = CasUtil.getType(aCas, getChainTypeName());

    for (FeatureStructure chainFs : selectFS(aCas, chainType)) {
        AnnotationFS linkFs = getFirstLink(chainFs);

        // Now we seek the link within the current chain
        while (linkFs != null) {
            if (WebAnnoCasUtil.isSame(linkFs, aLink)) {
                return chainFs;
            }
            linkFs = getNextLink(linkFs);
        }
    }

    // This should never happen unless the data in the CAS has been created wrongly
    throw new IllegalArgumentException("Link not part of any chain");
}
 
Example #10
Source File: TrainingTask.java    From inception with Apache License 2.0 6 votes vote down vote up
private boolean containsTargetTypeAndFeature(Recommender aRecommender, CAS aCas)
{
    Type type;
    try {
        type = CasUtil.getType(aCas, aRecommender.getLayer().getName());
    }
    catch (IllegalArgumentException e ) {
        // If the CAS does not contain the target type at all, then it cannot contain any
        // annotations of that type.
        return false;
    }
    
    if (type.getFeatureByBaseName(aRecommender.getFeature().getName()) == null) {
        // If the CAS does not contain the target feature, then there won't be any training
        // data.
        return false;            
    }
    
    return CasUtil.iterator(aCas, type).hasNext();
}
 
Example #11
Source File: StringMatchingRecommenderTest.java    From inception with Apache License 2.0 6 votes vote down vote up
private CAS getTestCasNoLabelLabels() throws Exception
{
    try {
        Dataset ds = loader.load("germeval2014-de", CONTINUE);
        CAS cas = loadData(ds, ds.getDataFiles()[0]).get(0);
        Type neType = CasUtil.getAnnotationType(cas, NamedEntity.class);
        Feature valFeature = neType.getFeatureByBaseName("value");
        select(cas.getJCas(), NamedEntity.class)
                .forEach(ne -> ne.setFeatureValueFromString(valFeature, null));

        return cas;
    }
    catch (Exception e) {
        // Workaround for https://github.com/dkpro/dkpro-core/issues/1469
        assumeThat(e).isNotInstanceOf(FileNotFoundException.class);
        throw e;
    }
}
 
Example #12
Source File: WebAnnoCasUtil.java    From webanno with Apache License 2.0 6 votes vote down vote up
public static AnnotationFS getNextToken(CAS aCas, int aBegin, int aEnd)
{
    Type tokenType = getType(aCas, Token.class);
    
    AnnotationFS currentToken = selectAt(aCas, tokenType, aBegin, aEnd).stream().findFirst()
            .orElse(null);
    // thid happens when tokens such as Dr. OR Ms. selected with double
    // click, which make seletected text as Dr OR Ms
    if (currentToken == null) {
        currentToken = selectAt(aCas, tokenType, aBegin, aEnd + 1).stream().findFirst()
                .orElse(null);
    }
    AnnotationFS nextToken = null;

    for (AnnotationFS token : CasUtil.selectFollowing(aCas, tokenType, currentToken, 1)) {
        nextToken = token;
    }

    return nextToken;
}
 
Example #13
Source File: DataMajorityNerRecommender.java    From inception with Apache License 2.0 6 votes vote down vote up
private List<Annotation> extractAnnotations(List<CAS> aCasses)
{
    List<Annotation> annotations = new ArrayList<>();

    for (CAS cas : aCasses) {
        Type annotationType = CasUtil.getType(cas, layerName);
        Feature predictedFeature = annotationType.getFeatureByBaseName(featureName);

        for (AnnotationFS ann : CasUtil.select(cas, annotationType)) {
            String label = ann.getFeatureValueAsString(predictedFeature);
            if (isNotEmpty(label)) {
                annotations.add(new Annotation(label, ann.getBegin(), ann.getEnd()));
            }
        }
    }

    return annotations;
}
 
Example #14
Source File: CasMetadataUtils.java    From webanno with Apache License 2.0 5 votes vote down vote up
public static void failOnConcurrentModification(CAS aCas, File aCasFile,
        SourceDocument aDocument, String aUsername)
    throws IOException
{
    // If the type system of the CAS does not yet support CASMetadata, then we do not add it
    // and wait for the next regular CAS upgrade before we include this data.
    if (aCas.getTypeSystem().getType(CASMetadata.class.getName()) == null) {
        LOG.info("Annotation file [{}] of user [{}] for document [{}]({}) in project [{}]({}) "
                + "does not support CASMetadata yet - unable to detect concurrent modifications",
                aCasFile.getName(), aUsername, aDocument.getName(),
                aDocument.getId(), aDocument.getProject().getName(),
                aDocument.getProject().getId());
        return;
    }
    
    List<AnnotationFS> cmds = new ArrayList<>(
            CasUtil.select(aCas, getType(aCas, CASMetadata.class)));
    if (cmds.size() > 1) {
        throw new IOException("CAS contains more than one CASMetadata instance");
    }
    else if (cmds.size() == 1) {
        AnnotationFS cmd = cmds.get(0);
        long lastChangedOnDisk = FSUtil.getFeature(cmd, "lastChangedOnDisk", Long.class);
        if (aCasFile.lastModified() != lastChangedOnDisk) {
            throw new IOException(
                    "Detected concurrent modification to file in storage (expected timestamp: "
                            + lastChangedOnDisk + "; actual timestamp in storage "
                            + aCasFile.lastModified() + ") - "
                            + "please try reloading before saving again.");
        }
    }
    else {
        LOG.info(
                "Annotation file [{}] of user [{}] for document [{}]({}) in project "
                        + "[{}]({}) does not support CASMetadata yet - unable to check for "
                        + "concurrent modifications",
                aCasFile.getName(), aUsername, aDocument.getName(), aDocument.getId(),
                aDocument.getProject().getName(), aDocument.getProject().getId());
    }
}
 
Example #15
Source File: WebAnnoCasUtil.java    From webanno with Apache License 2.0 5 votes vote down vote up
public static <T extends AnnotationFS> AnnotationFS selectByAddr(CAS aCas, Class<T> aType,
        int aAddress)
{
    // Check that the type passed is actually an annotation type
    CasUtil.getAnnotationType(aCas, aType);
    
    return aCas.getLowLevelCAS().ll_getFSForRef(aAddress);
}
 
Example #16
Source File: LocalFeaturesTcAnnotator.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
private void addTCSequenceAnnotation(JCas jcas)
{
    Type type = jcas.getCas().getTypeSystem().getType(nameSequence);

    Collection<AnnotationFS> sequenceAnnotation = CasUtil.select(jcas.getCas(), type);
    for (AnnotationFS seq : sequenceAnnotation) {
        TextClassificationSequence tcs = new TextClassificationSequence(jcas, seq.getBegin(),
                seq.getEnd());
        tcs.addToIndexes();
    }
}
 
Example #17
Source File: AutomationUtil.java    From webanno with Apache License 2.0 5 votes vote down vote up
public static void clearAnnotations(CAS aCas, AnnotationFeature aFeature)
    throws IOException
{
    // Check if annotation layer is attached to another layer
    String attachTypeName = aFeature.getLayer().getAttachType() == null ? null : aFeature
            .getLayer().getAttachType().getName();
    Type attachType = null;
    Feature attachFeature = null;
    if (attachTypeName != null) {
        attachType = CasUtil.getType(aCas, attachTypeName);
        attachFeature = attachType
                .getFeatureByBaseName(aFeature.getLayer().getAttachFeature().getName());
    }
    
    List<AnnotationFS> annotationsToRemove = new ArrayList<>();
    Type type = CasUtil.getType(aCas, aFeature.getLayer().getName());
    annotationsToRemove.addAll(select(aCas, type));
    
    for (AnnotationFS annotation : annotationsToRemove) {
        if (attachFeature != null) {
            // Unattach the annotation to be removed
            for (AnnotationFS attach : selectCovered(attachType, annotation)) {
                FeatureStructure existing = attach.getFeatureValue(attachFeature);
                if (annotation.equals(existing)) {
                    attach.setFeatureValue(attachFeature, null);
                }
            }
        }
        aCas.removeFsFromIndexes(annotation);
    }
}
 
Example #18
Source File: SpanAdapter.java    From webanno with Apache License 2.0 5 votes vote down vote up
/**
 * A Helper method to add annotation to CAS
 */
private AnnotationFS createSpanAnnotation(CAS aCas, int aBegin, int aEnd)
    throws AnnotationException
{
    Type type = CasUtil.getType(aCas, getAnnotationTypeName());
    AnnotationFS newAnnotation = aCas.createAnnotation(type, aBegin, aEnd);

    log.trace("Created span annotation {}-{} [{}]", newAnnotation.getBegin(),
            newAnnotation.getEnd(), newAnnotation.getCoveredText());

    // If if the layer attaches to a feature, then set the attach-feature to the newly
    // created annotation.
    if (getAttachFeatureName() != null) {
        Type theType = CasUtil.getType(aCas, getAttachTypeName());
        Feature attachFeature = theType.getFeatureByBaseName(getAttachFeatureName());
        if (CasUtil.selectCovered(aCas, theType, aBegin, aEnd).isEmpty()) {
            throw new IllegalPlacementException("No annotation of type [" + getAttachTypeName()
                    + "] to attach to at location [" + aBegin + "-" + aEnd + "].");
        }
        CasUtil.selectCovered(aCas, theType, aBegin, aEnd).get(0)
                .setFeatureValue(attachFeature, newAnnotation);
    }
    
    aCas.addFsToIndexes(newAnnotation);
    
    return newAnnotation;
}
 
Example #19
Source File: AutomationUtil.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Deprecated
private static void deleteSpanAnnotation(TypeAdapter aAdapter, AnnotatorState aState,
        CAS aCas, AnnotationFeature aFeature, int aBegin, int aEnd, Object aValue)
{
    Type type = CasUtil.getType(aCas, aAdapter.getAnnotationTypeName());
    for (AnnotationFS fs : CasUtil.selectCovered(aCas, type, aBegin, aEnd)) {
        if (fs.getBegin() == aBegin && fs.getEnd() == aEnd) {
            if (ObjectUtils.equals(aAdapter.getFeatureValue(aFeature, fs), aValue)) {
                aAdapter.delete(aState.getDocument(), aState.getUser().getUsername(), aCas,
                        new VID(getAddr(fs)));
            }
        }
    }
}
 
Example #20
Source File: CasMetadataUtils.java    From webanno with Apache License 2.0 5 votes vote down vote up
public static Optional<String> getSourceDocumentName(CAS aCas)
{
    try {
        FeatureStructure fs = CasUtil.selectSingle(aCas, getType(aCas, CASMetadata.class));
        return Optional.ofNullable(FSUtil.getFeature(fs, "sourceDocumentName", String.class));
    }
    catch (IllegalArgumentException e) {
        return Optional.empty();
    }
}
 
Example #21
Source File: WebannoTsv3Writer.java    From webanno with Apache License 2.0 5 votes vote down vote up
private void setAmbiguity(JCas aJCas)
{
    List<String> spanAndTokenLayers = spanLayers;
    spanAndTokenLayers.add(Token.class.getName());
    for (String l : spanAndTokenLayers) {
        Type type = getType(aJCas.getCas(), l);
        ambigUnits.putIfAbsent(type.getName(), new HashMap<>());
        for (AnnotationFS fs : CasUtil.select(aJCas.getCas(), type)) {
            AnnotationUnit unit = getFirstUnit(fs);
            // multiple token anno
            if (isMultipleTokenAnnotation(fs.getBegin(), fs.getEnd())) {
                SubTokenAnno sta = new SubTokenAnno();
                sta.setBegin(fs.getBegin());
                sta.setEnd(fs.getEnd());
                sta.setText(fs.getCoveredText());
                Set<AnnotationUnit> sus = new LinkedHashSet<>();
                for (AnnotationUnit newUnit : getSubUnits(sta, sus)) {
                    ambigUnits.get(type.getName()).put(newUnit, true);
                }
            }
            // stacked anno
            else if (ambigUnits.get(type.getName()).get(unit) != null) {
                ambigUnits.get(type.getName()).put(unit, true);
            }
            // single or first occurrence of stacked anno
            else {
                ambigUnits.get(type.getName()).put(unit, false);
            }
        }

    }
}
 
Example #22
Source File: CasMerge.java    From webanno with Apache License 2.0 5 votes vote down vote up
private static boolean existsSameAt(CAS aCas, AnnotationFS aFs)
{
    return CasUtil.selectAt(aCas, aFs.getType(), aFs.getBegin(), aFs.getEnd()).stream()
            .filter(cand -> isEquivalentAnnotation(aFs, cand))
            .findAny()
            .isPresent();
}
 
Example #23
Source File: ValuesGenerator.java    From webanno with Apache License 2.0 5 votes vote down vote up
public static List<AnnotationFS> selectAt(CAS aCas, final Type type, int aBegin, int aEnd)
{
    List<AnnotationFS> covered = CasUtil.selectCovered(aCas, type, aBegin, aEnd);

    // Remove all that do not have the exact same offset
    covered.removeIf(cur -> !(cur.getBegin() == aBegin && cur.getEnd() == aEnd));
    return covered;
}
 
Example #24
Source File: WebAnnoTsv3LegacyFormatSupport.java    From webanno with Apache License 2.0 5 votes vote down vote up
private boolean chainAnnotationExists(CAS aCas, String aType) {

        Type type = aCas.getTypeSystem().getType(aType);
        if (CasUtil.selectFS(aCas, type).size() == 0) {
            return false;
        }
        return true;
    }
 
Example #25
Source File: ActiveLearningSidebar.java    From inception with Apache License 2.0 5 votes vote down vote up
private Optional<AnnotationFS> getMatchingAnnotation(CAS aCas, LearningRecord aRecord)
{
    Type type = CasUtil.getType(aCas, alStateModel.getObject().getLayer().getName());
    Feature feature = type.getFeatureByBaseName(aRecord.getAnnotationFeature().getName());
    return selectAt(aCas, type, aRecord.getOffsetCharacterBegin(),
            aRecord.getOffsetCharacterEnd()).stream()
            .filter(fs -> aRecord.getAnnotation().equals(fs.getFeatureValueAsString(feature)))
            .findFirst();
}
 
Example #26
Source File: DataMajorityNerRecommender.java    From inception with Apache License 2.0 5 votes vote down vote up
@Override
public void predict(RecommenderContext aContext, CAS aCas) throws RecommendationException
{
    DataMajorityModel model = aContext.get(KEY_MODEL).orElseThrow(() ->
            new RecommendationException("Key [" + KEY_MODEL + "] not found in context"));

    // Make the predictions
    Type tokenType = CasUtil.getAnnotationType(aCas, Token.class);
    Collection<AnnotationFS> candidates = CasUtil.select(aCas, tokenType);
    List<Annotation> predictions = predict(candidates, model);

    // Add predictions to the CAS
    Type predictedType = getPredictedType(aCas);
    Feature scoreFeature = getScoreFeature(aCas);
    Feature scoreExplanationFeature = getScoreExplanationFeature(aCas);
    Feature predictedFeature = getPredictedFeature(aCas);
    Feature isPredictionFeature = getIsPredictionFeature(aCas);

    for (Annotation ann : predictions) {
        AnnotationFS annotation = aCas.createAnnotation(predictedType, ann.begin, ann.end);
        annotation.setStringValue(predictedFeature, ann.label);
        annotation.setDoubleValue(scoreFeature, ann.score);
        annotation.setStringValue(scoreExplanationFeature, ann.explanation);
        annotation.setBooleanValue(isPredictionFeature, true);
        aCas.addFsToIndexes(annotation);
    }
}
 
Example #27
Source File: OpenNlpPosRecommender.java    From inception with Apache License 2.0 5 votes vote down vote up
private String getFeatureValueCovering(CAS aCas, AnnotationFS aToken, Type aType,
        Feature aFeature)
{
    List<AnnotationFS> annotations = CasUtil.selectCovered(aType, aToken);

    if (annotations.isEmpty()) {
        return PAD;
    }

    String value = annotations.get(0).getFeatureValueAsString(aFeature);
    return isNoneBlank(value) ? value : PAD;
}
 
Example #28
Source File: RecommendationServiceImpl.java    From inception with Apache License 2.0 5 votes vote down vote up
@Override
public int upsertFeature(AnnotationSchemaService annotationService, SourceDocument aDocument,
        String aUsername, CAS aCas, AnnotationLayer layer, AnnotationFeature aFeature,
        String aValue, int aBegin, int aEnd)
    throws AnnotationException
{
    // The feature of the predicted label
    SpanAdapter adapter = (SpanAdapter) annotationService.getAdapter(layer);
    
    // Check if there is already an annotation of the target type at the given location
    Type type = CasUtil.getType(aCas, adapter.getAnnotationTypeName());
    AnnotationFS annoFS = selectAt(aCas, type, aBegin, aEnd).stream().findFirst().orElse(null);
    
    int address;
    if (annoFS != null) {
        // ... if yes, then we update the feature on the existing annotation
        address = getAddr(annoFS);
    }
    else {
        // ... if not, then we create a new annotation - this also takes care of attaching to 
        // an annotation if necessary
        address = getAddr(adapter.add(aDocument, aUsername, aCas, aBegin, aEnd));
    }

    // Update the feature value
    adapter.setFeatureValue(aDocument, aUsername, aCas, address, aFeature, aValue);
    
    return address;
}
 
Example #29
Source File: RecommendationServiceImplIntegrationTest.java    From inception with Apache License 2.0 5 votes vote down vote up
@Test
public void monkeyPatchTypeSystem_WithNer_CreatesScoreFeatures() throws Exception
{
    try (CasStorageSession session = CasStorageSession.open()) {
        JCas jCas = JCasFactory.createText("I am text CAS", "de");
        session.add("jCas", CasAccessMode.EXCLUSIVE_WRITE_ACCESS, jCas.getCas());
        
        when(annoService.getFullProjectTypeSystem(project))
                .thenReturn(typeSystem2TypeSystemDescription(jCas.getTypeSystem()));
        when(annoService.listAnnotationLayer(project))
                .thenReturn(asList(layer));
        doCallRealMethod().when(annoService)
                .upgradeCas(any(CAS.class), any(TypeSystemDescription.class));
        doCallRealMethod().when(annoService)
                .upgradeCas(any(CAS.class), any(CAS.class), any(TypeSystemDescription.class));

        sut.cloneAndMonkeyPatchCAS(project, jCas.getCas(), jCas.getCas());

        Type type = CasUtil.getType(jCas.getCas(), layer.getName());

        assertThat(type.getFeatures())
                .extracting(Feature::getShortName)
                .contains(feature.getName() + FEATURE_NAME_SCORE_SUFFIX)
                .contains(feature.getName() + FEATURE_NAME_SCORE_EXPLANATION_SUFFIX)
                .contains(FEATURE_NAME_IS_PREDICTION);
    }
}
 
Example #30
Source File: LocalFeaturesTcAnnotator.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
private void addTCUnitAndOutcomeAnnotation(JCas jcas)
{
    Type type = jcas.getCas().getTypeSystem().getType(nameUnit);

    Collection<AnnotationFS> unitAnnotation = CasUtil.select(jcas.getCas(), type);
    for (AnnotationFS unit : unitAnnotation) {
        TextClassificationTarget tcs = new TextClassificationTarget(jcas, unit.getBegin(),
                unit.getEnd());
        tcs.addToIndexes();
        TextClassificationOutcome tco = new TextClassificationOutcome(jcas, unit.getBegin(),
                unit.getEnd());
        tco.setOutcome(Constants.TC_OUTCOME_DUMMY_VALUE);
        tco.addToIndexes();
    }
}