繁体   English   中英

在UIMA中访问注释

[英]Accessing annotations in UIMA

UIMA中是否有一种方法可以像在CAS调试器GUI中一样从令牌访问注释? 您当然可以从索引存储库访问所有注释,但是我想循环访问令牌,并为每个令牌获取所有关联的注释。

这样做的原因很简单,我想检查一些注释并丢弃其他注释,这样便容易得多。 任何帮助表示赞赏:)

我是uimaFIT开发人员。

如果要在另一个注解的边界内找到所有注解,则可以选择较短且较快的变体

JCasUtil.selectCovered(referenceAnnotation, <T extends ANNOTATION>);

请注意,创建具有所需偏移量的“虚拟”注释并在其边界内进行搜索不是一个好主意,因为这会立即在CAS中分配内存,除非收集了完整的CAS,否则不会进行垃圾收集。

在搜索并询问了cTAKES(Apache临床文本分析和知识提取系统)的开发人员之后。 您可以使用以下库“ uimafit”,该库可在http://code.google.com/p/uimafit/上找到。 可以使用以下代码

List list = JCasUtil.selectCovered(jcas, <T extends Annotation>, startIndex, endIndex);

这将返回两个索引之间的所有值。

希望能有所帮助

如果您不想使用uimaFIT,则可以创建一个过滤的迭代器来遍历感兴趣的注释。 UIMA参考文档在这里: UIMA参考文档

我最近在某些代码中使用了这种方法来查找包含正则表达式注释的句子注释(此方法对于我们的项目是可以接受的,因为所有正则表达式匹配项都比文档中的句子短,并且每个句子只有一个正则表达式匹配项。显然,根据索引shorterAnnotationType规则,您的shorterAnnotationType可能会有所不同。如果您担心碰到另一个shorterAnnotationType ,请将内部代码放入while循环中):

static ArrayList<annotationsPair> process(Annotation shorterAnnotationType, 
        Annotation longerAnnotationType, JCas aJCas){

    ArrayList<annotationsPair> annotationsList = new ArrayList<annotationsPair>();

    FSIterator it = aJCas.getAnnotationIndex().iterator();
    FSTypeConstraint constraint = aJCas.getConstraintFactory().createTypeConstraint();
    constraint.add(shorterAnnotationType.getType());
    constraint.add(longerAnnotationType.getType());
    it = aJCas.createFilteredIterator(it, constraint);

    Annotation a = null;
    int shorterBegin = -1;
    int shorterEnd = -1;
    it.moveTo((shorterAnnotationType));
    while (it.isValid()) {
        a = (Annotation) it.get();
        if (a.getClass() == shorterAnnotationType.getClass()){
            shorterBegin = a.getBegin();
            shorterEnd = a.getEnd();
            System.out.println("Target annotation from " + shorterBegin 
                    + " to " + shorterEnd);
            //because assume that sentence type is longer than other type, 
            //the sentence gets indexed prior
            it.moveToPrevious(); 
            if(it.isValid()){
                Annotation prevAnnotation = (Annotation) it.get();
                if (prevAnnotation.getClass() == longerAnnotationType.getClass()){
                    int sentBegin = prevAnnotation.getBegin();
                    int sentEnd = prevAnnotation.getEnd();
                    System.out.println("found annotation [" + prevAnnotation.getCoveredText()
                            + "] location: " + sentBegin + ", " + sentEnd);
                    annotationsPair pair = new annotationsPair(a, prevAnnotation);
                    annotationsList.add(pair);
                }
                //return to where you started
                it.moveToNext(); //will not invalidate iter because just came from next
            }
        }
        it.moveToNext();
    }

    return annotationsList;

}

希望这可以帮助! 免责声明:我是UIMA的新手。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM