简体   繁体   English

Jena - 从任何语言的本体类中获取标签

[英]Jena - getting a label from an ontology class in any language

For user interface purposes I would like to find a label for a given ontology class within Jena. 出于用户界面的目的,我想在Jena中找到给定本体类的标签。 The label should be in the language preferred by the user, if possible, but if no label is available in the user's language, I would like to show just any label, and hope the user manages to understand. 如果可能,标签应该是用户首选的语言,但如果没有用户语言的标签,我想显示任何标签,并希望用户设法理解。 To do that I would like to loop over all available labels, and then pick one (maybe just the first one for the first attempt). 要做到这一点,我想循环所有可用的标签,然后选择一个(可能只是第一次尝试的第一个)。

So far the code looks like: 到目前为止代码看起来像:

import com.hp.hpl.jena.ontology.OntClass;
public class LabelUtils {
    public static String getLabel(OntClass ontClass, String preferredLanguage) {
        String label = ontClass.getLabel(preferredLanguage);
        if (label == null) {
            // here find another label, in any language, whatever
        }
        return label;
    }
}

How do I list all labels, no matter what the language? 无论语言是什么,我如何列出所有标签? I only found a method ontClass.listLabels(String language) which - of course - only returns labels for that language. 我只找到了一个方法ontClass.listLabels(String language) ,当然 - 它只返回该语言的标签。

I can just assume that an English label always exist, or loop over a list of hard wired locales that are checked for labels, but that seems really lame. 我可以假设一个英文标签总是存在,或者循环遍历检查标签的硬连线语言环境列表,但这看起来真的很蹩脚。

Rob Hall's answer works, but if you want to use the OntResource interface methods, you can use something closer to your original approach. Rob Hall的答案很有效,但是如果你想使用OntResource接口方法,你可以使用更接近原始方法的东西。 Note what the Javadoc for OntResource#listLabels says (emphasis added) the follwing. 注意什么是OntResource的Javadoc #listLabels (强调添加)后面的内容。

listLabels listLabels

com.hp.hpl.jena.util.iterator.ExtendedIterator listLabels(String lang) com.hp.hpl.jena.util.iterator.ExtendedIterator listLabels(String lang)

Answer an iterator over all of the label literals for this resource. 在此资源的所有标签文字上回答迭代器。

Parameters: 参数:

  • lang - The language tag to restrict the listed comments to, or null to select all [labels] lang - 限制列出的注释的语言标记, 或null以选择所有[标签]

Labels are related to elements in OWL through the rdfs:label property. 标签通过rdfs:label属性与OWL中的元素相关。

To verify this, we can start with a minimalistic model created through the following means: 为了验证这一点,我们可以从通过以下方式创建的简约模型开始:

final OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);
final OntClass clazz = model.createClass("urn:ex:class");
clazz.addLabel("className1", "en");
clazz.addLabel("className2", "fr");

Writing out the model as N3, we get the following content: 将模型写为N3,我们得到以下内容:

<urn:ex:class>
      a       owl:Class ;
      rdfs:label "className1"@en , "className2"@fr .

Now, because OntClass inherits listPropertyValues from OntResource , we can work with each declared label programmatically like this: 现在,因为OntClass继承listPropertyValuesOntResource ,我们可以与每个标签的申报程序是这样工作的:

final NodeIterator labels = clazz.listPropertyValues(RDFS.label);
while( labels.hasNext() ) {
    final RDFNode labelNode = labels.next();
    final Literal label = labelNode.asLiteral();
    System.out.println( label.getLanguage() );
}

For out example application, the following output should hopefully demonstrate that you can reflect on the language tag explicitly. 对于示例应用程序,以下输出应该有希望证明您可以明确地反映语言标记。

fr
en

I found this question today, and I thought the Rob Hall`s solution interesting because, using the method listLabels, you can list only the rdfs: labels. 我今天发现了这个问题,我认为Rob Hall的解决方案很有意思,因为使用listLabels方法,你只能列出rdfs:标签。 Using that approach, you can list, for example, the labels defined with the vocabulary SKOS (skos: altLabel and skos: prefLabel). 使用该方法,您可以列出,例如,使用词汇表SKOS定义的标签(skos:altLabel和skos:prefLabel)。

Posting this as an answer because I dont have reputation to comment... 将此作为答案发布,因为我没有评论的声誉......

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

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