Java Code Examples for org.apache.uima.fit.util.CasUtil#getType()
The following examples show how to use
org.apache.uima.fit.util.CasUtil#getType() .
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: TrainingTask.java From inception with Apache License 2.0 | 6 votes |
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 3
Source File: ChainAdapter.java From webanno with Apache License 2.0 | 6 votes |
/** * 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 4
Source File: AnnotationDetailEditorPanel.java From webanno with Apache License 2.0 | 6 votes |
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 5
Source File: SpanAdapter.java From webanno with Apache License 2.0 | 6 votes |
@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 6
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 7
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 8
Source File: BratRenderer.java From webanno with Apache License 2.0 | 6 votes |
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 9
Source File: RecommenderTestHelper.java From inception with Apache License 2.0 | 5 votes |
public static <T extends Annotation> List<T> getPredictions(CAS aCas, Class<T> aClass) throws Exception { Type type = CasUtil.getType(aCas, aClass); Feature feature = type.getFeatureByBaseName(FEATURE_NAME_IS_PREDICTION); return JCasUtil.select(aCas.getJCas(), aClass).stream() .filter(fs -> fs.getBooleanValue(feature)) .collect(Collectors.toList()); }
Example 10
Source File: AnnotationDetailEditorPanel.java From webanno with Apache License 2.0 | 5 votes |
private Set<FeatureStructure> getAttachedLinks(AnnotationFS aFs, AnnotationLayer aLayer) { CAS cas = aFs.getCAS(); Set<FeatureStructure> attachedLinks = new HashSet<>(); TypeAdapter adapter = annotationService.getAdapter(aLayer); if (adapter instanceof SpanAdapter) { for (AnnotationFeature linkFeature : annotationService .listAttachedLinkFeatures(aLayer)) { if (MultiValueMode.ARRAY.equals(linkFeature.getMultiValueMode()) && LinkMode.WITH_ROLE.equals(linkFeature.getLinkMode())) { // Fetch slot hosts that could link to the current FS and check if any of // them actually links to the current FS Type linkHost = CasUtil.getType(cas, linkFeature.getLayer().getName()); for (FeatureStructure linkFS : CasUtil.selectFS(cas, linkHost)) { List<LinkWithRoleModel> links = adapter.getFeatureValue(linkFeature, linkFS); for (int li = 0; li < links.size(); li++) { LinkWithRoleModel link = links.get(li); AnnotationFS linkTarget = selectByAddr(cas, AnnotationFS.class, link.targetAddr); // If the current annotation fills a slot, then add the slot host to // our list of attached links. if (isSame(linkTarget, aFs)) { attachedLinks.add(linkFS); } } } } } } return attachedLinks; }
Example 11
Source File: AutomationUtil.java From webanno with Apache License 2.0 | 5 votes |
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 12
Source File: ActiveLearningSidebar.java From inception with Apache License 2.0 | 5 votes |
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 13
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 14
Source File: DocumentMetadataLayerAdapter.java From inception with Apache License 2.0 | 5 votes |
/** * Add new document metadata annotation into the CAS and return the the id of the annotation. * * @param aDocument * the document to which the CAS belongs * @param aUsername * the user to which the CAS belongs * @param aCas * the CAS. * @return the ID. * @throws AnnotationException * if the annotation cannot be created/updated. */ public AnnotationBaseFS add(SourceDocument aDocument, String aUsername, CAS aCas) throws AnnotationException { Type type = CasUtil.getType(aCas, getAnnotationTypeName()); AnnotationBaseFS newAnnotation = aCas.createFS(type); aCas.addFsToIndexes(newAnnotation); publishEvent(new DocumentMetadataCreatedEvent(this, aDocument, aUsername, getLayer(), newAnnotation)); return newAnnotation; }
Example 15
Source File: RecommendationServiceImplIntegrationTest.java From inception with Apache License 2.0 | 5 votes |
@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 16
Source File: RecommendationServiceImpl.java From inception with Apache License 2.0 | 5 votes |
@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 17
Source File: WebannoTsv2Reader.java From webanno with Apache License 2.0 | 4 votes |
private int getLayerAndFeature(JCas aJcas, int columns, Map<Type, Set<Feature>> spanLayers, Map<Type, Type> relationayers, String line) throws IOException { StringTokenizer headerTk = new StringTokenizer(line, "#"); while (headerTk.hasMoreTokens()) { String layerNames = headerTk.nextToken().trim(); StringTokenizer layerTk = new StringTokenizer(layerNames, "|"); Set<Feature> features = new LinkedHashSet<>(); String layerName = layerTk.nextToken().trim(); Iterator<Type> types = aJcas.getTypeSystem().getTypeIterator(); boolean layerExists = false; while (types.hasNext()) { if (types.next().getName().equals(layerName)) { layerExists = true; break; } } if (!layerExists) { throw new IOException(fileName + " This is not a valid TSV File. The layer " + layerName + " is not created in the project."); } Type layer = CasUtil.getType(aJcas.getCas(), layerName); while (layerTk.hasMoreTokens()) { String ft = layerTk.nextToken().trim(); if (ft.startsWith("AttachTo=")) { Type attachLayer = CasUtil.getType(aJcas.getCas(), ft.substring(9)); relationayers.put(layer, attachLayer); columns++; continue; } Feature feature = layer.getFeatureByBaseName(ft); if (feature == null) { throw new IOException(fileName + " This is not a valid TSV File. The feature " + ft + " is not created for the layer " + layerName); } features.add(feature); columns++; } spanLayers.put(layer, features); } return columns; }
Example 18
Source File: WebannoTsv3Reader.java From webanno with Apache License 2.0 | 4 votes |
/** * Get the type and feature information from the TSV file header * * @param header * the header line * @throws IOException * If the type or the feature do not exist in the CAs */ private void setLayerAndFeature(JCas aJcas, String header) throws IOException { try { StringTokenizer headerTk = new StringTokenizer(header, "#"); while (headerTk.hasMoreTokens()) { String layerNames = headerTk.nextToken().trim(); StringTokenizer layerTk = new StringTokenizer(layerNames, "|"); Set<Feature> features = new LinkedHashSet<>(); String layerName = layerTk.nextToken().trim(); layerName = layerName.substring(layerName.indexOf("=") + 1); Iterator<Type> types = aJcas.getTypeSystem().getTypeIterator(); boolean layerExists = false; while (types.hasNext()) { if (types.next().getName().equals(layerName)) { layerExists = true; break; } } if (!layerExists) { throw new IOException(fileName + " This is not a valid TSV File. The layer " + layerName + " is not created in the project."); } Type layer = CasUtil.getType(aJcas.getCas(), layerName); // if the layer do not have a feature, just update columns count for the place // holder if (!layerTk.hasMoreTokens()) { columns++; allLayers.put(layer, features); layerMaps.put(layerMaps.size() + 1, layer); return; } while (layerTk.hasMoreTokens()) { String ft = layerTk.nextToken().trim(); columns++; Feature feature; if (ft.startsWith(BT)) { feature = layer.getFeatureByBaseName(DEPENDENT); depFeatures.put(layer, feature); depTypess.put(layer, CasUtil.getType(aJcas.getCas(), ft.substring(3))); } else { feature = layer.getFeatureByBaseName(ft); } if (ft.startsWith(ROLE)) { ft = ft.substring(5); String t = layerTk.nextToken(); columns++; Type tType = CasUtil.getType(aJcas.getCas(), t); String fName = ft.substring(0, ft.indexOf("_")); Feature slotF = layer .getFeatureByBaseName(fName.substring(fName.indexOf(":") + 1)); if (slotF == null) { throw new IOException( fileName + " This is not a valid TSV File. The feature " + ft + " is not created for the layer " + layerName); } features.add(slotF); roleLinks.put(slotF, tType); Type slotType = CasUtil.getType(aJcas.getCas(), ft.substring(ft.indexOf("_") + 1)); Feature tFeatore = slotType.getFeatureByBaseName("target"); if (tFeatore == null) { throw new IOException( fileName + " This is not a valid TSV File. The feature " + ft + " is not created for the layer " + layerName); } roleTargets.put(tFeatore, tType); features.add(tFeatore); slotLinkTypes.put(slotF, slotType); continue; } if (feature == null) { throw new IOException( fileName + " This is not a valid TSV File. The feature " + ft + " is not created for the layer " + layerName); } features.add(feature); } allLayers.put(layer, features); layerMaps.put(layerMaps.size() + 1, layer); } } catch (Exception e) { throw new IOException(e.getMessage() + "\nTSV header:\n" + header); } }
Example 19
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; }
Example 20
Source File: TypeAdapter.java From webanno with Apache License 2.0 | 2 votes |
/** * Get the CAS type of the this {@link TypeAdapter} * * @param cas * the CAS. * @return the type. */ default Type getAnnotationType(CAS cas) { return CasUtil.getType(cas, getAnnotationTypeName()); }