Skip to content

Commit cd74dde

Browse files
committed
samples, readme + fix version
1 parent 01fcdbe commit cd74dde

13 files changed

+549
-13
lines changed

sdk/textanalytics/Azure.AI.TextAnalytics/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Azure Cognitive Services Text Analytics is a cloud service that provides advance
44
* Sentiment Analysis
55
* Key Phrase Extraction
66
* Named Entity Recognition
7+
* Personally Identifiable Information (PII) Recognition
78
* Linked Entity Recognition
89

910
[Source code][textanalytics_client_src] | [Package (NuGet)][textanalytics_nuget_package] | [API reference documentation][textanalytics_refdocs] | [Product documentation][textanalytics_docs] | [Samples][textanalytics_samples]
@@ -124,6 +125,7 @@ The following section provides several code snippets using the `client` [created
124125
* [Analyze Sentiment](#analyze-sentiment)
125126
* [Extract Key Phrases](#extract-key-phrases)
126127
* [Recognize Entities](#recognize-entities)
128+
* [Recognize PII Entities](#recognize-pii-entities)
127129
* [Recognize Linked Entities](#recognize-linked-entities)
128130

129131
### Async examples
@@ -159,6 +161,8 @@ Console.WriteLine($" Negative confidence score: {docSentiment.ConfidenceScore
159161
```
160162
For samples on using the production recommended option `AnalyzeSentimentBatch` see [here][analyze_sentiment_sample].
161163

164+
To get more granular information about the opinions related to aspects of a product/service, also knows as Aspect-based Sentiment Analysis in Natural Language Processing (NLP), see sample on sentiment analysis with opinion mining [here][analyze_sentiment_opinion_mining_sample].
165+
162166
Please refer to the service documentation for a conceptual discussion of [sentiment analysis][sentiment_analysis].
163167

164168
### Extract Key Phrases
@@ -198,6 +202,26 @@ For samples on using the production recommended option `RecognizeEntitiesBatch`
198202

199203
Please refer to the service documentation for a conceptual discussion of [named entity recognition][named_entity_recognition].
200204

205+
### Recognize PII Entities
206+
Run a predictive model to identify a collection of entities containing Personally Identifiable Information found in the passed-in document or batch of documents, and categorize those entities into categories such as US social security number, drivers license number, or credit card number.
207+
208+
```C# Snippet:RecognizePiiEntities
209+
string document = "A developer with SSN 859-98-0987 whose phone number is 800-102-1100 is building tools with our APIs.";
210+
211+
PiiEntityCollection entities = client.RecognizePiiEntities(document).Value;
212+
213+
Console.WriteLine($"Redacted Text: {entities.RedactedText}");
214+
Console.WriteLine($"Recognized {entities.Count} PII entit{(entities.Count > 1 ? "ies" : "y")}:");
215+
foreach (PiiEntity entity in entities)
216+
{
217+
Console.WriteLine($"Text: {entity.Text}, Category: {entity.Category}, SubCategory: {entity.SubCategory}, Confidence score: {entity.ConfidenceScore}");
218+
}
219+
```
220+
221+
For samples on using the production recommended option `RecognizePiiEntitiesBatch` see [here][recognize_pii_entities_sample].
222+
223+
Please refer to the service documentation for supported [PII entity types][pii_entity_type].
224+
201225
### Recognize Linked Entities
202226
Run a predictive model to identify a collection of entities found in the passed-in document or batch of documents, and include information linking the entities to their corresponding entries in a well-known knowledge base.
203227

@@ -307,7 +331,11 @@ Samples are provided for each main functional area, and for each area, samples a
307331
- [Analyze Sentiment][analyze_sentiment_sample]
308332
- [Extract Key Phrases][extract_key_phrases_sample]
309333
- [Recognize Entities][recognize_entities_sample]
334+
- [Recognize PII Entities][recognize_pii_entities_sample]
310335
- [Recognize Linked Entities][recognize_linked_entities_sample]
336+
337+
### Advanced samples
338+
- [Analyze Sentiment with Opinion Mining][analyze_sentiment_opinion_mining_sample]
311339
- [Create a mock client][mock_client_sample] for testing using the [Moq][moq] library.
312340

313341
## Contributing
@@ -337,6 +365,7 @@ This project has adopted the [Microsoft Open Source Code of Conduct][code_of_con
337365
[key_phrase_extraction]: https://docs.microsoft.com/azure/cognitive-services/Text-Analytics/how-tos/text-analytics-how-to-keyword-extraction
338366
[named_entity_recognition]: https://docs.microsoft.com/azure/cognitive-services/Text-Analytics/how-tos/text-analytics-how-to-entity-linking
339367
[named_entities_categories]: https://docs.microsoft.com/azure/cognitive-services/Text-Analytics/named-entity-types
368+
[pii_entity_type]:https://docs.microsoft.com/azure/cognitive-services/text-analytics/named-entity-types?tabs=personal
340369

341370
[textanalytics_client_class]: https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/textanalytics/Azure.AI.TextAnalytics/src/TextAnalyticsClient.cs
342371
[azure_identity]: https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/identity/Azure.Identity
@@ -351,8 +380,12 @@ This project has adopted the [Microsoft Open Source Code of Conduct][code_of_con
351380

352381
[detect_language_sample]: https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/textanalytics/Azure.AI.TextAnalytics/samples/Sample1_DetectLanguage.md
353382
[analyze_sentiment_sample]: https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/textanalytics/Azure.AI.TextAnalytics/samples/Sample2_AnalyzeSentiment.md
383+
[analyze_sentiment_opinion_mining_sample]: https://github.com/maririos/azure-sdk-for-net/blob/f781a29042bb26e3d9f28b66dd78ed9e8487c434/sdk/textanalytics/Azure.AI.TextAnalytics/samples/Sample2.1_AnalyzeSentimentWithOpinionMining.md
384+
<!--[analyze_sentiment_opinion_mining_sample]: https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/textanalytics/Azure.AI.TextAnalytics/samples/Sample2.1_AnalyzeSentimentWithOpinionMining.md-->
354385
[extract_key_phrases_sample]: https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/textanalytics/Azure.AI.TextAnalytics/samples/Sample3_ExtractKeyPhrases.md
355386
[recognize_entities_sample]: https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/textanalytics/Azure.AI.TextAnalytics/samples/Sample4_RecognizeEntities.md
387+
[recognize_pii_entities_sample]: https://github.com/Azure/azure-sdk-for-net/tree/a0b01ea1678646c93385d0c675885c9f6177c561/sdk/textanalytics/Azure.AI.TextAnalytics/samples/Sample5_RecognizePiiEntities.md
388+
<!--[recognize_pii_entities_sample]: https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/textanalytics/Azure.AI.TextAnalytics/samples/Sample5_RecognizePiiEntities.md-->
356389
[recognize_linked_entities_sample]: https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/textanalytics/Azure.AI.TextAnalytics/samples/Sample6_RecognizeLinkedEntities.md
357390
[mock_client_sample]: https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/textanalytics/Azure.AI.TextAnalytics/samples/Sample_MockClient.md
358391

sdk/textanalytics/Azure.AI.TextAnalytics/api/Azure.AI.TextAnalytics.netstandard2.0.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,13 @@ public TextAnalyticsClient(System.Uri endpoint, Azure.Core.TokenCredential crede
322322
}
323323
public partial class TextAnalyticsClientOptions : Azure.Core.ClientOptions
324324
{
325-
public TextAnalyticsClientOptions(Azure.AI.TextAnalytics.TextAnalyticsClientOptions.ServiceVersion version = Azure.AI.TextAnalytics.TextAnalyticsClientOptions.ServiceVersion.V3_1_Preview_1) { }
325+
public TextAnalyticsClientOptions(Azure.AI.TextAnalytics.TextAnalyticsClientOptions.ServiceVersion version = Azure.AI.TextAnalytics.TextAnalyticsClientOptions.ServiceVersion.V3_1_Preview_2) { }
326326
public string DefaultCountryHint { get { throw null; } set { } }
327327
public string DefaultLanguage { get { throw null; } set { } }
328328
public enum ServiceVersion
329329
{
330330
V3_0 = 1,
331-
V3_1_Preview_1 = 2,
331+
V3_1_Preview_2 = 2,
332332
}
333333
}
334334
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]

sdk/textanalytics/Azure.AI.TextAnalytics/samples/README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,21 @@ Azure Cognitive Services Text Analytics is a cloud service that provides advance
1616
* Sentiment Analysis
1717
* Key Phrase Extraction
1818
* Named Entity Recognition
19+
* Personally Identifiable Information (PII) Recognition
1920
* Linked Entity Recognition
2021

21-
You can find samples for each of this main functions below, as well as a sample on how to create a mock client for testing purposes.
22-
To get started you'll need a Text Analytics endpoint and credentials. See Text Analytics Client Library [Readme][README] for more information and instructions.
23-
22+
## Common scenarios samples
2423
- [Detect Language](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/textanalytics/Azure.AI.TextAnalytics/samples/Sample1_DetectLanguage.md)
2524
- [Analyze Sentiment](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/textanalytics/Azure.AI.TextAnalytics/samples/Sample2_AnalyzeSentiment.md)
2625
- [Extract Key Phrases](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/textanalytics/Azure.AI.TextAnalytics/samples/Sample3_ExtractKeyPhrases.md)
2726
- [Recognize Entities](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/textanalytics/Azure.AI.TextAnalytics/samples/Sample4_RecognizeEntities.md)
27+
- [Recognize PII Entities](https://github.com/Azure/azure-sdk-for-net/tree/a0b01ea1678646c93385d0c675885c9f6177c561/sdk/textanalytics/Azure.AI.TextAnalytics/samples/Sample5_RecognizePiiEntities.md)
28+
<!--- [Recognize PII Entities](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/textanalytics/Azure.AI.TextAnalytics/samples/Sample5_RecognizePiiEntities.md)-->
2829
- [Recognize Linked Entities](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/textanalytics/Azure.AI.TextAnalytics/samples/Sample6_RecognizeLinkedEntities.md)
30+
31+
## Advanced samples
32+
- [Analyze Sentiment with Opinion Mining](https://github.com/maririos/azure-sdk-for-net/blob/f781a29042bb26e3d9f28b66dd78ed9e8487c434/sdk/textanalytics/Azure.AI.TextAnalytics/samples/Sample2.1_AnalyzeSentimentWithOpinionMining.md)
33+
<!--- [Analyze Sentiment with Opinion Mining](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/textanalytics/Azure.AI.TextAnalytics/samples/Sample2.1_AnalyzeSentimentWithOpinionMining.md)-->
2934
- [Mock client](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/textanalytics/Azure.AI.TextAnalytics/samples/Sample_MockClient.md)
3035

3136
[README]: https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/textanalytics/Azure.AI.TextAnalytics/README.md
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Analyze sentiment with Opinion Mining
2+
3+
This sample demonstrates how to analyze sentiment of documents and get more granular information about the opinions related to aspects of a product/service, also knows as Aspect-based Sentiment Analysis in Natural Language Processing (NLP). This feature is only available for clients with api version v3.1-preview.1 and higher.
4+
5+
For the purpose of the sample, we will be the administrator of a hotel and we've set a system to look at the online reviews customers are posting to identify the major complaints about our hotel.
6+
In order to do so, we will use the Sentiment Analysis feature of the Text Analytics client library. To get started you'll need a Text Analytics endpoint and credentials. See [README][README] for links and instructions.
7+
8+
## Creating a `TextAnalyticsClient`
9+
10+
To create a new `TextAnalyticsClient`, you need a Text Analytics endpoint and credentials. You can use the [DefaultAzureCredential][DefaultAzureCredential] to try a number of common authentication methods optimized for both running as a service and development. In the sample below, however, you'll use a Text Analytics API key credential by creating an `AzureKeyCredential` object, that if needed, will allow you to update the API key without creating a new client.
11+
12+
You can set `endpoint` and `apiKey` based on an environment variable, a configuration setting, or any way that works for your application.
13+
14+
```C# Snippet:TextAnalyticsSample1CreateClient
15+
var client = new TextAnalyticsClient(new Uri(endpoint), new AzureKeyCredential(apiKey));
16+
```
17+
18+
## Identify complaints
19+
20+
To get a deeper analysis into which are the aspects that people considered good or bad, we will need to include the `AdditionalSentimentAnalyses.OpinionMining` type into the `AnalyzeSentimentOptions`.
21+
22+
```C# Snippet:TAAnalyzeSentimentWithOpinionMining
23+
var documents = new List<string>
24+
{
25+
"The food and service were unacceptable, but the concierge were nice.",
26+
"The rooms were beautiful. The AC was good and quiet.",
27+
"The breakfast was good, but the toilet was smelly.",
28+
"Loved this hotel - good breakfast - nice shuttle service - clean rooms.",
29+
"I had a great unobstructed view of the Microsoft campus.",
30+
"Nice rooms but bathrooms were old and the toilet was dirty when we arrived.",
31+
"We changed rooms as the toilet smelled."
32+
};
33+
34+
AnalyzeSentimentResultCollection reviews = client.AnalyzeSentimentBatch(documents, options: new AnalyzeSentimentOptions() { AdditionalSentimentAnalyses = AdditionalSentimentAnalyses.OpinionMining });
35+
36+
Dictionary<string, int> complaints = GetComplaints(reviews);
37+
38+
var negativeAspect = complaints.Aggregate((l, r) => l.Value > r.Value ? l : r).Key;
39+
Console.WriteLine($"Alert! major complaint is *{negativeAspect}*");
40+
Console.WriteLine();
41+
Console.WriteLine("---All complaints:");
42+
foreach (KeyValuePair<string, int> complaint in complaints)
43+
{
44+
Console.WriteLine($" {complaint.Key}, {complaint.Value}");
45+
}
46+
```
47+
48+
Output:
49+
```
50+
Alert! major complaint is *toilet*
51+
52+
---All complaints:
53+
food, 1
54+
service, 1
55+
toilet, 3
56+
bathrooms, 1
57+
rooms, 1
58+
```
59+
60+
## Define method `GetComplaints`
61+
Implementation for calculating complaints:
62+
63+
```C# Snippet:TAGetComplaints
64+
private Dictionary<string, int> GetComplaints(AnalyzeSentimentResultCollection reviews)
65+
{
66+
var complaints = new Dictionary<string, int>();
67+
foreach (AnalyzeSentimentResult review in reviews)
68+
{
69+
foreach (SentenceSentiment sentence in review.DocumentSentiment.Sentences)
70+
{
71+
foreach (MinedOpinion minedOpinion in sentence.MinedOpinions)
72+
{
73+
if (minedOpinion.Aspect.Sentiment == TextSentiment.Negative)
74+
{
75+
complaints.TryGetValue(minedOpinion.Aspect.Text, out var value);
76+
complaints[minedOpinion.Aspect.Text] = value + 1;
77+
}
78+
}
79+
}
80+
}
81+
return complaints;
82+
}
83+
```
84+
85+
86+
To see the full example source files, see:
87+
* [Synchronous Analyze Sentiment with Opinion Mining](https://github.com/maririos/azure-sdk-for-net/blob/f781a29042bb26e3d9f28b66dd78ed9e8487c434/sdk/textanalytics/Azure.AI.TextAnalytics/tests/samples/Sample2.1_AnalyzeSentimentWithOpinionMining.cs)
88+
* [Asynchronous Analyze Sentiment with Opinion Mining](https://github.com/maririos/azure-sdk-for-net/blob/f781a29042bb26e3d9f28b66dd78ed9e8487c434/sdk/textanalytics/Azure.AI.TextAnalytics/tests/samples/Sample2.1_AnalyzeSentimentWithOpinionMiningAsync.cs)
89+
<!--* [Synchronous Analyze Sentiment with Opinion Mining](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/textanalytics/Azure.AI.TextAnalytics//tests/samples/Sample2.1_AnalyzeSentimentWithOpinionMining.cs)
90+
* [Asynchronous Analyze Sentiment with Opinion Mining](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/textanalytics/Azure.AI.TextAnalytics//tests/samples/Sample2.1_AnalyzeSentimentWithOpinionMiningAsync.cs)-->
91+
92+
[DefaultAzureCredential]: https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/identity/Azure.Identity/README.md
93+
[README]: https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/textanalytics/Azure.AI.TextAnalytics/README.md

sdk/textanalytics/Azure.AI.TextAnalytics/samples/Sample2_AnalyzeSentiment.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Console.WriteLine($" Neutral confidence score: {docSentiment.ConfidenceScores
2727
Console.WriteLine($" Negative confidence score: {docSentiment.ConfidenceScores.Negative}.");
2828
```
2929

30-
## Analyzing the sentiment of multipile documents
30+
## Analyzing the sentiment of multiple documents
3131

3232
To analyze the sentiment of a collection of documents in the same language, call `AnalyzeSentimentBatch` on an `IEnumerable` of strings. The results are returned as a `AnalyzeSentimentResultCollection`.
3333

0 commit comments

Comments
 (0)