예제 #1
0
        private async Task <DictionaryResult> Search(string word, LanguagePair languagePair, CultureInfo culture)
        {
            word = word.ToLower(culture);

            DictionaryEntry entry = SearchOffline(word, languagePair);

            if (entry != null)
            {
                return(entry.Result);
            }

            DictionaryResult result = await SearchOnline(word, languagePair, culture).ConfigureAwait(false);

            if (result?.Definitions?.Length > 0)
            {
                return(result);
            }

            string correction = await YandexService.Correct(word, languagePair.InputLanguage).ConfigureAwait(false);

            if (!String.IsNullOrWhiteSpace(correction) && String.Compare(word, correction, true, culture) != 0)
            {
                return(await Search(correction, languagePair, culture).ConfigureAwait(false));
            }

            return(null);
        }
예제 #2
0
        private async Task <DictionaryResult> SearchOnline(string word, LanguagePair languagePair, CultureInfo culture)
        {
            DictionaryResult result = await YandexService.Lookup(word, languagePair).ConfigureAwait(false);

            if (result?.Definitions?.Length > 0)
            {
                string          returnedWord = result.Definitions[0].Word.ToLower(culture);
                DictionaryEntry entry        = SearchOffline(returnedWord, languagePair);
                if (entry != null)
                {
                    entry.Variations += word + "|";
                    Dictionary.Update(entry);
                }
                else
                {
                    entry          = new DictionaryEntry();
                    entry.Word     = returnedWord;
                    entry.Result   = result;
                    entry.Language = languagePair.ToString();

                    if (String.Compare(word, returnedWord, true, culture) != 0)
                    {
                        entry.Variations += word + "|";
                    }

                    Dictionary.Insert(entry);
                }
            }

            return(result);
        }
예제 #3
0
        public async Task <SearchResult[]> Translate(string word, CultureInfo inputCulture, CultureInfo outputCulture)
        {
            if (word.IsNonAlpha())
            {
                return(null);
            }

            word = word.SingleWhiteSpace();

            if (word.Length < 2 || word.Length > 250)
            {
                return(null);
            }

            LanguagePair languagePair      = CreateLangPair(inputCulture, outputCulture);
            string       translationResult = await YandexService.Translate(word, languagePair).ConfigureAwait(false);

            if (String.IsNullOrWhiteSpace(translationResult))
            {
                return(null);
            }

            if (String.Compare(translationResult, word, true, inputCulture) == 0)
            {
                return(null);
            }

            return(new SearchResult[] { new SearchResult {
                                            Definition = translationResult
                                        } });
        }
예제 #4
0
        private async Task <DictionaryResult> SearchOnline(string word, LanguagePair languagePair, CultureInfo culture)
        {
            DictionaryResult result = await YandexService.Lookup(word, languagePair).ConfigureAwait(false);

            if (result != null && result.Definitions != null && result.Definitions.Length > 0)
            {
                string           returnedWord = result.Definitions[0].Word.ToLower(culture);
                DictionaryEntity entity       = SearchOffline(returnedWord, languagePair);
                if (entity != null)
                {
                    entity.Variations += word + "|";
                    entity.Save();
                }
                else
                {
                    entity            = new DictionaryEntity(UnitOfWork);
                    entity.Word       = returnedWord;
                    entity.Definition = result.ToJson();
                    entity.Language   = languagePair.ToString();

                    if (String.Compare(word, returnedWord, true, culture) != 0)
                    {
                        entity.Variations += word + "|";
                    }

                    entity.Save();
                }

                UnitOfWork.CommitChanges();
            }

            return(result);
        }
예제 #5
0
        static void Main(string[] args)
        {
            IWeatherProviderService    weatherProvider = new YandexService(new RequestProvider());
            ICityWeatherInfoRepository infoRepository  = new CityWeatherInfoRepository();
            var app = new Application(weatherProvider, infoRepository);

            Console.ReadLine();
            // Нет особого смысла вызывать здесь Dispose
            app.Dispose();
        }
예제 #6
0
        public async void Test1()
        {
            var service = new GoogleService();
            var track   = await service.GetTrack(_googleTrackShareUrl);

            var yaService = new YandexService();
            var url       = await yaService.GetUrl(track);

            Assert.NotNull(track);
        }
예제 #7
0
        public void InitTest()
        {
            _mockRepository = new MockRepository(MockBehavior.Strict);

            _conversationService = _mockRepository.Create <IConversationService>();
            _mapper = _mockRepository.Create <IMapper>();

            _target = new YandexService(_conversationService.Object, _mapper.Object);

            _fixture = new Fixture();
        }
예제 #8
0
        public async Task GetWeatherAsync_Success()
        {
            // Arrange
            var requestProviderMock = new Mock <IRequestProvider>();

            requestProviderMock.Setup(x => x.GetAsync(new Uri("https://yandex.ru/pogoda/saint-petersburg")))
            .Returns(Task.FromResult(_pageFull));
            var service = new YandexService(requestProviderMock.Object);

            // Act
            var weather = await service.GetWeatherAsync("https://yandex.ru/pogoda/saint-petersburg");

            // Assert
            Assert.NotEmpty(weather);
        }
예제 #9
0
        public async Task GetCities_Success()
        {
            // Arrange
            var citiesCount         = 14;
            var requestProviderMock = new Mock <IRequestProvider>();

            requestProviderMock.Setup(x => x.GetAsync(It.IsAny <Uri>())).Returns(Task.FromResult(_pageFull));
            var service = new YandexService(requestProviderMock.Object);

            // Act
            var cities = await service.GetCitiesAsync();

            // Assert
            var citiesList = cities.ToList();

            Assert.NotEmpty(citiesList);
            Assert.Equal(citiesCount, citiesList.Count);
        }
예제 #10
0
 public async Task <string> Pronounce(string word, CultureInfo inputCulture)
 {
     return(await YandexService.Pronounce(word, inputCulture.Name).ConfigureAwait(false));
 }
 public yandexdiscController()
 {
     _service = new YandexService();
 }