View Source

In this example, instead of the default GATE application, you can load a specific [pre-configured|Configuring the KIM IE Modules] GATE application:
{code:java}...
SemanticAnnotationAPI apiSemAnn1 = serviceKim.getSemanticAnnotationAPI("Custom.gapp");
SemanticAnnotationAPI apiSemAnn2 = serviceKim.getSemanticAnnotationAPI("test.gapp");
SemanticAnnotationAPI apiSemAnn3 = serviceKim.getSemanticAnnotationAPI("IE-no-keywords.gapp");
SemanticAnnotationAPI apiSemAnn4 = serviceKim.getSemanticAnnotationAPI("IE.gapp");
...
{code}

{note} The default KIM is not configured to use alternative GATE applications. Users have to set this manually with applications of their choice.
Annotation names used must end with *.gapp* in order to be correctly recognized.{note}

h2. Annotating documents

To annotate a document, you either have to create a new document or you can use a document loaded from persistence. You will also need a {{SemanticAnnotationAPI}} component.
{code:java}// ----------------------------------------------------------------------------------
// annotate documents
// ----------------------------------------------------------------------------------
URL url = new URL("http://www.ontotext.com/kim");
KIMDocument kdoc = apiCorpora.createDocument(url, "UTF-8");
apiSemAnn.execute(kdoc);
System.out.println("Document created from URL and annotated.");
// ----------------------------------------------------------------------------------
{code}


h2. Annotating texts

To annotate a text, just pass some string to the method.
{code:java}
// ----------------------------------------------------------------------------------
// annotate texts
// ----------------------------------------------------------------------------------
String content =
"Blair and Bush ? are they doing the right thing for Iraq, America," +
" Europe, the Earth... for civilization... " +
"or just guided by their blinded eyes are in favor of the big coporations:" +
"enter here new unrecognized corporations with a clue suffix:" +
"MicroZoftRR Inc.";
apiSemAnn.execute(content);
System.out.println("Free text annotated.");
// ----------------------------------------------------------------------------------
{code}

h2. Using specific annotation strategy



Using annotation strategies affects the generated annotations. Refer to JavaDoc for details on annotation strategies.
{code:java}
// ----------------------------------------------------------------------------------
// use specific annotation strategy
// ----------------------------------------------------------------------------------
// this does not change the running strategy of the annotation service
apiSemAnn.execute(kdoc, apiSemAnn.RUNNING_STRATEGY_DEFAULT);

// results in returning of all generated annotations and of the disabling of instance
// and relations generation and disabling of Ortho Matcher
apiSemAnn.execute(kdoc, apiSemAnn.RUNNING_STRATEGY_DISABLE_ORTHO_MATCHER | apiSemAnn.RUNNING_STRATEGY_DISABLE_INSTANCE_GENERATOR);
// ----------------------------------------------------------------------------------
{code}



h2. Using specific encoding


Using encoding affects the resulting annotations content, different charsets allow you to use various symbols.
{code:java}
// ----------------------------------------------------------------------------------
// use specific encoding
// ----------------------------------------------------------------------------------
apiSemAnn.execute(content, "UTF-8");
apiSemAnn.execute(content, "UTF-8", apiSemAnn.RUNNING_STRATEGY_DEFAULT);
System.out.println("Annotating using specific encoding done.");
// ----------------------------------------------------------------------------------
{code}

h2. Inline metadata in texts


When you pass a string to the {{executeAndInline()}} method, it returns the original string together with metadata in-lined according to the given in-line strategy. Refer to JavaDoc for details on inline strategies.
{code:lang=java}
// ----
// inline metadata in texts
// ----
String enrichedContent;

// places the annotation type name on the place of the annotation
enrichedContent = apiSemAnn.executeAndInline(content, InlineConfig.INLINE_STRATEGY_ANN_TYPE);
System.out.println("[ INLINED METADATA BEGIN ]\n" + enrichedContent + "\n[ INLINED METADATA END ]");

// preserves original string and places the localnames of the instances of the
// identifyable types for the others acts as INLINE_STRATEGY_ANN_TYPE
enrichedContent = apiSemAnn.executeAndInline(content, InlineConfig.INLINE_STRATEGY_PRESERVE_AND_LOCAL_NAME);
System.out.println("[ INLINED METADATA BEGIN ]\n" + enrichedContent + "\n[ INLINED METADATA END ]");

// like INLINE_STRATEGY_PRESERVE_AND_LOCAL_NAME but always puts the annotation type
enrichedContent = apiSemAnn.executeAndInline(content, InlineConfig.INLINE_STRATEGY_PRESERVE_AND_TYPE_AND_LOCAL_NAME);
System.out.println("[ INLINED METADATA BEGIN ]\n" + enrichedContent + "\n[ INLINED METADATA END ]");

// preserve the original content for all annotations except Data, Percent, Money, Number
// for all other excluding Position append some sequences with the instance encoded instead
// of the original text annotated
enrichedContent = apiSemAnn.executeAndInline(content, InlineConfig.INLINE_STRATEGY_PRESERVE_BUT_APPEND_CONTEXT_AT_END);
System.out.println("[ INLINED METADATA BEGIN ]\n" + enrichedContent + "\n[ INLINED METADATA END ]");

System.out.println("Annotating using different metadata inlining done.");
// ----
{code}