View Source

h2. Inspecting document features

To inspect the document features, first you need to check the supported features collection. This collection has a set, containing the keys of all items available there. So walk through the collection requesting items by key.

{code:java}
URL url = new URL("http://www.ontotext.com/kim");
KIMDocument kdoc = apiCorpora.createDocument(url, "UTF-8");

// ----
// inspect document's features
// ----
KIMFeatureMap kimFeatures = kdoc.getFeatures();
if (kimFeatures != null) {
Iterator iterator = kimFeatures.keySet().iterator();
System.out.println("[ Document's Features (begin) ]");
while (iterator.hasNext()) {
Object key = iterator.next();
System.out.println("[key: " + key + "] [feature: " + kimFeatures.get(key) + "]");
}
System.out.println("[ Document's Features (end) ]");
}
// ----
{code}


h2. Inspecting document annotations

Every KIM document has a default annotation set, which is returned by the {{getAnnotations()}} method. This annotation set holds the annotations to the document and they are available by ID or through iterating.

{code:java}
// ----
// inspect document's annotations
// ----
KIMAnnotationSet kimASet = kdoc.getAnnotations();
Iterator annIterator = kimASet.iterator();

System.out.println("[ Document's Annotations (begin) ]");
while (annIterator.hasNext()) {
System.out.println(annIterator.next());
}
System.out.println("[ Document's Annotations (end) ]");
// ----
{code}




h2. Inspecting all annotations of certain type(s)

When you have a certain annotation set, you can request a list of all annotation types used. Then, you can also request a subset of the current annotation set, which contains only annotations of a certain type. If you want to view annotations of more than one type (E.g "Person" and "Organization"), you should build a list of them and pass it to the {{KIMAnnotationSet get()}} method.

{code:java}

// ----
// inspect annotations of all types
// ----
KIMAnnotationSet kimASet = kdoc.getAnnotations();
Set typesSet = kimASet.getAllTypes();
Iterator iterator = typesSet.iterator();

// show annotations of every type separately
System.out.println("[ Document's Typed Annotations (begin) ]");

while (iterator.hasNext()) {
Object key = iterator.next();
KIMAnnotationSet kimFilteredASet = kimASet.get(String.valueOf(key));
Iterator annIterator = kimFilteredASet.iterator();

System.out.println(" = Annotations of type ["+String.valueOf(key)+"] :");
while (annIterator.hasNext()) {
System.out.println(" -- " + annIterator.next());
}
}

System.out.println("[ Document's Typed Annotations (end) ]");
// ----
{code}

{code:java}// ----
// inspect all annotations of certain types
// ----
// show annotations of types given in a set
KIMAnnotationSet kimASet = kdoc.getAnnotations();
HashSet typesNewSet = new HashSet();
typesNewSet.add("Person");
typesNewSet.add("Organization");

KIMAnnotationSet kimFilteredASet = kimASet.get(typesNewSet);
Iterator annIterator = kimFilteredASet.iterator();

System.out.println("[ Document's Filtered by Type Annotations (begin) ]");
System.out.println(" = Annotations of type [ Person and Organization ] :");

while (annIterator.hasNext()) {
System.out.println(" -- " + annIterator.next());
}
System.out.println("[ Document's Filtered by Type Annotations (end) ]");
// ----
{code}


h2. Inspecting annotation features


You can inspect the features for each annotation just like inspecting the document features. First, get the document annotation set. Then, walk through the annotations, get the features collection for each annotation and view the items.

{code:java}
// ----
// inspect each annotation's features
// ----
KIMAnnotationSet kimASet = kdoc.getAnnotations();
Iterator annIterator = kimASet.iterator();

System.out.println("[ Annotations' Features (begin) ]");
while (annIterator.hasNext()) {
KIMAnnotation kimAnnotation = (KIMAnnotation) annIterator.next();
System.out.println(" = [ Annotation ] : " + kimAnnotation);
KIMFeatureMap kimFeatures = kimAnnotation.getFeatures();

if (kimFeatures != null) {
Iterator iterator = kimFeatures.keySet().iterator();
System.out.println(" = [ Features ] : ");

while (iterator.hasNext()) {
Object key = iterator.next();
System.out.println(" -- [key: " + key + "] [feature: " + kimFeatures.get(key) + "]");
}
System.out.println("");
}
}

System.out.println("[ Annotations' Features (end) ]");
// ----
{code}




h2. Inspecting 'class' and 'inst' features



The following example is similar to the one above. The only difference is that here annotation features are requested by key. 'class' and 'inst' are essential properties of the features. The 'class' property gives the class of the feature according to the RDF repository. The 'inst' property gives the instance of an exact resource, which the feature represents (if any).

{code:java}// ----
// inspect 'class' and 'inst' features
// ----
KIMAnnotationSet kimASet = kdoc.getAnnotations();
Iterator annIterator = kimASet.iterator();

System.out.println("[ Annotations' Specific Features (begin) ]");
while (annIterator.hasNext()) {
KIMAnnotation kimAnnotation = (KIMAnnotation) annIterator.next();
System.out.println(" = [ Annotation ] : " + kimAnnotation.getId() + "]");
KIMFeatureMap kimFeatures = kimAnnotation.getFeatures();

if (kimFeatures != null) {
System.out.println(" -- [Name: " + kimFeatures.get(FeatureConstants.FEATURE_ORIGINAL_NAME) + "]" +
"[Class: " + kimFeatures.get(FeatureConstants.CLASS) + "]" +
"[Instance: " + kimFeatures.get(FeatureConstants.INSTANCE) + "]");
}
}

System.out.println("[ Annotations' Specific Features (end) ]");
// ----
{code}