Getting Started With the AYLIEN SDK for .Net (C#)

Getting Started With the AYLIEN SDK for .Net (C#)

This is the fourth edition, in our series of blogs on getting started with AYLIEN’s various SDKs. There are SDKs available for Node.js, Python, Ruby, PHP, GO, Java and .Net (C#). For this week’s instalment we’re going to focus on C#.

If you are new to AYLIEN and Text Analysis and you do not have an account yet, you can take a look at our blog on how to get started with the API or alternatively you can go directly to our Getting Started page which will take you through the signup process. We provide a free plan to get started which allows users to make up to 1,000 calls per day to the API for free.

Downloading and Installing the C# SDK

All of our SDK repositories are hosted on Github. You can find the C# repository here. The simplest way to install the repository is with “nuget package manager”. Simply type the following from a command line tool.

nuget install Aylien.TextApi

Alternatively, from Visual Studio under the “Project” Menu choose “Manage Nuget Packages” and search for the AYLIEN package under online packages.

Once you have installed the SDK you’re ready to start coding. In the remainder of this blog we will walk you through making calls using the C# SDK and show the output you should receive in each case.

Configuring the SDK with your AYLIEN credentials

Once you have received your AYLIEN APP_ID and APP_KEY from the signup process and you have downloaded the SDK, you can start making calls by adding the AYLIEN namespace to your C# code.

using Aylien.TextApi;
using System;

And initialising a client with your AYLIEN credentials

Client client = new Client(
                "YOUR_APP_ID", "YOUR_APP_KEY");

When calling the various API you can specify whether you want to analyze a piece of text directly or a URL linking to the text or article you wish to analyze.

Language Detection

First let’s take a look at the language detection endpoint by analyzing the following sentence: ‘What language is this sentence written in?’

You can call this endpoint using the following piece of code.

Language language = client.Language(text: "What language is this sentence written in?");
Console.WriteLine("Text: {0}", language.Text);
Console.WriteLine("Language: {0}", language.Lang);
Console.WriteLine("Confidence: {0}", language.Confidence);

You should receive an output very similar to the one shown below which shows the language detected as English and a confidence score. The confidence score is very close to 1, so, you can be pretty sure it’s correct.

Language Detection Results

Text: What language is this sentence written in?
Language: en
Confidence: 0.9999982

Sentiment Analysis

Next, we’ll look at analyzing the sentence “John is a very good football player” to determine it’s sentiment i.e. whether it’s positive, neutral or negative. The endpoint will also determine if the text is subjective or objective. You can call the endpoint with the following piece of code

Sentiment sentiment = client.Sentiment(text: "John is a very good football player!");
Console.WriteLine("Text: {0}", sentiment.Text);
Console.WriteLine("Sentiment Polarity  : {0}", sentiment.Polarity);
Console.WriteLine("Polarity Confidence  : {0}", sentiment.PolarityConfidence);
Console.WriteLine("Subjectivity  : {0}", sentiment.Subjectivity);
Console.WriteLine("Subjectivity Confidence  : {0}", sentiment.SubjectivityConfidence);

You should receive an output similar to the one shown below. This indicates that the sentence is objective and is positive, both with a high degree of confidence.

Sentiment Analysis Results

Text: John is a very good football player!
Sentiment Polarity  : positive
Polarity Confidence  : 0.999998827276487
Subjectivity  : objective
Subjectivity Confidence  : 0.989682159413825

Article Classification

We’re now going to take a look at the Classification endpoint. The Classification endpoint automatically assigns an article or piece of text to one or more categories making it easier to manage and sort. Our classification is based on IPTC International Subject News Codes and can identify up to 500 categories. The code below analyses a BBC news article about scientists who have managed to slow down the speed of light.

Classify classify= client.Classify(url: "");
Console.Write("nClassification: n");
foreach(var item in classify.Categories)
     Console.WriteLine("Label        :   {0}     ", item.Label.ToString());
     Console.WriteLine("IPTC code    :   {0}     ", item.Code.ToString());
     Console.WriteLine("Confidence   :   {0}     ",                                                                      item.Confidence.ToString());

When you run this code you should receive an output similar to that shown below which assigns the article an IPTC label of “applied science – particle physics” with an IPTC code of 13001004.

Article Classification Results

Label        :   applied science - particle physics
IPTC code    :   13001004
Confidence   :   0.9877892

Hashtag Analysis

Next, we’ll look analyze the same BBC article and extract hashtag suggestions for sharing the article on social media.

Hashtags hashtags = client.Hashtags(url: "");
Console.Write("nHashtags: n");
        foreach(var item in hashtags.HashtagsMember)

You should receive the output shown below.

Hashtag Suggestion Results


Check out our SDKs for node.js, Go, PHP, Python, Java and Ruby if C# isn’t your preferred language. For more information regarding the APIs go to the documentation section of our website.

Let's Talk