Question

For user interface purposes I would like to find a label for a given ontology class within 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.

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.

Was it helpful?

Solution 2

Labels are related to elements in OWL through the rdfs:label property.

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:

<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:

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

OTHER TIPS

Rob Hall's answer works, but if you want to use the OntResource interface methods, you can use something closer to your original approach. Note what the Javadoc for OntResource#listLabels says (emphasis added) the follwing.

listLabels

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]

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. Using that approach, you can list, for example, the labels defined with the vocabulary SKOS (skos: altLabel and skos: prefLabel).

Posting this as an answer because I dont have reputation to comment...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top