Text Analysis


This article will guide you through the process of integrating Azure's Text Analytics API with the controls from the Telerik UI for Xamarin suite. More specifically, we are going to use the RadButton, RadEntry and RadHorizontalLinearGauge controls in a page which can be used to determine whether the text which is entered is in English or not as well as analyze its sentiment. The more positive the input message is - the closer the result will be to 100 on the Gauge scale.

Create a Text Analytics API Account

Before you start, you need to create a new Text Analytics API account through the Azure portal. This has been explained in great detail in this article.

Once you've created the account, you have to obtain the subscription keys that have been generated for you. You will require them later on in order to make API calls from your application.

Create the Application

Once you have activated the Text Analytics service, you can proceed with creating a new Xamarin.Forms application. We are going to use the following page for the purposes of the example:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
<StackLayout Padding="20" Margin="0,40,0,0">
    <telerikInput:RadEntry x:Name="entry" 
                           WatermarkText="Text to analyze..."/>
    <telerikInput:RadButton Text="Analyze"
    <telerikGauges:RadHorizontalGauge x:Name="gauge">
            <telerikGauges:GaugeLinearAxis Maximum="100"
            <telerikGauges:GaugeShapeIndicator Value="{Binding Confidence}" />
                <telerikGauges:GaugeRange Color="Green"
                                  To="80" />
                <telerikGauges:GaugeGradientRange From="80" To="100">
                    <telerikCommon:RadGradientStop Offset="80" Color="Yellow" />
                    <telerikCommon:RadGradientStop Offset="100" Color="Red" />

Add the Microsoft.Azure.CognitiveServices.Language NuGet Package

You can now add the Microsoft.Azure.CognitiveServices.Language NuGet package which provides access to the Microsoft Cognitive Services Language APIs.

Add the Microsoft.Azure.CognitiveServices.Language NuGet Package

Using the Text Analytics API

The following code snippet shows the code-behind of the previously created page where the Text Analytics API is used:

public partial class MainPage : ContentPage, INotifyPropertyChanged

    const string subscriptionKey = "your subscription key";
    private ITextAnalyticsAPI client;
    public MainPage()
        this.BindingContext = this;

        client = new TextAnalyticsAPI();
        client.AzureRegion = AzureRegions.Eastus2;
        client.SubscriptionKey = subscriptionKey;

        btn.Clicked += Btn_ClickedAsync;

    private double confidence;

    public double Confidence
        get { return confidence; }
            this.confidence = value;

    private async Task<double?> MakeAnalysisRequest(string text)
        LanguageBatchResult languageResult = await client.DetectLanguageAsync(
                new BatchInput(
                    new List<Input>()
                        new Input("1", text)

        var language = languageResult.Documents.First().DetectedLanguages.First();

        SentimentBatchResult sentimentResult = await client.SentimentAsync(
            new MultiLanguageBatchInput(new List<MultiLanguageInput>()
                new MultiLanguageInput(language.Iso6391Name, "1", text)

        var sentiment = sentimentResult.Documents.First();

        return (language.Score + sentiment.Score) / 2;

    private async void Btn_ClickedAsync(object sender, EventArgs e)
        var result = await MakeAnalysisRequest(entry.Text);
        this.Confidence = (double)(result * 100);

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

Running the application and typing something positive in English should give you a confidence level close to 100. Here is the actual result:

See Also

Is this article helpful? Yes / No
Thank you for your feedback!

Give article feedback

Tell us how we can improve this article