public async static Task CheckSpellAsync(IDialogContext context, string key, string query) { ISpellCheckAPI client = new SpellCheckAPI(new Microsoft.Azure.CognitiveServices.SpellCheck.ApiKeyServiceClientCredentials(key)); var result = await client.SpellCheckerAsync(text : query); string suggestionText = query; foreach (var item in result.FlaggedTokens) { var firstSugestion = item.Suggestions.FirstOrDefault(); if (firstSugestion != null) { suggestionText = suggestionText.Replace(item.Token, $"**{firstSugestion.Suggestion}**"); } } if (query.Equals(suggestionText)) { await context.PostAsync("Not found any suggestions."); } else { await context.PostAsync($"Did you mean: {suggestionText}"); } }
public static async Task <string> SpellCheck(this SpellCheckAPI api, string spellcheck) { return((await api.SpellCheckerWithHttpMessagesAsync(text: spellcheck, mode: "proof")) .Body .FlaggedTokens .Where(token => token.Suggestions.First().Score > 0.70) .Select <SpellingFlaggedToken, Func <string, string> >(token => (value => value.Replace(token.Token, token.Suggestions.First().Suggestion))) .DefaultIfEmpty(_ => _) .Aggregate((a, b) => s => a(b(s))) .Invoke(spellcheck)); }
public static void SpellCheckError(string subscriptionKey) { var client = new SpellCheckAPI(new ApiKeyServiceClientCredentials(subscriptionKey)); try { var result = client.SpellCheckerAsync(mode: "proof").Result; Console.WriteLine("Correction for Query# \"empty text field\""); } catch (Exception ex) { if (ex.GetBaseException().GetType() == typeof(Exception)) { Console.WriteLine("Encountered exception. " + ex.Message); } } }
public static void SpellCheckCorrection(string subscriptionKey) { var client = new SpellCheckAPI(new ApiKeyServiceClientCredentials(subscriptionKey)); try { var result = client.SpellCheckerWithHttpMessagesAsync(text: "Bill Gatas", mode: "proof").Result; Console.WriteLine("Correction for Query# \"bill gatas\""); // SpellCheck Results if (result?.Body.FlaggedTokens?.Count > 0) { // find the first spellcheck result 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); 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); } } else { Console.WriteLine("Couldn't get any Spell check results!"); } } else { Console.WriteLine("Didn't see any SpellCheck results.."); } } catch (Exception ex) { Console.WriteLine("Encountered exception. " + ex.Message); } }
public static async Task <string> CheckGrammarAsync(string message) { Vars.StatusMessage.Text += "\n\nMESSAGE BEFORE\n" + message; // Add your Azure spellcheck API key here var client = new SpellCheckAPI(new ApiKeyServiceClientCredentials("<API KEY>")); var result = await client.SpellCheckerAsync(text : message, mode : "proof"); if (result?.FlaggedTokens?.Count > 0) { foreach (var token in result.FlaggedTokens) { message = message.Replace(token.Token, token.Suggestions[0].Suggestion); } } Vars.StatusMessage.Text += "\n\nMESSAGE AFTER\n" + message; return(message); }