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] Add flags enum for Opinion mining #14624

Merged
merged 3 commits into from
Sep 1, 2020
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
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
namespace Azure.AI.TextAnalytics
{
[System.FlagsAttribute]
public enum AdditionalSentimentAnalyses
maririos marked this conversation as resolved.
Show resolved Hide resolved
{
None = 0,
OpinionMining = 1,
}
public partial class AnalyzeSentimentOptions : Azure.AI.TextAnalytics.TextAnalyticsRequestOptions
{
public AnalyzeSentimentOptions() { }
public bool IncludeOpinionMining { get { throw null; } set { } }
public Azure.AI.TextAnalytics.AdditionalSentimentAnalyses AdditionalSentimentAnalyses { get { throw null; } set { } }
}
public partial class AnalyzeSentimentResult : Azure.AI.TextAnalytics.TextAnalyticsResult
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
 // Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;

namespace Azure.AI.TextAnalytics
{
/// <summary>
/// Additional types of Sentiment Analysis to be applied to the
/// AnalyzeSentiment method, like for example Opinion Mining.
/// </summary>
[Flags]
public enum AdditionalSentimentAnalyses
{
/// <summary>
/// Use standard sentiment analysis for documents and its sentences.
/// </summary>
None = 0,

/// <summary>
/// Sentiment analysis results with Opinion Mining,
/// also known as Aspect-based Sentiment Analysis.
/// Only available for Text Analytics Service version v3.1-preview.1 and above.
/// </summary>
OpinionMining = 1,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ internal AnalyzeSentimentOptions(TextAnalyticsRequestOptions options)
}

/// <summary>
/// If set to true, response will contain Opinion Mining sentiment analysis results.
/// Only available for Text Analytics Service version v3.1-preview.1 and above.
/// Additional types of Sentiment Analysis to be applied to the
/// AnalyzeSentiment method, like for example Opinion Mining.
/// </summary>
public bool IncludeOpinionMining { get; set; } = false;
}
public AdditionalSentimentAnalyses AdditionalSentimentAnalyses { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal SentenceSentiment(SentenceSentimentInternal sentenceSentiment, IReadOnl

/// <summary>
/// Gets the mined opinions of a sentence. This is only returned if
/// <see cref="AnalyzeSentimentOptions.IncludeOpinionMining"/> is set to true.
/// <see cref="AdditionalSentimentAnalyses.OpinionMining"/> is set in <see cref="AnalyzeSentimentOptions.AdditionalSentimentAnalyses"/>.
/// </summary>
public IReadOnlyCollection<MinedOpinion> MinedOpinions { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -788,11 +788,12 @@ public virtual async Task<Response<DocumentSentiment>> AnalyzeSentimentAsync(str
try
{
var documents = new List<MultiLanguageInput>() { ConvertToMultiLanguageInput(document, language) };
bool opinionMining = options.AdditionalSentimentAnalyses.HasFlag(AdditionalSentimentAnalyses.OpinionMining);
Response<SentimentResponse> result = await _serviceRestClient.SentimentAsync(
new MultiLanguageBatchInput(documents),
options.ModelVersion,
options.IncludeStatistics,
options.IncludeOpinionMining,
opinionMining,
_stringCodeUnit,
cancellationToken).ConfigureAwait(false);
Response response = result.GetRawResponse();
Expand Down Expand Up @@ -848,11 +849,12 @@ public virtual Response<DocumentSentiment> AnalyzeSentiment(string document, str
try
{
var documents = new List<MultiLanguageInput>() { ConvertToMultiLanguageInput(document, language) };
bool opinionMining = options.AdditionalSentimentAnalyses.HasFlag(AdditionalSentimentAnalyses.OpinionMining);
Response<SentimentResponse> result = _serviceRestClient.Sentiment(
new MultiLanguageBatchInput(documents),
options.ModelVersion,
options.IncludeStatistics,
options.IncludeOpinionMining,
opinionMining,
_stringCodeUnit,
cancellationToken);
Response response = result.GetRawResponse();
Expand Down Expand Up @@ -1126,11 +1128,12 @@ private async Task<Response<AnalyzeSentimentResultCollection>> AnalyzeSentimentB

try
{
bool opinionMining = options.AdditionalSentimentAnalyses.HasFlag(AdditionalSentimentAnalyses.OpinionMining);
Response<SentimentResponse> result = await _serviceRestClient.SentimentAsync(
batchInput,
options.ModelVersion,
options.IncludeStatistics,
options.IncludeOpinionMining,
opinionMining,
_stringCodeUnit,
cancellationToken).ConfigureAwait(false);
var response = result.GetRawResponse();
Expand All @@ -1153,10 +1156,12 @@ private Response<AnalyzeSentimentResultCollection> AnalyzeSentimentBatch(MultiLa

try
{
Response<SentimentResponse> result = _serviceRestClient.Sentiment(batchInput,
bool opinionMining = options.AdditionalSentimentAnalyses.HasFlag(AdditionalSentimentAnalyses.OpinionMining);
Response<SentimentResponse> result = _serviceRestClient.Sentiment(
batchInput,
options.ModelVersion,
options.IncludeStatistics,
options.IncludeOpinionMining,
opinionMining,
_stringCodeUnit,
cancellationToken);
var response = result.GetRawResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public async Task AnalyzeSentimentWithOpinionMining()
TextAnalyticsClient client = GetClient();
string document = "The park was clean and pretty. The bathrooms and restaurant were not clean.";

DocumentSentiment docSentiment = await client.AnalyzeSentimentAsync(document, options: new AnalyzeSentimentOptions() { IncludeOpinionMining = true });
DocumentSentiment docSentiment = await client.AnalyzeSentimentAsync(document, options: new AnalyzeSentimentOptions() { AdditionalSentimentAnalyses = AdditionalSentimentAnalyses.OpinionMining });

CheckAnalyzeSentimentProperties(docSentiment, opinionMining: true);
Assert.AreEqual("Mixed", docSentiment.Sentiment.ToString());
Expand All @@ -77,7 +77,7 @@ public async Task AnalyzeSentimentWithOpinionMiningEmpty()
TextAnalyticsClient client = GetClient();
string document = singleEnglish;

DocumentSentiment docSentiment = await client.AnalyzeSentimentAsync(document, "en", new AnalyzeSentimentOptions() { IncludeOpinionMining = true });
DocumentSentiment docSentiment = await client.AnalyzeSentimentAsync(document, "en", new AnalyzeSentimentOptions() { AdditionalSentimentAnalyses = AdditionalSentimentAnalyses.OpinionMining });

CheckAnalyzeSentimentProperties(docSentiment);
Assert.AreEqual("Positive", docSentiment.Sentiment.ToString());
Expand All @@ -89,7 +89,7 @@ public async Task AnalyzeSentimentWithOpinionMiningNegated()
TextAnalyticsClient client = GetClient();
string document = "The bathrooms are not clean.";

DocumentSentiment docSentiment = await client.AnalyzeSentimentAsync(document, options: new AnalyzeSentimentOptions() { IncludeOpinionMining = true });
DocumentSentiment docSentiment = await client.AnalyzeSentimentAsync(document, options: new AnalyzeSentimentOptions() { AdditionalSentimentAnalyses = AdditionalSentimentAnalyses.OpinionMining });

CheckAnalyzeSentimentProperties(docSentiment, opinionMining: true);
MinedOpinion minedOpinion = docSentiment.Sentences.FirstOrDefault().MinedOpinions.FirstOrDefault();
Expand Down Expand Up @@ -151,7 +151,7 @@ public async Task AnalyzeSentimentBatchConvenienceWithOpinionMiningTest()
"The food and service is not good."
};

AnalyzeSentimentResultCollection results = await client.AnalyzeSentimentBatchAsync(documents, options: new AnalyzeSentimentOptions() { IncludeOpinionMining = true });
AnalyzeSentimentResultCollection results = await client.AnalyzeSentimentBatchAsync(documents, options: new AnalyzeSentimentOptions() { AdditionalSentimentAnalyses = AdditionalSentimentAnalyses.OpinionMining });

foreach (AnalyzeSentimentResult docs in results)
{
Expand Down Expand Up @@ -334,7 +334,7 @@ public async Task AnalyzeSentimentBatchWithOpinionMiningTest()
}
};

AnalyzeSentimentResultCollection results = await client.AnalyzeSentimentBatchAsync(documents, options: new AnalyzeSentimentOptions() { IncludeOpinionMining = true });
AnalyzeSentimentResultCollection results = await client.AnalyzeSentimentBatchAsync(documents, options: new AnalyzeSentimentOptions() { AdditionalSentimentAnalyses = AdditionalSentimentAnalyses.OpinionMining });

foreach (AnalyzeSentimentResult docs in results)
{
Expand Down