public void RecognizePiiEntitiesBatchConvenience() { string endpoint = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_ENDPOINT"); string subscriptionKey = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_SUBSCRIPTION_KEY"); var client = new TextAnalyticsClient(new Uri(endpoint), subscriptionKey); var inputs = new List <string> { "A developer with SSN 555-55-5555 whose phone number is 555-555-5555 is building tools with our APIs.", "Your ABA number - 111000025 - is the first 9 digits in the lower left hand corner of your personal check.", }; #region Snippet:TextAnalyticsSample5RecognizePiiEntitiesConvenience RecognizePiiEntitiesResultCollection results = client.RecognizePiiEntities(inputs); #endregion Debug.WriteLine($"The following Personally Identifiable Information entities were recognized:"); int i = 0; foreach (RecognizePiiEntitiesResult result in results) { Debug.WriteLine($"For input: \"{inputs[i++]}\","); Debug.WriteLine($"the following {result.NamedEntities.Count()} PII entit{(result.NamedEntities.Count() > 1 ? "ies were" : "y was")} found:"); foreach (NamedEntity entity in result.NamedEntities) { Debug.WriteLine($" Text: {entity.Text}, Type: {entity.Type}, SubType: {entity.SubType ?? "N/A"}, Score: {entity.Score:0.00}, Offset: {entity.Offset}, Length: {entity.Length}"); } } }
public void RecognizePiiEntitiesBatchConvenience() { string endpoint = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_ENDPOINT"); string apiKey = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_API_KEY"); // Instantiate a client that will be used to call the service. var client = new TextAnalyticsClient(new Uri(endpoint), new TextAnalyticsApiKeyCredential(apiKey)); var inputs = new List <string> { "A developer with SSN 555-55-5555 whose phone number is 555-555-5555 is building tools with our APIs.", "Your ABA number - 111000025 - is the first 9 digits in the lower left hand corner of your personal check.", }; #region Snippet:TextAnalyticsSample5RecognizePiiEntitiesConvenience RecognizePiiEntitiesResultCollection results = client.RecognizePiiEntitiesBatch(inputs); #endregion Debug.WriteLine($"The following Personally Identifiable Information entities were recognized:"); int i = 0; foreach (RecognizePiiEntitiesResult result in results) { Debug.WriteLine($"For input: \"{inputs[i++]}\","); Debug.WriteLine($"the following {result.Entities.Count()} PII entit{(result.Entities.Count() > 1 ? "ies were" : "y was")} found:"); foreach (PiiEntity entity in result.Entities) { Debug.WriteLine($" Text: {entity.Text}, Category: {entity.Category}, SubCategory: {entity.SubCategory}, Confidence score: {entity.ConfidenceScore}"); } } }
public async Task AnalyzeOperationAllActionsAndDisableServiceLogs() { TextAnalyticsClient client = GetClient(); TextAnalyticsActions batchActions = new TextAnalyticsActions() { ExtractKeyPhrasesOptions = new List <ExtractKeyPhrasesOptions>() { new ExtractKeyPhrasesOptions() { DisableServiceLogs = true } }, RecognizeEntitiesOptions = new List <RecognizeEntitiesOptions>() { new RecognizeEntitiesOptions() { DisableServiceLogs = true } }, RecognizePiiEntitiesOptions = new List <RecognizePiiEntitiesOptions>() { new RecognizePiiEntitiesOptions() { DisableServiceLogs = false } }, RecognizeLinkedEntitiesOptions = new List <RecognizeLinkedEntitiesOptions>() { new RecognizeLinkedEntitiesOptions() { DisableServiceLogs = true } }, }; AnalyzeBatchActionsOperation operation = await client.StartAnalyzeBatchActionsAsync(batchConvenienceDocuments, batchActions); await operation.WaitForCompletionAsync(); //Take the first page AnalyzeBatchActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault(); ExtractKeyPhrasesResultCollection keyPhrasesResult = resultCollection.ExtractKeyPhrasesActionsResults.FirstOrDefault().Result; RecognizeEntitiesResultCollection entitiesResult = resultCollection.RecognizeEntitiesActionsResults.FirstOrDefault().Result; RecognizePiiEntitiesResultCollection piiResult = resultCollection.RecognizePiiEntitiesActionsResults.FirstOrDefault().Result; RecognizeLinkedEntitiesResultCollection entityLinkingResult = resultCollection.RecognizeLinkedEntitiesActionsResults.FirstOrDefault().Result; Assert.IsNotNull(keyPhrasesResult); Assert.AreEqual(2, keyPhrasesResult.Count); Assert.IsNotNull(entitiesResult); Assert.AreEqual(2, keyPhrasesResult.Count); Assert.IsNotNull(piiResult); Assert.AreEqual(2, keyPhrasesResult.Count); Assert.IsNotNull(entityLinkingResult); Assert.AreEqual(2, keyPhrasesResult.Count); }
public async Task RecognizePiiEntitiesBatchWithStatisticsTest() { TextAnalyticsClient client = GetClient(); var inputs = new List <TextDocumentInput> { new TextDocumentInput("1", "A developer with SSN 555-55-5555 whose phone number is 555-555-5555 is building tools with our APIs.") { Language = "en", }, new TextDocumentInput("2", "Your ABA number - 111000025 - is the first 9 digits in the lower left hand corner of your personal check.") { Language = "en", } }; RecognizePiiEntitiesResultCollection results = await client.RecognizePiiEntitiesBatchAsync(inputs, new TextAnalyticsRequestOptions { IncludeStatistics = true }); foreach (RecognizePiiEntitiesResult result in results) { Assert.GreaterOrEqual(result.Entities.Count(), 2); } Assert.IsNotNull(results.Statistics.DocumentCount); Assert.IsNotNull(results.Statistics.InvalidDocumentCount); Assert.IsNotNull(results.Statistics.TransactionCount); Assert.IsNotNull(results.Statistics.ValidDocumentCount); }
public async Task RecognizePiiEntitiesBatchTest() { TextAnalyticsClient client = GetClient(); List <TextDocumentInput> documents = batchDocuments; RecognizePiiEntitiesResultCollection results = await client.RecognizePiiEntitiesBatchAsync(documents); foreach (RecognizePiiEntitiesResult result in results) { Assert.GreaterOrEqual(result.Entities.Count(), 2); } }
public async Task RecognizePiiEntitiesBatchTest() { TextAnalyticsClient client = GetClient(); RecognizePiiEntitiesResultCollection results = await client.RecognizePiiEntitiesBatchAsync(s_batchDocuments); var expectedOutput = new Dictionary <string, List <string> >() { { "1", s_document1ExpectedOutput }, { "2", s_document2ExpectedOutput }, }; ValidateBatchDocumentsResult(results, expectedOutput); }
public async Task RecognizePiiEntitiesBatchWithStatisticsTest() { TextAnalyticsClient client = GetClient(); RecognizePiiEntitiesResultCollection results = await client.RecognizePiiEntitiesBatchAsync(s_batchDocuments, new RecognizePiiEntitiesOptions { IncludeStatistics = true }); var expectedOutput = new Dictionary <string, List <string> >() { { "1", s_document1ExpectedOutput }, { "2", s_document2ExpectedOutput }, }; ValidateBatchDocumentsResult(results, expectedOutput, includeStatistics: true); }
public async Task RecognizePiiEntitiesBatchConvenienceTest() { TextAnalyticsClient client = GetClient(); var inputs = new List <string> { "A developer with SSN 555-55-5555 whose phone number is 555-555-5555 is building tools with our APIs.", "Your ABA number - 111000025 - is the first 9 digits in the lower left hand corner of your personal check." }; RecognizePiiEntitiesResultCollection results = await client.RecognizePiiEntitiesBatchAsync(inputs); foreach (RecognizePiiEntitiesResult result in results) { Assert.GreaterOrEqual(result.Entities.Count(), 2); } }
public async Task RecognizePiiEntitiesBatchWithStatisticsTest() { TextAnalyticsClient client = GetClient(); List <TextDocumentInput> documents = batchDocuments; RecognizePiiEntitiesResultCollection results = await client.RecognizePiiEntitiesBatchAsync(documents, new RecognizePiiEntitiesOptions { IncludeStatistics = true }); foreach (RecognizePiiEntitiesResult result in results) { Assert.GreaterOrEqual(result.Entities.Count(), 2); } Assert.Greater(results.Statistics.DocumentCount, 0); Assert.AreEqual(0, results.Statistics.InvalidDocumentCount); Assert.Greater(results.Statistics.TransactionCount, 0); Assert.Greater(results.Statistics.ValidDocumentCount, 0); }
public async Task RecognizePiiEntitiesBatchWithDomainTest() { TextAnalyticsClient client = GetClient(); RecognizePiiEntitiesResultCollection results = await client.RecognizePiiEntitiesBatchAsync(s_batchDocuments, new RecognizePiiEntitiesOptions() { DomainFilter = PiiEntityDomainType.ProtectedHealthInformation }); var expectedOutput = new Dictionary <string, List <string> >() { { "1", s_document1ExpectedOutput }, { "2", s_document2ExpectedOutput }, }; ValidateBatchDocumentsResult(results, expectedOutput); }
public async Task RecognizePiiEntitiesBatchWithErrorTest() { TextAnalyticsClient client = GetClient(); var inputs = new List <string> { "A developer with SSN 555-55-5555 whose phone number is 555-555-5555 is building tools with our APIs.", "", "Your ABA number - 111000025 - is the first 9 digits in the lower left hand corner of your personal check.", }; RecognizePiiEntitiesResultCollection results = await client.RecognizePiiEntitiesBatchAsync(inputs); Assert.IsTrue(!results[0].HasError); Assert.IsTrue(!results[2].HasError); Assert.IsTrue(results[1].HasError); Assert.Throws <InvalidOperationException>(() => results[1].Entities.GetType()); }
public async Task AnalyzeOperationWithPiiCategories() { TextAnalyticsClient client = GetClient(); var documents = new List <string> { "A developer with SSN 859-98-0987 whose phone number is 800-102-1100 is building tools with our APIs. They work at Microsoft.", }; TextAnalyticsActions batchActions = new TextAnalyticsActions() { RecognizePiiEntitiesActions = new List <RecognizePiiEntitiesAction>() { new RecognizePiiEntitiesAction() { CategoriesFilter = { PiiEntityCategory.USSocialSecurityNumber } } }, }; AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(documents, batchActions, "en"); await operation.WaitForCompletionAsync(); //Take the first page AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault(); IReadOnlyCollection <RecognizePiiEntitiesActionResult> piiActionsResults = resultCollection.RecognizePiiEntitiesResults; Assert.IsNotNull(piiActionsResults); RecognizePiiEntitiesResultCollection piiDocumentsResults = piiActionsResults.FirstOrDefault().DocumentsResults; Assert.AreEqual(1, piiDocumentsResults.Count); Assert.IsNotEmpty(piiDocumentsResults[0].Entities.RedactedText); Assert.IsFalse(piiDocumentsResults[0].HasError); Assert.AreEqual(1, piiDocumentsResults[0].Entities.Count); Assert.AreEqual(PiiEntityCategory.USSocialSecurityNumber, piiDocumentsResults[0].Entities.FirstOrDefault().Category); }
public async Task AnalyzeOperationBatchWithPHIDomain() { TextAnalyticsClient client = GetClient(); var documents = new List <string> { "A patient with medical id 12345678 whose phone number is 800-102-1100 is going under heart surgery", }; TextAnalyticsActions batchActions = new TextAnalyticsActions() { RecognizePiiEntitiesOptions = new List <RecognizePiiEntitiesOptions>() { new RecognizePiiEntitiesOptions() { DomainFilter = PiiEntityDomainType.ProtectedHealthInformation } }, DisplayName = "AnalyzeOperationWithPHIDomain", }; AnalyzeBatchActionsOperation operation = await client.StartAnalyzeBatchActionsAsync(documents, batchActions, "en"); await operation.WaitForCompletionAsync(PollingInterval); //Take the first page AnalyzeBatchActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault(); RecognizePiiEntitiesResultCollection result = resultCollection.RecognizePiiEntitiesActionsResults.ElementAt(0).Result; Assert.IsNotNull(result); Assert.AreEqual(1, result.Count); // TODO - Update this once the service starts returning RedactedText //var redactedText = string.Empty; //Assert.AreEqual(redactedText, result[0].Entities.RedactedText); Assert.IsFalse(result[0].HasError); Assert.AreEqual(2, result[0].Entities.Count); }
public async Task AnalyzeOperationWithPHIDomain() { TextAnalyticsClient client = GetClient(); var documents = new List <string> { "A patient with medical id 12345678 whose phone number is 800-102-1100 is going under heart surgery", }; TextAnalyticsActions batchActions = new TextAnalyticsActions() { RecognizePiiEntitiesActions = new List <RecognizePiiEntitiesAction>() { new RecognizePiiEntitiesAction() { DomainFilter = PiiEntityDomainType.ProtectedHealthInformation } }, DisplayName = "AnalyzeOperationWithPHIDomain", }; AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(documents, batchActions, "en"); await operation.WaitForCompletionAsync(); //Take the first page AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault(); IReadOnlyCollection <RecognizePiiEntitiesActionResult> piiActionsResults = resultCollection.RecognizePiiEntitiesActionsResults; Assert.IsNotNull(piiActionsResults); RecognizePiiEntitiesResultCollection piiResult = piiActionsResults.FirstOrDefault().Result; Assert.AreEqual(1, piiResult.Count); Assert.IsNotEmpty(piiResult[0].Entities.RedactedText); Assert.IsFalse(piiResult[0].HasError); Assert.AreEqual(2, piiResult[0].Entities.Count); }
private void ValidateBatchDocumentsResult(RecognizePiiEntitiesResultCollection results, Dictionary <string, List <string> > minimumExpectedOutput, bool includeStatistics = default) { Assert.That(results.ModelVersion, Is.Not.Null.And.Not.Empty); if (includeStatistics) { Assert.IsNotNull(results.Statistics); Assert.Greater(results.Statistics.DocumentCount, 0); Assert.Greater(results.Statistics.TransactionCount, 0); Assert.GreaterOrEqual(results.Statistics.InvalidDocumentCount, 0); Assert.GreaterOrEqual(results.Statistics.ValidDocumentCount, 0); } else { Assert.IsNull(results.Statistics); } foreach (RecognizePiiEntitiesResult entitiesInDocument in results) { Assert.That(entitiesInDocument.Id, Is.Not.Null.And.Not.Empty); Assert.False(entitiesInDocument.HasError); //Even though statistics are not asked for, TA 5.0.0 shipped with Statistics default always present. Assert.IsNotNull(entitiesInDocument.Statistics); if (includeStatistics) { Assert.GreaterOrEqual(entitiesInDocument.Statistics.CharacterCount, 0); Assert.Greater(entitiesInDocument.Statistics.TransactionCount, 0); } else { Assert.AreEqual(0, entitiesInDocument.Statistics.CharacterCount); Assert.AreEqual(0, entitiesInDocument.Statistics.TransactionCount); } ValidateInDocumenResult(entitiesInDocument.Entities, minimumExpectedOutput[entitiesInDocument.Id]); } }
public async Task RecognizePiiEntitiesBatchWithErrorTest() { TextAnalyticsClient client = GetClient(); var documents = new List <string> { "A developer with SSN 859-98-0987 whose phone number is 800-102-1100 is building tools with our APIs.", "", "Your ABA number - 111000025 - is the first 9 digits in the lower left hand corner of your personal check.", }; RecognizePiiEntitiesResultCollection results = await client.RecognizePiiEntitiesBatchAsync(documents); Assert.IsFalse(results[0].HasError); Assert.IsFalse(results[2].HasError); var exceptionMessage = "Cannot access result for document 1, due to error InvalidDocument: Document text is empty."; Assert.IsTrue(results[1].HasError); InvalidOperationException ex = Assert.Throws <InvalidOperationException>(() => results[1].Entities.GetType()); Assert.AreEqual(exceptionMessage, ex.Message); }
public async Task RecognizePiiEntitiesBatchTest() { TextAnalyticsClient client = GetClient(); var documents = new List <TextDocumentInput> { new TextDocumentInput("1", "A developer with SSN 555-55-5555 whose phone number is 555-555-5555 is building tools with our APIs.") { Language = "en", }, new TextDocumentInput("2", "Your ABA number - 111000025 - is the first 9 digits in the lower left hand corner of your personal check.") { Language = "en", } }; RecognizePiiEntitiesResultCollection results = await client.RecognizePiiEntitiesBatchAsync(documents); foreach (RecognizePiiEntitiesResult result in results) { Assert.GreaterOrEqual(result.Entities.Count(), 2); } }
public async Task AnalyzeOperationBatchWithPHIDomain() { TextAnalyticsClient client = GetClient(); var documents = new List <string> { "A patient with medical id 12345678 whose phone number is 800-102-1100 is going under heart surgery", }; AnalyzeOperationOptions operationOptions = new AnalyzeOperationOptions() { PiiTaskParameters = new PiiTaskParameters() { Domain = PiiTaskParametersDomain.Phi }, DisplayName = "AnalyzeOperationWithPHIDomain" }; AnalyzeOperation operation = await client.StartAnalyzeOperationBatchAsync(documents, operationOptions, "en"); await operation.WaitForCompletionAsync(PollingInterval); //Take the first page AnalyzeOperationResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault(); RecognizePiiEntitiesResultCollection result = resultCollection.Tasks.EntityRecognitionPiiTasks[0].Results;; Assert.IsNotNull(result); Assert.AreEqual(1, result.Count); // TODO - Update this once the service starts returning RedactedText //var redactedText = string.Empty; //Assert.AreEqual(redactedText, result[0].Entities.RedactedText); Assert.IsFalse(result[0].HasError); Assert.AreEqual(2, result[0].Entities.Count); }
public void RecognizePiiEntitiesBatchConvenience() { 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)); var documents = new List <string> { "A developer with SSN 859-98-0987 whose phone number is 800-102-1100 is building tools with our APIs.", "Your ABA number - 111000025 - is the first 9 digits in the lower left hand corner of your personal check." }; #region Snippet:TextAnalyticsSample5RecognizePiiEntitiesConvenience RecognizePiiEntitiesResultCollection results = client.RecognizePiiEntitiesBatch(documents); #endregion int i = 0; foreach (RecognizePiiEntitiesResult result in results) { Console.WriteLine($"For document: {documents[i++]}"); if (result.Entities.Count > 0) { Console.WriteLine($"Redacted Text: {result.Entities.RedactedText}"); Console.WriteLine($"The following {result.Entities.Count} PII entit{(result.Entities.Count > 1 ? "ies were" : "y was")} found:"); foreach (PiiEntity entity in result.Entities) { Console.WriteLine($" Text: {entity.Text}, Category: {entity.Category}, SubCategory: {entity.SubCategory}, Confidence score: {entity.ConfidenceScore}"); } } else { Console.WriteLine("No entities were found."); } } }
public async Task RecognizePiiEntitiesBatchWitCategoryTest() { TextAnalyticsClient client = GetClient(); RecognizePiiEntitiesResultCollection results = await client.RecognizePiiEntitiesBatchAsync(s_batchDocuments, new RecognizePiiEntitiesOptions() { CategoriesFilter = { PiiEntityCategory.PhoneNumber } }); var expectedOutput = new Dictionary <string, List <string> >() { { "1", new List <string>() { "800-102-1100" } }, { "2", new List <string>() { "111000025" } }, }; ValidateBatchDocumentsResult(results, expectedOutput); }
public async Task AnalyzeOperationWithMultipleActions() { TextAnalyticsClient client = GetClient(); var batchDocuments = new List <TextDocumentInput> { new TextDocumentInput("1", "Microsoft was founded by Bill Gates and Paul Allen.") { Language = "en", }, new TextDocumentInput("2", "Mi perro y mi gato tienen que ir al veterinario.") { Language = "es", } }; TextAnalyticsActions batchActions = new TextAnalyticsActions() { ExtractKeyPhrasesOptions = new List <ExtractKeyPhrasesOptions>() { new ExtractKeyPhrasesOptions() }, RecognizeEntitiesOptions = new List <RecognizeEntitiesOptions>() { new RecognizeEntitiesOptions() }, RecognizePiiEntitiesOptions = new List <RecognizePiiEntitiesOptions>() { new RecognizePiiEntitiesOptions() }, RecognizeLinkedEntitiesOptions = new List <RecognizeLinkedEntitiesOptions>() { new RecognizeLinkedEntitiesOptions() }, DisplayName = "AnalyzeOperationWithMultipleTasks" }; AnalyzeBatchActionsOperation operation = await client.StartAnalyzeBatchActionsAsync(batchDocuments, batchActions); Assert.AreEqual(0, operation.ActionsFailed); Assert.AreEqual(0, operation.ActionsSucceeded); Assert.AreEqual(0, operation.ActionsInProgress); Assert.AreEqual(0, operation.TotalActions); await operation.WaitForCompletionAsync(PollingInterval); Assert.AreEqual(0, operation.ActionsFailed); Assert.AreEqual(4, operation.ActionsSucceeded); Assert.AreEqual(0, operation.ActionsInProgress); Assert.AreEqual(4, operation.TotalActions); Assert.AreNotEqual(new DateTimeOffset(), operation.CreatedOn); Assert.AreNotEqual(new DateTimeOffset(), operation.LastModified); Assert.AreNotEqual(new DateTimeOffset(), operation.ExpiresOn); //Take the first page AnalyzeBatchActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault(); RecognizeEntitiesResultCollection entitiesResult = resultCollection.RecognizeEntitiesActionsResults.ElementAt(0).Result; ExtractKeyPhrasesResultCollection keyPhrasesResult = resultCollection.ExtractKeyPhrasesActionsResults.ElementAt(0).Result; RecognizePiiEntitiesResultCollection piiResult = resultCollection.RecognizePiiEntitiesActionsResults.ElementAt(0).Result; RecognizeLinkedEntitiesResultCollection entityLinkingResult = resultCollection.RecognizeLinkedEntitiesActionsResults.ElementAt(0).Result; Assert.IsNotNull(keyPhrasesResult); Assert.IsNotNull(entitiesResult); Assert.IsNotNull(piiResult); Assert.IsNotNull(entityLinkingResult); Assert.AreEqual("AnalyzeOperationWithMultipleTasks", operation.DisplayName); // Keyphrases Assert.AreEqual(2, keyPhrasesResult.Count); var keyPhrasesListId1 = new List <string> { "Bill Gates", "Paul Allen", "Microsoft" }; var keyPhrasesListId2 = new List <string> { "gato", "perro", "veterinario" }; foreach (string keyphrase in keyPhrasesResult[0].KeyPhrases) { Assert.IsTrue(keyPhrasesListId1.Contains(keyphrase)); } foreach (string keyphrase in keyPhrasesResult[1].KeyPhrases) { Assert.IsTrue(keyPhrasesListId2.Contains(keyphrase)); } // Entities Assert.AreEqual(2, entitiesResult.Count); Assert.AreEqual(3, entitiesResult[0].Entities.Count); var entitiesList = new List <string> { "Bill Gates", "Microsoft", "Paul Allen" }; foreach (CategorizedEntity entity in entitiesResult[0].Entities) { Assert.IsTrue(entitiesList.Contains(entity.Text)); Assert.IsNotNull(entity.Category); Assert.IsNotNull(entity.Offset); Assert.IsNotNull(entity.ConfidenceScore); } // PII Assert.AreEqual(2, entitiesResult.Count); Assert.AreEqual(3, entitiesResult[0].Entities.Count); Assert.IsNotNull(entitiesResult[0].Id); Assert.IsNotNull(entitiesResult[0].Entities); Assert.IsNotNull(entitiesResult[0].Error); // Entity Linking Assert.AreEqual(2, entityLinkingResult.Count); Assert.AreEqual(3, entityLinkingResult[0].Entities.Count); Assert.IsNotNull(entityLinkingResult[0].Id); Assert.IsNotNull(entityLinkingResult[0].Entities); Assert.IsNotNull(entityLinkingResult[0].Error); foreach (LinkedEntity entity in entityLinkingResult[0].Entities) { if (entity.Name == "Bill Gates") { Assert.AreEqual("Bill Gates", entity.DataSourceEntityId); Assert.AreEqual("Wikipedia", entity.DataSource); } if (entity.Name == "Microsoft") { Assert.AreEqual("Microsoft", entity.DataSourceEntityId); Assert.AreEqual("Wikipedia", entity.DataSource); } } }
public void RecognizePiiEntitiesBatch() { string endpoint = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_ENDPOINT"); string apiKey = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_API_KEY"); // Instantiate a client that will be used to call the service. var client = new TextAnalyticsClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); #region Snippet:TextAnalyticsSample5RecognizePiiEntitiesBatch var documents = new List <TextDocumentInput> { new TextDocumentInput("1", "A developer with SSN 555-55-5555 whose phone number is 555-555-5555 is building tools with our APIs.") { Language = "en", }, new TextDocumentInput("2", "Your ABA number - 111000025 - is the first 9 digits in the lower left hand corner of your personal check.") { Language = "en", } }; RecognizePiiEntitiesResultCollection results = client.RecognizePiiEntitiesBatch(documents, new TextAnalyticsRequestOptions { IncludeStatistics = true }); #endregion int i = 0; Debug.WriteLine($"Results of Azure Text Analytics \"Pii Entity Recognition\" Model, version: \"{results.ModelVersion}\""); Debug.WriteLine(""); foreach (RecognizePiiEntitiesResult result in results) { TextDocumentInput document = documents[i++]; Debug.WriteLine($"On document (Id={document.Id}, Language=\"{document.Language}\", Text=\"{document.Text}\"):"); if (result.HasError) { Debug.WriteLine($" Document error code: {result.Error.Code}."); Debug.WriteLine($" Message: {result.Error.Message}."); } else { Debug.WriteLine($" Recognized the following {result.Entities.Count()} PII entit{(result.Entities.Count() > 1 ? "ies" : "y ")}:"); foreach (PiiEntity entity in result.Entities) { Debug.WriteLine($" Text: {entity.Text}, Category: {entity.Category}, SubCategory: {entity.SubCategory}, Confidence score: {entity.ConfidenceScore}"); } Debug.WriteLine($" Document statistics:"); Debug.WriteLine($" Character count (in Unicode graphemes): {result.Statistics.GraphemeCount}"); Debug.WriteLine($" Transaction count: {result.Statistics.TransactionCount}"); Debug.WriteLine(""); } } Debug.WriteLine($"Batch operation statistics:"); Debug.WriteLine($" Document count: {results.Statistics.DocumentCount}"); Debug.WriteLine($" Valid document count: {results.Statistics.ValidDocumentCount}"); Debug.WriteLine($" Invalid document count: {results.Statistics.InvalidDocumentCount}"); Debug.WriteLine($" Transaction count: {results.Statistics.TransactionCount}"); Debug.WriteLine(""); }
public async Task AnalyzeOperationAsync_AutomaticPollingWithSetInterval() { string endpoint = TestEnvironment.Endpoint; string apiKey = TestEnvironment.ApiKey; var client = new TextAnalyticsClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); #region Snippet:AnalyzeOperationBatchAsyncAutomaticPolling string document = @"We went to Contoso Steakhouse located at midtown NYC last week for a dinner party, and we adore the spot! They provide marvelous food and they have a great menu. The chief cook happens to be the owner (I think his name is John Doe) and he is super nice, coming out of the kitchen and greeted us all. We enjoyed very much dining in the place! The Sirloin steak I ordered was tender and juicy, and the place was impeccably clean. You can even pre-order from their online menu at www.contososteakhouse.com, call 312-555-0176 or send email to [email protected]! The only complaint I have is the food didn't come fast enough. Overall I highly recommend it!"; var batchDocuments = new List <TextDocumentInput> { new TextDocumentInput("1", document) { Language = "en", } }; AnalyzeOperationOptions operationOptions = new AnalyzeOperationOptions() { KeyPhrasesTaskParameters = new KeyPhrasesTaskParameters(), EntitiesTaskParameters = new EntitiesTaskParameters(), PiiTaskParameters = new PiiTaskParameters(), DisplayName = "AnalyzeOperationSample" }; AnalyzeOperation operation = await client.StartAnalyzeOperationBatchAsync(batchDocuments, operationOptions); TimeSpan pollingInterval = new TimeSpan(1000); await operation.WaitForCompletionAsync(pollingInterval); await foreach (AnalyzeOperationResult documentsInPage in operation.Value) { RecognizeEntitiesResultCollection entitiesResult = documentsInPage.Tasks.EntityRecognitionTasks[0].Results; ExtractKeyPhrasesResultCollection keyPhrasesResult = documentsInPage.Tasks.KeyPhraseExtractionTasks[0].Results; RecognizePiiEntitiesResultCollection piiResult = documentsInPage.Tasks.EntityRecognitionPiiTasks[0].Results; Console.WriteLine("Recognized Entities"); foreach (RecognizeEntitiesResult result in entitiesResult) { Console.WriteLine($" Recognized the following {result.Entities.Count} entities:"); foreach (CategorizedEntity entity in result.Entities) { Console.WriteLine($" Entity: {entity.Text}"); Console.WriteLine($" Category: {entity.Category}"); Console.WriteLine($" Offset: {entity.Offset}"); Console.WriteLine($" ConfidenceScore: {entity.ConfidenceScore}"); Console.WriteLine($" SubCategory: {entity.SubCategory}"); } Console.WriteLine(""); } Console.WriteLine("Recognized PII Entities"); foreach (RecognizePiiEntitiesResult result in piiResult) { Console.WriteLine($" Recognized the following {result.Entities.Count} PII entities:"); foreach (PiiEntity entity in result.Entities) { Console.WriteLine($" Entity: {entity.Text}"); Console.WriteLine($" Category: {entity.Category}"); Console.WriteLine($" Offset: {entity.Offset}"); Console.WriteLine($" ConfidenceScore: {entity.ConfidenceScore}"); Console.WriteLine($" SubCategory: {entity.SubCategory}"); } Console.WriteLine(""); } Console.WriteLine("Key Phrases"); foreach (ExtractKeyPhrasesResult result in keyPhrasesResult) { Console.WriteLine($" Recognized the following {result.KeyPhrases.Count} Keyphrases:"); foreach (string keyphrase in result.KeyPhrases) { Console.WriteLine($" {keyphrase}"); } Console.WriteLine(""); } } }
public void RecognizePiiEntitiesBatchConvenience() { 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), CreateSampleOptions()); #region Snippet:TextAnalyticsSample5RecognizePiiEntitiesConvenience string documentA = @"Parker Doe has repaid all of their loans as of 2020-04-25. Their SSN is 859-98-0987. To contact them, use their phone number 800-102-1100. They are originally from Brazil and have document ID number 998.214.865-68"; string documentB = @"Yesterday, Dan Doe was asking where they could find the ABA number. I explained that it is the first 9 digits in the lower left hand corner of their personal check. After looking at their account they confirmed the number was 111000025"; string documentC = string.Empty; var documents = new List <string> { documentA, documentB, documentC }; Response <RecognizePiiEntitiesResultCollection> response = client.RecognizePiiEntitiesBatch(documents); RecognizePiiEntitiesResultCollection entititesPerDocuments = response.Value; int i = 0; Console.WriteLine($"Results of Azure Text Analytics \"PII Entity Recognition\" Model, version: \"{entititesPerDocuments.ModelVersion}\""); Console.WriteLine(""); foreach (RecognizePiiEntitiesResult piiEntititesInDocument in entititesPerDocuments) { Console.WriteLine($"On document with Text: \"{documents[i++]}\""); Console.WriteLine(""); if (piiEntititesInDocument.HasError) { Console.WriteLine(" Error!"); Console.WriteLine($" Document error code: {piiEntititesInDocument.Error.ErrorCode}."); Console.WriteLine($" Message: {piiEntititesInDocument.Error.Message}"); } else { Console.WriteLine($" Redacted Text: {piiEntititesInDocument.Entities.RedactedText}"); Console.WriteLine(""); Console.WriteLine($" Recognized {piiEntititesInDocument.Entities.Count} PII entities:"); foreach (PiiEntity piiEntity in piiEntititesInDocument.Entities) { Console.WriteLine($" Text: {piiEntity.Text}"); Console.WriteLine($" Category: {piiEntity.Category}"); if (!string.IsNullOrEmpty(piiEntity.SubCategory)) { Console.WriteLine($" SubCategory: {piiEntity.SubCategory}"); } Console.WriteLine($" Confidence score: {piiEntity.ConfidenceScore}"); Console.WriteLine(""); } } Console.WriteLine(""); } #endregion }
public void RecognizePiiEntitiesBatch() { string endpoint = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_ENDPOINT"); string subscriptionKey = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_SUBSCRIPTION_KEY"); // Instantiate a client that will be used to call the service. var client = new TextAnalyticsClient(new Uri(endpoint), new TextAnalyticsSubscriptionKeyCredential(subscriptionKey)); #region Snippet:TextAnalyticsSample5RecognizePiiEntitiesBatch var inputs = new List <TextDocumentInput> { new TextDocumentInput("1", "A developer with SSN 555-55-5555 whose phone number is 555-555-5555 is building tools with our APIs.") { Language = "en", }, new TextDocumentInput("2", "Your ABA number - 111000025 - is the first 9 digits in the lower left hand corner of your personal check.") { Language = "en", } }; RecognizePiiEntitiesResultCollection results = client.RecognizePiiEntities(inputs, new TextAnalyticsRequestOptions { IncludeStatistics = true }); #endregion int i = 0; Debug.WriteLine($"Results of Azure Text Analytics \"Pii Entity Recognition\" Model, version: \"{results.ModelVersion}\""); Debug.WriteLine(""); foreach (RecognizePiiEntitiesResult result in results) { TextDocumentInput document = inputs[i++]; Debug.WriteLine($"On document (Id={document.Id}, Language=\"{document.Language}\", Text=\"{document.Text}\"):"); if (result.ErrorMessage != default) { Debug.WriteLine($"On document (Id={document.Id}, Language=\"{document.Language}\", Text=\"{document.Text}\"):"); } else { Debug.WriteLine($" Recognized the following {result.NamedEntities.Count()} PII entit{(result.NamedEntities.Count() > 1 ? "ies" : "y ")}:"); foreach (NamedEntity entity in result.NamedEntities) { Debug.WriteLine($" Text: {entity.Text}, Type: {entity.Type}, SubType: {entity.SubType ?? "N/A"}, Score: {entity.Score:0.00}, Offset: {entity.Offset}, Length: {entity.Length}"); } Debug.WriteLine($" Document statistics:"); Debug.WriteLine($" Character count: {result.Statistics.CharacterCount}"); Debug.WriteLine($" Transaction count: {result.Statistics.TransactionCount}"); Debug.WriteLine(""); } } Debug.WriteLine($"Batch operation statistics:"); Debug.WriteLine($" Document count: {results.Statistics.DocumentCount}"); Debug.WriteLine($" Valid document count: {results.Statistics.ValidDocumentCount}"); Debug.WriteLine($" Invalid document count: {results.Statistics.InvalidDocumentCount}"); Debug.WriteLine($" Transaction count: {results.Statistics.TransactionCount}"); Debug.WriteLine(""); }
public async Task RecognizePiiEntitiesBatchAsync() { 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)); var documents = new List <TextDocumentInput> { new TextDocumentInput("1", "A developer with SSN 859-98-0987 whose phone number is 800-102-1100 is building tools with our APIs.") { Language = "en", }, new TextDocumentInput("2", "Your ABA number - 111000025 - is the first 9 digits in the lower left hand corner of your personal check.") { Language = "en", } }; RecognizePiiEntitiesResultCollection results = await client.RecognizePiiEntitiesBatchAsync(documents, new RecognizePiiEntitiesOptions { IncludeStatistics = true }); int i = 0; Console.WriteLine($"Results of Azure Text Analytics \"Pii Entity Recognition\" Model, version: \"{results.ModelVersion}\""); Console.WriteLine(""); foreach (RecognizePiiEntitiesResult result in results) { TextDocumentInput document = documents[i++]; Console.WriteLine($"On document (Id={document.Id}, Language=\"{document.Language}\", Text=\"{document.Text}\"):"); if (result.HasError) { Console.WriteLine($" Document error code: {result.Error.ErrorCode}."); Console.WriteLine($" Message: {result.Error.Message}."); } else { if (result.Entities.Count > 0) { Console.WriteLine($" Redacted Text: {result.Entities.RedactedText}"); Console.WriteLine($" Recognized the following {result.Entities.Count} PII entit{(result.Entities.Count > 1 ? "ies" : "y ")}:"); foreach (PiiEntity entity in result.Entities) { Console.WriteLine($" Text: {entity.Text}, Category: {entity.Category}, SubCategory: {entity.SubCategory}, Confidence score: {entity.ConfidenceScore}"); } } else { Console.WriteLine("No entities were found."); } Console.WriteLine($" Document statistics:"); Console.WriteLine($" Character count (in Unicode graphemes): {result.Statistics.CharacterCount}"); Console.WriteLine($" Transaction count: {result.Statistics.TransactionCount}"); Console.WriteLine(""); } } Console.WriteLine($"Batch operation statistics:"); Console.WriteLine($" Document count: {results.Statistics.DocumentCount}"); Console.WriteLine($" Valid document count: {results.Statistics.ValidDocumentCount}"); Console.WriteLine($" Invalid document count: {results.Statistics.InvalidDocumentCount}"); Console.WriteLine($" Transaction count: {results.Statistics.TransactionCount}"); Console.WriteLine(""); }
public void AnalyzeOperationBatchConvenience() { string endpoint = TestEnvironment.Endpoint; string apiKey = TestEnvironment.ApiKey; var client = new TextAnalyticsClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); #region Snippet:AnalyzeOperationBatchConvenience string documentA = @"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."; string documentB = @"Last week we stayed at Hotel Foo to celebrate our anniversary. The staff knew about our anniversary so they helped me organize a little surprise for my partner. The room was clean and with the decoration I requested. It was perfect!"; string documentC = @"That was the best day of my life! We went on a 4 day trip where we stayed at Hotel Foo. They had great amenities that included an indoor pool, a spa, and a bar. The spa offered couples massages which were really good. The spa was clean and felt very peaceful. Overall the whole experience was great. We will definitely come back."; var batchDocuments = new List <string> { documentA, documentB, documentC }; TextAnalyticsActions actions = new TextAnalyticsActions() { ExtractKeyPhrasesOptions = new List <ExtractKeyPhrasesOptions>() { new ExtractKeyPhrasesOptions() }, RecognizeEntitiesOptions = new List <RecognizeEntitiesOptions>() { new RecognizeEntitiesOptions() }, RecognizePiiEntitiesOptions = new List <RecognizePiiEntitiesOptions>() { new RecognizePiiEntitiesOptions() }, RecognizeLinkedEntitiesOptions = new List <RecognizeLinkedEntitiesOptions>() { new RecognizeLinkedEntitiesOptions() }, DisplayName = "AnalyzeOperationSample" }; AnalyzeBatchActionsOperation operation = client.StartAnalyzeBatchActions(batchDocuments, actions); TimeSpan pollingInterval = new TimeSpan(1000); while (!operation.HasCompleted) { Thread.Sleep(pollingInterval); operation.UpdateStatus(); Console.WriteLine($"Status: {operation.Status}"); //If operation has not started, all other fields are null if (operation.Status != TextAnalyticsOperationStatus.NotStarted) { Console.WriteLine($"Expires On: {operation.ExpiresOn}"); Console.WriteLine($"Last modified: {operation.LastModified}"); if (!string.IsNullOrEmpty(operation.DisplayName)) { Console.WriteLine($"Display name: {operation.DisplayName}"); } Console.WriteLine($"Total actions: {operation.TotalActions}"); Console.WriteLine($" Succeeded actions: {operation.ActionsSucceeded}"); Console.WriteLine($" Failed actions: {operation.ActionsFailed}"); Console.WriteLine($" In progress actions: {operation.ActionsInProgress}"); } } foreach (AnalyzeBatchActionsResult documentsInPage in operation.GetValues()) { RecognizeEntitiesResultCollection entitiesResult = documentsInPage.RecognizeEntitiesActionsResults.FirstOrDefault().Result; ExtractKeyPhrasesResultCollection keyPhrasesResult = documentsInPage.ExtractKeyPhrasesActionsResults.FirstOrDefault().Result; RecognizePiiEntitiesResultCollection piiResult = documentsInPage.RecognizePiiEntitiesActionsResults.FirstOrDefault().Result; RecognizeLinkedEntitiesResultCollection linkedEntitiesResult = documentsInPage.RecognizeLinkedEntitiesActionsResults.FirstOrDefault().Result; Console.WriteLine("Recognized Entities"); foreach (RecognizeEntitiesResult result in entitiesResult) { Console.WriteLine($" Recognized the following {result.Entities.Count} entities:"); foreach (CategorizedEntity entity in result.Entities) { Console.WriteLine($" Entity: {entity.Text}"); Console.WriteLine($" Category: {entity.Category}"); Console.WriteLine($" Offset: {entity.Offset}"); Console.WriteLine($" Length: {entity.Length}"); Console.WriteLine($" ConfidenceScore: {entity.ConfidenceScore}"); Console.WriteLine($" SubCategory: {entity.SubCategory}"); } Console.WriteLine(""); } Console.WriteLine("Recognized PII Entities"); foreach (RecognizePiiEntitiesResult result in piiResult) { Console.WriteLine($" Recognized the following {result.Entities.Count} PII entities:"); foreach (PiiEntity entity in result.Entities) { Console.WriteLine($" Entity: {entity.Text}"); Console.WriteLine($" Category: {entity.Category}"); Console.WriteLine($" Offset: {entity.Offset}"); Console.WriteLine($" Length: {entity.Length}"); Console.WriteLine($" ConfidenceScore: {entity.ConfidenceScore}"); Console.WriteLine($" SubCategory: {entity.SubCategory}"); } Console.WriteLine(""); } Console.WriteLine("Key Phrases"); foreach (ExtractKeyPhrasesResult result in keyPhrasesResult) { Console.WriteLine($" Recognized the following {result.KeyPhrases.Count} Keyphrases:"); foreach (string keyphrase in result.KeyPhrases) { Console.WriteLine($" {keyphrase}"); } Console.WriteLine(""); } Console.WriteLine("Recognized Linked Entities"); foreach (RecognizeLinkedEntitiesResult result in linkedEntitiesResult) { Console.WriteLine($" Recognized the following {result.Entities.Count} linked entities:"); foreach (LinkedEntity entity in result.Entities) { Console.WriteLine($" Entity: {entity.Name}"); Console.WriteLine($" DataSource: {entity.DataSource}"); Console.WriteLine($" DataSource EntityId: {entity.DataSourceEntityId}"); Console.WriteLine($" Language: {entity.Language}"); Console.WriteLine($" DataSource Url: {entity.Url}"); Console.WriteLine($" Total Matches: {entity.Matches.Count()}"); foreach (LinkedEntityMatch match in entity.Matches) { Console.WriteLine($" Match Text: {match.Text}"); Console.WriteLine($" ConfidenceScore: {match.ConfidenceScore}"); Console.WriteLine($" Offset: {match.Offset}"); Console.WriteLine($" Length: {match.Length}"); } Console.WriteLine(""); } Console.WriteLine(""); } } }
public async Task AnalyzeOperationWithMultipleActions() { TextAnalyticsClient client = GetClient(); var batchDocuments = new List <TextDocumentInput> { new TextDocumentInput("1", "Microsoft was founded by Bill Gates and Paul Allen.") { Language = "en", }, new TextDocumentInput("2", "Mi perro y mi gato tienen que ir al veterinario.") { Language = "es", } }; TextAnalyticsActions batchActions = new TextAnalyticsActions() { ExtractKeyPhrasesActions = new List <ExtractKeyPhrasesAction>() { new ExtractKeyPhrasesAction() }, RecognizeEntitiesActions = new List <RecognizeEntitiesAction>() { new RecognizeEntitiesAction() }, RecognizePiiEntitiesActions = new List <RecognizePiiEntitiesAction>() { new RecognizePiiEntitiesAction() }, RecognizeLinkedEntitiesActions = new List <RecognizeLinkedEntitiesAction>() { new RecognizeLinkedEntitiesAction() }, AnalyzeSentimentActions = new List <AnalyzeSentimentAction>() { new AnalyzeSentimentAction() }, DisplayName = "AnalyzeOperationWithMultipleTasks" }; AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(batchDocuments, batchActions); Assert.AreEqual(0, operation.ActionsFailed); Assert.AreEqual(0, operation.ActionsSucceeded); Assert.AreEqual(0, operation.ActionsInProgress); Assert.AreEqual(0, operation.ActionsTotal); await operation.WaitForCompletionAsync(); Assert.AreEqual(0, operation.ActionsFailed); Assert.AreEqual(5, operation.ActionsSucceeded); Assert.AreEqual(0, operation.ActionsInProgress); Assert.AreEqual(5, operation.ActionsTotal); Assert.AreNotEqual(new DateTimeOffset(), operation.CreatedOn); Assert.AreNotEqual(new DateTimeOffset(), operation.LastModified); Assert.AreNotEqual(new DateTimeOffset(), operation.ExpiresOn); //Take the first page AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault(); IReadOnlyCollection <RecognizeEntitiesActionResult> entitiesActionsResults = resultCollection.RecognizeEntitiesResults; IReadOnlyCollection <ExtractKeyPhrasesActionResult> keyPhrasesActionsResults = resultCollection.ExtractKeyPhrasesResults; IReadOnlyCollection <RecognizePiiEntitiesActionResult> piiActionsResults = resultCollection.RecognizePiiEntitiesResults; IReadOnlyCollection <RecognizeLinkedEntitiesActionResult> entityLinkingActionsResults = resultCollection.RecognizeLinkedEntitiesResults; IReadOnlyCollection <AnalyzeSentimentActionResult> analyzeSentimentActionsResults = resultCollection.AnalyzeSentimentResults; Assert.IsNotNull(keyPhrasesActionsResults); Assert.IsNotNull(entitiesActionsResults); Assert.IsNotNull(piiActionsResults); Assert.IsNotNull(entityLinkingActionsResults); Assert.IsNotNull(analyzeSentimentActionsResults); Assert.AreEqual("AnalyzeOperationWithMultipleTasks", operation.DisplayName); // Keyphrases ExtractKeyPhrasesResultCollection keyPhrasesDocumentsResults = keyPhrasesActionsResults.FirstOrDefault().DocumentsResults; Assert.AreEqual(2, keyPhrasesDocumentsResults.Count); var keyPhrasesListId1 = new List <string> { "Bill Gates", "Paul Allen", "Microsoft" }; var keyPhrasesListId2 = new List <string> { "Mi", "gato", "perro", "veterinario" }; foreach (string keyphrase in keyPhrasesDocumentsResults[0].KeyPhrases) { Assert.IsTrue(keyPhrasesListId1.Contains(keyphrase)); } foreach (string keyphrase in keyPhrasesDocumentsResults[1].KeyPhrases) { Assert.IsTrue(keyPhrasesListId2.Contains(keyphrase)); } // Entities RecognizeEntitiesResultCollection entitiesDocumentsResults = entitiesActionsResults.FirstOrDefault().DocumentsResults; Assert.AreEqual(2, entitiesDocumentsResults.Count); Assert.AreEqual(3, entitiesDocumentsResults[0].Entities.Count); var entitiesList = new List <string> { "Bill Gates", "Microsoft", "Paul Allen" }; foreach (CategorizedEntity entity in entitiesDocumentsResults[0].Entities) { Assert.IsTrue(entitiesList.Contains(entity.Text)); Assert.IsNotNull(entity.Category); Assert.IsNotNull(entity.Offset); Assert.IsNotNull(entity.ConfidenceScore); } // PII RecognizePiiEntitiesResultCollection piiDocumentsResults = piiActionsResults.FirstOrDefault().DocumentsResults; Assert.AreEqual(2, piiDocumentsResults.Count); Assert.AreEqual(3, piiDocumentsResults[0].Entities.Count); Assert.IsNotNull(piiDocumentsResults[0].Id); Assert.IsNotNull(piiDocumentsResults[0].Entities); Assert.IsNotNull(piiDocumentsResults[0].Error); // Entity Linking RecognizeLinkedEntitiesResultCollection entityLinkingDocumentsResults = entityLinkingActionsResults.FirstOrDefault().DocumentsResults; // Disable because of bug https://github.com/Azure/azure-sdk-for-net/issues/22648 //Assert.AreEqual(2, entityLinkingDocumentsResults.Count); Assert.AreEqual(3, entityLinkingDocumentsResults[0].Entities.Count); Assert.IsNotNull(entityLinkingDocumentsResults[0].Id); Assert.IsNotNull(entityLinkingDocumentsResults[0].Entities); Assert.IsNotNull(entityLinkingDocumentsResults[0].Error); foreach (LinkedEntity entity in entityLinkingDocumentsResults[0].Entities) { if (entity.Name == "Bill Gates") { Assert.AreEqual("Bill Gates", entity.DataSourceEntityId); Assert.AreEqual("Wikipedia", entity.DataSource); } if (entity.Name == "Microsoft") { Assert.AreEqual("Microsoft", entity.DataSourceEntityId); Assert.AreEqual("Wikipedia", entity.DataSource); } } // Analyze sentiment AnalyzeSentimentResultCollection analyzeSentimentDocumentsResults = analyzeSentimentActionsResults.FirstOrDefault().DocumentsResults; Assert.AreEqual(2, analyzeSentimentDocumentsResults.Count); Assert.AreEqual(TextSentiment.Neutral, analyzeSentimentDocumentsResults[0].DocumentSentiment.Sentiment); Assert.AreEqual(TextSentiment.Neutral, analyzeSentimentDocumentsResults[1].DocumentSentiment.Sentiment); }
public async Task RecognizePiiEntitiesBatchAsync() { 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), CreateSampleOptions()); string documentA = @"Parker Doe has repaid all of their loans as of 2020-04-25. Their SSN is 859-98-0987. To contact them, use their phone number 800-102-1100. They are originally from Brazil and have document ID number 998.214.865-68"; string documentB = @"Hoy recibí una llamada al medio día del usuario Juanito Perez, quien preguntaba cómo acceder a su nuevo correo electrónico. Este trabaja en Microsoft y su correo es [email protected]. El usuario accedió a compartir su número para futuras comunicaciones. El número es 800-102-1101"; string documentC = @"Yesterday, Dan Doe was asking where they could find the ABA number. I explained that it is the first 9 digits in the lower left hand corner of their personal check. After looking at their account they confirmed the number was 111000025"; var documents = new List <TextDocumentInput> { new TextDocumentInput("1", documentA) { Language = "en", }, new TextDocumentInput("2", documentB) { Language = "es", }, new TextDocumentInput("3", documentC) { Language = "en", } }; var options = new RecognizePiiEntitiesOptions { IncludeStatistics = true }; Response <RecognizePiiEntitiesResultCollection> response = await client.RecognizePiiEntitiesBatchAsync(documents, options); RecognizePiiEntitiesResultCollection entititesPerDocuments = response.Value; int i = 0; Console.WriteLine($"Results of \"PII Entity Recognition\" Model, version: \"{entititesPerDocuments.ModelVersion}\""); Console.WriteLine(""); foreach (RecognizePiiEntitiesResult piiEntititesInDocument in entititesPerDocuments) { TextDocumentInput document = documents[i++]; Console.WriteLine($"On document (Id={document.Id}, Language=\"{document.Language}\"):"); if (piiEntititesInDocument.HasError) { Console.WriteLine(" Error!"); Console.WriteLine($" Document error code: {piiEntititesInDocument.Error.ErrorCode}."); Console.WriteLine($" Message: {piiEntititesInDocument.Error.Message}"); } else { Console.WriteLine($" Redacted Text: {piiEntititesInDocument.Entities.RedactedText}"); Console.WriteLine(""); Console.WriteLine($" Recognized {piiEntititesInDocument.Entities.Count} PII entities:"); foreach (PiiEntity piiEntity in piiEntititesInDocument.Entities) { Console.WriteLine($" Text: {piiEntity.Text}"); Console.WriteLine($" Category: {piiEntity.Category}"); if (!string.IsNullOrEmpty(piiEntity.SubCategory)) { Console.WriteLine($" SubCategory: {piiEntity.SubCategory}"); } Console.WriteLine($" Confidence score: {piiEntity.ConfidenceScore}"); Console.WriteLine(""); } Console.WriteLine($" Document statistics:"); Console.WriteLine($" Character count: {piiEntititesInDocument.Statistics.CharacterCount}"); Console.WriteLine($" Transaction count: {piiEntititesInDocument.Statistics.TransactionCount}"); } Console.WriteLine(""); } Console.WriteLine($"Batch operation statistics:"); Console.WriteLine($" Document count: {entititesPerDocuments.Statistics.DocumentCount}"); Console.WriteLine($" Valid document count: {entititesPerDocuments.Statistics.ValidDocumentCount}"); Console.WriteLine($" Invalid document count: {entititesPerDocuments.Statistics.InvalidDocumentCount}"); Console.WriteLine($" Transaction count: {entititesPerDocuments.Statistics.TransactionCount}"); Console.WriteLine(""); }
public async Task AnalyzeOperationWithMultipleTasks() { TextAnalyticsClient client = GetClient(); var batchDocuments = new List <TextDocumentInput> { new TextDocumentInput("1", "Microsoft was founded by Bill Gates and Paul Allen.") { Language = "en", }, new TextDocumentInput("2", "Mi perro y mi gato tienen que ir al veterinario.") { Language = "es", } }; AnalyzeOperationOptions operationOptions = new AnalyzeOperationOptions() { KeyPhrasesTaskParameters = new KeyPhrasesTaskParameters(), EntitiesTaskParameters = new EntitiesTaskParameters(), PiiTaskParameters = new PiiTaskParameters(), DisplayName = "AnalyzeOperationWithMultipleTasks" }; AnalyzeOperation operation = await client.StartAnalyzeOperationBatchAsync(batchDocuments, operationOptions); await operation.WaitForCompletionAsync(); AnalyzeOperationResult resultCollection = operation.Value; RecognizeEntitiesResultCollection entitiesResult = resultCollection.Tasks.EntityRecognitionTasks[0].Results; ExtractKeyPhrasesResultCollection keyPhrasesResult = resultCollection.Tasks.KeyPhraseExtractionTasks[0].Results; RecognizePiiEntitiesResultCollection piiResult = resultCollection.Tasks.EntityRecognitionPiiTasks[0].Results; Assert.IsNotNull(keyPhrasesResult); Assert.IsNotNull(entitiesResult); Assert.IsNotNull(piiResult); Assert.AreEqual("AnalyzeOperationWithMultipleTasks", resultCollection.DisplayName); // Keyphrases Assert.AreEqual(2, keyPhrasesResult.Count); var keyPhrasesListId1 = new List <string> { "Bill Gates", "Paul Allen", "Microsoft" }; var keyPhrasesListId2 = new List <string> { "gato", "perro", "veterinario" }; foreach (string keyphrase in keyPhrasesResult[0].KeyPhrases) { Assert.IsTrue(keyPhrasesListId1.Contains(keyphrase)); } foreach (string keyphrase in keyPhrasesResult[1].KeyPhrases) { Assert.IsTrue(keyPhrasesListId2.Contains(keyphrase)); } // Entities Assert.AreEqual(2, entitiesResult.Count); Assert.AreEqual(3, entitiesResult[0].Entities.Count); var entitiesList = new List <string> { "Bill Gates", "Microsoft", "Paul Allen" }; foreach (CategorizedEntity entity in entitiesResult[0].Entities) { Assert.IsTrue(entitiesList.Contains(entity.Text)); Assert.IsNotNull(entity.Category); Assert.IsNotNull(entity.Offset); Assert.IsNotNull(entity.ConfidenceScore); } // PII Assert.AreEqual(2, entitiesResult.Count); Assert.AreEqual(3, entitiesResult[0].Entities.Count); Assert.IsNotNull(entitiesResult[0].Id); Assert.IsNotNull(entitiesResult[0].Entities); Assert.IsNotNull(entitiesResult[0].Error); }