示例#1
0
        public async Task <IActionResult> WordExamples()
        {
            WordExamplesViewModel wordExamplesViewModel = new WordExamplesViewModel();

            wordExamplesViewModel.Senses = new List <TranslationViewModel>();
            wordExamplesViewModel.TranslaatedTextToReturn = PaginatedList <TranslationViewModel> .Create(
                wordExamplesViewModel.Senses,
                0, 1);

            return(View(wordExamplesViewModel));
        }
示例#2
0
        public async Task <IActionResult> WordExamples(WordExamplesViewModel wordExamplesViewModel,
                                                       int?pageNumber)
        {
            if (!ModelState.IsValid)
            {
                return(null);
            }
            int    pageSize    = 5;
            String Word        = wordExamplesViewModel.Word;
            var    SessionWord = HttpContext.Session.GetString(_SessionWord);

            if (!String.IsNullOrEmpty(SessionWord))
            {
                if (SessionWord.Equals(wordExamplesViewModel.Word))
                {
                    //Вчитај примери текст поле за вчитување преводи
                    //Ako requestot e od istiot korisni i veke e prevedeno zborceto
                    var SessionSenses                 = HttpContext.Session.GetObjectFromJson <List <TranslationViewModel> >(_SessionSenses);
                    var SessionTranslatedSenses       = HttpContext.Session.GetObjectFromJson <List <TranslationViewModel> >(_SessionTranslatedSenses);
                    var SessionTranslatedSensesSumary = HttpContext.Session.GetObjectFromJson <List <TranslationViewModel> >(_SessionTranslatedSensesSumary);
                    PaginatedList <TranslationViewModel> sessionPaginatedList = PaginatedList <TranslationViewModel> .Create(
                        SessionTranslatedSensesSumary,
                        pageNumber ?? 1, pageSize);

                    if (SessionSenses != null &&
                        SessionSenses.Count() > 0 &&
                        sessionPaginatedList.Any(m => m.TekstZaPreveduvanje == wordExamplesViewModel.Word && m.Hrvatski == null && m.Angliski == null))
                    {
                        wordExamplesViewModel.TranslaatedTextToReturn = PaginatedList <TranslationViewModel> .Create(
                            SessionSenses,
                            pageNumber ?? 1,
                            pageSize);

                        List <TranslationViewModel> Result = new List <TranslationViewModel>();
                        TranslationViewModel        result = new TranslationViewModel();
                        foreach (var tekst in wordExamplesViewModel.TranslaatedTextToReturn)
                        {
                            if (wordExamplesViewModel.selectLanguageTranslationListViewModel.SelectedPrevod.Equals("MkDe") && tekst.Hrvatski == null && tekst.Angliski == null)
                            {
                                result = await MacedonianCroatianEnglishGermanTranslation.MkCroEnDe(tekst.Makedonski, _googleCloudPlatformProjectName.Value.ProjectName, _logger, _googleTranslateApiCredentials);

                                result.TekstZaPreveduvanje = Word;
                                SessionTranslatedSenses.Add(tekst);
                                Result.Add(result);
                            }
                            else if (wordExamplesViewModel.selectLanguageTranslationListViewModel.SelectedPrevod.Equals("DeMk") && tekst.Hrvatski == null && tekst.Angliski == null)
                            {
                                result = await MacedonianCroatianEnglishGermanTranslation.DeEnCroMk(tekst.Germanski, _googleCloudPlatformProjectName.Value.ProjectName, _logger, _googleTranslateApiCredentials);

                                result.TekstZaPreveduvanje = Word;
                                SessionTranslatedSenses.Add(result);
                                Result.Add(result);
                            }
                        }
                        //wordExamplesViewModel.TranslaatedTextToReturn.Add(result);
                        for (int i = 0; i < Result.Count; i++)
                        {
                            for (int j = 0; j < SessionSenses.Count; j++)
                            {
                                if (SessionSenses[j].Germanski == Result[i].Germanski)
                                {
                                    SessionTranslatedSensesSumary[j].TekstZaPreveduvanje = Result[i].TekstZaPreveduvanje;
                                    SessionTranslatedSensesSumary[j].Makedonski          = Result[i].Makedonski;
                                    SessionTranslatedSensesSumary[j].Hrvatski            = Result[i].Hrvatski;
                                    SessionTranslatedSensesSumary[j].Angliski            = Result[i].Angliski;
                                    SessionTranslatedSensesSumary[j].Germanski           = Result[i].Germanski;

                                    SessionSenses[j].TekstZaPreveduvanje = Result[i].TekstZaPreveduvanje;
                                    SessionSenses[j].Makedonski          = Result[i].Makedonski;
                                    SessionSenses[j].Hrvatski            = Result[i].Hrvatski;
                                    SessionSenses[j].Angliski            = Result[i].Angliski;
                                    SessionSenses[j].Germanski           = Result[i].Germanski;
                                }
                            }
                        }

                        wordExamplesViewModel.TranslaatedTextToReturn = PaginatedList <TranslationViewModel> .Create(
                            SessionTranslatedSensesSumary,
                            pageNumber ?? 1,
                            pageSize);

                        HttpContext.Session.SetObjectAsJson(_SessionTranslatedSenses, SessionTranslatedSenses);
                        HttpContext.Session.SetObjectAsJson(_SessionTranslatedSensesSumary, SessionTranslatedSensesSumary);

                        return(PartialView("_sensesPartialView", wordExamplesViewModel));
                    }
                    else
                    {
                        wordExamplesViewModel.TranslaatedTextToReturn = PaginatedList <TranslationViewModel> .Create(
                            SessionTranslatedSensesSumary,
                            pageNumber ?? 1,
                            pageSize);

                        return(PartialView("_sensesPartialView", wordExamplesViewModel));
                    }
                }
            }

            if (String.IsNullOrEmpty(wordExamplesViewModel.Word))
            {
                return(null);
            }
            String UserWordToTranslate = wordExamplesViewModel.Word;

            if (wordExamplesViewModel.selectLanguageTranslationListViewModel.SelectedPrevod.Equals("MkDe"))
            {
                TranslationViewModel result = await MacedonianCroatianEnglishGermanTranslation.MkCroEnDe(wordExamplesViewModel.Word, _googleCloudPlatformProjectName.Value.ProjectName, _logger, _googleTranslateApiCredentials);

                wordExamplesViewModel.Word = result.Germanski;
            }

            wordExamplesViewModel.Word = await lemmas(wordExamplesViewModel.Word.ToLower());


            HttpClient httpClient = new HttpClient();

            httpClient.BaseAddress = new Uri("https://od-api.oxforddictionaries.com/api/v2/translations/de/en/" + HttpUtility.UrlEncode(wordExamplesViewModel.Word));

            Dictionary <string, string> queryString = new Dictionary <string, string>();

            queryString = new Dictionary <string, string>()
            {
                { "strictMatch", "true" },
                { "fields", "examples" }
            };

            var requestQueryString = QueryHelpers.AddQueryString("", queryString);
            var request            = new HttpRequestMessage(HttpMethod.Get, requestQueryString);

            request.Headers.Add("Accept", "application/json");
            request.Headers.Add("app_id", _oxfordDictionariesApi.Value.app_id);
            request.Headers.Add("app_key", _oxfordDictionariesApi.Value.app_key);

            var httpResponse = await httpClient.SendAsync(request);

            WordExamplesViewModel wordExamplesViewModelToReturn = wordExamplesViewModel;

            wordExamplesViewModelToReturn.Senses = new List <TranslationViewModel>();

            JObject data = new JObject();

            if (httpResponse.StatusCode == HttpStatusCode.OK)
            {
                data = await getRequestJson(httpResponse);
            }
            else if (httpResponse.StatusCode == HttpStatusCode.NotFound)
            {
                return(null);
            }

            //return new JsonResult(data["results"].Children()["lexicalEntries"].Children()["entries"].Children());

            foreach (var results in data["results"])
            {
                foreach (var lexicalEntries in results.Value <JToken>("lexicalEntries"))
                {
                    foreach (var entries in lexicalEntries.Value <JToken>("entries"))
                    {
                        foreach (var senses in entries.Value <JToken>("senses"))
                        {
                            foreach (var text in senses.Value <JToken>("examples"))
                            {
                                TranslationViewModel translationViewModel = new TranslationViewModel();
                                translationViewModel.Germanski           = text.Value <String>("text");
                                translationViewModel.TekstZaPreveduvanje = Word;
                                wordExamplesViewModelToReturn.Senses.Add(translationViewModel);
                            }
                        }
                    }
                }
            }
            PaginatedList <TranslationViewModel> tempPaginatedList = PaginatedList <TranslationViewModel> .Create(
                wordExamplesViewModelToReturn.Senses,
                pageNumber ?? 1, pageSize);

            List <TranslationViewModel> tempTranslationViewModel = new List <TranslationViewModel>();

            //prv pat zbor
            foreach (var tekst in tempPaginatedList)
            {
                TranslationViewModel result = await MacedonianCroatianEnglishGermanTranslation.DeEnCroMk(tekst.Germanski, _googleCloudPlatformProjectName.Value.ProjectName, _logger, _googleTranslateApiCredentials);

                result.TekstZaPreveduvanje = Word;
                tempTranslationViewModel.Add(result);
            }

            for (int i = 0; i < tempPaginatedList.Count; i++)
            {
                for (int j = 0; j < tempTranslationViewModel.Count; j++)
                {
                    if (tempPaginatedList[i].Germanski == tempTranslationViewModel[j].Germanski)
                    {
                        tempPaginatedList[j].Makedonski = tempTranslationViewModel[i].Makedonski;
                        tempPaginatedList[j].Hrvatski   = tempTranslationViewModel[i].Hrvatski;
                        tempPaginatedList[j].Angliski   = tempTranslationViewModel[i].Angliski;
                        tempPaginatedList[j].Germanski  = tempTranslationViewModel[i].Germanski;
                    }
                }
            }

            wordExamplesViewModelToReturn.TranslaatedTextToReturn = tempPaginatedList;

            List <TranslationViewModel> SessinTranslatedSensesSumary = wordExamplesViewModelToReturn.Senses;

            for (int i = 0; i < SessinTranslatedSensesSumary.Count; i++)
            {
                for (int j = 0; j < wordExamplesViewModelToReturn.TranslaatedTextToReturn.Count; j++)
                {
                    if (wordExamplesViewModelToReturn.TranslaatedTextToReturn[j].Germanski == SessinTranslatedSensesSumary[i].Germanski &&
                        wordExamplesViewModelToReturn.TranslaatedTextToReturn[j].Hrvatski != null && wordExamplesViewModelToReturn.TranslaatedTextToReturn[j].Angliski != null)
                    {
                        SessinTranslatedSensesSumary[i].Makedonski = wordExamplesViewModelToReturn.TranslaatedTextToReturn[j].Makedonski;
                        SessinTranslatedSensesSumary[i].Hrvatski   = wordExamplesViewModelToReturn.TranslaatedTextToReturn[j].Hrvatski;
                        SessinTranslatedSensesSumary[i].Angliski   = wordExamplesViewModelToReturn.TranslaatedTextToReturn[j].Angliski;
                        SessinTranslatedSensesSumary[i].Germanski  = wordExamplesViewModelToReturn.TranslaatedTextToReturn[j].Germanski;
                    }
                }
            }

            HttpContext.Session.SetObjectAsJson(_SessionSenses, wordExamplesViewModelToReturn.Senses);
            HttpContext.Session.SetObjectAsJson(_SessionTranslatedSenses, wordExamplesViewModelToReturn.TranslaatedTextToReturn);
            HttpContext.Session.SetObjectAsJson(_SessionTranslatedSensesSumary, SessinTranslatedSensesSumary);
            HttpContext.Session.SetString(_SessionWord, UserWordToTranslate);


            //return wordExamplesViewModelToReturn;
            return(PartialView("_sensesPartialView", wordExamplesViewModelToReturn));
        }