public async Task <string> Analyze(ContextAnalyzer ctx, string text) { using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _options.key); string uri = _options.host + _options.path + _options.@params; List <KeyValuePair <string, string> > values = new List <KeyValuePair <string, string> >(); values.Add(new KeyValuePair <string, string>("text", text)); using (FormUrlEncodedContent content = new FormUrlEncodedContent(values)) { content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); using (HttpResponseMessage response = await client.PostAsync(uri, content)) { string contentString = await response.Content.ReadAsStringAsync(); var spellCheckResponse = JsonConvert.DeserializeObject <BingSpellCheckResponse>(contentString); StringBuilder sb = new StringBuilder(); int previousOffset = 0; foreach (var flaggedToken in spellCheckResponse.FlaggedTokens) { // Append the text from the previous offset to the current misspelled word offset sb.Append(text.Substring(previousOffset, flaggedToken.Offset - previousOffset)); // Append the corrected word instead of the misspelled word sb.Append(flaggedToken.Suggestions.First().Suggestion); // Increment the offset by the length of the misspelled word previousOffset = flaggedToken.Offset + flaggedToken.Token.Length; } // Append the text after the last misspelled word. if (previousOffset < text.Length) { sb.Append(text.Substring(previousOffset)); } return(sb.ToString()); } } } }
public async Task <string> Analyze(ContextAnalyzer ctx, string text) { string host = _options.endPoint; string route = "/translate?api-version=3.0&to=" + toLanguage; string subscriptionKey = _options.Key; Object[] body = new Object[] { new { Text = text } }; var requestBody = JsonConvert.SerializeObject(body); using (var client = new HttpClient()) using (var request = new HttpRequestMessage()) { // Set the method to POST request.Method = HttpMethod.Post; // Construct the full URI request.RequestUri = new Uri(host + route); // Add the serialized JSON object to your request request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json"); // Add the authorization header request.Headers.Add("Ocp-Apim-Subscription-Key", subscriptionKey); // Send request, get response HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false); var jsonResponse = await response.Content.ReadAsStringAsync(); // Print the response var model = JsonConvert.DeserializeObject <TranslatorModel[]>(jsonResponse).FirstOrDefault(); if (model.detectedLanguage != null) { ctx.LanguageDetected = true; ctx.Language = model.detectedLanguage.language; } return(model.translations.FirstOrDefault().text); } }