Example #1
0
        public async Task AzureTextAnalyticsService_GetArticleSentiment()
        {
            var articleJson = File.ReadAllText(TestContext.CurrentContext.TestDirectory + "/CoreTests/NewsAPI-sample.json");

            JsonDocument doc      = JsonDocument.Parse(articleJson);
            JsonElement  root     = doc.RootElement;
            JsonElement  articles = root.GetProperty("articles");

            foreach (JsonElement article in articles.EnumerateArray())
            {
                System.Diagnostics.Debug.WriteLine($"Article name: {article.GetProperty("title")}; description: {article.GetProperty("description")}");
            }

            JsonElement           article1      = articles.EnumerateArray().FirstOrDefault <JsonElement>();
            string                textToAnalyze = article1.GetProperty("title") + ". " + article1.GetProperty("description");
            AzTextAnalyticsResult sentiment     = await _textAnalyticsService.AnalyzeText(textToAnalyze);

            JsonSerializerOptions options = new JsonSerializerOptions
            {
                WriteIndented = true
            };

            options.Converters.Add(new JsonStringEnumConverter());

            Assert.IsNotNull(sentiment);
            System.Diagnostics.Debug.WriteLine(JsonSerializer.Serialize(sentiment, options));
        }
        public async Task OnPostAsync()
        {
            var response = await _analyticsService.AnalyzeText(TextToAnalyze);

            var options = new JsonSerializerOptions
            {
                WriteIndented = true
            };

            Output = JsonSerializer.Serialize(response, options);
        }
Example #3
0
        private async Task PopulateSentimentForArticle(Article article)
        {
            var result = await _analyticsService.AnalyzeText(article.TextToAnalyze);

            article.TextAnalyticsResult = result;
        }