Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TA] Implement Pageable actions #18371

Merged
merged 4 commits into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -276,17 +301,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 @@ -308,17 +330,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 @@ -338,5 +357,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