private static List<AnnotationFS> select(CAS cas, Type localType) { List<AnnotationFS> annotationFSs = new ArrayList<AnnotationFS>(); for(AnnotationFS annotationFS: CasUtil.select(cas, localType)){ annotationFSs.add(annotationFS); } return annotationFSs; }
@Override public void predict(RecommenderContext aContext, CAS aCas) throws RecommendationException { try { JCas jCas = aCas.getJCas(); Type sentenceType = getType(aCas, Sentence.class); Type tokenType = getType(aCas, Token.class); for (AnnotationFS sentence : select(aCas, sentenceType)) { List<AnnotationFS> tokenAnnotations = selectCovered(tokenType, sentence); predictSentence(aContext, tokenAnnotations, jCas); } } catch (CASException e) { log.error("An error when to trying to access the JCas from Cas.", e); } }
public void process(JCas jCas) throws AnalysisEngineProcessException { CAS cas = jCas.getCas(); Type type = CasUtil.getAnnotationType(cas, printTypeName); Collection<AnnotationFS> annotations = CasUtil.select(cas, type); System.out.println(indication+"\n Here is a list of annotation '" + printTypeName + "':"); for (AnnotationFS annotation : annotations) { System.out.println(annotation.toString() + " Covered Text: \"" + annotation.getCoveredText() + "\""); } } }
/** * Convenience method to iterator over all annotations. * * @param aCas * the CAS hosting the type system. * @return A collection of the selected type. * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a> */ public static Collection<AnnotationFS> selectAll(final CAS aCas) { return select(aCas, getType(aCas, CAS.TYPE_NAME_ANNOTATION)); }
/** * Convenience method to iterator over all features structures. * * @param aCas * the CAS hosting the type system. * @return A collection of the selected type. * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a> */ public static Collection<FeatureStructure> selectAllFS(final CAS aCas) { return selectFS(aCas, getType(aCas, CAS.TYPE_NAME_TOP)); }
@Override public void predict(RecommenderContext aContext, CAS aCas) throws RecommendationException { DoccatModel model = aContext.get(KEY_MODEL).orElseThrow(() -> new RecommendationException("Key [" + KEY_MODEL + "] not found in context")); DocumentCategorizerME finder = new DocumentCategorizerME(model); Type sentenceType = getType(aCas, Sentence.class); Type predictionType = getAnnotationType(aCas, PredictedSpan.class); Type tokenType = getType(aCas, Token.class); Feature confidenceFeature = predictionType.getFeatureByBaseName("score"); Feature labelFeature = predictionType.getFeatureByBaseName("label"); for (AnnotationFS sentence : select(aCas, sentenceType)) { List<AnnotationFS> tokenAnnotations = selectCovered(tokenType, sentence); String[] tokens = tokenAnnotations.stream() .map(AnnotationFS::getCoveredText) .toArray(String[]::new); double[] outcome = finder.categorize(tokens); String label = finder.getBestCategory(outcome); AnnotationFS annotation = aCas.createAnnotation(predictionType, sentence.getBegin(), sentence.getEnd()); annotation.setDoubleValue(confidenceFeature, NumberUtils.max(outcome)); annotation.setStringValue(labelFeature, label); aCas.addFsToIndexes(annotation); } }
@Override public List<String> getAnnotation(JCas aJcas, AnnotationFeature aFeature, int begin, int end) { Type type = getType(aJcas.getCas(), getAnnotationTypeName()); List<String> annotations = new ArrayList<String>(); for (AnnotationFS fs : selectCovered(aJcas.getCas(), type, begin, end)) { Feature labelFeature = fs.getType().getFeatureByBaseName(aFeature.getName()); annotations.add(fs.getFeatureValueAsString(labelFeature)); } return annotations; }
@Override public Type getAnnotationType(CAS cas) { return CasUtil.getType(cas, annotationTypeName); }
public IobEncoder(CAS aCas, Type aType, Feature aValueFeature, boolean aIob1) { iob1 = aIob1; // fill map for whole JCas in order to efficiently encode IOB iobBeginMap = new Int2ObjectOpenHashMap<String>(); iobInsideMap = new Int2ObjectOpenHashMap<String>(); Map<AnnotationFS, Collection<AnnotationFS>> idx = CasUtil.indexCovered(aCas, aType, CasUtil.getType(aCas, Token.class)); String lastValue = null; for (AnnotationFS chunk : CasUtil.select(aCas, aType)) { String value = chunk.getStringValue(aValueFeature); for (AnnotationFS token : idx.get(chunk)) { if ( token.getBegin() == chunk.getBegin() && (!iob1 || (lastValue != null && lastValue.equals(value))) ) { iobBeginMap.put(token.getBegin(), value); } else { iobInsideMap.put(token.getBegin(), value); } } lastValue = value; } }
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; }
private List<DocumentSample> extractSamples(List<CAS> aCasses) { List<DocumentSample> samples = new ArrayList<>(); for (CAS cas : aCasses) { Type sentenceType = getType(cas, Sentence.class); Type tokenType = getType(cas, Token.class); Map<AnnotationFS, Collection<AnnotationFS>> sentences = indexCovered(cas, sentenceType, tokenType); for (Entry<AnnotationFS, Collection<AnnotationFS>> e : sentences.entrySet()) { AnnotationFS sentence = e.getKey(); Collection<AnnotationFS> tokens = e.getValue(); String[] tokenTexts = tokens.stream() .map(AnnotationFS::getCoveredText) .toArray(String[]::new); Type annotationType = getType(cas, layerName); Feature feature = annotationType.getFeatureByBaseName(featureName); for (AnnotationFS annotation : selectCovered(annotationType, sentence)) { String label = annotation.getFeatureValueAsString(feature); DocumentSample nameSample = new DocumentSample( label != null ? label : NO_CATEGORY, tokenTexts); if (nameSample.getCategory() != null) { samples.add(nameSample); } } } } return samples; }
@Override public void process(JCas aJCas) throws AnalysisEngineProcessException { Collection<AnnotationFS> sequences = CasUtil.select(aJCas.getCas(), sequenceSpanType); for (AnnotationFS s : sequences) { int seqSize = CasUtil.selectCovered(aJCas.getCas(), instanceType, s).size(); if (seqSize > maximumLength) { maximumLength = seqSize; } } }
private List<POSSample> extractPosSamples(List<CAS> aCasses) { List<POSSample> posSamples = new ArrayList<>(); for (CAS cas : aCasses) { Type sentenceType = getType(cas, Sentence.class); Type tokenType = getType(cas, Token.class); Map<AnnotationFS, Collection<AnnotationFS>> sentences = indexCovered(cas, sentenceType, tokenType); for (Map.Entry<AnnotationFS, Collection<AnnotationFS>> e : sentences.entrySet()) { AnnotationFS sentence = e.getKey(); Collection<AnnotationFS> tokens = e.getValue(); createPosSample(cas, sentence, tokens).map(posSamples::add); } } LOG.debug("Extracted {} POS samples", posSamples.size()); return posSamples; }
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(); }
/** * Get the CAS type for the given JCas wrapper class type making sure it inherits from * {@link Annotation}. * * @param jCas * the JCas containing the type system. * @param type * the JCas wrapper class type. * @return the CAS type. */ public static Type getAnnotationType(JCas jCas, Class<?> type) { return CasUtil.getAnnotationType(jCas.getCas(), type); }
private boolean chainAnnotationExists(CAS aCas, String aType) { Type type = aCas.getTypeSystem().getType(aType); if (CasUtil.selectFS(aCas, type).size() == 0) { return false; } return true; } }
public void process(JCas jCas) { CAS cas = jCas.getCas(); Type type = CasUtil.getAnnotationType(cas, typeName); Iterator<AnnotationFS> iter = CasUtil.iterator(cas, type); AnnotationFS anno = null; int i = 0; while (iter.hasNext() && i <= ind) { anno = iter.next(); } pass = true; if (anno == null) pass = false; Feature fObj = type.getFeatureByBaseName(featureName); if (fObj == null) pass = false; else { String value = anno.getFeatureValueAsString(fObj); if (!value.equals(featureValue)) pass = false; } if (begin > -1 && end > -1) { if (anno.getBegin() != begin || anno.getEnd() != end) pass = false; } } }
Map<AnnotationFS, Collection<AnnotationFS>> idxSentences = indexCovered( Map<AnnotationFS, Collection<AnnotationFS>> idxProperties = indexCovered( Map<AnnotationFS, Collection<AnnotationFS>> idxNeurons = indexCovered(