org.apache.uima.cas.text.AnnotationIndex类的使用及代码示例

x33g5p2x  于11个月前 转载在 其他  
字(13.3k)|赞(0)|评价(0)|浏览(36)

本文整理了Java中org.apache.uima.cas.text.AnnotationIndex类的一些代码示例,展示了AnnotationIndex类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。AnnotationIndex类的具体详情如下:
包路径:org.apache.uima.cas.text.AnnotationIndex
类名称:AnnotationIndex

AnnotationIndex介绍

[英]An annotation index provides additional iterator functionality that applies only to instances of uima.tcas.Annotation (or its subtypes). You can obtain an AnnotationIndex by calling:

AnnotationIndex idx = cas.getAnnotationIndex(); or
AnnotationIndex<SomeJCasType> idx = jcas.getAnnotationIndex(SomeJCasType.class);

Note that the AnnotationIndex defines the following sort order between two annotations:

  • Annotations are sorted in increasing order of their start offset. That is, for any annotations a and b, if a.start < b.start then a < b.
  • Annotations whose start offsets are equal are next sorted by decreasing order of their end offsets. That is, if a.start = b.start and a.end > b.end, then a < b. This causes annotations with larger spans to be sorted before annotations with smaller spans, which produces an iteration order similar to a preorder tree traversal.
  • Annotations whose start offsets are equal and whose end offsets are equal are sorted based on org.apache.uima.resource.metadata.TypePriorities (which is an element of the component descriptor). That is, if a.start = b.start, a.end = b.end, and the type of a is defined before the type of b in the type priorities, then a < b.
  • If none of the above rules apply, then the ordering is arbitrary. This will occur if you have two annotations of the exact same type that also have the same span. It will also occur if you have not defined any type priority between two annotations that have the same span.

In the method descriptions below, the notation a < b, where a and b are annotations, should be taken to mean a comes before b in the index, according to the above rules.
[中]注释索引提供了额外的迭代器功能,仅适用于uima.tcas.Annotation(或其子类型)的实例。您可以通过调用以下命令获取AnnotationIndex:
AnnotationIndex idx = cas.getAnnotationIndex();
AnnotationIndex<SomeJCasType> idx = jcas.getAnnotationIndex(SomeJCasType.class);
请注意,AnnotationIndex定义了两个注释之间的以下排序顺序:
*注释按其起始偏移量的递增顺序排序。也就是说,对于任何注释a和b,如果为a.start < b.start,则为a < b
*开始偏移量相等的注释接下来按其结束偏移量的降序排序。也就是说,如果a.start = b.starta.end > b.end,那么a < b。这会导致跨度较大的注释在跨度较小的注释之前排序,从而产生类似于前序树遍历的迭代顺序。
*起始偏移量相等且结束偏移量相等的注释将基于组织进行排序。阿帕奇。尤马。资源元数据。TypePriorities(组件描述符的一个元素)。也就是说,如果a.start = b.starta.end = b.enda类型在类型优先级中的b类型之前定义,则a < b
*如果上述规则均不适用,则排序是任意的。如果有两个类型完全相同且跨度相同的注释,则会发生这种情况。如果没有在具有相同跨度的两个注释之间定义任何类型优先级,也会发生这种情况。
在下面的方法描述中,根据上述规则,符号a < b,其中ab是注释,应被视为表示索引中a位于b之前。

代码示例

代码示例来源:origin: edu.cmu.lti.oaqa.cse/cse-framework

public static Annotation getFirst(JCas jcas, Type type) {
 AnnotationIndex<Annotation> index = jcas.getAnnotationIndex(type);
 FSIterator<Annotation> iterator = index.iterator();
 if (iterator.hasNext())
  return (Annotation) iterator.next();
 return null;
}

代码示例来源:origin: nlpie/biomedicus

private void divideAnnotation(AnnotationFS annotation) {
 Objects.requireNonNull(typeToCreate);
 Objects.requireNonNull(dividers);
 FSIterator<AnnotationFS> subiterator = dividers.subiterator(annotation);
 int begin = annotation.getBegin();
 while (subiterator.hasNext()) {
  int end = subiterator.next().getBegin();
  cas.addFsToIndexes(cas.createAnnotation(typeToCreate, begin, end));
  begin = end;
 }
}

代码示例来源:origin: CLLKazan/UIMA-Ext

@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));
  }
}

代码示例来源:origin: CLLKazan/UIMA-Ext

/**
 * @param cas
 * @param type
 * @param feature
 * @return feature value of the first annotation of given type from given
 *         CAS. E.g., it is useful to get document metadata values. If there
 *         is no such annotation method will return null.
 */
public static String getStringValue(CAS cas, Type type, Feature feature) {
  AnnotationIndex<AnnotationFS> metaIdx = cas.getAnnotationIndex(type);
  if (metaIdx.size() > 0) {
    AnnotationFS meta = metaIdx.iterator().next();
    return meta.getFeatureValueAsString(feature);
  } else {
    return null;
  }
}

代码示例来源:origin: org.apache.uima/ruta-core

private void updateIterators(CAS cas, Type basicType, FilterManager filter,
    AnnotationFS additionalWindow) {
 if (additionalWindow != null) {
  this.basicIt = cas.getAnnotationIndex(basicType).subiterator(additionalWindow);
 } else {
  this.basicIt = cas.getAnnotationIndex(basicType).iterator();
 }
 currentIt = filter.createFilteredIterator(cas, basicType);
}

代码示例来源:origin: org.apache.uima/ruta-core

private File getOutputFile(CAS cas) {
 if (StringUtils.isBlank(outputLocation)) {
  return null;
 }
 Type sdiType = cas.getTypeSystem().getType(RutaEngine.SOURCE_DOCUMENT_INFORMATION);
 String filename = "output.modified.html";
 File file = new File(outputLocation, filename);
 if (sdiType != null) {
  FSIterator<AnnotationFS> sdiit = cas.getAnnotationIndex(sdiType).iterator();
  if (sdiit.isValid()) {
   AnnotationFS annotationFS = sdiit.get();
   Feature uriFeature = sdiType.getFeatureByBaseName("uri");
   String stringValue = annotationFS.getStringValue(uriFeature);
   File f = new File(stringValue);
   String name = f.getName();
   if (!name.endsWith(".modified.html")) {
    name = name + ".modified.html";
   }
   file = new File(outputLocation, name);
  }
 }
 return file;
}

代码示例来源:origin: apache/uima-uimaj

public static FeatureStructure getTcasFS(CAS aCasView, String aTypeS) {
 org.apache.uima.cas.FeatureStructure idFS = null;
 Type type = aCasView.getTypeSystem().getType(aTypeS);
 if (type != null) {
  FSIterator<AnnotationFS> idIter = aCasView.getAnnotationIndex(type).iterator();
  while (idIter.isValid()) {
   idFS = idIter.get();
   idIter.moveToNext();
  }
 }
 return idFS;
}

代码示例来源:origin: org.apache.uima/textmarker-core

public static void addSourceDocumentInformation(CAS cas, File each) {
 Type sdiType = cas.getTypeSystem()
     .getType("org.apache.uima.examples.SourceDocumentInformation");
 if (sdiType != null) {
  if (cas.getAnnotationIndex(sdiType).size() == 0) {
   AnnotationFS sdi = cas.createAnnotation(sdiType, cas.getDocumentAnnotation().getBegin(),
       cas.getDocumentAnnotation().getEnd());
   Feature uriFeature = sdiType.getFeatureByBaseName("uri");
   sdi.setStringValue(uriFeature, each.toURI().getPath());
   cas.addFsToIndexes(sdi);
  }
 }
}

代码示例来源:origin: org.apache.ctakes/ctakes-coreference

public static ArrayList<BaseToken> selectBaseToken (JCas jcas) {
  ArrayList<BaseToken> ret = new ArrayList<BaseToken>();
  FSIterator<?> iter = jcas.getJFSIndexRepository().getAnnotationIndex(BaseToken.type).iterator();
  while (iter.hasNext())
    ret.add((BaseToken)iter.next());
  java.util.Collections.sort(ret, new AnnotOffsetComparator());
  return ret;
}

代码示例来源:origin: org.apache.uima/ruta-core

public Collection<AnnotationFS> getAnnotations(Type type) {
 Collection<AnnotationFS> result = new LinkedList<AnnotationFS>();
 AnnotationFS windowAnnotation = filter.getWindowAnnotation();
 if (windowAnnotation != null
     && (windowAnnotation.getBegin() != cas.getDocumentAnnotation().getBegin()
         || windowAnnotation.getEnd() != cas.getDocumentAnnotation().getEnd())) {
  AnnotationFS frame = cas.createAnnotation(cas.getTypeSystem().getType(RutaEngine.FRAME_TYPE),
      windowAnnotation.getBegin(), windowAnnotation.getEnd());
  FSIterator<AnnotationFS> subiterator = cas.getAnnotationIndex(type).subiterator(frame);
  while (subiterator.hasNext()) {
   AnnotationFS each = subiterator.next();
   if (isVisible(each)) {
    result.add(each);
   }
  }
 } else {
  AnnotationIndex<AnnotationFS> annotationIndex = cas.getAnnotationIndex(type);
  for (AnnotationFS each : annotationIndex) {
   if (isVisible(each)) {
    result.add(each);
   }
  }
 }
 return result;
}

代码示例来源:origin: de.julielab/jcore-bionlp09event-consumer

@Override
public void process(JCas aJCas) throws AnalysisEngineProcessException {
  CAS cas = aJCas.getCas();
  String id = getDocumentId(cas);
  String documentText = cas.getDocumentText();
  Type eventType = cas.getTypeSystem().getType(EventMention.class.getCanonicalName());
  try {
    if (!bioEventServiceMode) {
      DocumentWriter documentWriter = createDocumentWriter(id);
      EventWriter eventWriter = createEventWriter(documentText, id);
      documentWriter.write(cas.getJCas());
      Iterator eventIterator = cas.getAnnotationIndex(eventType).iterator();
      int i = 1;
      while (eventIterator.hasNext()) {
      Iterator eventIterator = cas.getAnnotationIndex(eventType).iterator();
      int i = 1;
      while (eventIterator.hasNext()) {

代码示例来源:origin: edu.cmu.lti.oaqa.cse/cse-framework

public static Annotation getFirst(JCas jcas, String typeName) {
 TypeSystem ts = jcas.getTypeSystem();
 Type type = ts.getType(typeName);
 AnnotationIndex<Annotation> index = jcas.getAnnotationIndex(type);
 FSIterator<Annotation> iterator = index.iterator();
 if (iterator.hasNext()) {
  return (Annotation) iterator.next();
 }
 return null;
}

代码示例来源:origin: org.apache.uima/textmarker-core

public List<AnnotationFS> getOverappingAnnotations(AnnotationFS window, Type type) {
 List<AnnotationFS> result = new ArrayList<AnnotationFS>();
 AnnotationFS newWindow = cas.createAnnotation(type, window.getBegin(), window.getEnd() - 1);
 FSIterator<AnnotationFS> iterator = cas.getAnnotationIndex(type).iterator(newWindow);
 if (!iterator.isValid()) {
  iterator.moveToLast();
 }
 while (iterator.isValid()) {
  FeatureStructure fs = iterator.get();
  if (fs instanceof AnnotationFS) {
   AnnotationFS a = (AnnotationFS) fs;
   if (a.getEnd() >= window.getEnd() && a.getBegin() <= window.getBegin()) {
    result.add(a);
   }
  }
  iterator.moveToPrevious();
 }
 return result;
}

代码示例来源:origin: org.apache.uima/textmarker-core

int size = 0;
try {
 jCas = cas.getJCas();
 size = jCas.getAnnotationIndex(TokenSeed.type).size();
 result = jCas.getTypeSystem().getType(seedType);
} catch (CASException e1) {
 cas.addFsToIndexes(a);
 try {
  a = sourceLexer.yylex();
 MARKUP markup = new MARKUP(jCas, begin, end);
 markup.addToIndexes();
 FSIterator<AnnotationFS> subiterator = cas.getAnnotationIndex(result).subiterator(markup);
 while(subiterator.isValid()) {
  AnnotationFS fs = subiterator.get();
  toRemove.add(fs);
  subiterator.moveToNext();

代码示例来源:origin: org.apache.uima/ruta-core

private boolean check(AnnotationFS annotation, RutaStream stream, Type t) {
 boolean result = false;
 FSIterator<AnnotationFS> it = stream.getCas().getAnnotationIndex(t).iterator(annotation);
 while (it.isValid()) {
  AnnotationFS a = it.get();
  if (a.getEnd() >= annotation.getBegin()) {
   result = true;
   break;
  }
  it.moveToNext();
 }
 return result;
}

代码示例来源:origin: apache/opennlp

/**
 * Create a new combo iterator.
 *
 * @param cas
 *          The CAS we're operating on.
 * @param upper
 *          The type of the upper iterator, e.g., sentence.
 * @param lower
 *          The type of the lower iterator, e.g., token.
 */
public AnnotationComboIterator(CAS cas, Type upper, Type lower) {
 this.upperIt = cas.getAnnotationIndex(upper).iterator();
 this.lowerIt = cas.getAnnotationIndex(lower).iterator();
 this.upperIt.moveToFirst();
 this.lowerIt.moveToFirst();
 if (this.upperIt.isValid()) {
  final AnnotationFS upperFS = this.upperIt.get();
  this.upperBegin = upperFS.getBegin();
  this.upperEnd = upperFS.getEnd();
 } else {
  this.nextLowerChecked = true;
 }
}

代码示例来源:origin: org.apache.ctakes/ctakes-assertion

private static JCas getViewWithGoldAnnotations(JCas correspondingCasThatHasGoldAnnotations) {
  JCas viewWithPreexistingGoldAnnotations = null;
  try {
    viewWithPreexistingGoldAnnotations = correspondingCasThatHasGoldAnnotations.getView(AssertionEvaluation.GOLD_VIEW_NAME);
  } catch (org.apache.uima.cas.CASRuntimeException cre) {
    // Let it just continue if there's an exception and check for null later
  } catch (org.apache.uima.cas.CASException viewException) {
    // Let it just continue if there's an exception and check for null later
  } catch (NullPointerException npe) {
    // Let it just continue if there's an exception and check for null later
  }
  if (viewWithPreexistingGoldAnnotations == null) {
    viewWithPreexistingGoldAnnotations = correspondingCasThatHasGoldAnnotations;
    LOGGER.debug("Using view " + viewWithPreexistingGoldAnnotations.getViewName());
    int n  = viewWithPreexistingGoldAnnotations.getAnnotationIndex().size();
    LOGGER.debug("With " + n + " annotations");
    if (n==0) {
      Iterator<CAS> iter = viewWithPreexistingGoldAnnotations.getCas().getViewIterator();
      while (iter.hasNext()) {
        CAS cas = iter.next();
        LOGGER.debug("view " + cas.getViewName() + " has " + cas.getAnnotationIndex().size() + " indexed annotations.");
        
      }
      throw new RuntimeException("n==0");
    }
  }
  return viewWithPreexistingGoldAnnotations;
}

代码示例来源:origin: org.apache.lucene/lucene-analyzers-uima

@Override
protected void initializeIterator() throws IOException {
 try {
  analyzeInput();
 } catch (AnalysisEngineProcessException | ResourceInitializationException e) {
  throw new IOException(e);
 }
 finalOffset = correctOffset(cas.getDocumentText().length());
 Type tokenType = cas.getTypeSystem().getType(tokenTypeString);
 iterator = cas.getAnnotationIndex(tokenType).iterator();
}

代码示例来源:origin: de.julielab/jcore-bionlp09event-consumer

private String getDocumentId(CAS cas) {
    Header header = null;
    Type headerType = cas.getTypeSystem().getType(Header.class.getCanonicalName());
    Iterator<org.apache.uima.jcas.tcas.Annotation> iterator;
    try {
      iterator = cas.getJCas().getAnnotationIndex(Header.type).iterator();

      header = (Header) iterator.next();
    } catch (CASRuntimeException e) {
      e.printStackTrace();
    } catch (CASException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    if (header != null) {
      return header.getDocId();
    }
    return new Integer(id++).toString();
  }
}

代码示例来源:origin: apache/opennlp

public void process(CAS cas) {

  FSIterator<AnnotationFS> tokenAnnotations = cas.getAnnotationIndex(mTokenType).iterator();
  List<String> tokensList = new ArrayList<>();

  while (tokenAnnotations.hasNext()) {
   tokensList.add(tokenAnnotations.next().getCoveredText());
  }

  double[] result =
    mCategorizer.categorize(tokensList.toArray(new String[tokensList.size()]));

  String bestCategory = mCategorizer.getBestCategory(result);

  setBestCategory(cas, bestCategory);
 }
}

相关文章

最新文章

更多