Quickstart: Check spelling with the Bing Spell Check SDK for C#
Warning
On October 30, 2020, the Bing Search APIs moved from Azure AI services to Bing Search Services. This documentation is provided for reference only. For updated documentation, see the Bing search API documentation. For instructions on creating new Azure resources for Bing search, see Create a Bing Search resource through the Azure Marketplace.
Use this quickstart to begin spell checking with the 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.
- 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
Start using the Bing Spell Check API by creating one of the following Azure resources:
- Available through the Azure portal until you delete the resource.
- Use the free pricing tier to try the service, and upgrade later to a paid tier for production.
- The Bing Spell Check API is also offered in some tiers of the Bing Search v7 resource.
- Available through the Azure portal until you delete the resource.
- Use the same key and endpoint for your applications, across multiple Azure AI services.
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)); } //... }
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); }
Build and run your project. If you're using Visual Studio, press F5 to debug the file.