-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathSpellCheckExtensions.cs
More file actions
26 lines (23 loc) · 967 Bytes
/
SpellCheckExtensions.cs
File metadata and controls
26 lines (23 loc) · 967 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using Microsoft.Azure.CognitiveServices.Language.SpellCheck;
using System.Linq;
using System.Threading.Tasks;
namespace Bot.Builder.Community.Middleware.SpellCheck
{
internal static class SpellCheckExtensions
{
internal static async Task<string> SpellCheck(this string text, string apiKey, string countryCode = "US", string market = "en-US")
{
if (string.IsNullOrEmpty(text))
{
return text;
}
var client = new SpellCheckClient(new ApiKeyServiceClientCredentials(apiKey));
var spellCheckResult = await client.SpellCheckerAsync(text, setLang: market, market: market, countryCode: countryCode, mode:"spell");
foreach (var flaggedToken in spellCheckResult.FlaggedTokens)
{
text = text.Replace(flaggedToken.Token, flaggedToken.Suggestions.FirstOrDefault().Suggestion);
}
return text;
}
}
}