Text Analytics - Azure Cognitive ServicešŸ˜Š

Through this article, let us take an overview of Microsoft Azure Text Analytics in a simple WPF Application.

Introduction

 
In this article, I am going to explain how to use Azure Text Analytics API in a simple WPF application. Azure Cognitive Services provides the Text Analytics API. It is an advanced cloud-based service that is used for natural language processing. The main functions of Text Analytics API are sentiment analysis, key phrase extraction, named entity recognition, and language detection.
 

Language Detection

 
The Language Detection API returns the detected language. It evaluates the input from the user and returns the language with a score.
 
The score completely depends on the strength of the analysis.
 

How can I use this API in my WPF Application?

 
You can use the Text Analytics API in any of your applications. For example, I am using it with a WPF application. In this article, I will not explain anything in detail about the WPF application. So, just refer to the WPF application and its uses before using Text Analytics API.
 

Let's get started

 
Follow the simple steps given below.
 
Open Visual Studio. Create a new WPF app (.NET Core) project.
 
Text Analytics - Azure Cognitive Service
 
Drag and drop the controls as given below in the screenshot.
 
Text Analytics - Azure Cognitive Service
 
Right-click the solution and select "Manage NuGet Packages for Solution". Download Newtonsoft.json from the list.
 
Text Analytics - Azure Cognitive Service
 
Create a new resource in the Azure portal. Search for Text Analytics in the search pane. Once done, extract the keys and copy paste them on the Notepad.
 
Text Analytics - Azure Cognitive Service
 
Follow the code given below for designing the mainwindow.xaml.
  1. <Grid Margin="-226,-26,-299,-82">    
  2.         <StackPanel HorizontalAlignment="Left" Height="413" Margin="261,50,0,0" VerticalAlignment="Top" Width="736" RenderTransformOrigin="0.5,0.5">    
  3.             <StackPanel.RenderTransform>    
  4.                 <TransformGroup>    
  5.                     <ScaleTransform ScaleX="-1"/>    
  6.                     <SkewTransform/>    
  7.                     <RotateTransform/>    
  8.                     <TranslateTransform/>    
  9.                 </TransformGroup>    
  10.             </StackPanel.RenderTransform>    
  11.             <Label Content="Write Message Here" Height="108" FontSize="48" FontFamily="Segoe UI Black" Margin="236,0,130,0" RenderTransformOrigin="0.5,0.5">    
  12.                 <Label.RenderTransform>    
  13.                     <TransformGroup>    
  14.                         <ScaleTransform ScaleX="-1"/>    
  15.                         <SkewTransform/>    
  16.                         <RotateTransform/>    
  17.                         <TranslateTransform/>    
  18.                     </TransformGroup>    
  19.                 </Label.RenderTransform>    
  20.             </Label>    
  21.             <TextBox Height="49" x:Name="txtbox" TextWrapping="Wrap"  Text="" Margin="13,0,8,0" RenderTransformOrigin="0.5,0.5">    
  22.                 <TextBox.RenderTransform>    
  23.                     <TransformGroup>    
  24.                         <ScaleTransform ScaleX="-1"/>    
  25.                         <SkewTransform/>    
  26.                         <RotateTransform/>    
  27.                         <TranslateTransform/>    
  28.                     </TransformGroup>    
  29.                 </TextBox.RenderTransform>    
  30.             </TextBox>    
  31.             <Button Content="Translate" Height="84" RenderTransformOrigin="0.5,0.5" Margin="173,0,159,0" FontSize="48" FontWeight="Bold" Click="Button_Click">    
  32.                 <Button.RenderTransform>    
  33.                     <TransformGroup>    
  34.                         <ScaleTransform ScaleX="-1"/>    
  35.                         <SkewTransform/>    
  36.                         <RotateTransform/>    
  37.                         <TranslateTransform/>    
  38.                     </TransformGroup>    
  39.                 </Button.RenderTransform>    
  40.             </Button>    
  41.             <Label Content="" x:Name="label1"  Height="87" FontSize="36" Margin="13,0,17,0" RenderTransformOrigin="0.5,0.5">    
  42.                 <Label.RenderTransform>    
  43.                     <TransformGroup>    
  44.                         <ScaleTransform ScaleX="-1"/>    
  45.                         <SkewTransform/>    
  46.                         <RotateTransform/>    
  47.                         <TranslateTransform/>    
  48.                     </TransformGroup>    
  49.                 </Label.RenderTransform>    
  50.             </Label>    
  51.         </StackPanel>      
  52.     </Grid>    
Follow the code given below for mainwindow.xaml.cs. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Net.Http;  
  6. using System.Threading.Tasks;  
  7. using System.Windows;  
  8. using System.Windows.Controls;  
  9. using System.Windows.Data;  
  10. using System.Windows.Documents;  
  11. using System.Windows.Input;  
  12. using System.Windows.Media;  
  13. using System.Windows.Media.Imaging;  
  14. using System.Windows.Navigation;  
  15. using System.Windows.Shapes;  
  16. using Newtonsoft.Json;  
  17.   
  18. namespace TextAalysis  
  19. {  
  20.     /// <summary>  
  21.     /// Interaction logic for MainWindow.xaml  
  22.     /// </summary>  
  23.     public partial class MainWindow : Window  
  24.     {  
  25.         public MainWindow()  
  26.         {  
  27.             InitializeComponent();  
  28.         }  
  29.   
  30.         private void Button_Click(object sender, RoutedEventArgs e)  
  31.         {  
  32.             getlanguage(txtbox.Text);  
  33.   
  34.         }  
  35.   
  36.         private async void getlanguage(string msg)  
  37.         {  
  38.             string url = "https://southeastasia.api.cognitive.microsoft.com/text/analytics/v2.0/languages";  
  39.             var body = new  
  40.             {  
  41.                 documents = new[]  
  42.                 {  
  43.                 new  
  44.                 {  
  45.                     ID="1", text=msg  
  46.                 } }  
  47.             };  
  48.             string json = JsonConvert.SerializeObject(body);  
  49.   
  50.             byte[] bytedata = Encoding.UTF8.GetBytes(json);  
  51.             using (var con = new ByteArrayContent(bytedata))  
  52.             {  
  53.   
  54.                 using (HttpClient client = new HttpClient())  
  55.                 {  
  56.   
  57.                     client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key""c642da5d8cd5404bb861ffd341d19c42");  
  58.                     var response = await client.PostAsync(url, con);  
  59.                     var resstring = await response.Content.ReadAsStringAsync();  
  60.                     var ann = new  
  61.                     {  
  62.                         documents = new[] { new { ID = "1", detectedlanguages = new[] { new { name = "" } } } }  
  63.                     };  
  64.                     var responseobj = JsonConvert.DeserializeAnonymousType(resstring, ann);  
  65.                     label1.Content = responseobj.documents[0].detectedlanguages[0].name;  
  66.                 }  
  67.             }  
  68.         }  
  69.     }  
  70. }   

Output 

 
Once you are done, run the application. In my output, I am giving some text in the textbox and once my "Translate" button is clicked, the language I give in my textbox is shown down in the given label.
 
Text Analytics - Azure Cognitive Service
 
Here, I tried with two languages - Spanish and Tamil. The output can be seen below.
 
Text Analytics - Azure Cognitive Service
 

Summary 

 
Hope this article is interesting. The Text Analytics API can detect up to 120 languages. Language detection returns a script of a language.
 
You can try this API with any of your application. For any feedback, post in the comments section. Thank you!! Have a nice day.