public static async Task <string> CorrectSpelling(string sentence) { var client = new HttpClient(); var queryString = HttpUtility.ParseQueryString(String.Empty); // Request headers client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "79c54e7969ae46a685d2c09b62ab4a6a"); var uri = "https://api.cognitive.microsoft.com/bing/v5.0/spellcheck?"; string text = sentence; string mode = "proof"; string mkt = "en-us"; try { var result = await client.GetAsync(string.Format("{0}text={1}&mode={2}&mkt={3}", uri, text, mode, mkt)); result.EnsureSuccessStatusCode(); var json = await result.Content.ReadAsStringAsync(); SpellCheckResponse spellCheckResponse = JsonConvert.DeserializeObject <SpellCheckResponse>(json); foreach (var correction in spellCheckResponse.FlaggedTokens) { sentence = sentence.Replace(correction.Token, correction.suggestions[0].Suggestion); } } catch (Exception ex) { // do nothing } return(sentence); }
/// <summary> /// Function to parse a given spell check result. /// Makes sure all is displayed in the UI /// </summary> /// <param name="response"><see cref="SpellCheckResponse"/></param> private void ParseResults(SpellCheckResponse response) { if (response == null || response.flaggedTokens == null || response.flaggedTokens.Count == 0) { Result = "No suggestions found"; return; } StringBuilder sb = new StringBuilder(); sb.Append("Spell checking results:\n\n"); foreach (FlaggedTokens tokens in response.flaggedTokens) { if (!string.IsNullOrEmpty(tokens.token)) { sb.AppendFormat("Token is: {0}\n", tokens.token); } if (tokens.suggestions != null || tokens.suggestions.Count != 0) { foreach (Suggestions suggestion in tokens.suggestions) { sb.AppendFormat("Suggestion: {0} - with score: {1}\n", suggestion.suggestion, suggestion.score); } sb.Append("\n"); } } Result = sb.ToString(); }
public async Task <List <Correction> > SpellCheck(string data, Language toLang) { var spellCheckClient = new SpellCheckClient(toLang); SpellCheckResponse response = await spellCheckClient.SpellCheck(data); var corrections = new List <Correction>(); foreach (FlaggedToken flaggedToken in response.FlaggedTokens) { var correction = new Correction(flaggedToken); corrections.Add(correction); } return(corrections); }
/// <summary> /// Command function to execute the spell check operation. /// </summary> /// <param name="obj"></param> private async void ExecuteOperation(object obj) { var queryString = HttpUtility.ParseQueryString(string.Empty); queryString["text"] = InputQuery; queryString["mkt"] = "en-us"; //queryString["mode"] = "proof"; if (!string.IsNullOrEmpty(PreContext)) { queryString["preContextText"] = PreContext; } if (!string.IsNullOrEmpty(PostContext)) { queryString["postContextText"] = PostContext; } SpellCheckResponse response = await _webRequest.MakeRequest <object, SpellCheckResponse>(HttpMethod.Get, queryString.ToString()); ParseResults(response); }
public async Task <SpellCheckResponse> SpellCheck(string inputText) { List <KeyValuePair <string, string> > values = new List <KeyValuePair <string, string> >(); values.Add(new KeyValuePair <string, string>("text", inputText)); HttpResponseMessage response = new HttpResponseMessage(); string queryParams = string.Format(params_, this.market); string uri = $"{host}{path}{params_}"; using (FormUrlEncodedContent content = new FormUrlEncodedContent(values)) { content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); response = await client.PostAsync(uri, content); } string contentString = await response.Content.ReadAsStringAsync(); SpellCheckResponse spellCheckResponse = JsonConvert.DeserializeObject <SpellCheckResponse>(contentString); return(spellCheckResponse); }