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.
Drag and drop the controls as given below in the screenshot.
Right-click the solution and select "Manage NuGet Packages for Solution". Download Newtonsoft.json from the list.
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.
Follow the code given below for designing the mainwindow.xaml.
- <Grid Margin="-226,-26,-299,-82">
- <StackPanel HorizontalAlignment="Left" Height="413" Margin="261,50,0,0" VerticalAlignment="Top" Width="736" RenderTransformOrigin="0.5,0.5">
- <StackPanel.RenderTransform>
- <TransformGroup>
- <ScaleTransform ScaleX="-1"/>
- <SkewTransform/>
- <RotateTransform/>
- <TranslateTransform/>
- </TransformGroup>
- </StackPanel.RenderTransform>
- <Label Content="Write Message Here" Height="108" FontSize="48" FontFamily="Segoe UI Black" Margin="236,0,130,0" RenderTransformOrigin="0.5,0.5">
- <Label.RenderTransform>
- <TransformGroup>
- <ScaleTransform ScaleX="-1"/>
- <SkewTransform/>
- <RotateTransform/>
- <TranslateTransform/>
- </TransformGroup>
- </Label.RenderTransform>
- </Label>
- <TextBox Height="49" x:Name="txtbox" TextWrapping="Wrap" Text="" Margin="13,0,8,0" RenderTransformOrigin="0.5,0.5">
- <TextBox.RenderTransform>
- <TransformGroup>
- <ScaleTransform ScaleX="-1"/>
- <SkewTransform/>
- <RotateTransform/>
- <TranslateTransform/>
- </TransformGroup>
- </TextBox.RenderTransform>
- </TextBox>
- <Button Content="Translate" Height="84" RenderTransformOrigin="0.5,0.5" Margin="173,0,159,0" FontSize="48" FontWeight="Bold" Click="Button_Click">
- <Button.RenderTransform>
- <TransformGroup>
- <ScaleTransform ScaleX="-1"/>
- <SkewTransform/>
- <RotateTransform/>
- <TranslateTransform/>
- </TransformGroup>
- </Button.RenderTransform>
- </Button>
- <Label Content="" x:Name="label1" Height="87" FontSize="36" Margin="13,0,17,0" RenderTransformOrigin="0.5,0.5">
- <Label.RenderTransform>
- <TransformGroup>
- <ScaleTransform ScaleX="-1"/>
- <SkewTransform/>
- <RotateTransform/>
- <TranslateTransform/>
- </TransformGroup>
- </Label.RenderTransform>
- </Label>
- </StackPanel>
- </Grid>
Follow the code given below for mainwindow.xaml.cs.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net.Http;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using Newtonsoft.Json;
-
- namespace TextAalysis
- {
-
-
-
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
-
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- getlanguage(txtbox.Text);
-
- }
-
- private async void getlanguage(string msg)
- {
- string url = "https://southeastasia.api.cognitive.microsoft.com/text/analytics/v2.0/languages";
- var body = new
- {
- documents = new[]
- {
- new
- {
- ID="1", text=msg
- } }
- };
- string json = JsonConvert.SerializeObject(body);
-
- byte[] bytedata = Encoding.UTF8.GetBytes(json);
- using (var con = new ByteArrayContent(bytedata))
- {
-
- using (HttpClient client = new HttpClient())
- {
-
- client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "c642da5d8cd5404bb861ffd341d19c42");
- var response = await client.PostAsync(url, con);
- var resstring = await response.Content.ReadAsStringAsync();
- var ann = new
- {
- documents = new[] { new { ID = "1", detectedlanguages = new[] { new { name = "" } } } }
- };
- var responseobj = JsonConvert.DeserializeAnonymousType(resstring, ann);
- label1.Content = responseobj.documents[0].detectedlanguages[0].name;
- }
- }
- }
- }
- }
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.
Here, I tried with two languages - Spanish and Tamil. The output can be seen below.
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.