public async Task <TranslationResponse> GetTranslation(string word) { var origin = HttpUtility.UrlDecode(word); if (!origin.Contains(' ')) { if (_yandexDictionaryApiClient.IsOnline) { var yandexResponse = await _yandexDictionaryApiClient.Translate(origin); var result = yandexResponse.SelectMany(t => t.Tr) .Select(t => new Translation(t.Text, TranslationSource.Yadic)).ToArray(); if (result.Any()) { return(new TranslationResponse(origin, result)); } } } if (!_yaTransApi.IsOnline) { return(TranslationResponse.Empty(origin)); } var res = await _yaTransApi.Translate(origin); if (string.IsNullOrWhiteSpace(res)) { return(TranslationResponse.Empty(origin)); } return(new TranslationResponse(origin, new Translation(res, TranslationSource.Yatrans))); }
public void Enter(NewWordsService service) { var dicPing = _yapiDicClient.Ping(); var transPing = _yapiTransClient.Ping(); Task.WaitAll(dicPing, transPing); var timer = new Timer(5000) { AutoReset = false }; timer.Enabled = true; timer.Elapsed += (s, e) => { var pingDicApi = _yapiDicClient.Ping(); var pingTransApi = _yapiTransClient.Ping(); Task.WaitAll(pingDicApi, pingTransApi); timer.Enabled = true; }; if (_yapiDicClient.IsOnline) { Console.WriteLine("Yandex dic is online"); } else { Console.WriteLine("Yandex dic is offline"); } if (_yapiTransClient.IsOnline) { Console.WriteLine("Yandex trans is online"); } else { Console.WriteLine("Yandex trans is offline"); } while (true) { Console.Write("Enter [e] for exit or "); Console.Write("Enter english word: "); string word = Console.ReadLine(); if (word == "e") { break; } Task <YaDefenition[]> task = null; if (_yapiDicClient.IsOnline) { task = _yapiDicClient.Translate(word); } task?.Wait(); List <TranslationAndContext> translations = new List <TranslationAndContext>(); if (task?.Result?.Any() == true) { var variants = task.Result.SelectMany(r => r.Tr); foreach (var yandexTranslation in variants) { var phrases = yandexTranslation.GetPhrases(word); translations.Add(new TranslationAndContext(word, yandexTranslation.Text, yandexTranslation.Pos, phrases.ToArray())); } } else { var dictionaryMatch = service.GetTranslations(word); if (dictionaryMatch != null) { translations.AddRange( dictionaryMatch.Translations.Select(t => new TranslationAndContext(dictionaryMatch.Origin, t, dictionaryMatch.Transcription, new Phrase[0]))); } } if (!translations.Any()) { try { var transAnsTask = _yapiTransClient.Translate(word); transAnsTask.Wait(); if (string.IsNullOrWhiteSpace(transAnsTask.Result)) { Console.WriteLine("No translations found. Check the word and try again"); } else { translations.Add(new TranslationAndContext(word, transAnsTask.Result, null, new Phrase[0])); } } catch (Exception e) { Console.WriteLine("No translations found. Check the word and try again"); } } if (translations.Any()) { Console.WriteLine("e: [back to main menu]"); Console.WriteLine("c: [CANCEL THE ENTRY]"); int i = 1; foreach (var translation in translations) { if (translation.Phrases.Any()) { Console.WriteLine($"{i}: {translation.Translation}\t (+{translation.Phrases.Length})"); } else { Console.WriteLine($"{i}: {translation.Translation}"); } i++; } try { var results = ChooseTranslation(translations.ToArray()); if (results?.Any() == true) { var allTranslations = results.Select(t => t.Translation).ToArray(); var allPhrases = results.SelectMany(t => t.Phrases).ToArray(); service.SaveForExams( word: word, transcription: translations[0].Transcription, translations: allTranslations, allMeanings: translations.Select(t => t.Translation.Trim().ToLower()).ToArray(), phrases: allPhrases); Console.WriteLine($"Saved. Tranlations: {allTranslations.Length}, Phrases: {allPhrases.Length}"); } } catch (OperationCanceledException) { return; } } } }
public List <UserWordForLearning> TranslateAndAddToDictionary(string word) { word = word.ToLower(); List <UserWordForLearning> WordsForLearning = new List <UserWordForLearning>(); List <int> phrasesId = new List <int>(); if (!word.Contains(' ')) { Task <YaDefenition[]> task = null; task = _yapiDicClient.Translate(word); task?.Wait(); //Создаем из ответа(если он есть) TranslationAndContext или WordDictionary? if (task?.Result?.Any() == true) { var variants = task.Result.SelectMany(r => r.Tr); var transcription = task.Result.Select(r => r.Ts).FirstOrDefault(); foreach (var yandexTranslation in variants) { var yaPhrases = yandexTranslation.GetPhrases(word); var dbPhrases = new List <Phrase>(); //Заполняем бд(wordDictionary и фразы) var id = _dictionaryService.AddNewWordPairToDictionary( word, yandexTranslation.Text, transcription, TranslationSource.Yadic); foreach (var phrase in yaPhrases) { dbPhrases.Add(phrase); phrasesId.Add(_dictionaryService.AddPhraseForWordPair(id, word, yandexTranslation.Text, phrase.EnPhrase, phrase.PhraseRuTranslate)); } WordsForLearning.Add(new UserWordForLearning() { EnWord = word, UserTranslations = yandexTranslation.Text, Transcription = transcription, Phrases = dbPhrases, PhrasesIds = string.Join(",", phrasesId), IsPhrase = false, }); } return(WordsForLearning); } } try { var transAnsTask = _yapiTransClient.Translate(word); transAnsTask.Wait(); if (!string.IsNullOrWhiteSpace(transAnsTask.Result)) { //1. Заполняем бд(wordDictionary, фраз нет) var id = _dictionaryService.AddNewWordPairToDictionary( word, transAnsTask.Result, "[]", TranslationSource.Yadic); //2. Дополняем ответ WordsForLearning.Add(new UserWordForLearning() { EnWord = word, UserTranslations = transAnsTask.Result, Transcription = "", PhrasesIds = "", IsPhrase = false, }); } } catch (Exception e) { return(WordsForLearning); } return(WordsForLearning); }
async Task <bool> EnterSingleWord(string?word = null) { if (word == null) { await _chat.SendMessage("Enter english word", new InlineKeyboardButton { CallbackData = "/exit", Text = "Cancel" }); while (true) { var input = await _chat.WaitUserInput(); if (input.CallbackQuery != null && input.CallbackQuery.Data == "/exit") { throw new ProcessInteruptedWithMenuCommand("/start"); } if (!string.IsNullOrEmpty(input.Message.Text)) { word = input.Message.Text; break; } } } YaDefenition[]? definitions = null; if (_yapiDicClient.IsOnline) { definitions = await _yapiDicClient.Translate(word); } var translations = new List <TranslationAndContext>(); if (definitions?.Any() == true) { var variants = definitions.SelectMany(r => r.Tr); foreach (var yandexTranslation in variants) { var phrases = yandexTranslation.GetPhrases(word); translations.Add(new TranslationAndContext(word, yandexTranslation.Text, yandexTranslation.Pos, phrases.ToArray())); } } else { var dictionaryMatch = _wordService.GetTranslations(word); if (dictionaryMatch != null) { translations.AddRange( dictionaryMatch.Translations.Select(t => new TranslationAndContext(dictionaryMatch.Origin, t, dictionaryMatch.Transcription, new Phrase[0]))); } } if (!translations.Any()) { try { var translateResponse = await _yapiTransClient.Translate(word); if (string.IsNullOrWhiteSpace(translateResponse)) { await _chat.SendMessage("No translations found. Check the word and try again"); } else { translations.Add(new TranslationAndContext(word, translateResponse, null, new Phrase[0])); } } catch (Exception) { await _chat.SendMessage("No translations found. Check the word and try again"); } } if (!translations.Any()) { return(true); } await _chat.SendMessage($"Choose translation for '{word}'", InlineButtons.CreateVariants(translations.Select(t => t.Translation))); while (true) { var input = await _chat.TryWaitInlineIntKeyboardInput(); if (!input.HasValue) { return(false); } if (input.Value >= 0 && input.Value < translations.Count) { var selected = translations[input.Value]; //var allTranslations = results.Select(t => t.Translation).ToArray(); var allPhrases = selected.Phrases; // results.SelectMany(t => t.Phrases).ToArray(); _wordService.SaveForExams( word: word, transcription: translations[0].Transcription, translations: new[] { selected.Translation }, allMeanings: translations.Select(t => t.Translation.Trim().ToLower()).ToArray(), phrases: allPhrases); await _chat.SendMessage($"Saved. Translations: {1}, Phrases: {allPhrases.Length}"); return(true); } } }
public List <TranslationAndContext> TranslateAndAddToDb(string word) { List <TranslationAndContext> translationsWithContext = new List <TranslationAndContext>(); if (!word.Contains(' ')) { Task <YaDefenition[]> task = null; task = _yapiDicClient.Translate(word); task?.Wait(); //Создаем из ответа(если он есть) TranslationAndContext или WordDictionary? if (task?.Result?.Any() == true) { var variants = task.Result.SelectMany(r => r.Tr); var transcription = task.Result.Select(r => r.Ts).FirstOrDefault(); foreach (var yandexTranslation in variants) { var yaPhrases = yandexTranslation.GetPhrases(word); var dbPhrases = new List <Phrase>(); //Заполняем бд(wordDictionary и фразы) var id = _dictionaryService.AddNewWordPairToDictionary( word, yandexTranslation.Text, transcription, TranslationSource.Yadic); foreach (var yaPhrase in yaPhrases) { var phrase = yaPhrase.MapToDbPhrase(); dbPhrases.Add(phrase); _dictionaryService.AddPhraseForWordPair(id, word, null, phrase.EnPhrase, phrase.RuTranslate); } translationsWithContext.Add(new TranslationAndContext(id, word, yandexTranslation.Text, transcription, dbPhrases.ToArray())); } return(translationsWithContext); } } try { var transAnsTask = _yapiTransClient.Translate(word); transAnsTask.Wait(); if (!string.IsNullOrWhiteSpace(transAnsTask.Result)) { //1. Заполняем бд(wordDictionary, фраз нет) var id = _dictionaryService.AddNewWordPairToDictionary( word, transAnsTask.Result, "[]", TranslationSource.Yadic); //2. Дополняем ответ translationsWithContext.Add(new TranslationAndContext(id, word, transAnsTask.Result, "[]", new Phrase[0])); } } catch (Exception e) { return(translationsWithContext); } return(translationsWithContext); }
public void Enter(int userId) { var yaStatus = _addWordService.PingYandex(); if (yaStatus.isYaDicOnline) { Console.WriteLine("Yandex dic is online"); } else { Console.WriteLine("Yandex dic is offline"); } if (yaStatus.isYaTransOnline) { Console.WriteLine("Yandex trans is online"); } else { Console.WriteLine("Yandex trans is offline"); } while (true) { Console.Write("Enter [e] for exit or "); Console.Write("Enter english word: "); string word = Console.ReadLine(); if (word == "e") { break; } Task <YaDefenition[]> task = null; // List<TranslationAndContext> translations = new List<TranslationAndContext>(); if (yaStatus.isYaDicOnline) { _addWordService.Translate(word); } task = _addWordService.Translate(word); task?.Wait(); List <TranslationAndContext> translations = new List <TranslationAndContext>(); if (task?.Result?.Any() == true) { sourse = "Yandex Dictionary"; var variants = task.Result.SelectMany(r => r.Tr); foreach (var yandexTranslation in variants) { var phrases = yandexTranslation.GetPhrases(word); translations.Add(new TranslationAndContext(word, yandexTranslation.Text, yandexTranslation.Pos, new Phrase[0])); //добавить переведенные фразы вместо Phrase[0] } } if (!translations.Any()) { try { var transAnsTask = _yapiTransClient.Translate(word); transAnsTask.Wait(); if (string.IsNullOrWhiteSpace(transAnsTask.Result)) { Console.WriteLine("No translations found. Check the word and try again"); } else { translations.Add(new TranslationAndContext(word, transAnsTask.Result, null, new Phrase[0])); } } catch (Exception e) { Console.WriteLine("No translations found. Check the word and try again"); } } if (translations.Any()) { Console.WriteLine("e: [back to main menu]"); Console.WriteLine("c: [CANCEL THE ENTRY]"); int i = 1; foreach (var translation in translations) { if (translation.Phrases.Any()) { Console.WriteLine($"{i}: {translation.Translation}\t (+{translation.Phrases.Length})"); } else { Console.WriteLine($"{i}: {translation.Translation}"); } i++; } try { var results = ChooseTranslation(translations.ToArray()); if (results?.Any() == true) { var userTranslations = results.Select(t => t.Translation).ToArray(); var allPhrases = results.SelectMany(t => t.Phrases).ToArray(); var allMeanings = translations.Select(t => t.Translation).ToArray(); //TODO Adding To dictionary /* _dictionaryService.AddNewWordToDictionary( * enword: word, * transcription: translations[0].Transcription, * ruword: allMeanings, * phrases: allPhrases, * sourse: sourse); * _usersWordService.SavePairToUser( * userId: user.UserId, * word: word, * userTranslations: userTranslations, * IsPhrase: false); */ Console.WriteLine( $"Saved. Tranlations: {userTranslations.Length}, Phrases: {allPhrases.Length}"); } } catch (OperationCanceledException) { return; } } } }