Cognitive Service Bing Spell Check API Using Azure, XAML And C#

Before reading this article, please go through the articles, mentioned below.

  1. Introduction To Universal Windows Platform (UWP) App Development Using Windows 10 And Visual Studio 2015
  2. How To Create Microsoft Cognitive Service Bing Spell Check API On Azure Portal

Microsoft Cognitive Services (formerly Project Oxford) are a set of APIs, SDKs and Services available to the developers to make their Applications more intelligent, engaging and discoverable. Microsoft Cognitive Services expands on Microsoft’s evolving portfolio of the machine learning APIs and enables the developers to easily add intelligent features. Microsoft Cognitive Services allows you to build apps with powerful algorithms, using a few lines of code. They work across the devices and platforms such as iOS, Android and Windows, keep improving and are easy to set up.

Bing Spell Check API

It helps users to correct the spelling errors, recognize the differences among names, brand names and slang, as well as understand homophones as they’re typing.

After reading this article, you can learn how to do Cognitive Service Bing Spell Check API in Universal Windows apps development with Azure, XAML and Visual C#.

The important tools, mentioned below, are required for developing UWP.

  1. Windows 10 (Recommended).
  2. Visual Studio 2015 Community Edition (It is a free software available online).
  3. Cognitive Service Bing Spell Check API Key (using azure How To Create Microsoft Cognitive Service Bing Spell Check API On Azure Portal).

Now, we can discuss step by step app development.

Step 1

Open Visual studio 2015 -> Start -> New Project-> Select Universal (under Visual C#->Windows)-> Blank app -> Give the suitable name for your app (UWPCogBingSpellCheck)->OK.



After choosing the Target and minimum platform version for your Windows, Universal Application will support the Project creates App.xaml and MainPage.xaml.



Step 2

Open (double click) the file MainPage.xaml in Solution Explorer and add the Newtonsoft.Json reference in the project. Right click on your Project (UWPCogBingSpellCheck) and select Manage NuGet Packages.



To add Newtonsoft.Json Reference, choose Browse and search Newtonsoft.Json. Select the package and install it.



Reference is added to your project



Step 3

Add TextBlock control, change the Name and text property for the title.



Add two TextBlock controls, change the Name and text property for SampleTex.,



Step 4

Add GridView Resources, using the code, mentioned below.

  1. <Grid.Resources>  
  2.    <CollectionViewSource x:Name="SearchResultsCol" Source="{x:Bind SearchResults}" IsSourceGrouped="False" />  
  3. </Grid.Resources>  
Add a GridView Control and set the ItemSource properties,
  1. <GridView x:Name="gvSpell" ItemsSource="{Binding Source={StaticResource SearchResultsCol}}" SelectionMode="None" IsItemClickEnabled="False" HorizontalAlignment="Left" Height="276" Margin="96,233,0,0" VerticalAlignment="Top" Width="1054">  


Add the code, mentioned below for Grid View Item Template for Spell check review details.
  1. <GridView.ItemTemplate>  
  2.     <DataTemplate>  
  3.         <Border Background="AliceBlue" Width="330" Height="80" Margin="8">  
  4.             <Grid>  
  5.                 <Grid.ColumnDefinitions>  
  6.                     <ColumnDefinition Width="auto" />  
  7.                     <ColumnDefinition/>  
  8.                 </Grid.ColumnDefinitions>  
  9.                 <Grid Grid.Column="1" Margin="3,3,3,10">  
  10.                     <Grid.RowDefinitions>  
  11.                         <RowDefinition Height="auto" />  
  12.                     </Grid.RowDefinitions>  
  13.                     <StackPanel Margin="0,0,0,-143">  
  14.                         <TextBlock TextWrapping="Wrap" Text="{Binding spco.Offset}" Style="{StaticResource CaptionTextBlockStyle}" />  
  15.                         <TextBlock TextWrapping="Wrap" Text="{Binding spco.token}" Style="{StaticResource CaptionTextBlockStyle}" />  
  16.                         <TextBlock TextWrapping="Wrap" Text="{Binding spco.Suggestion}" Style="{StaticResource CaptionTextBlockStyle}" />  
  17.                     </StackPanel>  
  18.                 </Grid>  
  19.             </Grid>  
  20.         </Border>  
  21.     </DataTemplate>  
  22. </GridView.ItemTemplate>  


Step 5

Add a button control, set the Name and add the Edit icon for Spell Check.



Add the Click event method for search button.



Note

Automatically, the code, mentioned below will be generated in XAML code view, while we are done in the design view.
  1. <Page x:Class="UWPCogBingSpellCheck.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:UWPCogBingSpellCheck" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">  
  2.     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  3.         <Grid.Resources>  
  4.             <CollectionViewSource x:Name="SearchResultsCol" Source="{x:Bind SearchResults}" IsSourceGrouped="False" />  
  5.         </Grid.Resources>  
  6.         <TextBlock x:Name="tblTitle" HorizontalAlignment="Left" Margin="380,96,0,0" TextWrapping="Wrap" Text="UWP Cognitive Service Bing Spell Check API Demo" VerticalAlignment="Top" FontWeight="Bold" FontSize="24" />  
  7.         <TextBlock x:Name="tbltest" HorizontalAlignment="Left" Margin="46,166,0,0" TextWrapping="Wrap" Text="Sample Text : " VerticalAlignment="Top" FontWeight="Bold" FontSize="20" />  
  8.         <TextBlock x:Name="tblText" HorizontalAlignment="Left" Height="36" Margin="178,166,0,0" TextWrapping="Wrap" Text="The Spell Check lets patners chck a text sting for speling and gramar erors. " VerticalAlignment="Top" Width="748" FontSize="22" />  
  9.         <GridView x:Name="gvSpell" ItemsSource="{Binding Source={StaticResource SearchResultsCol}}" SelectionMode="None" IsItemClickEnabled="False" HorizontalAlignment="Left" Height="276" Margin="96,233,0,0" VerticalAlignment="Top" Width="1054">  
  10.             <GridView.ItemTemplate>  
  11.                 <DataTemplate>  
  12.                     <Border Background="AliceBlue" Width="330" Height="80" Margin="8">  
  13.                         <Grid>  
  14.                             <Grid.ColumnDefinitions>  
  15.                                 <ColumnDefinition Width="auto" />  
  16.                                 <ColumnDefinition/>  
  17.                             </Grid.ColumnDefinitions>  
  18.                             <Grid Grid.Column="1" Margin="3,3,3,10">  
  19.                                 <Grid.RowDefinitions>  
  20.                                     <RowDefinition Height="auto" />  
  21.                                 </Grid.RowDefinitions>  
  22.                                 <StackPanel Margin="0,0,0,-143">  
  23.                                     <TextBlock TextWrapping="Wrap" Text="{Binding spco.Offset}" Style="{StaticResource CaptionTextBlockStyle}" />  
  24.                                     <TextBlock TextWrapping="Wrap" Text="{Binding spco.token}" Style="{StaticResource CaptionTextBlockStyle}" />  
  25.                                     <TextBlock TextWrapping="Wrap" Text="{Binding spco.Suggestion}" Style="{StaticResource CaptionTextBlockStyle}" />  
  26.                                 </StackPanel>  
  27.                             </Grid>  
  28.                         </Grid>  
  29.                     </Border>  
  30.                 </DataTemplate>  
  31.             </GridView.ItemTemplate>  
  32.         </GridView>  
  33.         <Button x:Name="btnSpellchk" HorizontalAlignment="Left" Margin="1002,162,0,0" VerticalAlignment="Top" Click="btnSpellchk_Click" RenderTransformOrigin="3.6,0.312" ToolTipService.ToolTip="Click to Spell Check">  
  34. <SymbolIcon Symbol="Edit"></SymbolIcon>  
  35. </Button>  
  36.     </Grid>  
  37. </Page>  
Step 6

Add the namespaces, mentioned below in Mainpage.xaml.cs for Bing Spell Check.
  1. using System.Net.Http;  
  2. using System.Threading.Tasks;  
  3. using Newtonsoft.Json.Linq;  
  4. using System.Collections.ObjectModel;  
  5. using System.Net;  
Step 7

Add the Academic Knowledge, where Client keys use Azure Service and generate it (For more information, please refer the article How To Create Microsoft Cognitive Service Bing Spell Check API On Azure Portal .)
  • Bing Spell Check API endpoint.
    https://api.cognitive.microsoft.com/bing/v5.0/spellcheck/?
Add the code, mentioned below with Azure generated key for Bing Spell Check.
  1. public class Spellc {  
  2.     public string Offset {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string token {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public string Suggestion {  
  11.         get;  
  12.         set;  
  13.     }  
  14. }  
  15. public class Spellcol {  
  16.     public Spellc spco {  
  17.         get;  
  18.         set;  
  19.     }  
  20. }  
  21. public ObservableCollection < Spellcol > SearchResults {  
  22.     get;  
  23.     set;  
  24. } = new ObservableCollection < Spellcol > ();  
  25. private async void btnSpellchk_Click(object sender, RoutedEventArgs e) {  
  26.     var sp = await spellcheck();  
  27.     for (int i = 0; i < sp.Count(); i++) {  
  28.         Spellc sc = sp.ElementAt(i);  
  29.         SearchResults.Add(new Spellcol {  
  30.             spco = sc  
  31.         });  
  32.     }  
  33. }  
  34. async Task < IEnumerable < Spellc >> spellcheck() {  
  35.     List < Spellc > spelc = new List < Spellc > ();  
  36.     var client = new HttpClient();  
  37.     client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key""9456ddf5ea3b4752ac8f87b63a7e6013");  
  38.     string text = "The Spell Check lets patners chck a text sting for speling and gramar erors. ";  
  39.     string mode = "proof";  
  40.     string mkt = "en-us";  
  41.     var SpellEndPoint = "https://api.cognitive.microsoft.com/bing/v5.0/spellcheck/?";  
  42.     var result = await client.GetAsync(string.Format("{0}text={1}&mode={2}&mkt={3}", SpellEndPoint, text, mode, mkt));  
  43.     result.EnsureSuccessStatusCode();  
  44.     var json = await result.Content.ReadAsStringAsync();  
  45.     dynamic data = JObject.Parse(json);  
  46.     for (int i = 0; i < 6; i++) {  
  47.         spelc.Add(new Spellc {  
  48.             Offset = "Offset : " + data.flaggedTokens[i].offset,  
  49.                 token = "Wrong Word : " + data.flaggedTokens[i].token,  
  50.                 Suggestion = "Spelling Suggestion : " + data.flaggedTokens[i].suggestions[0].suggestion  
  51.         });  
  52.     }  
  53.     return spelc;  


Step 8

Deploy your app in Local Machine and the output of the UWPCogBingSpellCheck app is shown below.



After Clicking the Button,



Summary

Now, you have successfully tested Cognitive Service Bing Spell Check API, using Azure, XAML and C# with UWP environment.

If you are interested, please read the UWP Cognitive Service articles, mentioned below.
  1. Interpret Method Output Value To Evaluate Method Expression In Cognitive Service Academic Knowledge API
  2. Cognitive Service Academic Knowledge API - Evaluate Method Using UWP With Azure, XAML And C#
  3. Cognitive Service Bing Web Search API Using UWP With Azure, XAML And C#
  4. Cognitive Service Bing Video Search API Using UWP With Azure, XAML And C#
  5. Cognitive Service Bing Image Search API Using UWP With Azure, XAML And C#
  6. Cognitive Service Bing News Search API Using UWP With Azure, XAML And C#
  7. Cognitive Service Bing AutoSuggest API Using UWP With Azure, XAML And C#
  8. Multiple People Emotions In UWP With Cognitive Service Emotion API, Azure, XAML And C#
  9. Cognitive Service Emotion API In UWP With Azure, XAML And C#
  10. Retrieving Face Attributes Using Cognitive Service Face API In UWP With Azure, XAML And C#
  11. Cognitive Service Face API, Using UWP With Azure, XAML And C#

Up Next
    Ebook Download
    View all
    Learn
    View all