Java Code Examples for org.apache.uima.jcas.JCas#getAnnotationIndex()

The following examples show how to use org.apache.uima.jcas.JCas#getAnnotationIndex() . 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: ChineseNormalizer.java    From termsuite-core with Apache License 2.0 6 votes vote down vote up
@Override
public void process(JCas cas) throws AnalysisEngineProcessException {
	
	try {
		AnnotationIndex<Annotation> index = cas.getAnnotationIndex(WordAnnotation.type);
		FSIterator<Annotation> iterator = index.iterator();
		while (iterator.hasNext()) {
			WordAnnotation annotation = (WordAnnotation) iterator.next();
			String norm = annotation.getCoveredText();
			annotation.setLemma(norm);
			annotation.setStem(norm);
		}
	} catch (Exception e) {
		throw new AnalysisEngineProcessException(e);
	}
}
 
Example 2
Source File: CasExporter.java    From termsuite-core with Apache License 2.0 6 votes vote down vote up
protected String getExportFilePath(JCas cas, String extension) {
	AnnotationIndex<Annotation> index = cas.getAnnotationIndex(SourceDocumentInformation.type);
	FSIterator<Annotation> iterator = index.iterator();
	if (iterator.hasNext()) {
		SourceDocumentInformation annotation = (SourceDocumentInformation) iterator.next();
		File file = new File(annotation.getUri());
		String name = file.getName();
		int i = name.lastIndexOf('.');
		if (i == -1) {
			return name + "." + extension;
		} else {
			return name.substring(0, i) + "." + extension;
		}
	} else {
		return null;
	}
}
 
Example 3
Source File: XmiCasExporter.java    From termsuite-core with Apache License 2.0 6 votes vote down vote up
protected String getExportFilePath(JCas cas) {
	AnnotationIndex<Annotation> index = cas.getAnnotationIndex(SourceDocumentInformation.type);
	FSIterator<Annotation> iterator = index.iterator();
	if (iterator.hasNext()) {
		SourceDocumentInformation annotation = (SourceDocumentInformation) iterator.next();
		File file = new File(annotation.getUri());
		String name = file.getName();
		int i = name.lastIndexOf('.');
		if (i == -1) {
			return name + ".xmi";
		} else {
			return name.substring(0, i) + ".xmi";
		}
	} else {
		return null;
	}
}
 
Example 4
Source File: LeaveOnlyKeepsEnclosedAnnotationsAnnotator.java    From bluima with Apache License 2.0 6 votes vote down vote up
@Override
public void process(JCas jCas) throws AnalysisEngineProcessException {

    // store annotations referenced by Keeps in a whitelist
    // (NOT the Keep annotation itself, this one gets deleted as well)
    Set<Integer> annotsToKeepIndex = newHashSet();
    for (Keep k : select(jCas, Keep.class))
        annotsToKeepIndex.add(k.getEnclosedAnnot().getAddress());

    // prune all other content-annotations
    List<Annotation> toDelete = newArrayList();
    for (Annotation a : jCas.getAnnotationIndex()) {

        if (!NON_CONTENT_ANNOTATIONS.contains(a.getClass().getName())
                && !annotsToKeepIndex.contains(a.getAddress())) {
            toDelete.add(a);
        }
    }
    Annotation[] arr = toDelete.toArray(new Annotation[toDelete.size()]);
    for (int i = 0; i < arr.length; i++) {
        arr[i].removeFromIndexes(jCas);
    }
}
 
Example 5
Source File: AnnotationVerifier.java    From biomedicus with Apache License 2.0 5 votes vote down vote up
@Override
public void process(JCas aJCas) throws AnalysisEngineProcessException {
  try {
    Iterator<JCas> viewIterator = aJCas.getViewIterator();

    while (viewIterator.hasNext()) {
      JCas view = viewIterator.next();

      String sofaDataString = view.getSofaDataString();
      int length = sofaDataString != null ? sofaDataString.length() : -1;

      AnnotationIndex<Annotation> annotationIndex = view.getAnnotationIndex();

      for (Annotation annotation : annotationIndex) {
        int begin = annotation.getBegin();
        int end = annotation.getEnd();

        if (begin > end) {
          LOGGER
              .error("Annotation {} begin {} after end {}", annotation.getType().getName(), begin,
                  end);
        }

        if (begin < 0) {
          LOGGER.error("Annotation {} begin {} before 0", annotation.getType().getName(), begin);
        }

        if (end > length) {
          LOGGER.error("Annotation {} end {} after length of sofa {}",
              annotation.getType().getName(),
              end, length);
        }

      }
    }

  } catch (CASException e) {
    throw new AnalysisEngineProcessException(e);
  }
}
 
Example 6
Source File: Blacklist.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Override
public void doProcess(JCas aJCas) throws AnalysisEngineProcessException {
  Entity e;
  try {
    e = (Entity) et.getConstructor(JCas.class).newInstance(aJCas);
  } catch (Exception ex) {
    throw new AnalysisEngineProcessException(ex);
  }

  Set<Entity> toRemove = new HashSet<>();

  FSIndex<Annotation> index = aJCas.getAnnotationIndex(e.getType());
  for (Annotation a : index) {
    Entity entity = (Entity) a;

    String coveredText = entity.getCoveredText();
    String value = entity.getValue();

    if (isBlacklisted(coveredText) || (checkEntityValue && isBlacklisted(value))) {
      getMonitor().info("Removing entity '{}' because it appears on the blacklist", coveredText);
      toRemove.add(entity);
    }

    getMonitor().debug("{} has removed {} entities", this.getClass().getName(), toRemove.size());
  }
  for (Entity ent : toRemove) {
    removeFromJCasIndex(ent);
  }
}
 
Example 7
Source File: LatvianTildeTagger.java    From termsuite-core with Apache License 2.0 5 votes vote down vote up
@Override
public void process(JCas cas) throws AnalysisEngineProcessException {
	try {
		AnnotationIndex<Annotation> index = cas.getAnnotationIndex(WordAnnotation.type);
		FSIterator<Annotation> iterator = index.iterator();
		while (iterator.hasNext()) {
			WordAnnotation annotation = (WordAnnotation) iterator.next();
			String tag = annotation.getTag().toLowerCase();
			this.setCategory(annotation, tag);
		}
	} catch (Exception e) {
		throw new AnalysisEngineProcessException(e);
	}
}
 
Example 8
Source File: TSVCasSerializer.java    From termsuite-core with Apache License 2.0 5 votes vote down vote up
public static void serialize(JCas cas, Writer writer) throws IOException {
AnnotationIndex<Annotation> index = cas
		.getAnnotationIndex(WordAnnotation.type);
WordAnnotation word;
for (Annotation annot : index) {
	word = (WordAnnotation) annot;
	writer.append(word.getCoveredText()).append('\t');
	writer.append(word.getCategory()).append('\t');
	writer.append(word.getLemma()).append('\n');
}
      writer.close();
  }
 
Example 9
Source File: BlueCasUtil.java    From bluima with Apache License 2.0 5 votes vote down vote up
/**
 * @return all annotations of this jcas that start and end at the same char
 *         as annotIn
 */
public static List<Annotation> findOverlapping(JCas jcas, Annotation annotIn) {

    List<Annotation> overlappings = new ArrayList<Annotation>();
    for (Annotation a : jcas.getAnnotationIndex()) {

        if (a.getBegin() == annotIn.getBegin()
                && a.getEnd() == annotIn.getEnd()) {
            overlappings.add(a);
        }
    }

    return overlappings;
}
 
Example 10
Source File: AnnotationTypeWriter.java    From bluima with Apache License 2.0 5 votes vote down vote up
@Override
public void process(JCas jcas) throws AnalysisEngineProcessException {
    try {
        for (Annotation annotation : jcas.getAnnotationIndex()) {
            if (annotation.getType().getName().equals(annotationClass)) {
                LOG.trace("printing " + annotation.getCoveredText());
                if (featureName != null) {
                    Feature feature = annotation.getType()
                            .getFeatureByBaseName(featureName);
                    String featureStr = annotation
                            .getFeatureValueAsString(feature);

                    if (!(filterFeaturesWithValue != null && featureStr
                            .equals(filterFeaturesWithValue))) {
                        writer.append(annotation.getCoveredText() + "<"
                                + featureStr + ">");
                        if (newLine) {
                            writer.println();
                        } else {
                            writer.append(" ");
                        }
                    }
                } else {
                    writer.append(annotation.getCoveredText());
                    if (newLine) {
                        writer.println();
                    } else {
                        writer.append(" ");
                    }
                }
            }
        }
        //writer.println();
        writer.flush();

    } catch (Exception e) {
        throw new AnalysisEngineProcessException(e);
    }
}
 
Example 11
Source File: AnnotationTypeWriter2.java    From bluima with Apache License 2.0 5 votes vote down vote up
@Override
public void process(JCas jCas) throws AnalysisEngineProcessException {
    
    String pmId = getHeaderDocId(jCas);
    
    try {
        for (Annotation a : jCas.getAnnotationIndex()) {
            if (a.getClass().getName().equals(annotationClass)) {
                String featureStr;
                if (featureName != null) {
                    featureStr = a.getFeatureValueAsString(a.getType()
                            .getFeatureByBaseName(featureName));
                } else {
                    featureStr = a.getCoveredText();
                }

                if (!(filterFeaturesWithValue != null && featureStr
                        .equals(filterFeaturesWithValue))) {

                    writer.append(format("{}\t{}\t{}\t{}\n",//
                            pmId, a.getBegin(), a.getEnd(), featureStr));
                }
            }
        }
        writer.flush();

    } catch (Exception e) {
        LOG.warn("could not process " + pmId, e);
    }
}
 
Example 12
Source File: SubiteratorTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
private void iterateAndcheck(JCas jcas) {
  AnnotationIndex<Token> tokenIndex = jcas.getAnnotationIndex(Token.class);
  Annotation sentence = jcas.getAnnotationIndex(Sentence.class).iterator().next();
  FSIterator<Token> tokenIterator = tokenIndex.subiterator(sentence);
  Annotation token = tokenIndex.iterator().next();
  // debug token.toString();
  tokenIterator.moveTo(token); //throws ClassCastException 
  
  // check unambiguous iterator creation
  
  FSIterator<Token> it = tokenIndex.iterator(false);
  it.moveTo(token);
}
 
Example 13
Source File: MeetingAnnotator.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * @see JCasAnnotator_ImplBase#process(JCas)
 */
public void process(JCas aJCas) {
  // get annotation indexes
  FSIndex<RoomNumber> roomNumberIndex = aJCas.getAnnotationIndex(RoomNumber.class);
  FSIndex<DateAnnot> dateIndex = aJCas.getAnnotationIndex(DateAnnot.class);
  FSIndex<TimeAnnot> timeIndex = aJCas.getAnnotationIndex(TimeAnnot.class);

  // store end position of last meeting we identified, to prevent multiple
  // annotations over same span
  int lastMeetingEnd = -1;

  for (RoomNumber room : roomNumberIndex.select()) {
    for (DateAnnot date : dateIndex.select()) {
      for(TimeAnnot time1 : timeIndex.select()) {
        for (TimeAnnot time2 : timeIndex.select()) {

          // times must be different annotations
          if (time1 != time2) {
            // compute the begin and end of the span
            int minBegin = Math.min(Math.min(time1.getBegin(), time2.getBegin()), 
                                    Math.min(date .getBegin(), room .getBegin()));
            int maxEnd = Math.max(Math.max(time1.getEnd(), time2.getEnd()), 
                                  Math.max(date .getEnd(), room .getEnd()));

            // span must be smaller than the window size?
            if (maxEnd - minBegin < mWindowSize && 
              // span must not overlap the last annotation we made
                minBegin > lastMeetingEnd) {
              // annotate
              Meeting mtg = new Meeting(aJCas, minBegin, maxEnd, room, date, time1, time2);
              mtg.addToIndexes();
              lastMeetingEnd = maxEnd;
            }
          }
        }
      }
    }   
  }
}
 
Example 14
Source File: CasAnnotationViewer.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * Refreshes the selected annotation tree.
 * 
 * @param aPosition
 *          the currently selected offset into the document. All annotations overlapping this
 *          point will be rendered in the tree.
 */
private void updateSelectedAnnotationTree(int aPosition) {
  if (this.cas == null || this.selectedAnnotationTreeModel == null) {
    return;
  }
  JCas jcas = null;
  try {
    jcas = this.cas.getJCas();
  } catch (CASException e) {
      e.printStackTrace();
      return;
  }

  DefaultMutableTreeNode root = (DefaultMutableTreeNode) this.selectedAnnotationTreeModel.getRoot();
  root.removeAllChildren();

  AnnotationIndex<Annotation> annotationIndex = jcas.getAnnotationIndex();
  if (annotationIndex == null) {
    return;
  }
  FSIterator<Annotation> annotationIterator = annotationIndex.iterator();
  if (annotationIterator == null || !annotationIterator.hasNext()) {
    return;
  }

  while (annotationIterator.hasNext()) {
    Annotation annotation = annotationIterator.next();
    int begin = annotation.getBegin();
    if (begin > aPosition) {
      // Assuming all annotation objects are sorted by begin/end, if this one passed the given offset,
      // there is no need to keep looking at the subsequent ones.
      break;
    }
    if (annotation.getEnd() > aPosition && this.isMatch(annotation)) {
      // The annotation covers the given position (begins before and ends after the given position).
      this.addAnnotationToTree(annotation);
    }
  }
  this.selectedAnnotationTreeModel.nodeStructureChanged(root);
  // hmmm.. how to get scroll pane to resize properly??
  this.selectedAnnotationTree.treeDidChange();
  // this.selectedAnnotationTree.setPreferredSize(this.selectedAnnotationTree.getSize());
  this.selectedAnnotationTree.revalidate();
  this.horizontalSplitPane.revalidate();
}