/// <summary> /// Submits text analytics requests depending on the settings (sentimentAnalysisSetting). Every utterance will be submitted independently to text analytics. /// (This means for instance that every utterance will have a separate sentiment score). /// </summary> /// <param name="speechTranscript">The speech transcript object.</param> /// <param name="sentimentAnalysisSetting">The sentiment analysis setting.</param> /// <returns>The job ids and errors, if any were found.</returns> public async Task <(IEnumerable <string> jobIds, IEnumerable <string> errors)> SubmitUtteranceLevelRequests( SpeechTranscript speechTranscript, SentimentAnalysisSetting sentimentAnalysisSetting) { speechTranscript = speechTranscript ?? throw new ArgumentNullException(nameof(speechTranscript)); if (sentimentAnalysisSetting != SentimentAnalysisSetting.UtteranceLevel) { return(new List <string>(), new List <string>()); } var documents = speechTranscript.RecognizedPhrases.Where(r => r.NBest.FirstOrDefault() != null && !string.IsNullOrEmpty(r.NBest.First().Display)).Select(r => new TextDocumentInput($"{r.Channel}_{r.Offset}", r.NBest.First().Display) { Language = Locale }); var actions = new TextAnalyticsActions { DisplayName = "IngestionClient", AnalyzeSentimentActions = new List <AnalyzeSentimentAction>() { new AnalyzeSentimentAction() } }; return(await SubmitDocumentsAsync(documents, actions).ConfigureAwait(false)); }
public async Task SingleCategoryClassifyWithDisableServiceLogs() { TextAnalyticsClient client = GetClient(); TextAnalyticsActions batchActions = new TextAnalyticsActions() { SingleCategoryClassifyActions = new List <SingleCategoryClassifyAction>() { new SingleCategoryClassifyAction(TestEnvironment.SingleClassificationProjectName, TestEnvironment.SingleClassificationDeploymentName) { DisableServiceLogs = true } } }; AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(s_singleCategoryClassifyBatchConvenienceDocuments, batchActions); await PollUntilTimeout(operation); Assert.IsTrue(operation.HasCompleted); // Take the first page AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault(); IReadOnlyCollection <SingleCategoryClassifyActionResult> singleCategoryClassifyActionsResults = resultCollection.SingleCategoryClassifyResults; Assert.IsNotNull(singleCategoryClassifyActionsResults); Assert.AreEqual(2, singleCategoryClassifyActionsResults.FirstOrDefault().DocumentsResults.Count); }
public void AnalyzeOperationWithErrorTest() { TextAnalyticsClient client = GetClient(); var documents = new List <string> { "Subject is taking 100mg of ibuprofen twice daily" }; TextAnalyticsActions batchActions = new TextAnalyticsActions() { ExtractKeyPhrasesActions = new List <ExtractKeyPhrasesAction>() { new ExtractKeyPhrasesAction(), new ExtractKeyPhrasesAction() { ModelVersion = "InvalidVersion" } }, DisplayName = "AnalyzeOperationBatchWithErrorTest", }; RequestFailedException ex = Assert.ThrowsAsync <RequestFailedException>(async() => await client.StartAnalyzeActionsAsync(documents, batchActions)); Assert.AreEqual(TextAnalyticsErrorCode.InvalidRequest, ex.ErrorCode); }
public void Inject(TextAnalyticsActions textAnalyticsActions) { textAnalyticsActions.RecognizeCustomEntitiesActions = new List <RecognizeCustomEntitiesAction>() { new RecognizeCustomEntitiesAction(_projectName, _deploymentName) }; }
public async Task ExtractSummaryBatchWithStatisticsTest() { TextAnalyticsClient client = GetClient(); TextAnalyticsActions batchActions = new TextAnalyticsActions() { ExtractSummaryActions = new List <ExtractSummaryAction>() { new ExtractSummaryAction() { MaxSentenceCount = ExtractSummaryMaxSentenceCount } } }; AnalyzeActionsOptions options = new AnalyzeActionsOptions() { IncludeStatistics = true }; AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(s_extractSummaryBatchDocuments, batchActions, options); await operation.WaitForCompletionAsync(); // Take the first page AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault(); IReadOnlyCollection <ExtractSummaryActionResult> summaryActionsResults = resultCollection.ExtractSummaryResults; ExtractSummaryResultCollection summaryDocumentsResults = summaryActionsResults.FirstOrDefault().DocumentsResults; ValidateSummaryBatchResult(summaryDocumentsResults, includeStatistics: true); }
public async Task MultiCategoryClassifyBatchTest() { TextAnalyticsClient client = GetClient(); TextAnalyticsActions batchActions = new TextAnalyticsActions() { MultiCategoryClassifyActions = new List <MultiCategoryClassifyAction>() { new MultiCategoryClassifyAction(TestEnvironment.MultiClassificationProjectName, TestEnvironment.MultiClassificationDeploymentName) } }; AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(s_multiCategoryClassifyBatchDocuments, batchActions); await PollUntilTimeout(operation); Assert.IsTrue(operation.HasCompleted); // Take the first page AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault(); IReadOnlyCollection <MultiCategoryClassifyActionResult> multiCategoryClassifyActionsResults = resultCollection.MultiCategoryClassifyResults; MultiCategoryClassifyResultCollection multiCategoryClassifyResults = multiCategoryClassifyActionsResults.FirstOrDefault().DocumentsResults; ValidateSummaryBatchResult(multiCategoryClassifyResults); }
public async Task AnalyzeOperationAnalyzeSentimentWithAnalyzeSentimentOptions() { var mockResponse = new MockResponse(202); mockResponse.AddHeader(new HttpHeader("Operation-Location", "something/jobs/2a96a91f-7edf-4931-a880-3fdee1d56f15")); var mockTransport = new MockTransport(new[] { mockResponse, mockResponse }); var client = CreateTestClient(mockTransport); var documents = new List <string> { "Elon Musk is the CEO of SpaceX and Tesla." }; var options = new AnalyzeSentimentOptions(); var actions = new AnalyzeSentimentAction(options); TextAnalyticsActions batchActions = new TextAnalyticsActions() { AnalyzeSentimentActions = new List <AnalyzeSentimentAction>() { actions }, }; await client.StartAnalyzeActionsAsync(documents, batchActions); var contentString = GetString(mockTransport.Requests.Single().Content); ValidateRequestOptions(contentString); Assert.AreEqual(-1, contentString.IndexOf("opinionMining")); }
public async Task AnalyzeOperationRecognizeLinkedEntitiesWithRequestOptions() { var mockResponse = new MockResponse(202); mockResponse.AddHeader(new HttpHeader("Operation-Location", "something/jobs/2a96a91f-7edf-4931-a880-3fdee1d56f15")); var mockTransport = new MockTransport(new[] { mockResponse, mockResponse }); var client = CreateTestClient(mockTransport); var documents = new List <string> { "Elon Musk is the CEO of SpaceX and Tesla." }; var options = new TextAnalyticsRequestOptions(); var actions = new RecognizeLinkedEntitiesAction(options); TextAnalyticsActions batchActions = new TextAnalyticsActions() { RecognizeLinkedEntitiesActions = new List <RecognizeLinkedEntitiesAction>() { actions }, }; await client.StartAnalyzeActionsAsync(documents, batchActions); var contentString = GetString(mockTransport.Requests.Single().Content); ValidateRequestOptions(contentString); }
public async Task AnalyzeOperationBatchWithAllErrorsTest() { TextAnalyticsClient client = GetClient(); var documents = new List <string> { "Subject is taking 100mg of ibuprofen twice daily", "", }; TextAnalyticsActions batchActions = new TextAnalyticsActions() { ExtractKeyPhrasesOptions = new List <ExtractKeyPhrasesOptions>() { new ExtractKeyPhrasesOptions() { ModelVersion = "InvalidVersion" } } }; AnalyzeBatchActionsOperation operation = await client.StartAnalyzeBatchActionsAsync(documents, batchActions, "en"); await operation.WaitForCompletionAsync(PollingInterval); //Take the first page AnalyzeBatchActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault(); //Key phrases var keyPhrasesActions = resultCollection.ExtractKeyPhrasesActionsResults.ToList(); Assert.AreEqual(1, keyPhrasesActions.Count); Assert.IsTrue(keyPhrasesActions[0].HasError); Assert.AreEqual(TextAnalyticsErrorCode.InvalidRequest, keyPhrasesActions[0].Error.ErrorCode.ToString()); }
public async Task AnalyzeOperationWithAADTest() { TextAnalyticsClient client = GetClient(useTokenCredential: true); TextAnalyticsActions batchActions = new TextAnalyticsActions() { ExtractKeyPhrasesOptions = new List <ExtractKeyPhrasesOptions>() { new ExtractKeyPhrasesOptions() }, }; AnalyzeBatchActionsOperation operation = await client.StartAnalyzeBatchActionsAsync(batchDocuments, batchActions); await operation.WaitForCompletionAsync(PollingInterval); //Take the first page AnalyzeBatchActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault(); IReadOnlyCollection <RecognizeEntitiesActionResult> entitiesResult = resultCollection.RecognizeEntitiesActionsResults; ExtractKeyPhrasesResultCollection keyPhrasesResult = resultCollection.ExtractKeyPhrasesActionsResults.ElementAt(0).Result; IReadOnlyCollection <RecognizePiiEntitiesActionResult> piiResult = resultCollection.RecognizePiiEntitiesActionsResults; IReadOnlyCollection <RecognizeLinkedEntitiesActionResult> elResult = resultCollection.RecognizeLinkedEntitiesActionsResults; Assert.IsNotNull(keyPhrasesResult); Assert.IsNotNull(entitiesResult); Assert.IsNotNull(piiResult); Assert.IsNotNull(elResult); Assert.AreEqual(2, keyPhrasesResult.Count); }
public async Task SingleCategoryClassifyBatchWithStatisticsTest() { TextAnalyticsClient client = GetClient(); TextAnalyticsActions batchActions = new TextAnalyticsActions() { SingleCategoryClassifyActions = new List <SingleCategoryClassifyAction>() { new SingleCategoryClassifyAction(TestEnvironment.SingleClassificationProjectName, TestEnvironment.SingleClassificationDeploymentName) } }; AnalyzeActionsOptions options = new AnalyzeActionsOptions() { IncludeStatistics = true }; AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(s_singleCategoryClassifyBatchDocuments, batchActions, options); await PollUntilTimeout(operation); Assert.IsTrue(operation.HasCompleted); // Take the first page AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault(); IReadOnlyCollection <SingleCategoryClassifyActionResult> singleCategoryClassifyActionsResults = resultCollection.SingleCategoryClassifyResults; SingleCategoryClassifyResultCollection singleCategoryClassifyResults = singleCategoryClassifyActionsResults.FirstOrDefault().DocumentsResults; ValidateSummaryBatchResult(singleCategoryClassifyResults, includeStatistics: true); }
public async Task ExtractSummaryBatchWithErrorTest() { TextAnalyticsClient client = GetClient(); var documents = new List <string> { "Subject is taking 100mg of ibuprofen twice daily", "", }; TextAnalyticsActions batchActions = new TextAnalyticsActions() { ExtractSummaryActions = new List <ExtractSummaryAction>() { new ExtractSummaryAction() } }; AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(documents, batchActions, "en"); await operation.WaitForCompletionAsync(); // Take the first page AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault(); List <ExtractSummaryActionResult> summaryActions = resultCollection.ExtractSummaryResults.ToList(); Assert.AreEqual(1, summaryActions.Count); ExtractSummaryResultCollection documentsResults = summaryActions[0].DocumentsResults; Assert.IsFalse(documentsResults[0].HasError); Assert.IsTrue(documentsResults[1].HasError); Assert.AreEqual(TextAnalyticsErrorCode.InvalidDocument, documentsResults[1].Error.ErrorCode.ToString()); }
public async Task RecognizeCustomEntitiesTest() { TextAnalyticsClient client = GetClient(); TextAnalyticsActions batchActions = new TextAnalyticsActions() { RecognizeCustomEntitiesActions = new List <RecognizeCustomEntitiesAction>() { new RecognizeCustomEntitiesAction(TestEnvironment.RecognizeCustomEntitiesProjectName, TestEnvironment.RecognizeCustomEntitiesDeploymentName) } }; var operation = await client.StartAnalyzeActionsAsync(new List <string> { EnglishDocument1 }, batchActions); await PollUntilTimeout(operation); Assert.IsTrue(operation.HasCompleted); RecognizeCustomEntitiesResultCollection results = ExtractDocumentsResultsFromResponse(operation); RecognizeEntitiesResult firstResult = results.First(); CategorizedEntityCollection entites = firstResult.Entities; ValidateInDocumentResult(entites, e_document1ExpectedOutput); }
public async Task AnalyzeOperationTest() { TextAnalyticsClient client = GetClient(); TextAnalyticsActions batchActions = new TextAnalyticsActions() { ExtractKeyPhrasesActions = new List <ExtractKeyPhrasesAction>() { new ExtractKeyPhrasesAction() }, }; AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(batchConvenienceDocuments, batchActions, "en"); await operation.WaitForCompletionAsync(); //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; IReadOnlyCollection <ExtractSummaryActionResult> extractSummaryActionsResults = resultCollection.ExtractSummaryResults; IReadOnlyCollection <RecognizeCustomEntitiesActionResult> recognizeCustomEntitiesActionResults = resultCollection.RecognizeCustomEntitiesResults; IReadOnlyCollection <SingleCategoryClassifyActionResult> singleCategoryClassifyResults = resultCollection.SingleCategoryClassifyResults; IReadOnlyCollection <MultiCategoryClassifyActionResult> multiCategoryClassifyResults = resultCollection.MultiCategoryClassifyResults; Assert.IsNotNull(keyPhrasesActionsResults); Assert.IsNotNull(entitiesActionsResults); Assert.IsNotNull(piiActionsResults); Assert.IsNotNull(entityLinkingActionsResults); Assert.IsNotNull(analyzeSentimentActionsResults); Assert.IsNotNull(extractSummaryActionsResults); Assert.IsNotNull(singleCategoryClassifyResults); Assert.IsNotNull(multiCategoryClassifyResults); Assert.IsNotNull(recognizeCustomEntitiesActionResults); var keyPhrasesListId1 = new List <string> { "CEO", "SpaceX", "Elon Musk", "Tesla" }; var keyPhrasesListId2 = new List <string> { "Tesla stock" }; ExtractKeyPhrasesResultCollection keyPhrasesDocumentsResults = keyPhrasesActionsResults.FirstOrDefault().DocumentsResults; Assert.AreEqual(2, keyPhrasesDocumentsResults.Count); foreach (string keyphrase in keyPhrasesDocumentsResults[0].KeyPhrases) { Assert.IsTrue(keyPhrasesListId1.Contains(keyphrase)); } foreach (string keyphrase in keyPhrasesDocumentsResults[1].KeyPhrases) { Assert.IsTrue(keyPhrasesListId2.Contains(keyphrase)); } }
public async Task RecognizeCustomEntitiesBatchTest() { TextAnalyticsClient client = GetClient(); TextAnalyticsActions batchActions = new TextAnalyticsActions() { RecognizeCustomEntitiesActions = new List <RecognizeCustomEntitiesAction>() { new RecognizeCustomEntitiesAction(TestEnvironment.RecognizeCustomEntitiesProjectName, TestEnvironment.RecognizeCustomEntitiesDeploymentName) } }; var operation = await client.StartAnalyzeActionsAsync(s_batchDocuments, batchActions); await PollUntilTimeout(operation); Assert.IsTrue(operation.HasCompleted); var results = ExtractDocumentsResultsFromResponse(operation); var expectedOutput = new Dictionary <string, List <string> >() { { "1", e_document1ExpectedOutput }, { "2", s_document1ExpectedOutput }, }; ValidateBatchDocumentsResult(results, expectedOutput); }
public void Inject(TextAnalyticsActions textAnalyticsActions) { textAnalyticsActions.SingleCategoryClassifyActions = new List <SingleCategoryClassifyAction>() { new SingleCategoryClassifyAction(_projectName, _deploymentName) }; }
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 CreateAnalyzeOperationConvenienceSetsOperationId() { var mockResponse = new MockResponse(202); mockResponse.AddHeader(new HttpHeader("Operation-Location", "something/jobs/2a96a91f-7edf-4931-a880-3fdee1d56f15")); var mockTransport = new MockTransport(new[] { mockResponse, mockResponse }); var client = CreateTestClient(mockTransport); var documents = new List <string> { "Elon Musk is the CEO of SpaceX and Tesla.", "Microsoft was founded by Bill Gates and Paul Allen.", "My cat and my dog might need to see a veterinarian." }; TextAnalyticsActions batchActions = new TextAnalyticsActions() { ExtractKeyPhrasesActions = new List <ExtractKeyPhrasesAction>() { new ExtractKeyPhrasesAction() }, }; AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(documents, batchActions); OperationContinuationToken continuationToken = OperationContinuationToken.Deserialize(operation.Id); Assert.IsNull(continuationToken.ShowStats); Assert.AreEqual("2a96a91f-7edf-4931-a880-3fdee1d56f15", continuationToken.JobId); Assert.AreEqual(3, continuationToken.InputDocumentOrder.Count); Assert.AreEqual(0, continuationToken.InputDocumentOrder["0"]); Assert.AreEqual(1, continuationToken.InputDocumentOrder["1"]); Assert.AreEqual(2, continuationToken.InputDocumentOrder["2"]); }
public async Task ExtractSummaryWithDisableServiceLogs() { TextAnalyticsClient client = GetClient(); TextAnalyticsActions batchActions = new TextAnalyticsActions() { ExtractSummaryActions = new List <ExtractSummaryAction>() { new ExtractSummaryAction() { DisableServiceLogs = true } } }; AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(s_extractSummaryBatchConvenienceDocuments, batchActions); await operation.WaitForCompletionAsync(); // Take the first page AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault(); IReadOnlyCollection <ExtractSummaryActionResult> extractSummaryActionsResults = resultCollection.ExtractSummaryResults; Assert.IsNotNull(extractSummaryActionsResults); Assert.AreEqual(2, extractSummaryActionsResults.FirstOrDefault().DocumentsResults.Count); }
public async Task AnalyzeOperationWithPagination() { TextAnalyticsClient client = GetClient(); List <string> documents = new(); for (int i = 0; i < 23; i++) { documents.Add("Elon Musk is the CEO of SpaceX and Tesla."); } TextAnalyticsActions batchActions = new TextAnalyticsActions() { ExtractKeyPhrasesOptions = new List <ExtractKeyPhrasesOptions>() { new ExtractKeyPhrasesOptions() }, DisplayName = "AnalyzeOperationWithPagination", }; AnalyzeBatchActionsOperation operation = await client.StartAnalyzeBatchActionsAsync(documents, batchActions); Assert.IsFalse(operation.HasCompleted); Assert.IsFalse(operation.HasValue); Assert.ThrowsAsync <InvalidOperationException>(async() => await Task.Run(() => operation.Value)); Assert.Throws <InvalidOperationException>(() => operation.GetValues()); await operation.WaitForCompletionAsync(PollingInterval); Assert.IsTrue(operation.HasCompleted); Assert.IsTrue(operation.HasValue); await operation.WaitForCompletionAsync(PollingInterval); // try async //There most be 2 pages as service limit is 20 documents per page List <AnalyzeBatchActionsResult> asyncPages = operation.Value.ToEnumerableAsync().Result; Assert.AreEqual(2, asyncPages.Count); // First page should have 20 results Assert.AreEqual(20, asyncPages[0].ExtractKeyPhrasesActionsResults.FirstOrDefault().Result.Count); // Second page should have remaining 3 results Assert.AreEqual(3, asyncPages[1].ExtractKeyPhrasesActionsResults.FirstOrDefault().Result.Count); // try sync //There most be 2 pages as service limit is 20 documents per page List <AnalyzeBatchActionsResult> pages = operation.GetValues().AsEnumerable().ToList(); Assert.AreEqual(2, pages.Count); // First page should have 20 results Assert.AreEqual(20, pages[0].ExtractKeyPhrasesActionsResults.FirstOrDefault().Result.Count); // Second page should have remaining 3 results Assert.AreEqual(3, pages[1].ExtractKeyPhrasesActionsResults.FirstOrDefault().Result.Count); }
public async Task AnalyzeOperationWithLanguageTest() { 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() }, DisplayName = "AnalyzeOperationWithLanguageTest" }; AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(batchDocuments, batchActions); await operation.WaitForCompletionAsync(); //Take the first page AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault(); IReadOnlyCollection <ExtractKeyPhrasesActionResult> keyPhrasesActionsResults = resultCollection.ExtractKeyPhrasesActionsResults; Assert.IsNotNull(keyPhrasesActionsResults); ExtractKeyPhrasesResultCollection keyPhrasesResult = keyPhrasesActionsResults.FirstOrDefault().Result; Assert.AreEqual(2, keyPhrasesResult.Count); Assert.AreEqual("AnalyzeOperationWithLanguageTest", operation.DisplayName); 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)); } }
public async Task AnalyzeOperationBatchWithStatisticsTest() { 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", }, new TextDocumentInput("3", "") { Language = "es", } }; TextAnalyticsActions batchActions = new TextAnalyticsActions() { ExtractKeyPhrasesOptions = new List <ExtractKeyPhrasesOptions>() { new ExtractKeyPhrasesOptions() }, DisplayName = "AnalyzeOperationTest", }; AnalyzeBatchActionsOptions options = new AnalyzeBatchActionsOptions() { IncludeStatistics = true }; AnalyzeBatchActionsOperation operation = await client.StartAnalyzeBatchActionsAsync(batchDocuments, batchActions, options); await operation.WaitForCompletionAsync(PollingInterval); //Take the first page AnalyzeBatchActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault(); ExtractKeyPhrasesResultCollection result = resultCollection.ExtractKeyPhrasesActionsResults.ElementAt(0).Result; Assert.IsNotNull(result); Assert.AreEqual(3, result.Count); Assert.AreEqual(3, result.Statistics.DocumentCount); Assert.AreEqual(2, result.Statistics.TransactionCount); Assert.AreEqual(2, result.Statistics.ValidDocumentCount); Assert.AreEqual(1, result.Statistics.InvalidDocumentCount); Assert.AreEqual(51, result[0].Statistics.CharacterCount); Assert.AreEqual(1, result[0].Statistics.TransactionCount); }
GetChunkedResults(int chunkId, List <TextDocumentInput> documentChunk, TextAnalyticsActions actions) { var errors = new List <string>(); var sentimentResults = new List <AnalyzeSentimentResult>(); var piiResults = new List <RecognizePiiEntitiesResult>(); try { Log.LogInformation($"Sending text analytics request for document chunk with id {chunkId}."); using var cts = new CancellationTokenSource(); cts.CancelAfter(RequestTimeout); var operation = await TextAnalyticsClient.StartAnalyzeActionsAsync(documentChunk, actions, cancellationToken : cts.Token).ConfigureAwait(false); await operation.WaitForCompletionAsync(PollingInterval, cts.Token).ConfigureAwait(false); Log.LogInformation($"Received text analytics response for document chunk with id {chunkId}."); await foreach (var documentsInPage in operation.Value) { foreach (var piiResult in documentsInPage.RecognizePiiEntitiesResults) { if (piiResult.HasError) { errors.Add($"PII recognition failed with error: {piiResult.Error.Message}"); } piiResults.AddRange(piiResult.DocumentsResults); } foreach (var sentimentResult in documentsInPage.AnalyzeSentimentResults) { if (sentimentResult.HasError) { errors.Add($"Sentiment analysis failed with error: {sentimentResult.Error.Message}"); } sentimentResults.AddRange(sentimentResult.DocumentsResults); } } } catch (OperationCanceledException) { throw new TimeoutException($"The operation has timed out after {RequestTimeout.TotalSeconds} seconds."); } // do not catch throttling errors, rather throw and retry catch (RequestFailedException e) when(e.Status != 429) { errors.Add($"Text analytics request failed with error: {e.Message}"); } return(sentimentResults, piiResults, errors); }
public async Task AnalyzeOperationTest() { TextAnalyticsClient client = GetClient(); TextAnalyticsActions batchActions = new TextAnalyticsActions() { ExtractKeyPhrasesOptions = new List <ExtractKeyPhrasesOptions>() { new ExtractKeyPhrasesOptions() }, }; AnalyzeBatchActionsOperation operation = await client.StartAnalyzeBatchActionsAsync(batchConvenienceDocuments, batchActions, "en"); await operation.WaitForCompletionAsync(); //Take the first page AnalyzeBatchActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault(); IReadOnlyCollection <RecognizeEntitiesActionResult> entitiesResult = resultCollection.RecognizeEntitiesActionsResults; IReadOnlyCollection <ExtractKeyPhrasesActionResult> keyPhrasesResult = resultCollection.ExtractKeyPhrasesActionsResults; IReadOnlyCollection <RecognizePiiEntitiesActionResult> piiResult = resultCollection.RecognizePiiEntitiesActionsResults; IReadOnlyCollection <RecognizeLinkedEntitiesActionResult> elResult = resultCollection.RecognizeLinkedEntitiesActionsResults; Assert.IsNotNull(keyPhrasesResult); Assert.IsNotNull(entitiesResult); Assert.IsNotNull(piiResult); Assert.IsNotNull(elResult); var keyPhrasesListId1 = new List <string> { "CEO of SpaceX", "Elon Musk", "Tesla" }; var keyPhrasesListId2 = new List <string> { "Tesla stock", "year" }; var keyPhrases = keyPhrasesResult.FirstOrDefault().Result; Assert.AreEqual(2, keyPhrases.Count); foreach (string keyphrase in keyPhrases[0].KeyPhrases) { Assert.IsTrue(keyPhrasesListId1.Contains(keyphrase)); } foreach (string keyphrase in keyPhrases[1].KeyPhrases) { Assert.IsTrue(keyPhrasesListId2.Contains(keyphrase)); } }
/// <summary> /// Submits text analytics requests depending on the settings (sentimentAnalysisSetting). The whole transcript (per channel) will be submitted in a single request. /// (This means for instance that one single sentiment score will be generated per channel). /// </summary> /// <param name="speechTranscript">The speech transcript object.</param> /// <param name="sentimentAnalysisSetting">The sentiment analysis setting.</param> /// <param name="piiRedactionSetting">The PII redaction setting.</param> /// <returns>The job ids and errors, if any were found.</returns> public async Task <(IEnumerable <string> jobIds, IEnumerable <string> errors)> SubmitAudioLevelRequests( SpeechTranscript speechTranscript, SentimentAnalysisSetting sentimentAnalysisSetting, PiiRedactionSetting piiRedactionSetting) { speechTranscript = speechTranscript ?? throw new ArgumentNullException(nameof(speechTranscript)); if (sentimentAnalysisSetting != SentimentAnalysisSetting.AudioLevel && piiRedactionSetting != PiiRedactionSetting.UtteranceAndAudioLevel) { return(new List <string>(), new List <string>()); } var documents = speechTranscript.CombinedRecognizedPhrases.Where(r => !string.IsNullOrEmpty(r.Display)).Select(r => new TextDocumentInput($"{r.Channel}", r.Display) { Language = Locale }); var actions = new TextAnalyticsActions { DisplayName = "IngestionClient" }; if (sentimentAnalysisSetting == SentimentAnalysisSetting.AudioLevel) { actions.AnalyzeSentimentActions = new List <AnalyzeSentimentAction>() { new AnalyzeSentimentAction() }; } if (piiRedactionSetting == PiiRedactionSetting.UtteranceAndAudioLevel) { var action = new RecognizePiiEntitiesAction(); if (!string.IsNullOrEmpty(FetchTranscriptionEnvironmentVariables.PiiCategories)) { var piiEntityCategories = FetchTranscriptionEnvironmentVariables.PiiCategories.Split(",").Select(c => new PiiEntityCategory(c)); foreach (var category in piiEntityCategories) { action.CategoriesFilter.Add(category); } } actions.RecognizePiiEntitiesActions = new List <RecognizePiiEntitiesAction>() { action }; } return(await SubmitDocumentsAsync(documents, actions).ConfigureAwait(false)); }
public async Task <IEnumerable <string> > AddUtteranceLevelEntitiesAsync( SpeechTranscript speechTranscript, SentimentAnalysisSetting sentimentAnalysisSetting) { speechTranscript = speechTranscript ?? throw new ArgumentNullException(nameof(speechTranscript)); var errors = new List <string>(); if (sentimentAnalysisSetting != SentimentAnalysisSetting.UtteranceLevel) { return(errors); } var documents = speechTranscript.RecognizedPhrases.Where(r => r.NBest.FirstOrDefault() != null && !string.IsNullOrEmpty(r.NBest.First().Display)).Select(r => new TextDocumentInput($"{r.Channel}_{r.Offset}", r.NBest.First().Display) { Language = Locale }); var actions = new TextAnalyticsActions { DisplayName = "IngestionClient", AnalyzeSentimentActions = new List <AnalyzeSentimentAction>() { new AnalyzeSentimentAction() } }; var(sentimentResults, piiResults, requestErrors) = await this.GetDocumentResultsAsync(documents, actions).ConfigureAwait(false); errors.AddRange(requestErrors); foreach (var recognizedPhrase in speechTranscript.RecognizedPhrases) { var index = $"{recognizedPhrase.Channel}_{recognizedPhrase.Offset}"; var firstNBest = recognizedPhrase.NBest.FirstOrDefault(); var sentimentResult = sentimentResults.Where(s => s.Id == index).FirstOrDefault(); if (firstNBest != null) { firstNBest.Sentiment = new Sentiment() { Negative = sentimentResult?.DocumentSentiment.ConfidenceScores.Negative ?? 0.0, Positive = sentimentResult?.DocumentSentiment.ConfidenceScores.Positive ?? 0.0, Neutral = sentimentResult?.DocumentSentiment.ConfidenceScores.Neutral ?? 0.0, }; } } return(errors); }
public async Task AnalyzeOperationRecognizePiiEntitiesWithPiiOptionsFull() { var mockResponse = new MockResponse(202); mockResponse.AddHeader(new HttpHeader("Operation-Location", "something/jobs/2a96a91f-7edf-4931-a880-3fdee1d56f15")); var mockTransport = new MockTransport(new[] { mockResponse, mockResponse }); var client = CreateTestClient(mockTransport); var documents = new List <string> { "Elon Musk is the CEO of SpaceX and Tesla." }; var options = new RecognizePiiEntitiesOptions() { ModelVersion = "latest", DisableServiceLogs = true, IncludeStatistics = true, DomainFilter = PiiEntityDomain.ProtectedHealthInformation, CategoriesFilter = { PiiEntityCategory.USSocialSecurityNumber } }; var actions = new RecognizePiiEntitiesAction(options); TextAnalyticsActions batchActions = new TextAnalyticsActions() { RecognizePiiEntitiesActions = new List <RecognizePiiEntitiesAction>() { actions }, }; await client.StartAnalyzeActionsAsync(documents, batchActions); var contentString = GetString(mockTransport.Requests.Single().Content); ValidateRequestOptions(contentString, true); string domaintFilter = contentString.Substring(contentString.IndexOf("domain"), 13); var expectedDomainFilterContent = "domain\":\"phi\""; Assert.AreEqual(expectedDomainFilterContent, domaintFilter); string piiCategories = contentString.Substring(contentString.IndexOf("piiCategories"), 41); var expectedPiiCategoriesContent = "piiCategories\":[\"USSocialSecurityNumber\"]"; Assert.AreEqual(expectedPiiCategoriesContent, piiCategories); }
GetDocumentResultsAsync( IEnumerable <TextDocumentInput> documents, TextAnalyticsActions actions) { var errors = new List <string>(); var sentimentResults = new List <AnalyzeSentimentResult>(); var piiResults = new List <RecognizePiiEntitiesResult>(); if (!documents.Any()) { return(sentimentResults, piiResults, errors); } var chunkedDocuments = new List <List <TextDocumentInput> >(); var totalDocuments = documents.Count(); for (int i = 0; i < totalDocuments; i += MaxRecordsPerRequest) { var chunk = documents.Skip(i).Take(Math.Min(MaxRecordsPerRequest, totalDocuments - i)).ToList(); chunkedDocuments.Add(chunk); } Log.LogInformation($"Sending text analytics requests for {chunkedDocuments.Count} chunks in total."); var tasks = new List <Task <( IEnumerable <AnalyzeSentimentResult> sentimentResults, IEnumerable <RecognizePiiEntitiesResult> piiResults, IEnumerable <string> errors)> >(); var counter = 0; foreach (var documentChunk in chunkedDocuments) { var index = counter; tasks.Add(GetChunkedResults(index, documentChunk, actions)); counter++; } var results = await Task.WhenAll(tasks).ConfigureAwait(false); foreach (var result in results) { sentimentResults.AddRange(result.sentimentResults); piiResults.AddRange(result.piiResults); errors.AddRange(result.errors); } return(sentimentResults, piiResults, errors); }
public async Task AnalyzeOperationAnalyzeSentimentWithAnalyzeSentimentOptionsFull() { var mockResponse = new MockResponse(202); mockResponse.AddHeader(new HttpHeader("Operation-Location", "something/jobs/2a96a91f-7edf-4931-a880-3fdee1d56f15")); var mockTransport = new MockTransport(new[] { mockResponse, mockResponse }); var client = CreateTestClient(mockTransport); var documents = new List <string> { "Elon Musk is the CEO of SpaceX and Tesla." }; var options = new AnalyzeSentimentOptions() { ModelVersion = "latest", DisableServiceLogs = true, IncludeStatistics = true, IncludeOpinionMining = true }; var actions = new AnalyzeSentimentAction(options); TextAnalyticsActions batchActions = new TextAnalyticsActions() { AnalyzeSentimentActions = new List <AnalyzeSentimentAction>() { actions }, }; await client.StartAnalyzeActionsAsync(documents, batchActions); var contentString = GetString(mockTransport.Requests.Single().Content); ValidateRequestOptions(contentString, true); string opinionMining = contentString.Substring(contentString.IndexOf("opinionMining"), 19); var expectedOpinionMiningContent = "opinionMining\":true"; Assert.AreEqual(expectedOpinionMiningContent, opinionMining); }
public async Task AnalyzeOperationRecognizePiiEntitiesWithDisableServiceLogs() { var mockResponse = new MockResponse(202); mockResponse.AddHeader(new HttpHeader("Operation-Location", "something/jobs/2a96a91f-7edf-4931-a880-3fdee1d56f15")); var mockTransport = new MockTransport(new[] { mockResponse, mockResponse }); var client = CreateTestClient(mockTransport); var documents = new List <string> { "Elon Musk is the CEO of SpaceX and Tesla." }; var actions = new RecognizePiiEntitiesAction() { DisableServiceLogs = true }; TextAnalyticsActions batchActions = new TextAnalyticsActions() { RecognizePiiEntitiesActions = new List <RecognizePiiEntitiesAction>() { actions }, }; await client.StartAnalyzeActionsAsync(documents, batchActions); var content = mockTransport.Requests.Single().Content; using var stream = new MemoryStream(); await content.WriteToAsync(stream, default); stream.Position = 0; using var streamReader = new StreamReader(stream); string contentString = streamReader.ReadToEnd(); string logging = contentString.Substring(contentString.IndexOf("loggingOptOut"), 19); var expectedContent = "loggingOptOut\":true"; Assert.AreEqual(expectedContent, logging); }