Exemplo n.º 1
0
        private async Task SearchAsync(string query)
        {
            try
            {
                this.TopKeyPhrases.Clear();
                this.latestSearchResult.Clear();
                this.FilteredNewsResults.Clear();
                this.sentimentDistributionControl.UpdateData(Enumerable.Empty <double>());

                this.progressRing.IsActive = true;

                string userLanguage = this.languageComboBox.SelectedValue.ToString();

                var news = await BingSearchHelper.GetNewsSearchResults(query, count : 50, offset : 0, market : GetBingSearchMarketFromLanguage(userLanguage));

                Task <SentimentResult> sentimentTask = TextAnalyticsHelper.GetTextSentimentAsync(news.Select(n => n.Title).ToArray(), language: GetTextAnalyticsLanguageCodeFromLanguage(userLanguage));

                Task <KeyPhrasesResult> keyPhrasesTask;
                if (IsLanguageSupportedByKeyPhraseAPI(userLanguage))
                {
                    keyPhrasesTask = TextAnalyticsHelper.GetKeyPhrasesAsync(news.Select(n => n.Title).ToArray(), language: GetTextAnalyticsLanguageCodeFromLanguage(userLanguage));
                }
                else
                {
                    keyPhrasesTask = Task.FromResult(new KeyPhrasesResult {
                        KeyPhrases = new string[][] { new string [] { "Not available in this language" } }
                    });
                }

                await Task.WhenAll(sentimentTask, keyPhrasesTask);

                var sentiment = sentimentTask.Result;

                for (int i = 0; i < news.Count(); i++)
                {
                    NewsArticle article = news.ElementAt(i);
                    this.latestSearchResult.Add(new NewsAndSentimentScore {
                        Article = article, TitleSentiment = Math.Round(sentiment.Scores.ElementAt(i), 2)
                    });
                }

                UpdateFilteredResults();

                this.sentimentDistributionControl.UpdateData(this.latestSearchResult.Select(n => n.TitleSentiment));

                var wordGroups = keyPhrasesTask.Result.KeyPhrases.SelectMany(k => k).GroupBy(phrase => phrase, StringComparer.OrdinalIgnoreCase).OrderByDescending(g => g.Count()).Take(10).OrderBy(g => g.Key);
                this.TopKeyPhrases.AddRange(wordGroups.Select(g => new KeyPhraseCount {
                    KeyPhrase = g.Key, Count = g.Count()
                }));
            }
            catch (HttpRequestException ex)
            {
                await Util.GenericApiCallExceptionHandler(ex, "Processing error");
            }
            finally
            {
                this.progressRing.IsActive = false;
            }
        }
        private async Task SearchAsync(string query)
        {
            try
            {
                this.TopKeyPhrases.Clear();
                this.latestSearchResult.Clear();
                this.FilteredNewsResults.Clear();
                this.sentimentDistributionControl.UpdateData(Enumerable.Empty <double>());

                this.progressRing.IsActive = true;

                // get language and bing news titles
                string userLanguage            = this.languageComboBox.SelectedValue.ToString();
                IEnumerable <NewsArticle> news = await BingSearchHelper.GetNewsSearchResults(query, count : TotalNewsCount, offset : 0, market : GetBingSearchMarketFromLanguage(userLanguage));

                // analyze news titles
                AnalyzeTextResult analyzedNews = news.Any() ? await AnalyzeNewsAsync(news, userLanguage) : null;

                List <double>         scores        = analyzedNews?.Scores ?? new List <double>();
                List <KeyPhraseCount> topKeyPhrases = analyzedNews?.TopKeyPhrases ?? new List <KeyPhraseCount>();

                // display result
                for (int i = 0; i < news.Count(); i++)
                {
                    NewsArticle article = news.ElementAt(i);
                    double      score   = i < scores.Count ? scores.ElementAt(i) : 0.5;
                    this.latestSearchResult.Add(new NewsAndSentimentScore {
                        Article = article, TitleSentiment = Math.Round(score, 2)
                    });
                }

                UpdateFilteredResults();

                this.sentimentDistributionControl.UpdateData(this.latestSearchResult.Select(n => n.TitleSentiment));
                this.TopKeyPhrases.AddRange(topKeyPhrases);
            }
            catch (HttpRequestException ex)
            {
                await Util.GenericApiCallExceptionHandler(ex, "Processing error");
            }
            finally
            {
                this.progressRing.IsActive = false;
            }
        }