private string TranslateLanguage(string TextToTranslate) { if (string.IsNullOrWhiteSpace(TextToTranslate)) { return(TextToTranslate); } object[] _objBody = new object[] { new { Text = TextToTranslate } }; string _RequestBody = JsonConvert.SerializeObject(_objBody); using (HttpRequestMessage _HttpRequestMessage = GetHttpRequestMessage(HttpMethod.Post, GetTranslateUrl, TranslateSubscriptionKey)) { _HttpRequestMessage.Content = new StringContent(_RequestBody, Encoding.UTF8, Properties.Resources.RequestHeaderContentJson); HttpResponseMessage _HttpResponseMessage = HttpClients.SendAsync(_HttpRequestMessage).Result; _HttpResponseMessage.EnsureSuccessStatusCode(); List <Dictionary <string, List <Dictionary <string, string> > > > _ListDictionary = JsonConvert.DeserializeObject <List <Dictionary <string, List <Dictionary <string, string> > > > >(_HttpResponseMessage.Content.ReadAsStringAsync().Result); List <Dictionary <String, String> > _Translations = _ListDictionary[0][Properties.Resources.MSAPITranslations]; if (!TextToTranslate.ToLower().Equals(_Translations[0][Properties.Resources.MSAPITranslationText].ToString().ToLower())) { TextToTranslate = _Translations[0][Properties.Resources.MSAPITranslationText].ToString(); } } return(TextToTranslate); }
private string CorrectSpelling(string TextToSpellCheck) { if (string.IsNullOrWhiteSpace(TextToSpellCheck)) { return(TextToSpellCheck); } using (HttpRequestMessage _HttpRequestMessage = GetHttpRequestMessage(HttpMethod.Post, GetBingSpellCheckUrl, BingSpellCheckSubscriptionKey)) { Dictionary <string, string> _TextParameter = new Dictionary <string, string> { { Properties.Resources.MSBingText, TextToSpellCheck } }; _HttpRequestMessage.Content = new FormUrlEncodedContent(_TextParameter); _HttpRequestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue(Properties.Resources.RequestHeaderContentUrlEncoded); _HttpRequestMessage.Content.Headers.ContentType.CharSet = Properties.Resources.RequestHeaderAcceptCharSet; HttpResponseMessage _HttpResponseMessage = HttpClients.SendAsync(_HttpRequestMessage).Result; _HttpResponseMessage.EnsureSuccessStatusCode(); JObject _JsonResponse = JObject.Parse(JsonConvert.DeserializeObject(_HttpResponseMessage.Content.ReadAsStringAsync().Result).ToString()); JArray _FlaggedTokens = (JArray)_JsonResponse[Properties.Resources.MSBingFlaggedTokens]; SortedDictionary <int, string[]> _SortedDictionary = new SortedDictionary <int, string[]>(Comparer.Create <int>((a, b) => b.CompareTo(a))); for (int i = 0; i < _FlaggedTokens.Count; i++) { JToken _Correction = _FlaggedTokens[i]; JToken _Suggestion = _Correction[Properties.Resources.MSBingSuggestions][0]; if (decimal.Parse((_Suggestion[Properties.Resources.MSBingScore].ToString())) > (decimal)0.7) { _SortedDictionary[(int)_Correction[Properties.Resources.MSBingOffset]] = new string[] { _Correction[Properties.Resources.MSBingToken].ToString(), _Suggestion[Properties.Resources.MSBingSuggestion].ToString() } } ; // dict value = {error, correction} } // apply the corrections in order from right to left foreach (int i in _SortedDictionary.Keys) { string _OldSpelling = _SortedDictionary[i][0]; string _NewSpelling = _SortedDictionary[i][1]; // apply capitalization from original text to correction - all caps or initial caps if (TextToSpellCheck.Substring(i, _OldSpelling.Length).All(char.IsUpper)) { _NewSpelling = _NewSpelling.ToUpper(); } else if (char.IsUpper(TextToSpellCheck[i])) { _NewSpelling = _NewSpelling[0].ToString().ToUpper() + _NewSpelling.Substring(1); } TextToSpellCheck = TextToSpellCheck.Substring(0, i) + _NewSpelling + TextToSpellCheck.Substring(i + _OldSpelling.Length); } } return(TextToSpellCheck); }
private SortedDictionary <string, string> GetAvailableLanguages() { SortedDictionary <string, string> _LanguageCodesAndTitles = new SortedDictionary <string, string>(Comparer.Create <string>((a, b) => string.Compare(a, b, true))); using (HttpRequestMessage _HttpRequestMessage = GetHttpRequestMessage(HttpMethod.Get, GetLanguageUrl, TranslateSubscriptionKey)) { HttpResponseMessage _HttpResponseMessage = HttpClients.SendAsync(_HttpRequestMessage).Result; _HttpResponseMessage.EnsureSuccessStatusCode(); JObject _JObject = JObject.Parse(_HttpResponseMessage.Content.ReadAsStringAsync().Result); Dictionary <string, Dictionary <string, string> > _Languages = JsonConvert.DeserializeObject <Dictionary <string, Dictionary <string, string> > >(_JObject[Properties.Resources.MSAPITranslation].ToString()); _LanguageCodes = _Languages.Keys.ToArray(); foreach (KeyValuePair <string, Dictionary <string, string> > _KeyValue in _Languages) { _LanguageCodesAndTitles.Add(_KeyValue.Value[Properties.Resources.MSAPITranslationLangName], _KeyValue.Key); } } return(_LanguageCodesAndTitles); }