private async Task AnalyzeTextAsync()
        {
            try
            {
                if (!string.IsNullOrEmpty(this.speechRecognitionTextBox.Text))
                {
                    SentimentResult textAnalysisResult = await TextAnalyticsHelper.GetSentimentAsync(new string[] { this.speechRecognitionTextBox.Text });

                    double score = textAnalysisResult.Scores.ElementAt(0);
                    this.sentimentControl.Sentiment = score;
                }
                else
                {
                    this.sentimentControl.Sentiment = 0.5;
                }

                this.OnSpeechRecognitionAndSentimentProcessed(new SpeechRecognitionAndSentimentResult {
                    SpeechRecognitionText = this.speechRecognitionTextBox.Text, TextAnalysisSentiment = this.sentimentControl.Sentiment
                });
            }
            catch (Exception ex)
            {
                await Util.GenericApiCallExceptionHandler(ex, "Error during Text Analytics call.");
            }
        }
Ejemplo n.º 2
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.GetSentimentAsync(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 (Exception ex)
            {
                await Util.GenericApiCallExceptionHandler(ex, "Processing error");
            }
            finally
            {
                this.progressRing.IsActive = false;
            }
        }