Ejemplo n.º 1
0
        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;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        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);
                }
            }
        }