Azure cognitive services are services created to make life easier for developers who don’t have a lot of knowledge in Machine Learning and AI. Only with HTTP requests can you obtain functions to see, hear, speak, research, understand, and make decisions in any application.
In this article, we will need the following requirements:
- Azure Account and if you don’t have one, no problem in this link(https://azure.microsoft.com/en-us/free/) you can create your account with $200 credit.
- Dot Net Core (recommend 3.1)
- VS Code
Creating Cognitive Service Endpoint
- First all I strongly recommend check API Documentation in this link: https://westus.dev.cognitive.microsoft.com/docs/services/TextAnalytics.V2.0/operations/56f30ceeeda5650db055a3c7 so that you understand what the API actually delivers and how it happens.
- With the Azure account already created, we will follow the following steps to provide our service.
- Inside the portal, click in Create a resource to open the resource list.
The steps mentioned should look like in the images below.
Figure 1 – Create a resource
Figure 2 – Resource List
In the search box typing Cognitive Services and press enter.
- After that you should see a screen like the image below.
Now click on the Create button to open the screen to configure your service, this new screen you see fields to fill with some information like the name to a resource, subscription if you have more than one, location, pricing tier, and resource group.
This step needs some explanations.
Location: This is where your resource/service is going to stay allocated and the closer to your physical region it means that the latency will be lower in requests.
Subscription: Here you choose which account billing where service will be charged.
Pricing tier: Show how your service will be charged.
Resource group: In which group of the resource your service is.
- Now click the Create button on the page bottom and your service will start the creation.
This process will leave a few minutes, after that you see the page inform that your service was created with a button Go to resource clicking it, you will be taken to Quick start page of your service. At this moment it isn’t necessary to expend time but after the finish of the article goes back in this page to get more information about this API.
- Great your service was created, now we can start to consume in our application. To this click in Keys and Endpoint below Quick start on the side menu, you will see a screen that looks like the following image.
Figure 5 – Key and Endpoint Screen
We can see some important information like Endpoint and two keys that we need when were consume, we service in our application.
Creating .Net Core Console Application
Open VS Core and check if you have plugin to C# enable/installed, like image below
if not please install.
Now you can open any terminal and type this command to create your application.
dotnet new console –name [name your application]
If you want, you can use dotnet build to ensure your application doesn´t have errors and dotnet run to ensure if running correctly.
Now you need to install a package that helps you use cognitive services on the application, so execute this command in your terminal:
dotnet add package Microsoft.Azure.CognitiveServices.Language.TextAnalytics –version 4.0.0
If this process executes with success the csproj from your application will be like this:
Figure 6 – CsProj
Now let’s create a folder called Service inside that create a CS file called ApiKeyServiceClientCredentialsService. In this service add these using:
We will create ApiKeyServiceClientCredentials, this class will inherit ServiceClientCredentials which helps to authenticate the service, we also have a private and read-only property called apikey of type string, a method called ProcessHttpRequestAsync that will receive an HttpRequestMessage called request and CancellationToken with the same name of the type. This method is responsible for the requisition itself and this class will also have the builder receiving the apikey that will come in the construction of this class in the Program class, let’s see how our class was.
In the Program class, we will create the fields being private, static and read-only which will be informed in the creation of the class we created earlier these fields are: key and endpoint, we will also create two static methods one being void which will be the sentimentAnalysisExample and will receive in your signature the ITextAnalyticsClient interface and the second one called authenticateClient which will be of type TextAnalyticsClient, let’s see how it looks.
Note that we have added to class” using Microsoft.Azure.CognitiveServices.Language.TextAnalytics;
” it´s necessary to consume Text Analytics API. In the authenticateClient method, we instantiate the ApiKeyServiceClientCredentials class with the name credentials, passing the key and the endpoint that we created, and instantiating the TextAnalyticsClient class with the name client, which is the API we are using, passing the credentials we have just created and re-using this client.
In the sentimentAnalysisExample method he will receive an ITextAnalyticsClient that we will go to and finally within it we will consume the sentiment analysis service informing a text to be analyzed also informing the language we want to use and finally we return the result of this analysis.
To finish within the main method of our Program class we create a variable called client and give it the value returned from the authenticateClient method, we use this variable in the call of the sentimentAnalysisExample method that will execute our text and display the analysis return by the API and we leave two process instructions for finalizing the application.
Let’s run our application and check the return of this analysis and understand what it means. In the terminal type dotnet run and see the result that occurred, it should be like the image below
What does Sentiment Score: 0.87 mean? The sentiment analysis API returns the decimal value between 0.0 and 1.0 and the closer to 0 it means that the text has a negative connotation, the closer to 1 the text means to be more positive, as simple as that.