internal static bool AddUserEditedItem(string translationSourceName, string sourceLang, string destinationLang, string originalText, string editedText) { if (string.IsNullOrEmpty(translationSourceName) || string.IsNullOrEmpty(destinationLang) || string.IsNullOrEmpty(originalText) || string.IsNullOrEmpty(editedText)) { return(false); } var originalTextClean = originalText.Trim(); var editedTextClean = editedText.Trim(); if (originalTextClean.Equals(editedTextClean, StringComparison.Ordinal)) { return(false); } var translationCacheKey = new TranslationCacheKey(translationSourceName, sourceLang, destinationLang, originalText); TranslationResult translationResult; if (!TryGetValue(translationCacheKey, out translationResult)) { translationResult = AddOrUpdate(translationCacheKey, new TranslationResult { TranslationSource = translationSourceName, SourceLanguage = sourceLang, DestinationLanguage = destinationLang, OriginalText = originalText, FromCache = true }); } var dictionaryItem = translationResult.DictionaryItems.FirstOrDefault(p => p.Title.Equals(Constants.TranslationCache.UserEditedItemHeader, StringComparison.OrdinalIgnoreCase)); if (dictionaryItem == null) { dictionaryItem = new DictionaryItem { Title = Constants.TranslationCache.UserEditedItemHeader }; translationResult.DictionaryItems.Add(dictionaryItem); } var alreadyHasItem = dictionaryItem.Terms.Any(p => p.Equals(editedTextClean, StringComparison.Ordinal)); if (alreadyHasItem) { return(false); } dictionaryItem.Terms.Add(editedTextClean); return(true); }
internal static bool AddUserEditedItem(string translationSourceName, string sourceLang, string destinationLang, string originalText, string editedText) { if(string.IsNullOrEmpty(translationSourceName) || string.IsNullOrEmpty(destinationLang) || string.IsNullOrEmpty(originalText) || string.IsNullOrEmpty(editedText)) return false; var originalTextClean = originalText.Trim(); var editedTextClean = editedText.Trim(); if (originalTextClean.Equals(editedTextClean, StringComparison.Ordinal)) return false; var translationCacheKey = new TranslationCacheKey(translationSourceName, sourceLang, destinationLang, originalText); TranslationResult translationResult; if (!TryGetValue(translationCacheKey, out translationResult)) { translationResult = AddOrUpdate(translationCacheKey, new TranslationResult { TranslationSource = translationSourceName, SourceLanguage = sourceLang, DestinationLanguage = destinationLang, OriginalText = originalText, FromCache = true }); } var dictionaryItem = translationResult.DictionaryItems.FirstOrDefault(p => p.Title.Equals(Constants.TranslationCache.UserEditedItemHeader, StringComparison.OrdinalIgnoreCase)); if (dictionaryItem == null) { dictionaryItem = new DictionaryItem { Title = Constants.TranslationCache.UserEditedItemHeader }; translationResult.DictionaryItems.Add(dictionaryItem); } var alreadyHasItem = dictionaryItem.Terms.Any(p=> p.Equals(editedTextClean, StringComparison.Ordinal)); if (alreadyHasItem) return false; dictionaryItem.Terms.Add(editedTextClean); return true; }
private TranslationResult ParseResponse(JObject json) { TranslationResult res = new TranslationResult(); JToken sentences = json["sentences"]; List <string> sentenceTexts = new List <string>(); foreach (JToken sent in sentences.Children()) { sentenceTexts.Add(sent.Value <string>("trans")); } string fullText = string.Join(" ", sentenceTexts); if (!string.IsNullOrWhiteSpace(fullText)) { res.Sentences.Add(fullText); } JToken dictionary = json["dict"]; if (dictionary != null) { foreach (JToken dToken in dictionary.Children()) { DictionaryItem d = new DictionaryItem(); d.Title = dToken.Value <string>("pos"); JToken terms = dToken["terms"]; foreach (JToken term in terms.Children()) { d.Terms.Add(term.Value <string>()); } res.DictionaryItems.Add(d); } } res.SourceLanguage = json.Value <string>("src"); return(res); }
private TranslationResult ParseResponse(JObject json) { TranslationResult res = new TranslationResult(); JToken sentences = json["sentences"]; List<string> sentenceTexts = new List<string>(); foreach (JToken sent in sentences.Children()) { sentenceTexts.Add(sent.Value<string>("trans")); } string fullText = string.Join(" ", sentenceTexts); if (!string.IsNullOrWhiteSpace(fullText)) { res.Sentences.Add(fullText); } JToken dictionary = json["dict"]; if (dictionary != null) { foreach (JToken dToken in dictionary.Children()) { DictionaryItem d = new DictionaryItem(); d.Title = dToken.Value<string>("pos"); JToken terms = dToken["terms"]; foreach (JToken term in terms.Children()) { d.Terms.Add(term.Value<string>()); } res.DictionaryItems.Add(d); } } res.SourceLanguage = json.Value<string>("src"); return res; }