Java Code Examples for org.apache.uima.fit.util.CasUtil#select()
The following examples show how to use
org.apache.uima.fit.util.CasUtil#select() .
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: DataMajorityNerRecommender.java From inception with Apache License 2.0 | 6 votes |
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 2
Source File: RemoteStringMatchingNerRecommender.java From inception with Apache License 2.0 | 6 votes |
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 3
Source File: CasAssert.java From inception with Apache License 2.0 | 6 votes |
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 4
Source File: CasMetadataUtils.java From webanno with Apache License 2.0 | 6 votes |
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 5
Source File: LocalFeaturesTcAnnotator.java From ambiverse-nlu with Apache License 2.0 | 5 votes |
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(); } }
Example 6
Source File: LocalFeaturesTcAnnotator.java From ambiverse-nlu with Apache License 2.0 | 5 votes |
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 7
Source File: DataMajorityNerRecommender.java From inception with Apache License 2.0 | 5 votes |
@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 8
Source File: CasAssert.java From inception with Apache License 2.0 | 5 votes |
public ListAssert<AnnotationFS> extractNamedEntities() { isNotNull(); Type type = CasUtil.getType(actual, TYPE_NE); List<AnnotationFS> result = new ArrayList<>(CasUtil.select(actual, type)); return new ListAssert<>(result); }
Example 9
Source File: WebannoTsv3Writer.java From webanno with Apache License 2.0 | 5 votes |
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 10
Source File: CasMetadataUtils.java From webanno with Apache License 2.0 | 5 votes |
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 11
Source File: WebannoTsv2Writer.java From webanno with Apache License 2.0 | 4 votes |
private void setTokenAnnos(CAS aCas, Map<Integer, String> aTokenAnnoMap, Type aType, Feature aFeature) { LowLevelCAS llCas = aCas.getLowLevelCAS(); for (AnnotationFS annoFs : CasUtil.select(aCas, aType)) { boolean first = true; boolean previous = false; // exists previous annotation, place-holed O-_ should be kept for (Token token : selectCovered(Token.class, annoFs)) { if (annoFs.getBegin() <= token.getBegin() && annoFs.getEnd() >= token.getEnd()) { String annotation = annoFs.getFeatureValueAsString(aFeature); if (annotation == null) { annotation = aType.getName() + "_"; } if (aTokenAnnoMap.get(llCas.ll_getFSRef(token)) == null) { if (previous) { if (!multipleSpans.contains(aType.getName())) { aTokenAnnoMap.put(llCas.ll_getFSRef(token), annotation); } else { aTokenAnnoMap.put(llCas.ll_getFSRef(token), "O-_|" + (first ? "B-" : "I-") + annotation); first = false; } } else { if (!multipleSpans.contains(aType.getName())) { aTokenAnnoMap.put(llCas.ll_getFSRef(token), annotation); } else { aTokenAnnoMap.put(llCas.ll_getFSRef(token), (first ? "B-" : "I-") + annotation); first = false; } } } else { if (!multipleSpans.contains(aType.getName())) { aTokenAnnoMap.put(llCas.ll_getFSRef(token), aTokenAnnoMap.get(llCas.ll_getFSRef(token)) + "|" + annotation); previous = true; } else { aTokenAnnoMap.put(llCas.ll_getFSRef(token), aTokenAnnoMap.get(llCas.ll_getFSRef(token)) + "|" + (first ? "B-" : "I-") + annotation); first = false; previous = true; } } } } } }
Example 12
Source File: WebannoTsv3Writer.java From webanno with Apache License 2.0 | 4 votes |
private void setRelationAnnotation(JCas aJCas) { for (String l : relationLayers) { if (l.equals(Token.class.getName())) { continue; } Map<AnnotationUnit, List<List<String>>> annotationsPertype; if (annotationsPerPostion.get(l) == null) { annotationsPertype = new HashMap<>(); } else { annotationsPertype = annotationsPerPostion.get(l); } Type type = getType(aJCas.getCas(), l); Feature dependentFeature = null; Feature governorFeature = null; List<Feature> features = type.getFeatures(); Collections.sort(features, (a, b) -> StringUtils.compare(a.getShortName(), b.getShortName())); for (Feature feature : features) { if (feature.getShortName().equals(DEPENDENT)) { // check if the dependent is dependentFeature = feature; } if (feature.getShortName().equals(GOVERNOR)) { governorFeature = feature; } } for (AnnotationFS fs : CasUtil.select(aJCas.getCas(), type)) { AnnotationFS depFs = (AnnotationFS) fs.getFeatureValue(dependentFeature); AnnotationFS govFs = (AnnotationFS) fs.getFeatureValue(governorFeature); Type govType = govFs.getType(); AnnotationUnit govUnit = getFirstUnit( getUnit(govFs.getBegin(), govFs.getEnd(), govFs.getCoveredText())); if (ambigUnits.get(govType.getName()).get(govUnit) == null) { govUnit = getUnit(govFs.getBegin(), govFs.getEnd(), govFs.getCoveredText()); } AnnotationUnit depUnit = getFirstUnit( getUnit(depFs.getBegin(), depFs.getEnd(), depFs.getCoveredText())); if (ambigUnits.get(govType.getName()).get(depUnit) == null) { depUnit = getUnit(depFs.getBegin(), depFs.getEnd(), depFs.getCoveredText()); } // Since de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency is over // Over POS anno which itself attached to Token, we need the POS type here if (type.getName().equals(Dependency.class.getName())) { govType = aJCas.getCas().getTypeSystem().getType(POS.class.getName()); } int govRef = 0; int depRef = 0; // For that unit test case only, where annotations are on Tokens. // The WebAnno world do not ever process Token as an annotation if (!govType.getName().equals(Token.class.getName()) && ambigUnits.get(govType.getName()).get(govUnit).equals(true)) { govRef = annotaionRefPerType.get(govType).get(govFs); } if (!govType.getName().equals(Token.class.getName()) && ambigUnits.get(govType.getName()).get(depUnit).equals(true)) { depRef = annotaionRefPerType.get(govType).get(depFs); } setRelationAnnoPerFeature(annotationsPertype, type, fs, depUnit, govUnit, govRef, depRef, govType); } if (annotationsPertype.keySet().size() > 0) { annotationsPerPostion.put(l, annotationsPertype); } } }
Example 13
Source File: WebAnnoCasUtil.java From webanno with Apache License 2.0 | 4 votes |
public static Collection<AnnotationFS> selectSentences(CAS aCas) { return CasUtil.select(aCas, getType(aCas, Sentence.class)); }
Example 14
Source File: WebAnnoCasUtil.java From webanno with Apache License 2.0 | 4 votes |
public static Collection<AnnotationFS> selectTokens(CAS aCas) { return CasUtil.select(aCas, getType(aCas, Token.class)); }
Example 15
Source File: CasMetadataUtils.java From webanno with Apache License 2.0 | 4 votes |
public static void addOrUpdateCasMetadata(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 - not adding", aCasFile.getName(), aUsername, aDocument.getName(), aDocument.getId(), aDocument.getProject().getName(), aDocument.getProject().getId()); return; } Type casMetadataType = getType(aCas, CASMetadata.class); FeatureStructure cmd; List<AnnotationFS> cmds = new ArrayList<>(CasUtil.select(aCas, casMetadataType)); if (cmds.size() > 1) { throw new IOException("CAS contains more than one CASMetadata instance!"); } else if (cmds.size() == 1) { cmd = cmds.get(0); } else { cmd = aCas.createAnnotation(casMetadataType, 0, 0); } if (cmd.getType().getFeatureByBaseName("username") != null) { FSUtil.setFeature(cmd, "username", aUsername); } if (cmd.getType().getFeatureByBaseName("sourceDocumentId") != null) { FSUtil.setFeature(cmd, "sourceDocumentId", aDocument.getId()); } if (cmd.getType().getFeatureByBaseName("sourceDocumentName") != null) { FSUtil.setFeature(cmd, "sourceDocumentName", aDocument.getName()); } if (cmd.getType().getFeatureByBaseName("projectId") != null) { FSUtil.setFeature(cmd, "projectId", aDocument.getProject().getId()); } if (cmd.getType().getFeatureByBaseName("projectName") != null) { FSUtil.setFeature(cmd, "projectName", aDocument.getProject().getName()); } if (cmd.getType().getFeatureByBaseName("lastChangedOnDisk") != null) { FSUtil.setFeature(cmd, "lastChangedOnDisk", aCasFile.lastModified()); LOG.trace("CAS [{}] for [{}]@[{}]({}): set lastChangedOnDisk: {}", aCas.hashCode(), aUsername, aDocument.getName(), aDocument.getId(), aCasFile.lastModified()); } aCas.addFsToIndexes(cmd); }
Example 16
Source File: AnnotationDetailEditorPanel.java From webanno with Apache License 2.0 | 4 votes |
private static Set<AnnotationFS> getAttachedRels(AnnotationSchemaService aAS, AnnotationFS aFs, AnnotationLayer aLayer) { CAS cas = aFs.getCAS(); Set<AnnotationFS> toBeDeleted = new HashSet<>(); for (AnnotationLayer relationLayer : aAS.listAttachedRelationLayers(aLayer)) { RelationAdapter relationAdapter = (RelationAdapter) aAS.getAdapter(relationLayer); Type relationType = CasUtil.getType(cas, relationLayer.getName()); Feature sourceFeature = relationType.getFeatureByBaseName(relationAdapter .getSourceFeatureName()); Feature targetFeature = relationType.getFeatureByBaseName(relationAdapter .getTargetFeatureName()); // This code is already prepared for the day that relations can go between // different layers and may have different attach features for the source and // target layers. Feature relationSourceAttachFeature = null; Feature relationTargetAttachFeature = null; if (relationAdapter.getAttachFeatureName() != null) { relationSourceAttachFeature = sourceFeature.getRange().getFeatureByBaseName( relationAdapter.getAttachFeatureName()); relationTargetAttachFeature = targetFeature.getRange().getFeatureByBaseName( relationAdapter.getAttachFeatureName()); } for (AnnotationFS relationFS : CasUtil.select(cas, relationType)) { // Here we get the annotations that the relation is pointing to in the UI FeatureStructure sourceFS; if (relationSourceAttachFeature != null) { sourceFS = relationFS.getFeatureValue(sourceFeature).getFeatureValue( relationSourceAttachFeature); } else { sourceFS = relationFS.getFeatureValue(sourceFeature); } FeatureStructure targetFS; if (relationTargetAttachFeature != null) { targetFS = relationFS.getFeatureValue(targetFeature).getFeatureValue( relationTargetAttachFeature); } else { targetFS = relationFS.getFeatureValue(targetFeature); } if (isSame(sourceFS, aFs) || isSame(targetFS, aFs)) { toBeDeleted.add(relationFS); LOG.debug("Deleted relation [" + getAddr(relationFS) + "] from layer [" + relationLayer.getName() + "]"); } } } return toBeDeleted; }