Skip to content

Commit

Permalink
[TA] Implement Pageable actions (#18371)
Browse files Browse the repository at this point in the history
  • Loading branch information
maririos authored Feb 4, 2021
1 parent 34066b4 commit 0f35e48
Show file tree
Hide file tree
Showing 21 changed files with 2,413 additions and 1,417 deletions.
79 changes: 40 additions & 39 deletions sdk/textanalytics/Azure.AI.TextAnalytics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -505,59 +505,60 @@ The Analyze functionality allows to choose which of the supported Text Analytics

await operation.WaitForCompletionAsync();

AnalyzeOperationResult resultCollection = operation.Value;

RecognizeEntitiesResultCollection entitiesResult = resultCollection.Tasks.EntityRecognitionTasks[0].Results;
foreach (AnalyzeOperationResult documentsInPage in operation.GetValues())
{
RecognizeEntitiesResultCollection entitiesResult = documentsInPage.Tasks.EntityRecognitionTasks[0].Results;

ExtractKeyPhrasesResultCollection keyPhrasesResult = resultCollection.Tasks.KeyPhraseExtractionTasks[0].Results;
ExtractKeyPhrasesResultCollection keyPhrasesResult = documentsInPage.Tasks.KeyPhraseExtractionTasks[0].Results;

RecognizePiiEntitiesResultCollection piiResult = resultCollection.Tasks.EntityRecognitionPiiTasks[0].Results;
RecognizePiiEntitiesResultCollection piiResult = documentsInPage.Tasks.EntityRecognitionPiiTasks[0].Results;

Console.WriteLine("Recognized Entities");
Console.WriteLine("Recognized Entities");

foreach (RecognizeEntitiesResult result in entitiesResult)
{
Console.WriteLine($" Recognized the following {result.Entities.Count} entities:");

foreach (CategorizedEntity entity in result.Entities)
foreach (RecognizeEntitiesResult result in entitiesResult)
{
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 the following {result.Entities.Count} entities:");

Console.WriteLine("Recognized PII 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("");
}

foreach (RecognizePiiEntitiesResult result in piiResult)
{
Console.WriteLine($" Recognized the following {result.Entities.Count} PII entities:");
Console.WriteLine("Recognized PII Entities");

foreach (PiiEntity entity in result.Entities)
foreach (RecognizePiiEntitiesResult result in piiResult)
{
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 the following {result.Entities.Count} PII entities:");

Console.WriteLine("Key Phrases");
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("");
}

foreach (ExtractKeyPhrasesResult result in keyPhrasesResult)
{
Console.WriteLine($" Recognized the following {result.KeyPhrases.Count} Keyphrases:");
Console.WriteLine("Key Phrases");

foreach (string keyphrase in result.KeyPhrases)
foreach (ExtractKeyPhrasesResult result in keyPhrasesResult)
{
Console.WriteLine($" {keyphrase}");
Console.WriteLine($" Recognized the following {result.KeyPhrases.Count} Keyphrases:");

foreach (string keyphrase in result.KeyPhrases)
{
Console.WriteLine($" {keyphrase}");
}
Console.WriteLine("");
}
Console.WriteLine("");
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,20 @@ internal AnalyzeHealthcareEntitiesResultCollection() : base (default(System.Coll
public string ModelVersion { get { throw null; } }
public Azure.AI.TextAnalytics.TextDocumentBatchStatistics Statistics { get { throw null; } }
}
public partial class AnalyzeOperation : Azure.Operation<Azure.AI.TextAnalytics.AnalyzeOperationResult>
public partial class AnalyzeOperation : Azure.AI.TextAnalytics.PageableOperation<Azure.AI.TextAnalytics.AnalyzeOperationResult>
{
public AnalyzeOperation(string operationId, Azure.AI.TextAnalytics.TextAnalyticsClient client) { }
public override bool HasCompleted { get { throw null; } }
public override bool HasValue { get { throw null; } }
public override string Id { get { throw null; } }
public override Azure.AI.TextAnalytics.AnalyzeOperationResult Value { get { throw null; } }
public override Azure.AsyncPageable<Azure.AI.TextAnalytics.AnalyzeOperationResult> Value { get { throw null; } }
public override Azure.Response GetRawResponse() { throw null; }
public override Azure.Pageable<Azure.AI.TextAnalytics.AnalyzeOperationResult> GetValues() { throw null; }
public override Azure.AsyncPageable<Azure.AI.TextAnalytics.AnalyzeOperationResult> GetValuesAsync() { throw null; }
public override Azure.Response UpdateStatus(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public override System.Threading.Tasks.ValueTask<Azure.Response> UpdateStatusAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public override System.Threading.Tasks.ValueTask<Azure.Response<Azure.AI.TextAnalytics.AnalyzeOperationResult>> WaitForCompletionAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public override System.Threading.Tasks.ValueTask<Azure.Response<Azure.AI.TextAnalytics.AnalyzeOperationResult>> WaitForCompletionAsync(System.TimeSpan pollingInterval, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public override System.Threading.Tasks.ValueTask<Azure.Response<Azure.AsyncPageable<Azure.AI.TextAnalytics.AnalyzeOperationResult>>> WaitForCompletionAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public override System.Threading.Tasks.ValueTask<Azure.Response<Azure.AsyncPageable<Azure.AI.TextAnalytics.AnalyzeOperationResult>>> WaitForCompletionAsync(System.TimeSpan pollingInterval, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
}
public partial class AnalyzeOperationOptions : Azure.AI.TextAnalytics.TextAnalyticsRequestOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,59 +41,60 @@ To run analyze operation in multiple documents, call `StartAnalyzeOperationBatch

await operation.WaitForCompletionAsync();

AnalyzeOperationResult resultCollection = operation.Value;

RecognizeEntitiesResultCollection entitiesResult = resultCollection.Tasks.EntityRecognitionTasks[0].Results;

ExtractKeyPhrasesResultCollection keyPhrasesResult = resultCollection.Tasks.KeyPhraseExtractionTasks[0].Results;
foreach (AnalyzeOperationResult documentsInPage in operation.GetValues())
{
RecognizeEntitiesResultCollection entitiesResult = documentsInPage.Tasks.EntityRecognitionTasks[0].Results;

RecognizePiiEntitiesResultCollection piiResult = resultCollection.Tasks.EntityRecognitionPiiTasks[0].Results;
ExtractKeyPhrasesResultCollection keyPhrasesResult = documentsInPage.Tasks.KeyPhraseExtractionTasks[0].Results;

Console.WriteLine("Recognized Entities");
RecognizePiiEntitiesResultCollection piiResult = documentsInPage.Tasks.EntityRecognitionPiiTasks[0].Results;

foreach (RecognizeEntitiesResult result in entitiesResult)
{
Console.WriteLine($" Recognized the following {result.Entities.Count} entities:");
Console.WriteLine("Recognized Entities");

foreach (CategorizedEntity entity in result.Entities)
foreach (RecognizeEntitiesResult result in entitiesResult)
{
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($" 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("");
}

Console.WriteLine("Recognized PII Entities");
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)
foreach (RecognizePiiEntitiesResult result in piiResult)
{
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($" 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("");
}

Console.WriteLine("Key Phrases");
Console.WriteLine("Key Phrases");

foreach (ExtractKeyPhrasesResult result in keyPhrasesResult)
{
Console.WriteLine($" Recognized the following {result.KeyPhrases.Count} Keyphrases:");

foreach (string keyphrase in result.KeyPhrases)
foreach (ExtractKeyPhrasesResult result in keyPhrasesResult)
{
Console.WriteLine($" {keyphrase}");
Console.WriteLine($" Recognized the following {result.KeyPhrases.Count} Keyphrases:");

foreach (string keyphrase in result.KeyPhrases)
{
Console.WriteLine($" {keyphrase}");
}
Console.WriteLine("");
}
Console.WriteLine("");
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class AnalyzeHealthcareEntitiesOperation : PageableOperation<AnalyzeHealt
public TextAnalyticsOperationStatus Status => _status;

/// <summary>
/// Gets the final result of the long-running operation in a synchronous way.
/// Gets the final result of the long-running operation asynchronously.
/// </summary>
/// <remarks>
/// This property can be accessed only after the operation completes successfully (HasValue is true).
Expand All @@ -59,25 +59,50 @@ public class AnalyzeHealthcareEntitiesOperation : PageableOperation<AnalyzeHealt
/// </summary>
public override bool HasValue => _firstPage != null;

/// <summary>Provides communication with the Text Analytics Azure Cognitive Service through its REST API.</summary>
/// <summary>
/// Provides communication with the Text Analytics Azure Cognitive Service through its REST API.
/// </summary>
private readonly TextAnalyticsRestClient _serviceClient;

/// <summary>Provides tools for exception creation in case of failure.</summary>
/// <summary>
/// Provides tools for exception creation in case of failure.
/// </summary>
private readonly ClientDiagnostics _diagnostics;

/// <summary><c>true</c> if the long-running operation has completed. Otherwise, <c>false</c>.</summary>
/// <summary>
/// <c>true</c> if the long-running operation has completed. Otherwise, <c>false</c>.
/// </summary>
private bool _hasCompleted;

/// <summary>
/// Represents the status of the long-running operation.
/// </summary>
private TextAnalyticsOperationStatus _status;

/// <summary>
/// If the operation has an exception, this property saves its information.
/// </summary>
private RequestFailedException _requestFailedException;

/// <summary>
/// Represents the HTTP response from the service.
/// </summary>
private Response _response;

/// <summary>
/// Provides the results for the first page.
/// </summary>
private Page<AnalyzeHealthcareEntitiesResultCollection> _firstPage;

/// <summary>
/// Represents the desire of the user to request statistics.
/// This is used in every GET request.
/// </summary>
private readonly bool? _showStats;

/// <summary>
/// Provides the api version to use when doing pagination.
/// </summary>
private readonly string _apiVersion;

/// <summary>
Expand Down Expand Up @@ -294,17 +319,14 @@ public virtual async Task CancelAsync(CancellationToken cancellationToken = defa
}

/// <summary>
/// Gets the final result of the long-running operation in an asynchronous way.
/// Gets the final result of the long-running operation asynchronously.
/// </summary>
/// <remarks>
/// Operation must complete successfully (HasValue is true) for it to provide values.
/// </remarks>
public override AsyncPageable<AnalyzeHealthcareEntitiesResultCollection> GetValuesAsync()
{
if (!HasCompleted)
throw new InvalidOperationException("The operation has not completed yet.");
if (HasCompleted && !HasValue)
throw _requestFailedException;
ValidateOperationStatus();

async Task<Page<AnalyzeHealthcareEntitiesResultCollection>> NextPageFunc(string nextLink, int? pageSizeHint)
{
Expand All @@ -326,17 +348,14 @@ async Task<Page<AnalyzeHealthcareEntitiesResultCollection>> NextPageFunc(string
}

/// <summary>
/// Gets the final result of the long-running operation in an asynchronous way.
/// Gets the final result of the long-running operation in synchronously.
/// </summary>
/// <remarks>
/// Operation must complete successfully (HasValue is true) for it to provide values.
/// </remarks>
public override Pageable<AnalyzeHealthcareEntitiesResultCollection> GetValues()
{
if (!HasCompleted)
throw new InvalidOperationException("The operation has not completed yet.");
if (HasCompleted && !HasValue)
throw _requestFailedException;
ValidateOperationStatus();

Page<AnalyzeHealthcareEntitiesResultCollection> NextPageFunc(string nextLink, int? pageSizeHint)
{
Expand All @@ -356,5 +375,13 @@ Page<AnalyzeHealthcareEntitiesResultCollection> NextPageFunc(string nextLink, in

return PageableHelpers.CreateEnumerable(_ => _firstPage, NextPageFunc);
}

private void ValidateOperationStatus()
{
if (!HasCompleted)
throw new InvalidOperationException("The operation has not completed yet.");
if (!HasValue)
throw _requestFailedException;
}
}
}
Loading

0 comments on commit 0f35e48

Please sign in to comment.