public void Enter(int userId) { // check Ya status 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"); } // ask word for translation while (true) { Console.Write("Enter [e] for exit or "); Console.Write("Enter english word: "); string word = Console.ReadLine(); if (word == "e") { break; } //find word in local dictionary(if not, find it in Ya dictionary) var translations = _addWordService.FindInDictionaryWithPhrases(word); if (!translations.Any() && yaStatus.isYaDicOnline) { translations = _addWordService.TranslateAndAddToDictionary(word); } if (!translations.Any()) { Console.WriteLine("No translations found. Check the word and try again"); } // choose thr translation else { 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.GetTranslations().FirstOrDefault()}\t (+{translation.Phrases.Count})"); } else { Console.WriteLine($"{i}: {translation.GetTranslations().FirstOrDefault()}"); } i++; } try { var results = ChooseTranslation(translations.ToArray()); if (results?.Any() == true) { var count = _addWordService.AddResultToUserCollection(userId, results); Console.WriteLine($"Saved. Translations: {count}"); } } catch (OperationCanceledException) { return; } } } }
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; } } } }