javaowlontologyapache-jena

Retrieval of synonyms of an instance from whole ontology


Individual ind = model.createIndividual("http://www.semanticweb.org/ontologies/Word#Human", isSynonymOf);

    System.out.println( "Synonyms of given instance are:" );

   StmtIterator it =ind.listProperties(isSynonymOf);
    while( it.hasNext() ) {
      Statement stmt = ((StmtIterator) it).nextStatement();
      System.out.println( " * "+stmt.getObject());
    }

Output

Synonyms of given instance are:

  http://www.semanticweb.org/ontologies/Word#Human
  http://www.semanticweb.org//ontologies/Word#Mortal
  http://www.semanticweb.org/ontologies/Word#Person

Problem 1: My output shows whole URI but I need output as under

 Synonyms of given instance are:
 Human
 Mortal
 Person

Problem 2: I have 26 instances and every time I have to mention its URI to show its synonyms. How will I show synonyms of any instance from whole ontology model instead of mentioning URIs again and again. I am using eclipse Mars 2.0 and Jena API


Solution

    1. You can use REGEX or simply Java string operations to extract the substring after #. Note, best practice is to provide human readable representations of URIs and not to encode it in the URI. For instance, rdfs:label is a common property for doing that.

    2. It is simply iterating over all individuals of the ontology which are returned by

      model.listIndividuals()

    Some comments:

    model.listIndividuals().forEachRemaining(ind -> {
        System.out.println("Synonyms of instance " + ind + " are:");
        ind.listPropertyValues(isSynonymOf).forEachRemaining(val -> {
            System.out.println(" * " + val);
        });
    });
    

    Java 6 compatible version:

    ExtendedIterator<Individual> indIter = model.listIndividuals();
    while(indIter.hasNext()) {
        Individual ind = indIter.next();
        System.out.println("Synonyms of instance " + ind + " are:");
        NodeIterator valueIter = ind.listPropertyValues(isSynonymOf);
        while(valueIter.hasNext()) {
            RDFNode val = valueIter.next();
            System.out.println(" * " + val);
        }
    }