/** * 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); }
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() + "\""); } } }
public void process(JCas jCas) throws AnalysisEngineProcessException { CAS cas = jCas.getCas(); Type type = CasUtil.getAnnotationType(cas, typeName); Collection<AnnotationFS> annos = CasUtil.select(cas, type); int counter = 0; for (AnnotationFS anno : annos) { if (checkFeatureValues(type, anno)) counter++; } if (counter != count) pass = false; else pass = true; }
int i = 0; for (String conceptName : conceptFeaturesRuleMap.keySet()) { Type type = CasUtil.getAnnotationType(cas, DeterminantValueSet.checkNameSpace(conceptName)); Annotation mergedAnno = null; if (!conceptFeaturesMap.containsKey(type)) {
@SuppressWarnings("unchecked") public static <A extends AnnotationFS> A getSingleAnnotation(JCas cas, Class<A> typeJCasClass) { Type annoType = CasUtil.getAnnotationType(cas.getCas(), typeJCasClass); AnnotationIndex<Annotation> annoIdx = cas.getAnnotationIndex(annoType); if (annoIdx.size() == 0) { return null; } else if (annoIdx.size() == 1) { return (A) annoIdx.iterator().next(); } else { throw new IllegalStateException(String.format( "Too much (>1) annotations of type %s", annoType)); } }
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; } } }
@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 = getAnnotationType(aCas, Token.class); Collection<AnnotationFS> candidates = CasUtil.select(aCas, tokenType); List<Annotation> predictions = predict(candidates, model); // Add predictions to the CAS Type predictionType = getAnnotationType(aCas, PredictedSpan.class); Feature confidenceFeature = predictionType.getFeatureByBaseName("score"); Feature labelFeature = predictionType.getFeatureByBaseName("label"); for (Annotation ann : predictions) { AnnotationFS annotation = aCas.createAnnotation(predictionType, ann.begin, ann.end); annotation.setDoubleValue(confidenceFeature, ann.score); annotation.setStringValue(labelFeature, ann.label); aCas.addFsToIndexes(annotation); } } // end::predict1[]
Type sentenceType = getType(aCas, Sentence.class); Type tokenType = getType(aCas, Token.class); Type predictionType = getAnnotationType(aCas, PredictedSpan.class); Feature confidenceFeature = predictionType.getFeatureByBaseName("score"); Feature labelFeature = predictionType.getFeatureByBaseName("label");
Type sentenceType = getType(aCas, Sentence.class); Type tokenType = getType(aCas, Token.class); Type predictionType = getAnnotationType(aCas, PredictedSpan.class); Feature confidenceFeature = predictionType.getFeatureByBaseName("score"); Feature labelFeature = predictionType.getFeatureByBaseName("label");
private void predictToken(String aCoveredText, int aBegin, int aEnd, JCas aJcas) { List<KBHandle> handles = new ArrayList<>(); AnnotationFeature feat = recommender.getFeature(); FeatureSupport<ConceptFeatureTraits> fs = fsRegistry.getFeatureSupport(feat); ConceptFeatureTraits conceptFeatureTraits = fs.readTraits(feat); if (conceptFeatureTraits.getRepositoryId() != null) { Optional<KnowledgeBase> kb = kbService.getKnowledgeBaseById(recommender.getProject(), conceptFeatureTraits.getRepositoryId()); if (kb.isPresent() && kb.get().isSupportConceptLinking()) { handles.addAll(readCandidates(kb.get(), aCoveredText, aBegin, aJcas)); } } else { for (KnowledgeBase kb : kbService.getEnabledKnowledgeBases(recommender.getProject())) { if (kb.isSupportConceptLinking()) { handles.addAll(readCandidates(kb, aCoveredText, aBegin, aJcas)); } } } Type predictionType = getAnnotationType(aJcas.getCas(), PredictedSpan.class); Feature labelFeature = predictionType.getFeatureByBaseName("label"); for (KBHandle prediction : handles.stream().limit(recommender.getMaxRecommendations()) .collect(Collectors.toList())) { AnnotationFS annotation = aJcas.getCas().createAnnotation(predictionType, aBegin, aEnd); annotation.setStringValue(labelFeature, prediction.getIdentifier()); aJcas.getCas().addFsToIndexes(annotation); } }
Type predictionType = getAnnotationType(aCas, PredictedSpan.class); Type tokenType = getType(aCas, Token.class);
private void predictToken(String aCoveredText, int aBegin, int aEnd, JCas aJcas) { List<KBHandle> handles = new ArrayList<>(); AnnotationFeature feat = recommender.getFeature(); FeatureSupport<ConceptFeatureTraits> fs = fsRegistry.getFeatureSupport(feat); ConceptFeatureTraits conceptFeatureTraits = fs.readTraits(feat); if (conceptFeatureTraits.getRepositoryId() != null) { Optional<KnowledgeBase> kb = kbService.getKnowledgeBaseById(recommender.getProject(), conceptFeatureTraits.getRepositoryId()); if (kb.isPresent() && kb.get().isSupportConceptLinking()) { handles.addAll(readCandidates(kb.get(), aCoveredText, aBegin, aJcas)); } } else { for (KnowledgeBase kb : kbService.getEnabledKnowledgeBases(recommender.getProject())) { if (kb.isSupportConceptLinking()) { handles.addAll(readCandidates(kb, aCoveredText, aBegin, aJcas)); } } } Type predictionType = getAnnotationType(aJcas.getCas(), PredictedSpan.class); Feature labelFeature = predictionType.getFeatureByBaseName("label"); for (KBHandle prediction : handles.stream().limit(recommender.getMaxRecommendations()) .collect(Collectors.toList())) { AnnotationFS annotation = aJcas.getCas().createAnnotation(predictionType, aBegin, aEnd); annotation.setStringValue(labelFeature, prediction.getIdentifier()); aJcas.getCas().addFsToIndexes(annotation); } }
Type type = CasUtil.getAnnotationType(aCoveringAnnotation.getCAS(), typeName); Feature feature = type.getFeatureByBaseName(featureName); if (feature == null) {
Type predictionType = getAnnotationType(aCas, PredictedSpan.class); Type tokenType = getType(aCas, Token.class); Feature confidenceFeature = predictionType.getFeatureByBaseName("score");
CasUtil.getAnnotationType(cas, annotationType))) {
CasUtil.getAnnotationType(cas, annotationType))) {
CasUtil.select(cas, CasUtil.getAnnotationType(cas, annotationType))); Collection<Annotation> toRemove = new ArrayList<Annotation>(); for (AnnotationFS cover : covers) {
@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 void predict(RecommenderContext aContext, CAS aCas) throws RecommendationException { Trie<DictEntry> dict = aContext.get(KEY_MODEL).orElseThrow(() -> new RecommendationException("Key [" + KEY_MODEL + "] not found in context")); Type predictionType = getAnnotationType(aCas, PredictedSpan.class); Feature confidenceFeature = predictionType.getFeatureByBaseName("score"); Feature labelFeature = predictionType.getFeatureByBaseName("label"); List<Sample> data = predict(0, aCas, dict); for (Sample sample : data) { for (Span span : sample.getSpans()) { AnnotationFS annotation = aCas.createAnnotation(predictionType, span.getBegin(), span.getEnd()); annotation.setDoubleValue(confidenceFeature, span.getScore()); annotation.setStringValue(labelFeature, span.getLabel()); aCas.addFsToIndexes(annotation); } } }
@Override public void predict(RecommenderContext aContext, CAS aCas) throws RecommendationException { Trie<DictEntry> dict = aContext.get(KEY_MODEL).orElseThrow(() -> new RecommendationException("Key [" + KEY_MODEL + "] not found in context")); Type predictionType = getAnnotationType(aCas, PredictedSpan.class); Feature confidenceFeature = predictionType.getFeatureByBaseName("score"); Feature labelFeature = predictionType.getFeatureByBaseName("label"); List<Sample> data = predict(0, aCas, dict); for (Sample sample : data) { for (Span span : sample.getSpans()) { AnnotationFS annotation = aCas.createAnnotation(predictionType, span.getBegin(), span.getEnd()); annotation.setDoubleValue(confidenceFeature, span.getScore()); annotation.setStringValue(labelFeature, span.getLabel()); aCas.addFsToIndexes(annotation); } } }