Пример #1
0
        private void ValidateInDocumenResult(CategorizedEntityCollection entities, List <string> minimumExpectedOutput)
        {
            Assert.IsNotNull(entities.Warnings);
            Assert.GreaterOrEqual(entities.Count, minimumExpectedOutput.Count);
            foreach (CategorizedEntity entity in entities)
            {
                Assert.That(entity.Text, Is.Not.Null.And.Not.Empty);
                Assert.IsTrue(minimumExpectedOutput.Contains(entity.Text, StringComparer.OrdinalIgnoreCase));
                Assert.IsNotNull(entity.Category);
                Assert.GreaterOrEqual(entity.ConfidenceScore, 0.0);
                Assert.GreaterOrEqual(entity.Offset, 0);
                Assert.Greater(entity.Length, 0);

                if (entity.SubCategory != null)
                {
                    Assert.IsNotEmpty(entity.SubCategory);
                }
            }
        }
Пример #2
0
        public async Task RecognizeEntitiesTest()
        {
            TextAnalyticsClient client = GetClient();
            string document            = "Microsoft was founded by Bill Gates and Paul Allen.";

            CategorizedEntityCollection entities = await client.RecognizeEntitiesAsync(document);

            Assert.AreEqual(3, entities.Count);

            var entitiesList = new List <string> {
                "Bill Gates", "Microsoft", "Paul Allen"
            };

            foreach (CategorizedEntity entity in entities)
            {
                Assert.IsTrue(entitiesList.Contains(entity.Text));
                Assert.IsNotNull(entity.ConfidenceScore);
            }
        }
Пример #3
0
        public void RecognizeEntities()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

            var client = new TextAnalyticsClient(new Uri(endpoint), new AzureKeyCredential(apiKey));

            #region Snippet:RecognizeEntities
            string document = @"We love this trail and make the trip every year. The views are breathtaking and well
                                worth the hike! Yesterday was foggy though, so we missed the spectacular views.
                                We tried again today and it was amazing. Everyone in my family liked the trail although
                                it was too challenging for the less athletic among us.
                                Not necessarily recommended for small children.
                                A hotel close to the trail offers services for childcare in case you want that.";

            try
            {
                Response <CategorizedEntityCollection> response           = client.RecognizeEntities(document);
                CategorizedEntityCollection            entitiesInDocument = response.Value;

                Console.WriteLine($"Recognized {entitiesInDocument.Count} entities:");
                foreach (CategorizedEntity entity in entitiesInDocument)
                {
                    Console.WriteLine($"  Text: {entity.Text}");
                    Console.WriteLine($"  Offset: {entity.Offset}");
                    Console.WriteLine($"  Length: {entity.Length}");
                    Console.WriteLine($"  Category: {entity.Category}");
                    if (!string.IsNullOrEmpty(entity.SubCategory))
                    {
                        Console.WriteLine($"  SubCategory: {entity.SubCategory}");
                    }
                    Console.WriteLine($"  Confidence score: {entity.ConfidenceScore}");
                    Console.WriteLine("");
                }
            }
            catch (RequestFailedException exception)
            {
                Console.WriteLine($"Error Code: {exception.ErrorCode}");
                Console.WriteLine($"Message: {exception.Message}");
            }
            #endregion
        }
Пример #4
0
        public async Task RecognizeEntitiesAsync()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

            // Instantiate a client that will be used to call the service.
            var client = new TextAnalyticsClient(new Uri(endpoint), new AzureKeyCredential(apiKey));

            #region Snippet:RecognizeEntitiesAsync
            string document = "Microsoft was founded by Bill Gates and Paul Allen.";

            CategorizedEntityCollection entities = await client.RecognizeEntitiesAsync(document);

            Console.WriteLine($"Recognized {entities.Count} entities:");
            foreach (CategorizedEntity entity in entities)
            {
                Console.WriteLine($"Text: {entity.Text}, Category: {entity.Category}, SubCategory: {entity.SubCategory}, Confidence score: {entity.ConfidenceScore}");
            }
            #endregion
        }
Пример #5
0
        public void RecognizeEntities()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

            #region Snippet:TextAnalyticsSample4CreateClient
            var client = new TextAnalyticsClient(new Uri(endpoint), new AzureKeyCredential(apiKey));
            #endregion

            #region Snippet:RecognizeEntities
            string document = "Microsoft was founded by Bill Gates and Paul Allen.";

            CategorizedEntityCollection entities = client.RecognizeEntities(document);

            Console.WriteLine($"Recognized {entities.Count} entities:");
            foreach (CategorizedEntity entity in entities)
            {
                Console.WriteLine($"Text: {entity.Text}, Category: {entity.Category}, SubCategory: {entity.SubCategory}, Confidence score: {entity.ConfidenceScore}");
            }
            #endregion
        }
        public async Task RecognizeEntitiesNullCategory()
        {
            using var stream = new MemoryStream(Encoding.UTF8.GetBytes(@"
                {
                    ""kind"": ""EntityRecognitionResults"",
                    ""results"": {
                        ""documents"": [
                            {
                                ""id"": ""0"",
                                ""entities"": [
                                    {
                                        ""text"": ""Microsoft"",
                                        ""category"": null,
                                        ""offset"": 0,
                                        ""length"": 9,
                                        ""confidenceScore"": 0.81
                                    }
                                ],
                                ""warnings"": []
                            }
                        ],
                        ""errors"": [],
                        ""modelVersion"": ""2020 -04-01""
                    }
                }"));

            var mockResponse = new MockResponse(200);

            mockResponse.ContentStream = stream;

            var mockTransport = new MockTransport(new[] { mockResponse });
            var client        = CreateTestClient(mockTransport);

            CategorizedEntityCollection response = await client.RecognizeEntitiesAsync("Microsoft was founded");

            Assert.IsNotNull(response.FirstOrDefault().Category);
        }
        private async Task AnalyzeTextAsync()
        {
            try
            {
                this.progressControl.IsActive = true;
                DisplayProcessingUI();

                // detect language
                string           input            = this.inputText.Text;
                DetectedLanguage detectedLanguage = await TextAnalyticsHelper.DetectLanguageAsync(input);

                string languageCode = TextAnalyticsHelper.GetLanguageCode(detectedLanguage);

                // check supported languages
                bool isOpinionMiningSupported = TextAnalyticsHelper.OpinionMiningSupportedLanguages.Any(l => string.Equals(l, languageCode, StringComparison.OrdinalIgnoreCase));
                bool isSentimentSupported     = TextAnalyticsHelper.SentimentAnalysisSupportedLanguages.Any(l => string.Equals(l, languageCode, StringComparison.OrdinalIgnoreCase));
                bool isKeyPhraseSupported     = TextAnalyticsHelper.KeyPhraseExtractionSupportedLanguages.Any(l => string.Equals(l, languageCode, StringComparison.OrdinalIgnoreCase));
                bool isNamedEntitySupported   = TextAnalyticsHelper.NamedEntitySupportedLanguages.Any(l => string.Equals(l, languageCode, StringComparison.OrdinalIgnoreCase));
                bool isEntityLinkingSupported = TextAnalyticsHelper.EntityLinkingSupportedLanguages.Any(l => string.Equals(l, languageCode, StringComparison.OrdinalIgnoreCase));

                // sentiment analysis, key phrase extraction, named entity recognition and entity linking
                Task <DocumentSentiment>           documentSentimentTask      = isSentimentSupported ? TextAnalyticsHelper.AnalyzeSentimentAsync(input, languageCode, isOpinionMiningSupported) : Task.FromResult <DocumentSentiment>(null);
                Task <KeyPhraseCollection>         detectedKeyPhrasesTask     = isKeyPhraseSupported ? TextAnalyticsHelper.ExtractKeyPhrasesAsync(input, languageCode) : Task.FromResult <KeyPhraseCollection>(null);
                Task <CategorizedEntityCollection> namedEntitiesResponseTask  = isNamedEntitySupported ? TextAnalyticsHelper.RecognizeEntitiesAsync(input, languageCode) : Task.FromResult <CategorizedEntityCollection>(null);
                Task <LinkedEntityCollection>      linkedEntitiesResponseTask = isEntityLinkingSupported ? TextAnalyticsHelper.RecognizeLinkedEntitiesAsync(input, languageCode) : Task.FromResult <LinkedEntityCollection>(null);

                await Task.WhenAll(documentSentimentTask, detectedKeyPhrasesTask, namedEntitiesResponseTask, linkedEntitiesResponseTask);

                DocumentSentiment           documentSentiment      = documentSentimentTask.Result;
                KeyPhraseCollection         detectedKeyPhrases     = detectedKeyPhrasesTask.Result;
                CategorizedEntityCollection namedEntitiesResponse  = namedEntitiesResponseTask.Result;
                LinkedEntityCollection      linkedEntitiesResponse = linkedEntitiesResponseTask.Result;

                // display results
                this.detectedLangTextBlock.Text = !string.IsNullOrEmpty(detectedLanguage.Name) ? $"{detectedLanguage.Name} (confidence: {(int)(detectedLanguage.ConfidenceScore * 100)}%)" : NotFound;

                this.detectedKeyPhrasesTextBlock.Text = detectedKeyPhrases != null && detectedKeyPhrases.Any()
                    ? string.Join(", ", detectedKeyPhrases)
                    : isKeyPhraseSupported?NotFound : LanguageNotSupported;

                this.namesEntitiesGridView.ItemsSource = namedEntitiesResponse != null && namedEntitiesResponse.Any()
                    ? namedEntitiesResponse.Select(x => new { x.Text, Category = $"[{x.Category}]" })
                    : new[] { new { Text = isNamedEntitySupported ? "No entities" : LanguageNotSupported, Category = "" } };

                this.linkedEntitiesGridView.ItemsSource = linkedEntitiesResponse != null && linkedEntitiesResponse.Any()
                    ? linkedEntitiesResponse.Select(x => new { Name = $"{x.Name} ({x.DataSource})", x.Url })
                    : new[] {
                    isEntityLinkingSupported
                        ? new { Name = "No linked entities", Url = new Uri("about:blank") }
                        : new { Name = LanguageNotSupported, Url = TextAnalyticsHelper.LanguageSupportUri }
                };

                if (isSentimentSupported)
                {
                    CreateSentimentChart(documentSentiment);

                    // mined opinions
                    OpinionMiningCollection.Clear();
                    var minedOpinions = documentSentiment?.Sentences.SelectMany(s => s.MinedOpinions);
                    if (minedOpinions != null && minedOpinions.Any())
                    {
                        var minedOpinionList = minedOpinions.Select(om => new MinedOpinion()
                        {
                            Aspect   = om.Aspect.Text,
                            Opinions = string.Join(", ", om.Opinions.Select(o => $"{o.Text} ({o.Sentiment.ToString("G")})"))
                        });
                        OpinionMiningCollection.AddRange(minedOpinionList);
                    }
                }
                else
                {
                    this.sentimentTextBlock.Text   = LanguageNotSupported;
                    this.sentimentChart.Visibility = Visibility.Collapsed;
                }

                // prepare json result
                var jsonResult = new
                {
                    LanguageDetection = detectedLanguage,
                    KeyPhrases        = detectedKeyPhrases,
                    Sentiment         = documentSentiment,
                    Entities          = namedEntitiesResponse,
                    EntityLinking     = linkedEntitiesResponse
                };
                this.jsonResultTextBlock.Text = JsonConvert.SerializeObject(jsonResult, Formatting.Indented);
            }
            catch (Exception ex)
            {
                await Util.GenericApiCallExceptionHandler(ex, "Failure analyzing text");
            }
            finally
            {
                this.progressControl.IsActive = false;
            }
        }