C Sharp SDK

Skip to end of metadata
Go to start of metadata
Table of Contents
Source code & documentation
The source code and the documentation for the C# SDK are available from the S4 GitHub

Installation Instructions

NuGet integration

  • Open the Package Manager Console of Visual Studio (go to TOOLS > Library Package Manager > Package Manager Console)
  • Now run this command:
    PM> Install-Package Ontotext.S4.ClientAPI
    

Direct Download

NuGet Gallery Download

Source Code

API keys

As described in the API documentation page, access to all S4 REST services require API key/secret pair, which can be generated from the S4 Management Console.

Workflow

Text Annotation

Step 1: Find the news pipeline via the ServiceCatalog:

ServiceDescriptor descriptor = ServiceCatalog.getItem("news");

Step 2: Instantiate a S4AnnotationClient used for communicating with the S4 processing service and specify your API key details:

S4AnnotationClientImpl client = (S4AnnotationClientImpl)ServiceClientsFactory.createAnnotationClient(descriptor, "<api-key>", "<key-secret>");

Alternatively, you can get the pipeline's endpoint URL directly and create the S4AnnotationClient instance with it:

Uri serviceURL = new Uri("https://text.s4.ontotext.com/v1/news");
S4AnnotationClientImpl client = (S4AnnotationClientImpl)ServiceClientsFactory.createAnnotationClient(serviceURL,  "<api-key>", "<key-secret>");

Step 3: Process the sample file content:

FileStream FS = new FileStream("path/to/file/with/content", FileMode.Open, FileAccess.Read, FileShare.Read);
AnnotatedDocument doc = client.annotateDocument(FS, "UTF-8", SupportedMimeType.PLAINTEXT);

Step 4: Retrieve all annotations of a given type:

String text = doc.text;
List < Annotation > annotationsOfType = doc.entities["Person"];

Step 5: Get the annotation values and perform further processing, analysis or visualisation:

foreach(Annotation anno in annotationsOfType)
{
    Console.WriteLine(anno.startOffset);
    Console.WriteLine(anno.endOffset);
}

Text and Images Annotation

Step 1: Find the news pipeline via the ServiceCatalog:

ServiceDescriptor descriptor = ServiceCatalog.getItem("news");

Step 2: Instantiate a S4AnnotationClient used for communicating with the S4 processing service and specify your API key details:

S4AnnotationClientImpl client = (S4AnnotationClientImpl)ServiceClientsFactory.createAnnotationClient(descriptor, "<api-key>", "<key-secret>");

Alternatively, you can get the pipeline's endpoint URL directly and create the S4AnnotationClient instance with it:

Uri serviceURL = new Uri("https://text.s4.ontotext.com/v1/news");
S4AnnotationClientImpl cli = (S4AnnotationClientImpl)ServiceClientsFactory.createAnnotationClient(serviceURL, "<api-key>", "<key-secret>");

Step 3: Process the remote article:

Uri remoteDocumentWithImages = new Uri("http://www.bbc.com/news/uk-36086291");
// the two boolean tags are flags for imageTagging and imageCategorization
AnnotatedDocument doc = client.annotateDocument(remoteDocumentWithImages, SupportedMimeType.HTML, true, true);

Step 4: Retrieve all textual annotations of a given type:

List<Annotation> textAnnotations = doc.entities["Organization"];

Step 5: Retrieve the discovered images and their annotations:

List<ClassifiedImage> annotatedImages = doc.images;

Step 6: Get the annotation values and perform further processing, analysis or visualisation:

foreach(ClassifiedImage img in annotatedImages)
{
    Console.WriteLine("Image '" + img.image + "' properties");
    Console.WriteLine("Tags:");
    foreach (ImageTag imgTag in img.tags)
    {
        Console.WriteLine(imgTag.tag + ": " + imgTag.confidence);
    }
    Console.WriteLine("Categories");
    foreach (ImageCategory cat in img.categories)
    {
        Console.WriteLine(cat.name + ": " + cat.confidence);
    }
}

Text Classification

Step 1: Instantiate a S4ClassificationClient used for communicating with the S4 processing service and specify your API key details:

S4ClassificationClientImpl client = (S4ClassificationClientImpl)ServiceClientsFactory.createClassificationClient("<api-key>", "<key-secret>");

Alternatively, you can get the pipeline's endpoint URL directly and create the S4ClassificationClient instance with it.

Uri classifierURL = new Uri("https://text.s4.ontotext.com/v1/news-classifier");
S4ClassificationClientImpl cli = (S4ClassificationClientImpl)ServiceClientsFactory.createClassificationClient(classifierURL, "<api-key>", "<key-secret>")

Step 2: Send a sample file content for classification:

FileStream FS = new FileStream("path/to/file/with/content", FileMode.Open, FileAccess.Read, FileShare.Read);
ClassifiedDocument doc = client.classifyDocument(FS, "UTF-8", SupportedMimeType.PLAINTEXT);

Step 3: Retrieve the classification information and print it in the console:

Console.WriteLine("Main category: " + doc.category);
Console.WriteLine("Details:");
foreach (ClassificationCategory catInfo in doc.allScores)
{
    Console.WriteLine("- category: " + catInfo.label + ", confidence score: " + catInfo.score);
}

Documentation

Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.