예제 #1
0
        private string GetWordText(
            PaginationCollection <WordInLearningSet> collection,
            DictionaryWord dictionaryWord,
            IReadOnlyList <Translation> translations,
            WordInLearningSet wordInLearningSet)
        {
            var engWord             = dictionaryWord.Word;
            var transcription       = dictionaryWord.Transcription;
            var allowedTranslations = SearchForAllowedTranslations(translations, wordInLearningSet);
            var example             = GetExampleOrNull(wordInLearningSet, allowedTranslations);

            var msg = new StringBuilder();

            msg.AppendLine($"*{engWord.EscapeForMarkdown().Capitalize()}*");
            if (!string.IsNullOrWhiteSpace(transcription))
            {
                msg.Append($"```\r\n[{transcription.EscapeForMarkdown()}]\r\n```");
            }
            msg.AppendLine(
                $"\r\n*{string.Join("\r\n", allowedTranslations.Select(a => a.TranslatedText.EscapeForMarkdown().Capitalize()))}*");
            if (example != null)
            {
                msg.Append(
                    $"```\r\n\r\n" +
                    $"{Emojis.OpenQuote}{example.OriginPhrase.EscapeForMarkdown()}{Emojis.CloseQuote}\r\n" +
                    $"{Emojis.OpenQuote}{example.TranslatedPhrase.EscapeForMarkdown()}{Emojis.CloseQuote}" +
                    $"\r\n```");
            }
            msg.AppendLine($"\r\n{Chat.Texts.XofYMarkdown(collection.Page + 1, collection.Count)}");
            return(msg.ToString());
        }
예제 #2
0
        private async Task AddWordToUser(WordInLearningSet word)
        {
            var tranlsations = await _addWordService.GetOrDownloadTranslation(word.Word);

            var allowedTranlsations = tranlsations.Where(
                translation => word.AllowedTranslations.Any(
                    allowed => allowed.Equals(translation.TranslatedText, StringComparison.InvariantCultureIgnoreCase)));

            foreach (var tranlsation in allowedTranlsations)
            {
                await _addWordService.AddTranslationToUser(Chat.User, tranlsation);
            }
        }
예제 #3
0
        private static Example GetExampleOrNull(
            WordInLearningSet wordInLearningSet, Translation[] allowedTranslations)
        {
            var allowed = allowedTranslations.SelectMany(t => t.Examples)
                          .Where(e => wordInLearningSet.AllowedExamples.Contains(e.Id));
            var bestFit = allowed.Where(
                a => a.TranslatedPhrase.Contains(
                    a.TranslatedWord, StringComparison.InvariantCultureIgnoreCase))
                          .OrderBy(e => e.TranslatedPhrase.Length)
                          .FirstOrDefault();

            return(bestFit ??
                   allowed
                   .OrderBy(e => e.TranslatedPhrase.Length)
                   .FirstOrDefault());
        }
예제 #4
0
        private static Translation[] SearchForAllowedTranslations(
            IReadOnlyList <Translation> translations, WordInLearningSet wordInLearningSet)
        {
            var allowedTranslations = translations.Where(
                t =>
                wordInLearningSet.AllowedTranslations.Any(
                    a => a.Equals(
                        t.TranslatedText,
                        StringComparison.InvariantCultureIgnoreCase))
                )
                                      .Take(3)
                                      .ToArray();

            if (!allowedTranslations.Any())
            {
                allowedTranslations = new[] { translations.First() }
            }
            ;
            return(allowedTranslations);
        }
    }
예제 #5
0
 private Task <bool> HasUserWord(WordInLearningSet current) =>
 _usersWordsService.Contains(Chat.User, current.Word.ToLower());