Skip to content

Commit 81f68f4

Browse files
authored
Merge pull request #67 from FrankieTF/frank-updateSDK
Update TextAnalytics to T2 package.
2 parents e6401f6 + 0b26e61 commit 81f68f4

File tree

6 files changed

+63
-114
lines changed

6 files changed

+63
-114
lines changed

samples/CrawlFeaturizer/ActionFeaturizer/CognitiveTextAnalyticsFeaturizer.cs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
using CrawlFeaturizer.Model;
2-
using CrawlFeaturizer.Util;
1+
using Azure;
2+
using Azure.AI.TextAnalytics;
3+
using CrawlFeaturizer.Model;
34
using System.Collections.Generic;
45
using System.Linq;
56
using System.Threading.Tasks;
@@ -13,11 +14,11 @@ namespace CrawlFeaturizer.ActionFeaturizer
1314
/// </summary>
1415
public class CognitiveTextAnalyticsFeaturizer : IActionFeaturizer
1516
{
16-
private CognitiveTextAnalyzer cognitiveTextAnalyzer = null;
17+
private readonly TextAnalyticsClient textAnalyticsClient = null;
1718

18-
internal CognitiveTextAnalyticsFeaturizer(CognitiveTextAnalyzer cognitiveTextAnalyzer)
19+
internal CognitiveTextAnalyticsFeaturizer(TextAnalyticsClient textAnalyticsClient)
1920
{
20-
this.cognitiveTextAnalyzer = cognitiveTextAnalyzer;
21+
this.textAnalyticsClient = textAnalyticsClient;
2122
}
2223

2324
/// <summary>
@@ -27,21 +28,21 @@ internal CognitiveTextAnalyticsFeaturizer(CognitiveTextAnalyzer cognitiveTextAna
2728
public async Task FeaturizeActionsAsync(IEnumerable<CrawlAction> actions)
2829
{
2930
await Task.WhenAll(actions.Select(async a =>
30-
{
31-
Metadata metadata = a.Metadata.ToObject<Metadata>();
32-
string content = $"{metadata.Title ?? string.Empty} {metadata.Description ?? string.Empty}";
33-
34-
// Get key phrases from the article title and description
35-
IList<string> keyPhrases = await cognitiveTextAnalyzer.GetKeyPhrasesAsync(content);
36-
37-
// Create a dictionary of key phrases (with a constant values) since at this time we do not support list of strings features.
38-
var keyPhrasesWithConstValues = keyPhrases.ToDictionary(x => x, x=>1);
39-
a.Features.Add(new { keyPhrases = keyPhrasesWithConstValues});
40-
41-
// Get sentiment score for the article
42-
double? sentiment = await cognitiveTextAnalyzer.GetSentimentAsync(content);
43-
a.Features.Add(new { sentiment });
44-
}
31+
{
32+
Metadata metadata = a.Metadata.ToObject<Metadata>();
33+
string content = $"{metadata.Title ?? string.Empty} {metadata.Description ?? string.Empty}";
34+
35+
// Get key phrases from the article title and description
36+
Response<KeyPhraseCollection> keyPhrases = await textAnalyticsClient.ExtractKeyPhrasesAsync(content);
37+
38+
// Create a dictionary of key phrases (with a constant values) since at this time we do not support list of strings features.
39+
var keyPhrasesWithConstValues = keyPhrases.Value.ToDictionary(x => x, x => 1);
40+
a.Features.Add(new { keyPhrases = keyPhrasesWithConstValues });
41+
42+
// Get sentiment score for the article
43+
DocumentSentiment sentiment = await textAnalyticsClient.AnalyzeSentimentAsync(content);
44+
a.Features.Add(new { sentiment.ConfidenceScores });
45+
}
4546
));
4647
}
4748

samples/CrawlFeaturizer/ActionProvider/RSSFeedActionProvider.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,24 @@ public class RSSFeedActionProvider : IActionProvider
1919
public async Task<IEnumerable<CrawlAction>> GetActionsAsync(string rssFeedUrl)
2020
{
2121
IEnumerable<RSSParsedElement> rssElements = await rssParser.ParseAsync(rssFeedUrl);
22-
return rssElements.Select(rssElement => ConvertToCrawlAction(rssElement)).Where(ca => ca.Id!=null);
22+
return rssElements.Select(rssElement => ConvertToCrawlAction(rssElement)).Where(ca => ca.Id != null);
2323
}
2424

2525
/// <summary>
2626
/// Creates a <see cref="CrawlAction"/> object by extracting data from the provided RSSParsedElement object.
2727
/// </summary>
2828
private CrawlAction ConvertToCrawlAction(RSSParsedElement rssParsedElement)
2929
{
30+
string Id = null;
31+
if (rssParsedElement.Guid != null)
32+
{
33+
string[] guidIdArray = rssParsedElement.Guid.Split("/");
34+
Id = guidIdArray[guidIdArray.Length - 2];
35+
}
36+
3037
return new CrawlAction
3138
{
32-
Id = rssParsedElement.Guid,
39+
Id = Id,
3340
Features = new List<object> { new { title = rssParsedElement.Title } },
3441
Metadata = JObject.FromObject(new { rssParsedElement.Title, rssParsedElement.Description })
3542
};

samples/CrawlFeaturizer/CrawlFeaturizer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.Azure.CognitiveServices.Language.TextAnalytics" Version="2.8.0-preview" />
9+
<PackageReference Include="Azure.AI.TextAnalytics" Version="5.0.0" />
1010
<PackageReference Include="Microsoft.Azure.CognitiveServices.Personalizer" Version="0.8.0-preview" />
1111
</ItemGroup>
1212

samples/CrawlFeaturizer/Program.cs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Net.Http;
1+
using Azure;
2+
using Azure.AI.TextAnalytics;
53
using CrawlFeaturizer.ActionFeaturizer;
64
using CrawlFeaturizer.ActionProvider;
75
using CrawlFeaturizer.Model;
86
using CrawlFeaturizer.Util;
9-
using Microsoft.Azure.CognitiveServices.Language.TextAnalytics;
107
using Microsoft.Azure.CognitiveServices.Personalizer;
118
using Microsoft.Azure.CognitiveServices.Personalizer.Models;
12-
using Newtonsoft.Json;
9+
using System.Text.Json;
10+
using System;
11+
using System.Collections.Generic;
12+
using System.Linq;
1313

1414
namespace CrawlFeaturizer
1515
{
@@ -21,24 +21,24 @@ internal class Program
2121
// The endpoint specific to your personalization service instance; e.g. https://westus2.api.cognitive.microsoft.com/
2222
private const string ServiceEndpoint = "";
2323

24-
// Cognitive Service Endpoint
25-
private const string cognitiveTextAnalyticsEndpoint = "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0";
24+
// Cognitive Service TextAnalytics Endpoint
25+
private const string CognitiveTextAnalyticsEndpoint = "";
2626

27-
// Subscription Key for the Cognitive Service for Text Analytics e.g 1325eb544363468ba3de1f25e2e11246
28-
private const string cognitiveTextAnalyticsSubscriptionKey = "";
27+
// API Key for the Cognitive Service for Text Analytics. See this Azure CLI Link to get the key: https://docs.microsoft.com/en-us/cli/azure/cognitiveservices/account/keys?view=azure-cli-latest#az-cognitiveservices-account-keys-list-examples
28+
private const string CognitiveTextAnalyticsAPIKey = "";
2929

3030
/// <summary>
3131
/// RSS feeds for different news topics
3232
/// </summary>
3333
private static readonly Dictionary<string, string> newsRSSFeeds = new Dictionary<string, string>()
34-
{
35-
{ "World" , "http://rss.cnn.com/rss/cnn_world.rss" },
36-
{ "Business", "http://rss.cnn.com/rss/money_latest.rss"},
37-
{ "Technology", "http://rss.cnn.com/rss/cnn_tech.rss"},
38-
{ "Health", "http://rss.cnn.com/rss/cnn_health.rss"},
39-
{ "Entertainment", "http://rss.cnn.com/rss/cnn_showbiz.rss"},
40-
{ "Travel", "http://rss.cnn.com/rss/cnn_travel.rss"}
41-
};
34+
{
35+
{ "World" , "http://rss.cnn.com/rss/cnn_world.rss" },
36+
{ "Business", "http://rss.cnn.com/rss/money_latest.rss"},
37+
{ "Technology", "http://rss.cnn.com/rss/cnn_tech.rss"},
38+
{ "Health", "http://rss.cnn.com/rss/cnn_health.rss"},
39+
{ "Entertainment", "http://rss.cnn.com/rss/cnn_showbiz.rss"},
40+
{ "Travel", "http://rss.cnn.com/rss/cnn_travel.rss"}
41+
};
4242

4343
private static void Main(string[] args)
4444
{
@@ -56,13 +56,10 @@ private static void Main(string[] args)
5656
});
5757

5858
// Initialize the Cognitive Services TextAnalyticsClient for featurizing the crawled action articles
59-
ITextAnalyticsClient textAnalyticsClient = new TextAnalyticsClient(new ApiKeyServiceClientCredentials(cognitiveTextAnalyticsSubscriptionKey))
60-
{
61-
// Cognitive Service Endpoint
62-
Endpoint = cognitiveTextAnalyticsEndpoint.Split("/text/analytics")[0]
63-
};
59+
TextAnalyticsClient textAnalyticsClient = new TextAnalyticsClient(new Uri(CognitiveTextAnalyticsEndpoint), new AzureKeyCredential(CognitiveTextAnalyticsAPIKey));
60+
6461
// Initialize the Cognitive Text Analytics actions featurizer
65-
IActionFeaturizer actionFeaturizer = new CognitiveTextAnalyticsFeaturizer(new CognitiveTextAnalyzer(textAnalyticsClient));
62+
IActionFeaturizer actionFeaturizer = new CognitiveTextAnalyticsFeaturizer(textAnalyticsClient);
6663

6764
var newsActions = new List<RankableAction>();
6865

@@ -73,7 +70,7 @@ private static void Main(string[] args)
7370
IList<CrawlAction> crawlActions = actionProvider.GetActionsAsync(newsTopic.Value).Result.ToList();
7471
Console.WriteLine($"Fetched {crawlActions.Count} actions");
7572

76-
actionFeaturizer.FeaturizeActionsAsync(crawlActions).Wait(10000);
73+
actionFeaturizer.FeaturizeActionsAsync(crawlActions).ConfigureAwait(false);
7774
Console.WriteLine($"Featurized actions for {newsTopic.Key}");
7875

7976
// Generate a rankable action for each crawlAction and add the news topic as additional feature
@@ -111,7 +108,13 @@ private static void Main(string[] args)
111108

112109
Console.WriteLine("Personalization service thinks you would like to read: ");
113110
Console.WriteLine("Id: " + recommendedAction.Id);
114-
Console.WriteLine("Features : " + JsonConvert.SerializeObject(recommendedAction.Features, Formatting.Indented));
111+
112+
JsonSerializerOptions options = new JsonSerializerOptions
113+
{
114+
WriteIndented = true
115+
};
116+
Console.WriteLine("Features : " + JsonSerializer.Serialize(recommendedAction.Features, options));
117+
115118
Console.WriteLine("Do you like this article ?(y/n)");
116119

117120
float reward = 0.0f;
@@ -211,7 +214,7 @@ private static string GetLocation()
211214

212215
private static string GetKey()
213216
{
214-
string key = Console.ReadKey().Key.ToString().Last().ToString().ToUpper();
217+
string key = Console.ReadKey().Key.ToString().Last().ToString().ToUpper();
215218
Console.WriteLine();
216219
return key;
217220
}

samples/CrawlFeaturizer/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ git clone https://github.com/Azure/personalization-rl.git
2727

2828
- Replace `cognitiveTextAnalyticsEndpoint` variable's value in `Programs.cs` with one of the `Endpoints`
2929

30-
- Replace `cognitiveTextAnalyticsSubscriptionKey` variable's value in `Programs.cs` with one of the `Keys`
30+
- Replace `CognitiveTextAnalyticsAPIKey ` variable's value in `Programs.cs` with one of the `Keys`
3131

3232

3333
## Visual Studio
3434
- Set `CrawlFeaturizer` as the Start Up project in Visual Studio
3535

3636
- Run the project (press `F5` key)
3737

38-
## Crawl Pipeline
38+
## Crawl Pipeline
3939
The Crawl pipeline consists of 2 stages
4040
1. Crawl a feed url and get all the items listed in the feed. These items are the `Actions` that will be ranked by Personalization API. This is exposed through the `IActionsProvider` interface.
4141
2. Each `Action` is decorated with `Features` by using some `ActionFeaturizer` e.g Cognitive Services Text Analytics, Cognitive Services Vision. This functionality is exposed through the `IActionFeaturizer` interface. Once we have a set of actions with features, those actions can be ranked using the Personalization API.

samples/CrawlFeaturizer/Util/CognitiveTextAnalyzer.cs

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)