从 UIMA FSArray 中检索值

Retrieve values from UIMA FSArray

我有一个注释,它具有 FSArray 类型的特征。此功能应包含字符串列表。

FSArray fsArray = (FSArray)annotation.getFeatureValue(fe);

如何从 FSArray 中获取字符串列表?

仅循环 fsArray.toStringArray() returns 字符串 "FSArray" 而不是实际值。

在 UIMA 中从 FSArray 检索值时,有一些重要的概念需要理解:

  • org.apache.uima.cas.Type - Type 描述了数据模型。它是 类似于java中类的概念。类型有一个名称 space 它定义了属性(特征)。
  • org.apache.uima.cas.Feature - 是由类型描述的属性。
  • org.apache.uima.jcas.cas.TOP - 是最通用的类​​型,可以与 java.lang.Object.
  • 进行比较
  • org.apache.uima.cas.Feature结构 - FeatureStructure 最好是 描述为类型的实例。 FeatureStructure 就是你 用于访问数据。

假设我们有以下两种类型:

  • com.a.b.c.ColoredCar
  • com.a.b.c.Car

而我们有下面这句话:

Car A and car B are both blue.

假设之前的 UIMA 阶段使用类型 com.a.b.c.ColoredCar 对整个句子进行了注释,如下所示:

begin: 0
end: 24
color: "blue"
cars: FSArray

我们还假设我们从类型定义中知道特征车是 com.a.b.c.Car 的 FSArray 并且 Car 包含以下值:

begin: 4
end: 5
manufacturer: "Volvo"

begin: 14
end: 15
manufacturer: "Toyota"

接下来的代码将演示如何检索汽车 FSArray 的制造商属性/特征。

public void process(JCas aJCas) throws AnalysisEngineProcessException {
    List<TOP> tops = new ArrayList<TOP>(JCasUtil.selectAll(aJCas));
    List<String> manufacturers = new ArrayList<>();
    for (TOP t : tops) {
        if (t.getType().getName().endsWith("ColoredCar")) {
            Feature carsFeature = t.getType().getFeatureByBaseName("cars");
            FSArray fsArray = (FSArray) t.getFeatureValue(carsFeature);
            FeatureStructure[] arrayStructures = fsArray.toArray();
            for (int i = 0; i < arrayStructures.length; i++) {
                FeatureStructure fs = arrayStructures[i];
                Feature manufacturerFeature = fs.getType().getFeatureByBaseName("cars");
                manufacturers.add(fs.getStringValue(manufacturerFeature) );
            }
        }
    }
}

要深入了解这一点,最好阅读类型系统、堆和索引存储库在 CAS 中的工作原理。