Quickstart: Check spelling with Bing Spell Check SDK for C#
Use this quickstart to begin spell checking with Bing Spell Check SDK for C#. While Bing Spell Check has a REST API compatible with most programming languages, the SDK provides an easy way to integrate the service into your applications. The source code for this sample can be found on GitHub.
Application dependencies
- Any edition of Visual Studio 2017 or later.
- The Bing Spell Check NuGet Package.
To add the Bing Spell Check SDK to your project, select Manage NuGet Packages from Solution Explorer in Visual Studio. Add the Microsoft.Azure.CognitiveServices.Language.SpellCheck
package. The package also installs the following dependencies:
- Microsoft.Rest.ClientRuntime
- Microsoft.Rest.ClientRuntime.Azure
- Newtonsoft.Json
Create and initialize the application
Create a new C# console solution in Visual Studio. Then add the following
using
statement:using System; using System.Linq; using System.Threading.Tasks; using Microsoft.Azure.CognitiveServices.Language.SpellCheck; using Microsoft.Azure.CognitiveServices.Language.SpellCheck.Models;
Create a new class. Then create an asynchronous function called
SpellCheckCorrection()
that takes a subscription key and sends the spell check request.Instantiate the client by creating a new
ApiKeyServiceClientCredentials
object.public static class SpellCheckSample{ public static async Task SpellCheckCorrection(string key){ var client = new SpellCheckClient(new ApiKeyServiceClientCredentials(key)); } //... }
Send the request and read the response
In the function created above, perform the following steps. Send the spell check request with the client. Add the text to be checked to the
text
parameter, and set the mode toproof
.var result = await client.SpellCheckerWithHttpMessagesAsync(text: "Bill Gatas", mode: "proof");
Get the first spell check result, if there is one. Print the first misspelled word (token) returned, the token type, and the number of suggestions.
var firstspellCheckResult = result.Body.FlaggedTokens.FirstOrDefault(); if (firstspellCheckResult != null) { Console.WriteLine("SpellCheck Results#{0}", result.Body.FlaggedTokens.Count); Console.WriteLine("First SpellCheck Result token: {0} ", firstspellCheckResult.Token); Console.WriteLine("First SpellCheck Result Type: {0} ", firstspellCheckResult.Type); Console.WriteLine("First SpellCheck Result Suggestion Count: {0} ", firstspellCheckResult.Suggestions.Count); }
Get the first suggested correction, if there is one. Print the suggestion score, and the suggested word.
var suggestions = firstspellCheckResult.Suggestions; if (suggestions?.Count > 0) { var firstSuggestion = suggestions.FirstOrDefault(); Console.WriteLine("First SpellCheck Suggestion Score: {0} ", firstSuggestion.Score); Console.WriteLine("First SpellCheck Suggestion : {0} ", firstSuggestion.Suggestion); }
Run the application
Build and run your project. If you're using Visual Studio, press F5 to debug the file.
Next steps
- What is the Bing Spell Check API?
- Bing Spell Check C# SDK reference guide