Skip to content

Commit

Permalink
Add method-specific request options to TextAnalyticsClient (#20049)
Browse files Browse the repository at this point in the history
* Add method-specific request options to TextAnalyticsClient

Fixes #18482

* Assert options are no different

* Fix build issues

* Update public APIs
* Fix nuget error in DemoApp by not packing it (non-shipping)

* Use RecognizeEntitiesOptions in new overloads

* Update samples

* Add overloads for ExtractKeyPhrasesBatch(Async)
  • Loading branch information
heaths authored Apr 2, 2021
1 parent 6803042 commit b2a376b
Show file tree
Hide file tree
Showing 31 changed files with 2,454 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
Expand Down
5 changes: 4 additions & 1 deletion sdk/textanalytics/Azure.AI.TextAnalytics/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Release History

## 5.1.0-beta.6 (Unreleased)

### New features
- Add overloads to `ExtractKeyPhrasesBatch` and `ExtractKeyPhrasesBatchAsync` to on `TextAnalyticsClient` to accept `ExtractKeyPhrasesOptions` and hid the previous methods (non-breaking change).
- Add overloads to `RecognizeEntitiesBatch` and `RecognizeEntitiesBatchAsync` to on `TextAnalyticsClient` to accept `RecognizeEntitiesOptions` and hid the previous methods (non-breaking change).
- Add overloads to `RecognizeLinkedEntitiesBatch` and `RecognizeLinkedEntitiesBatch` to on `TextAnalyticsClient` to accept `RecognizeLinkedEntitiesOptions` and hid the previous methods (non-breaking change).

## 5.1.0-beta.5 (2021-03-09)
### New features
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ var documents = new List<TextDocumentInput>
new TextDocumentInput("4", string.Empty)
};

var options = new TextAnalyticsRequestOptions { IncludeStatistics = true };
var options = new ExtractKeyPhrasesOptions { IncludeStatistics = true };
Response<ExtractKeyPhrasesResultCollection> response = client.ExtractKeyPhrasesBatch(documents, options);
ExtractKeyPhrasesResultCollection keyPhrasesInDocuments = response.Value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ var documents = new List<TextDocumentInput>
new TextDocumentInput("4", string.Empty)
};

var options = new TextAnalyticsRequestOptions { IncludeStatistics = true };
var options = new RecognizeEntitiesOptions { IncludeStatistics = true };
Response<RecognizeEntitiesResultCollection> response = client.RecognizeEntitiesBatch(documents, options);
RecognizeEntitiesResultCollection entitiesInDocuments = response.Value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ var documents = new List<TextDocumentInput>
new TextDocumentInput("4", string.Empty)
};

var options = new TextAnalyticsRequestOptions { IncludeStatistics = true };
var options = new RecognizeLinkedEntitiesOptions { IncludeStatistics = true };
Response<RecognizeLinkedEntitiesResultCollection> response = client.RecognizeLinkedEntitiesBatch(documents, options);
RecognizeLinkedEntitiesResultCollection entitiesPerDocuments = response.Value;

Expand Down
516 changes: 492 additions & 24 deletions sdk/textanalytics/Azure.AI.TextAnalytics/src/TextAnalyticsClient.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,39 @@ public async Task ExtractKeyPhrasesBatchConvenienceTest()
[Test]
public async Task ExtractKeyPhrasesBatchConvenienceWithStatisticsTest()
{
var options = new TextAnalyticsRequestOptions { IncludeStatistics = true };
TextAnalyticsClient client = GetClient();
var documents = batchConvenienceDocuments;

ExtractKeyPhrasesResultCollection results = await client.ExtractKeyPhrasesBatchAsync(documents, "en", new TextAnalyticsRequestOptions { IncludeStatistics = true });
ExtractKeyPhrasesResultCollection results = await client.ExtractKeyPhrasesBatchAsync(documents, "en", options);

ValidateBatchDocumentsResult(results, 3, includeStatistics: true);

Assert.AreEqual(documents.Count, results.Statistics.DocumentCount);

// Assert the options classes since overloads were added and the original now instantiates a RecognizeEntitiesOptions.
Assert.IsTrue(options.IncludeStatistics);
Assert.IsNull(options.ModelVersion);
Assert.AreEqual(StringIndexType.Utf16CodeUnit, options.StringIndexType);
}

[Test]
public async Task ExtractKeyPhrasesBatchConvenienceWithExtractKeyPhrasesOptionsStatisticsTest()
{
var options = new ExtractKeyPhrasesOptions { IncludeStatistics = true };
TextAnalyticsClient client = GetClient();
var documents = batchConvenienceDocuments;

ExtractKeyPhrasesResultCollection results = await client.ExtractKeyPhrasesBatchAsync(documents, "en", options);

ValidateBatchDocumentsResult(results, 3, includeStatistics: true);

Assert.AreEqual(documents.Count, results.Statistics.DocumentCount);

// Assert the options classes since overloads were added and the original now instantiates a RecognizeEntitiesOptions.
Assert.IsTrue(options.IncludeStatistics);
Assert.IsNull(options.ModelVersion);
Assert.AreEqual(StringIndexType.Utf16CodeUnit, options.StringIndexType);
}

[Test]
Expand All @@ -149,14 +174,39 @@ public async Task ExtractKeyPhrasesBatchTest()
[Test]
public async Task ExtractKeyPhrasesBatchWithSatisticsTest()
{
var options = new TextAnalyticsRequestOptions { IncludeStatistics = true };
TextAnalyticsClient client = GetClient();
List<TextDocumentInput> documents = batchDocuments;

ExtractKeyPhrasesResultCollection results = await client.ExtractKeyPhrasesBatchAsync(documents, new TextAnalyticsRequestOptions { IncludeStatistics = true });
ExtractKeyPhrasesResultCollection results = await client.ExtractKeyPhrasesBatchAsync(documents, options);

ValidateBatchDocumentsResult(results, 3, includeStatistics: true);

Assert.AreEqual(documents.Count, results.Statistics.DocumentCount);

// Assert the options classes since overloads were added and the original now instantiates a RecognizeEntitiesOptions.
Assert.IsTrue(options.IncludeStatistics);
Assert.IsNull(options.ModelVersion);
Assert.AreEqual(StringIndexType.Utf16CodeUnit, options.StringIndexType);
}

[Test]
public async Task ExtractKeyPhrasesBatchWithExtractKeyPhrasesOptionsSatisticsTest()
{
var options = new ExtractKeyPhrasesOptions { IncludeStatistics = true };
TextAnalyticsClient client = GetClient();
List<TextDocumentInput> documents = batchDocuments;

ExtractKeyPhrasesResultCollection results = await client.ExtractKeyPhrasesBatchAsync(documents, options);

ValidateBatchDocumentsResult(results, 3, includeStatistics: true);

Assert.AreEqual(documents.Count, results.Statistics.DocumentCount);

// Assert the options classes since overloads were added and the original now instantiates a RecognizeEntitiesOptions.
Assert.IsTrue(options.IncludeStatistics);
Assert.IsNull(options.ModelVersion);
Assert.AreEqual(StringIndexType.Utf16CodeUnit, options.StringIndexType);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ public async Task RecognizeEntitiesWithLanguageTest()
[Test]
public async Task RecognizeEntitiesWithSubCategoryTest()
{
TextAnalyticsRequestOptions options = new TextAnalyticsRequestOptions() { ModelVersion = "2020-04-01" };
TextAnalyticsClient client = GetClient();
string document = "I had a wonderful trip to Seattle last week.";

RecognizeEntitiesResultCollection result = await client.RecognizeEntitiesBatchAsync(new List<string>() { document }, options: new TextAnalyticsRequestOptions() { ModelVersion = "2020-04-01" } );
RecognizeEntitiesResultCollection result = await client.RecognizeEntitiesBatchAsync(new List<string>() { document }, options: options );

var documentResult = result.FirstOrDefault();
Assert.IsFalse(documentResult.HasError);
Expand All @@ -93,6 +94,37 @@ public async Task RecognizeEntitiesWithSubCategoryTest()
if (entity.Text == "last week")
Assert.AreEqual("DateRange", entity.SubCategory);
}

// Assert the options classes since overloads were added and the original now instantiates a RecognizeEntitiesOptions.
Assert.IsFalse(options.IncludeStatistics);
Assert.AreEqual("2020-04-01", options.ModelVersion);
Assert.AreEqual(StringIndexType.Utf16CodeUnit, options.StringIndexType);
}

[Test]
public async Task RecognizeEntitiesWithRecognizeEntitiesOptionsSubCategoryTest()
{
RecognizeEntitiesOptions options = new RecognizeEntitiesOptions() { ModelVersion = "2020-04-01" };
TextAnalyticsClient client = GetClient();
string document = "I had a wonderful trip to Seattle last week.";

RecognizeEntitiesResultCollection result = await client.RecognizeEntitiesBatchAsync(new List<string>() { document }, options: options );

var documentResult = result.FirstOrDefault();
Assert.IsFalse(documentResult.HasError);

Assert.GreaterOrEqual(documentResult.Entities.Count, 3);

foreach (CategorizedEntity entity in documentResult.Entities)
{
if (entity.Text == "last week")
Assert.AreEqual("DateRange", entity.SubCategory);
}

// Assert the options classes since overloads were added and the original now instantiates a RecognizeEntitiesOptions.
Assert.IsFalse(options.IncludeStatistics);
Assert.AreEqual("2020-04-01", options.ModelVersion);
Assert.AreEqual(StringIndexType.Utf16CodeUnit, options.StringIndexType);
}

[Test]
Expand Down Expand Up @@ -155,8 +187,30 @@ public async Task RecognizeEntitiesBatchConvenienceTest()
[Test]
public async Task RecognizeEntitiesBatchConvenienceWithStatisticsTest()
{
TextAnalyticsRequestOptions options = new TextAnalyticsRequestOptions { IncludeStatistics = true };
TextAnalyticsClient client = GetClient();
RecognizeEntitiesResultCollection results = await client.RecognizeEntitiesBatchAsync(s_batchConvenienceDocuments, "en", options);

var expectedOutput = new Dictionary<string, List<string>>()
{
{ "0", s_document1ExpectedOutput },
{ "1", s_document2ExpectedOutput },
};

ValidateBatchDocumentsResult(results, expectedOutput, includeStatistics: true);

// Assert the options classes since overloads were added and the original now instantiates a RecognizeEntitiesOptions.
Assert.IsTrue(options.IncludeStatistics);
Assert.IsNull(options.ModelVersion);
Assert.AreEqual(StringIndexType.Utf16CodeUnit, options.StringIndexType);
}

[Test]
public async Task RecognizeEntitiesBatchConvenienceWithRecognizeEntitiesOptionsStatisticsTest()
{
RecognizeEntitiesOptions options = new RecognizeEntitiesOptions { IncludeStatistics = true };
TextAnalyticsClient client = GetClient();
RecognizeEntitiesResultCollection results = await client.RecognizeEntitiesBatchAsync(s_batchConvenienceDocuments, "en", new TextAnalyticsRequestOptions { IncludeStatistics = true });
RecognizeEntitiesResultCollection results = await client.RecognizeEntitiesBatchAsync(s_batchConvenienceDocuments, "en", options);

var expectedOutput = new Dictionary<string, List<string>>()
{
Expand All @@ -165,6 +219,11 @@ public async Task RecognizeEntitiesBatchConvenienceWithStatisticsTest()
};

ValidateBatchDocumentsResult(results, expectedOutput, includeStatistics: true);

// Assert the options classes since overloads were added and the original now instantiates a RecognizeEntitiesOptions.
Assert.IsTrue(options.IncludeStatistics);
Assert.IsNull(options.ModelVersion);
Assert.AreEqual(StringIndexType.Utf16CodeUnit, options.StringIndexType);
}

[Test]
Expand All @@ -185,8 +244,9 @@ public async Task RecognizeEntitiesBatchTest()
[Test]
public async Task RecognizeEntitiesBatchWithStatisticsTest()
{
TextAnalyticsRequestOptions options = new TextAnalyticsRequestOptions { IncludeStatistics = true };
TextAnalyticsClient client = GetClient();
RecognizeEntitiesResultCollection results = await client.RecognizeEntitiesBatchAsync(s_batchDocuments, new TextAnalyticsRequestOptions { IncludeStatistics = true });
RecognizeEntitiesResultCollection results = await client.RecognizeEntitiesBatchAsync(s_batchDocuments, options);

var expectedOutput = new Dictionary<string, List<string>>()
{
Expand All @@ -195,6 +255,32 @@ public async Task RecognizeEntitiesBatchWithStatisticsTest()
};

ValidateBatchDocumentsResult(results, expectedOutput, includeStatistics: true);

// Assert the options classes since overloads were added and the original now instantiates a RecognizeEntitiesOptions.
Assert.IsTrue(options.IncludeStatistics);
Assert.IsNull(options.ModelVersion);
Assert.AreEqual(StringIndexType.Utf16CodeUnit, options.StringIndexType);
}

[Test]
public async Task RecognizeEntitiesBatchWithRecognizeEntitiesOptionsStatisticsTest()
{
RecognizeEntitiesOptions options = new RecognizeEntitiesOptions { IncludeStatistics = true };
TextAnalyticsClient client = GetClient();
RecognizeEntitiesResultCollection results = await client.RecognizeEntitiesBatchAsync(s_batchDocuments, options);

var expectedOutput = new Dictionary<string, List<string>>()
{
{ "1", s_document1ExpectedOutput },
{ "2", s_document1ExpectedOutput },
};

ValidateBatchDocumentsResult(results, expectedOutput, includeStatistics: true);

// Assert the options classes since overloads were added and the original now instantiates a RecognizeEntitiesOptions.
Assert.IsTrue(options.IncludeStatistics);
Assert.IsNull(options.ModelVersion);
Assert.AreEqual(StringIndexType.Utf16CodeUnit, options.StringIndexType);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ public async Task RecognizeLinkedEntitiesBatchConvenienceTest()
[Test]
public async Task RecognizeLinkedEntitiesBatchConvenienceWithStatisticsTest()
{
TextAnalyticsRequestOptions options = new TextAnalyticsRequestOptions { IncludeStatistics = true };
TextAnalyticsClient client = GetClient();
RecognizeLinkedEntitiesResultCollection results = await client.RecognizeLinkedEntitiesBatchAsync(s_batchConvenienceDocuments, "en", new TextAnalyticsRequestOptions { IncludeStatistics = true });
RecognizeLinkedEntitiesResultCollection results = await client.RecognizeLinkedEntitiesBatchAsync(s_batchConvenienceDocuments, "en", options);

var expectedOutput = new Dictionary<string, List<string>>()
{
Expand All @@ -146,6 +147,32 @@ public async Task RecognizeLinkedEntitiesBatchConvenienceWithStatisticsTest()
};

ValidateBatchDocumentsResult(results, expectedOutput, includeStatistics: true);

// Assert the options classes since overloads were added and the original now instantiates a RecognizeLinkedEntitiesOptions.
Assert.IsTrue(options.IncludeStatistics);
Assert.IsNull(options.ModelVersion);
Assert.AreEqual(StringIndexType.Utf16CodeUnit, options.StringIndexType);
}

[Test]
public async Task RecognizeLinkedEntitiesBatchConvenienceWithRecognizeLinkedEntitiesOptionsStatisticsTest()
{
RecognizeLinkedEntitiesOptions options = new RecognizeLinkedEntitiesOptions { IncludeStatistics = true };
TextAnalyticsClient client = GetClient();
RecognizeLinkedEntitiesResultCollection results = await client.RecognizeLinkedEntitiesBatchAsync(s_batchConvenienceDocuments, "en", options);

var expectedOutput = new Dictionary<string, List<string>>()
{
{ "0", s_document1ExpectedOutput },
{ "1", s_document2ExpectedOutput },
};

ValidateBatchDocumentsResult(results, expectedOutput, includeStatistics: true);

// Assert the options classes since overloads were added and the original now instantiates a RecognizeLinkedEntitiesOptions.
Assert.IsTrue(options.IncludeStatistics);
Assert.IsNull(options.ModelVersion);
Assert.AreEqual(StringIndexType.Utf16CodeUnit, options.StringIndexType);
}

[Test]
Expand All @@ -166,8 +193,9 @@ public async Task RecognizeLinkedEntitiesBatchTest()
[Test]
public async Task RecognizeLinkedEntitiesBatchWithStatisticsTest()
{
TextAnalyticsRequestOptions options = new TextAnalyticsRequestOptions { IncludeStatistics = true };
TextAnalyticsClient client = GetClient();
RecognizeLinkedEntitiesResultCollection results = await client.RecognizeLinkedEntitiesBatchAsync(s_batchDocuments, new TextAnalyticsRequestOptions { IncludeStatistics = true });
RecognizeLinkedEntitiesResultCollection results = await client.RecognizeLinkedEntitiesBatchAsync(s_batchDocuments, options);

var expectedOutput = new Dictionary<string, List<string>>()
{
Expand All @@ -176,6 +204,32 @@ public async Task RecognizeLinkedEntitiesBatchWithStatisticsTest()
};

ValidateBatchDocumentsResult(results, expectedOutput, includeStatistics: true);

// Assert the options classes since overloads were added and the original now instantiates a RecognizeLinkedEntitiesOptions.
Assert.IsTrue(options.IncludeStatistics);
Assert.IsNull(options.ModelVersion);
Assert.AreEqual(StringIndexType.Utf16CodeUnit, options.StringIndexType);
}

[Test]
public async Task RecognizeLinkedEntitiesBatchWithRecognizeLinkedEntitiesOptionsStatisticsTest()
{
RecognizeLinkedEntitiesOptions options = new RecognizeLinkedEntitiesOptions { IncludeStatistics = true };
TextAnalyticsClient client = GetClient();
RecognizeLinkedEntitiesResultCollection results = await client.RecognizeLinkedEntitiesBatchAsync(s_batchDocuments, options);

var expectedOutput = new Dictionary<string, List<string>>()
{
{ "1", s_document1ExpectedOutput },
{ "3", s_document1ExpectedOutput },
};

ValidateBatchDocumentsResult(results, expectedOutput, includeStatistics: true);

// Assert the options classes since overloads were added and the original now instantiates a RecognizeLinkedEntitiesOptions.
Assert.IsTrue(options.IncludeStatistics);
Assert.IsNull(options.ModelVersion);
Assert.AreEqual(StringIndexType.Utf16CodeUnit, options.StringIndexType);
}

[Test]
Expand Down
Loading

0 comments on commit b2a376b

Please sign in to comment.