Esempio 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;
            }
        }
Esempio n. 2
0
        private async Task GetKeyPhrasesAsync()
        {
            try
            {
                KeyPhrasesResult keyPhrasesResult = await TextAnalyticsHelper.GetKeyPhrasesAsync(this.CallerTextBox.Text);

                this.KeyPhrases.AddRange(keyPhrasesResult.KeyPhrases.OrderBy(i => i));
            }
            catch (Exception ex)
            {
                await Util.GenericApiCallExceptionHandler(ex, "Error during Text Analytics 'Key Phrases' call.");
            }
        }