org.apache.uima.jcas.tcas.Annotation Java Examples
The following examples show how to use
org.apache.uima.jcas.tcas.Annotation.
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: ExtractCoocurrences.java From bluima with Apache License 2.0 | 6 votes |
/** * Adds all cooccurrences (no filtering). Subclasses can implement finer * filtering. * * @param jCas * @param enclosingAnnot * @param annot1 * @param annot2 * @param firstIds * @param firstIds */ protected Cooccurrence filterCooccurence(JCas jCas, Annotation enclosingAnnot, Annotation annot1, Annotation annot2, String[] firstIds, String[] secondIds) { Cooccurrence cooccurence = new Cooccurrence(jCas, min( annot1.getBegin(), annot2.getBegin()), max(annot1.getEnd(), annot2.getEnd())); cooccurence.setFirstEntity(annot1); cooccurence.setSecondEntity(annot2); cooccurence.setFirstIds(convertToStringArray(jCas, firstIds)); cooccurence.setSecondIds(convertToStringArray(jCas, secondIds)); cooccurence.setSnippetBegin(enclosingAnnot.getBegin()); cooccurence.setSnippetEnd(enclosingAnnot.getEnd()); cooccurence.setCooccurrenceType(cooccurrenceType); cooccurence.addToIndexes(); return cooccurence; }
Example #2
Source File: Subiterator.java From uima-uimaj with Apache License 2.0 | 6 votes |
@Override public void moveToPreviousNvc() { // no isValid check because caller checked: "Nvc" if (isListForm) { --this.pos; return; } if (isUnambiguous) { // Convert to list form Annotation currentAnnotation = it.getNvc(); // save to restore position convertToListForm(); pos = Collections.binarySearch(this.list, currentAnnotation, annotationComparator_withId); --this.pos; return; } // is ambiguous, not list form maybeMoveToPrevBounded(); // makes iterator invalid if moving before startId adjustForStrictOrCoveringAndBoundSkip_backwards(); }
Example #3
Source File: AnnotationIteratorTest.java From uima-uimaj with Apache License 2.0 | 6 votes |
private int assertCountCmn(String msg, int expected, FSIterator<? extends Annotation> it) { msg = flatStateMsg(msg); // add with-flattened-index if isSave is false int count = 0; callCount ++; int fssStart; if (isSave) { fssStarts.add(fssStart = fss.size()); } else { fssStart = fssStarts.get(callCount); } while (it.isValid()) { ++count; Annotation fs = it.next(); if (showFSs) { System.out.format("assertCountCmn: %2d " + msg + " %10s %d - %d%n", count, fs.getType().getName(), fs.getBegin(), fs.getEnd() ); } if (isSave) { fss.add(fs); } else { assertEquals(msg, fss.get(fssStart + count -1).hashCode(), fs.hashCode()); } } assertEquals(msg, expected, count); return fssStart; }
Example #4
Source File: SelectFSs_impl.java From uima-uimaj with Apache License 2.0 | 6 votes |
private String maybeMsgPosition() { StringBuilder sb = new StringBuilder(); if (startingFs != null) { if (startingFs instanceof Annotation) { Annotation a = (Annotation)startingFs; sb.append(" at position begin: ").append(a.getBegin()).append(", end: ") .append(a.getEnd()); } else { sb.append(" at moveTo position given by Feature Structure:\n"); startingFs.prettyPrint(2, 2, sb, false); sb.append("\n "); } } if (shift != 0) { sb.append(" shifted by: ").append(shift); } return sb.toString(); }
Example #5
Source File: JCasUtilTest.java From uima-uimafit with Apache License 2.0 | 6 votes |
private void check(JCas jcas, Annotation t, Collection<? extends Annotation> a1, Collection<? extends Annotation> a2) { // List<Annotation> annos = new ArrayList<Annotation>(); // FSIterator fs = jcas.getAnnotationIndex().iterator(); // while (fs.hasNext()) { // annos.add((Annotation) fs.next()); // } // // System.out.println("--- Index"); // print(annos); // System.out.println("--- Container"); // print(Collections.singleton(t)); // System.out.println("--- Naive"); // print(a1); // System.out.println("--- Optimized"); // print(a2); assertEquals("Container: [" + t.getBegin() + ".." + t.getEnd() + "]", a1, a2); }
Example #6
Source File: AnnotationIteratorTest.java From uima-uimaj with Apache License 2.0 | 6 votes |
/** * * @param ba * @param setup * @param boundsUse * @param flags: TP type priority * NO non overlapping * LE include annotation with ends beyond bounds * ST skip when same begin end type * * @param count */ private void edge(Annotation ba, String setup, BoundsUse boundsUse, String flags, int count) { String[] fa = flags.split("\\:"); cas.reset(); AnnotationIndex<Annotation> ai = cas.getAnnotationIndex(); FSIterator<Annotation> it; SelectFSs<Annotation> sa; setupEdges(setup); switch (boundsUse) { case notBounded: sa = ai.select(); break; case coveredBy: sa = ai.select().coveredBy(ba); break; case sameBeginEnd: sa = ai.select().at(ba); break; default: case covering: sa = ai.select().covering(ba); break; } if (fa[0].equals("TP")) sa.typePriority(); if (fa[1].equals("NO")) sa.nonOverlapping(); if (fa[2].equals("LE")) sa.includeAnnotationsWithEndBeyondBounds(); if (fa[3].equals("ST")) sa.skipWhenSameBeginEndType(); assertEquals(count, sa.fsIterator().size()); }
Example #7
Source File: TokenBuilderTest.java From uima-uimafit with Apache License 2.0 | 6 votes |
@Test public void test3() { String text = "If you like line writer, then you should really check out line rider."; tokenBuilder.buildTokens(jCas, text); FSIndex<Annotation> tokenIndex = jCas.getAnnotationIndex(Token.type); assertEquals(13, tokenIndex.size()); Token token = JCasUtil.selectByIndex(jCas, Token.class, 0); testToken(token, "If", 0, 2, null, null); token = JCasUtil.selectByIndex(jCas, Token.class, 12); testToken(token, "rider.", 63, 69, null, null); FSIndex<Annotation> sentenceIndex = jCas.getAnnotationIndex(Sentence.type); assertEquals(1, sentenceIndex.size()); Sentence sentence = JCasUtil.selectByIndex(jCas, Sentence.class, 0); assertEquals(text, sentence.getCoveredText()); }
Example #8
Source File: JCasTest.java From uima-uimaj with Apache License 2.0 | 6 votes |
public void testUndefinedType() throws Exception { //create jcas with no type system JCas localJcas = CasCreationUtils.createCas(new TypeSystemDescription_impl(), null, null).getJCas(); localJcas.setDocumentText("This is a test."); try { //this should throw an exception localJcas.getCasType(Sentence.type); fail(); } catch(CASRuntimeException e) { assertEquals(CASRuntimeException.JCAS_TYPE_NOT_IN_CAS, e.getMessageKey()); } //check that this does not leave JCAS in an inconsistent state //(a check for bug UIMA-738) Iterator<Annotation> iter = localJcas.getAnnotationIndex().iterator(); assertTrue(iter.hasNext()); Annotation annot = iter.next(); assertEquals("This is a test.", annot.getCoveredText()); }
Example #9
Source File: UimaTests.java From bluima with Apache License 2.0 | 6 votes |
/** * @param annotations * @param texts * the EXACT corresponding text of ALL the annotations */ public static void assertResultsContainsText( Collection<? extends Annotation> annotations, String... texts) { if (annotations.size() == 0) throw new AssertionError("no annotations found"); if (annotations.size() != texts.length) throw new AssertionError("annotation size not matching text size, " + annotations.size() + " vs " + texts.length); int i = 0; for (Annotation a : annotations) { if (!a.getCoveredText().equals(texts[i])) { throw new AssertionError("Text '" + texts[i] + "' at position " + (i) + " does not match '" + a.getCoveredText() + "'"); } i++; } }
Example #10
Source File: PrintAnnotations.java From uima-uimaj with Apache License 2.0 | 6 votes |
/** * Prints all Annotations to a PrintStream. * * @param aCAS * the CAS containing the FeatureStructures to print * @param aOut * the PrintStream to which output will be written */ public static void printAnnotations(CAS aCAS, PrintStream aOut) { // Version 3 using select with Stream support aCAS.select(Annotation.class).forEach(fs -> printFS(fs, aCAS, 0, aOut)); // // Version 3 using select with extended for // for (Annotation fs : aCAS.getAnnotationIndex().select(Annotation.class)) { // printFS(fs, aCAS, 0, aOut); // } // // // version 2 style using iterators // FSIterator<AnnotationFS> iter = aCAS.getAnnotationIndex().iterator(); // // // iterate // while (iter.isValid()) { // FeatureStructure fs = iter.get(); // printFS(fs, aCAS, 0, aOut); // iter.moveToNext(); // } }
Example #11
Source File: UimaSupport.java From baleen with Apache License 2.0 | 6 votes |
/** * Remove an annotation to the JCas index, notifying UimaMonitor of the fact we have done so. * * <p>Relations that refer to the given annotation will also be removed. * * @param annotations Annotation(s) to remove */ public void remove(Collection<? extends Annotation> annotations) { for (Annotation annot : annotations) { if (annot instanceof Recordable) { try { addToHistory( annot.getCAS().getJCas(), HistoryEvents.createAdded((Recordable) annot, referrer)); } catch (CASException e) { monitor.error("Unable to add to history on remove", e); } } if (annot instanceof Entity) { for (Relation r : getRelations((Entity) annot)) { monitor.entityRemoved(r.getType().getName()); r.removeFromIndexes(); } } monitor.entityRemoved(annot.getType().getName()); annot.removeFromIndexes(); } }
Example #12
Source File: TestMistAnalysisEngine.java From ctakes-docker with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws Exception { // TypeSystemDescription tsd = TypeSystemDescriptionFactory.createTypeSystemDescriptionFromPath("../desc/TypeSystem.xml"); JCas jcas = JCasFactory.createJCas(); jcas.setDocumentText("Patient is a 30-year-old man named Leroy Butler from Green Bay, WI."); AnalysisEngineDescription aed = AnalysisEngineFactory.createEngineDescription(MistAnalysisEngine.class, MistAnalysisEngine.PARAM_MODEL_PATH, "SHARP/model/model"); SimplePipeline.runPipeline(jcas, aed); for(Annotation annot : JCasUtil.select(jcas, Annotation.class)){ System.out.println("Found annotation: " + annot.getCoveredText()); } JCas deidView = jcas.getView(MistAnalysisEngine.DEID_VIEW_NAME); System.out.println("Deidentified version:"); System.out.println(deidView.getDocumentText()); }
Example #13
Source File: SimpleNormalizerAnnotator.java From bluima with Apache License 2.0 | 6 votes |
@Override public void process(JCas jCas) throws AnalysisEngineProcessException { for (Keep k : select(jCas, Keep.class)) { Annotation a = k.getEnclosedAnnot(); String normalized = null; // sometimes, Tokens already have a lemma form, use this one. if (a instanceof Token) { normalized = ((Token) a).getLemmaStr(); } // Simple normalizer if (normalized == null) normalized = a.getCoveredText().trim(); if (!caseSensitive) normalized = normalized.toLowerCase(); k.setNormalizedText(normalized); } }
Example #14
Source File: CopyAnnotationsAnnotatorTest.java From bluima with Apache License 2.0 | 6 votes |
@Test public void testDoNotDelete() throws Exception { AnalysisEngineDescription copyAnnots = createEngineDescription( CopyAnnotationsAnnotator.class, // FROM_ANNOTATION, Protein.class.getName(),// TO_ANNOTATION, Measure.class.getName(),// DELETE_FROM, false); JCas jCas = getTestCas(); assertEquals("has a DocumentAnnotation at first", 1, select(jCas, Annotation.class).size()); Protein p = new Protein(jCas, 5, 10); p.addToIndexes(); runPipeline(jCas, copyAnnots); assertTrue(exists(jCas, Protein.class)); }
Example #15
Source File: Subiterator.java From uima-uimaj with Apache License 2.0 | 6 votes |
/** * * @param forward * @return true if iterator still valid, false if not valid */ private boolean adjustForStrictNvc_forward() { if (isStrict) { Annotation item = it.getNvc(); while (item.getEnd() > this.boundEnd) { it.moveToNextNvc(); if (!isValid()) { return false; } item = it.getNvc(); if (item.getBegin() > this.boundEnd) { // not >= because could of 0 length annot at end makeInvalid(); return false; } } return true; } else { return true; } }
Example #16
Source File: JCasUtils.java From termsuite-core with Apache License 2.0 | 6 votes |
public static void showSdiWithCategory(JCas jcas) { FSIterator<Annotation> it = jcas.getAnnotationIndex(WordAnnotation.type).iterator(); int wordCnt = 0; while(it.hasNext()) { wordCnt++; WordAnnotation a = (WordAnnotation) it.next(); System.out.print(a.getCoveredText() + "_" + a.getTag()); if(wordCnt < 12) { System.out.print(" "); } else { System.out.println(); wordCnt = 0; } } System.out.println(Joiner.on(" ").join(it)); }
Example #17
Source File: CopyAnnotationsAnnotator2.java From bluima with Apache License 2.0 | 5 votes |
public static Annotation copyAnnotationToView(Annotation a, JCas view) { // To copy the annotation we must process in three steps // 1- Clone the annotation from the original view Annotation a2 = (Annotation) a.clone(); // 2- Change the Sofa of the cloned annotation Feature sofaFeature = a2.getType().getFeatureByBaseName("sofa"); a2.setFeatureValue(sofaFeature, view.getSofa()); // 3- Add this annotation to the indexes of the new view a2.addToIndexes(view); return a2; }
Example #18
Source File: PrintMissingTest.java From bluima with Apache License 2.0 | 5 votes |
public void process_old(JCas jCas) throws AnalysisEngineProcessException { FSIterator<Annotation> it = jCas.getAnnotationIndex().iterator(); StringBuffer sb = new StringBuffer(); while (it.hasNext()) { Annotation a = it.next(); System.out.println(a.getType().getName()); sb.append(a.getCoveredText() + '\n'); a.prettyPrint(2, 2, sb, false); sb.append('\n'); } }
Example #19
Source File: CasSerializerSupport.java From uima-uimaj with Apache License 2.0 | 5 votes |
public int compare(TOP fs1, TOP fs2) { int c = Integer.compare(fs1._getTypeImpl().getCode(), fs2._getTypeImpl().getCode()); if (c != 0) { return c; } // final boolean hasSofa = tsi.subsumes(tsi.annotBaseTypeCode, typeCode1); // if (hasSofa) { // c = compareFeat(o1, o2, tsi.annotSofaFeatCode); // if (c != 0) { // return c; // } if (fs1 instanceof Annotation) { Annotation fs1a = (Annotation) fs1; Annotation fs2a = (Annotation) fs2; c = Integer.compare(fs1a.getBegin(), fs2a.getBegin()); if (c != 0) return c; c = Integer.compare(fs2a.getEnd(), fs1a.getEnd()); // reverse order if (c != 0) return c; // fall thru to do id compare } // not annotation, or equal begin/end/type return Integer.compare(fs1._id, fs2._id); // return in @id order }
Example #20
Source File: FilterIfNotRodent.java From bluima with Apache License 2.0 | 5 votes |
@Override public void process(JCas jCas) throws AnalysisEngineProcessException { boolean hasRodent = false; for (LinnaeusSpecies sp : select(jCas, LinnaeusSpecies.class)) { // e.g. species:ncbi:9685 int species = parseInt(sp.getMostProbableSpeciesId().substring( "species:ncbi:".length())); if (NCBI_MURIDAE.contains(species)) { hasRodent = true; break; } } if (!hasRodent) { // Copy the tokens into a new collection to avoid // ConcurrentModificationExceptions if (className.equals("all")) { for (TOP t : newArrayList(select(jCas, TOP.class))) t.removeFromIndexes(); } else { for (Annotation a : newArrayList(select(jCas, aClass))) a.removeFromIndexes(); } } }
Example #21
Source File: EventFactory.java From baleen with Apache License 2.0 | 5 votes |
private <T extends Annotation> Optional<T> checkFor( Class<T> type, int startOffset, int endOffset) { try { return Optional.of(JCasUtil.selectSingleAt(jCas, type, startOffset, endOffset)); } catch (IllegalArgumentException e) { return Optional.empty(); } }
Example #22
Source File: JCasBuilder.java From baleen with Apache License 2.0 | 5 votes |
/** * Apply the text and annotations to the jCas. * * <p>Once call once. */ public void build() { jCas.setDocumentText(documentText.toString()); for (Annotation a : annotations) { a.addToIndexes(jCas); } }
Example #23
Source File: IndexRepositoryTest.java From uima-uimaj with Apache License 2.0 | 5 votes |
public void testMissingSofaRef() throws Exception { JCas jcas = cas.getJCas(); Annotation a = new Annotation(jcas, 0, 4); FeatureImpl feat = (FeatureImpl) cas.getTypeSystem().getType(CAS.TYPE_NAME_ANNOTATION_BASE) .getFeatureByBaseName(CAS.FEATURE_BASE_NAME_SOFA); a._setFeatureValueNcNj(feat, null); try { jcas.addFsToIndexes(a); } catch (CASRuntimeException e) { assertEquals("SOFAREF_NOT_SET", e.getMessageKey()); return; } fail("required exception not thrown"); // fail }
Example #24
Source File: DatabaseAnnotationWriter.java From bluima with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") @Override public void initialize(UimaContext context) throws ResourceInitializationException { super.initialize(context); inserted = 0; try { db = getDb(db_connection); if (createTableStatement != null && createTableStatement.length() > 0) { db.execute(createTableStatement); LOG.info("created table with {}", createTableStatement); } preparedStatement = db.prepareStatement(insertStatement); annotationClass = (Class<? extends Annotation>) Class .forName(annotation); annotationMethods = newArrayList(); for (String annotationField : annotationFields) { boolean found = false; for (Method m : annotationClass.getMethods()) { if (m.getName().equals("get" + capitalize(annotationField))) { found = true; annotationMethods.add(m); break; } } if (!found) { throw new Exception("field " + annotationField + " not found in class " + annotation); } } checkArgument(annotationMethods.size() == annotationFields.length); } catch (Exception e) { throw new ResourceInitializationException(e); } }
Example #25
Source File: IndexRepositoryMergingTest.java From uima-uimaj with Apache License 2.0 | 5 votes |
public void testIndexes() { FSIndex<Annotation> ix1 = ir.getIndex("Annot Index"); FSIndex<Annotation> ix2 = ir.getIndex("Annot Index2"); FSIndex<Annotation> ix3 = ir.getIndex("Annot Index", annotSubtype); FSIndex<Annotation> ix4 = ir.getIndex("Annot Index Subtype"); assertEquals(ix1, ix2); assertFalse(ix1.equals(cas.getAnnotationIndex())); assertFalse(ix1.equals(ix3)); assertEquals(ix3, ix4); }
Example #26
Source File: AnnotatorTestBase.java From baleen with Apache License 2.0 | 5 votes |
/** * Process the {@link AnnotatorTestBase} jCas object looking for supplied annotations. * * <p>You must call processJcas (or equivalent first). * * @param size total number of annotations to expect of this type * @param annotationClass the annotation class to look for * @param annotations (a subset of) annotations to test * @throws AnalysisEngineProcessException * @throws ResourceInitializationException */ @SafeVarargs protected final <T extends Annotation> void assertAnnotations( int size, Class<T> annotationClass, TestAnnotation<T>... annotations) throws AnalysisEngineProcessException, ResourceInitializationException { assertEquals(size, JCasUtil.select(jCas, annotationClass).size()); for (TestAnnotation<T> a : annotations) { T t = JCasUtil.selectByIndex(jCas, annotationClass, a.getIndex()); a.validate(t); } }
Example #27
Source File: AnnotationUtilsTest.java From baleen with Apache License 2.0 | 5 votes |
@Test public void testGetSingleCovered() { final Annotation a = new Annotation(jCas); a.setBegin(0); a.setEnd(4); final Optional<Annotation> single = AnnotationUtils.getSingleCovered(Annotation.class, a); Assert.assertEquals("012", single.get().getCoveredText()); }
Example #28
Source File: CopyAnnotationsAnnotator2Test.java From bluima with Apache License 2.0 | 5 votes |
@Test public void test() throws Exception { AnalysisEngineDescription copyAnnots = createEngineDescription( CopyAnnotationsAnnotator2.class, // TO_VIEW, "blah",// PARAM_ANNOTATION_CLASS, Protein.class.getName(),// DELETE_FROM, true); JCas jCas = getTestCas(); assertEquals("has a DocumentAnnotation at first", 1, select(jCas, Annotation.class).size()); Protein p = new Protein(jCas, 5, 10); p.addToIndexes(); runPipeline(jCas, copyAnnots); assertTrue("no more Protein in initial view", !exists(jCas, Protein.class)); JCas newView = jCas.getView("blah"); Collection<Protein> pNew = select(newView, Protein.class); assertEquals("copied to new view", 1, pNew.size()); assertTrue(haveSameBeginEnd(p, pNew.iterator().next())); // copy it back AnalysisEngineDescription copyAnnotsBack = createEngineDescription( CopyAnnotationsAnnotator2.class, // FROM_VIEW, "blah",// TO_VIEW, BlueUima.VIEW_SYSTEM,// PARAM_ANNOTATION_CLASS, Protein.class.getName(),// DELETE_FROM, true); runPipeline(jCas, copyAnnotsBack); assertTrue(!exists(newView, Protein.class)); Collection<Protein> pBack = select(jCas, Protein.class); assertEquals("copied to protein", 1, pBack.size()); assertTrue(haveSameBeginEnd(p, pBack.iterator().next())); }
Example #29
Source File: UimaSupport.java From baleen with Apache License 2.0 | 5 votes |
private void addMergeToHistory(Annotation keep, Annotation removed) { if (keep instanceof Recordable && removed instanceof Base) { Recordable r = (Recordable) keep; Base b = (Base) removed; try { addToHistory( keep.getCAS().getJCas(), HistoryEvents.createMerged(r, referrer, b.getInternalId())); } catch (CASException e) { monitor.error("Unable to add merge to history", e); } } }
Example #30
Source File: EvaluationPreprocessorAnnotator.java From bluima with Apache License 2.0 | 5 votes |
@Override public void process(JCas jCas) throws AnalysisEngineProcessException { // because of later evaluation, copy annotation to view_gold (later used // by AnnotationEvaluator) and remove it from view_system. Collection<? extends Annotation> goldsFromInitialView = select(jCas, goldAnnotation); JCas goldView = null; try { goldView = jCas.createView(VIEW_GOLD); } catch (Throwable e) { throw new AnalysisEngineProcessException( NO_RESOURCE_FOR_PARAMETERS, new Object[] { VIEW_GOLD }, e); } CasCopier casCopier = new CasCopier(jCas.getCas(), goldView.getCas()); goldView.setDocumentText(jCas.getDocumentText()); // view_system annot. stored in List for later delete // (conccurentModifExeption) List<Annotation> toDelete = new ArrayList<Annotation>(); for (Annotation g : goldsFromInitialView) { goldView.addFsToIndexes(casCopier.copyFs(g)); if (deleteFrom) { toDelete.add(g); } } Annotation[] arr = toDelete.toArray(new Annotation[toDelete.size()]); for (int i = 0; i < arr.length; i++) { arr[i].removeFromIndexes(jCas); } }