예제 #1
0
        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);
        }
예제 #2
0
        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);
        }