Skip to content

Commit 164d180

Browse files
authored
Merge pull request #39 from aahill/ta-sentiment-3-0
Code additions
2 parents 6b7af2e + e806b31 commit 164d180

File tree

2 files changed

+245
-3
lines changed

2 files changed

+245
-3
lines changed

Tutorials/Bing-Visual-Search/BingVisualSearchInsightsTokens.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ static void Main(string[] args)
1313
{
1414
String subscriptionKey = "YOUR-ACCESS-KEY";
1515

16-
insightsToken = ImageResults(subscriptionKey);
16+
var insightsToken = ImageResults(subscriptionKey);
1717

1818
VisualSearchInsightsToken(subscriptionKey, insightsToken);
1919

@@ -29,7 +29,7 @@ private static String ImageResults(String subKey)
2929
String insightTok = "None";
3030
try
3131
{
32-
var client = new ImageSearchAPI(new Microsoft.Azure.CognitiveServices.Search.ImageSearch.ApiKeyServiceClientCredentials(subKey)); //
32+
var client = new ImageSearchClient(new Microsoft.Azure.CognitiveServices.Search.ImageSearch.ApiKeyServiceClientCredentials(subKey)); //
3333
var imageResults = client.Images.SearchAsync(query: "canadian rockies").Result;
3434
Console.WriteLine("Search images for query \"canadian rockies\"");
3535

@@ -74,7 +74,7 @@ private static String ImageResults(String subKey)
7474

7575
public static void VisualSearchInsightsToken(string subscriptionKey, string insightsTok)
7676
{
77-
var client = new VisualSearchAPI(new Microsoft.Azure.CognitiveServices.Search.VisualSearch.ApiKeyServiceClientCredentials(subscriptionKey));
77+
var client = new VisualSearchClient(new Microsoft.Azure.CognitiveServices.Search.VisualSearch.ApiKeyServiceClientCredentials(subscriptionKey));
7878

7979
try
8080
{

dotnet/Language/SentimentV3.cs

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Net;
4+
using System.Net.Http;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Newtonsoft.Json;
8+
9+
namespace SampleSentimentV3
10+
{
11+
public class Program
12+
{
13+
public class TextAnalyticsSentimentV3Client
14+
{
15+
private static readonly string textAnalyticsUrl = "<ADD_TEXT_ANALYTICS_URL_HERE>/v3.0-preview/sentiment";
16+
17+
private static readonly string textAnalyticsKey = "<ADD_TEXT_ANALYTICS_KEY_HERE>";
18+
19+
public static async Task<SentimentV3Response> SentimentV3PreviewPredictAsync(TextAnalyticsBatchInput inputDocuments)
20+
{
21+
using (var httpClient = new HttpClient())
22+
{
23+
httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", textAnalyticsKey);
24+
25+
var httpContent = new StringContent(JsonConvert.SerializeObject(inputDocuments), Encoding.UTF8, "application/json");
26+
27+
var httpResponse = await httpClient.PostAsync(new Uri(textAnalyticsUrl), httpContent);
28+
var responseContent = await httpResponse.Content.ReadAsStringAsync();
29+
30+
if (!httpResponse.StatusCode.Equals(HttpStatusCode.OK) || httpResponse.Content == null)
31+
{
32+
throw new Exception(responseContent);
33+
}
34+
35+
return JsonConvert.DeserializeObject<SentimentV3Response>(responseContent, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });
36+
}
37+
}
38+
}
39+
40+
public static void Main(string[] args)
41+
{
42+
var inputDocuments = new TextAnalyticsBatchInput()
43+
{
44+
Documents = new List<TextAnalyticsInput>()
45+
{
46+
new TextAnalyticsInput()
47+
{
48+
Id = "1",
49+
50+
Text = "Hello world. This is some input text that I love."
51+
},
52+
53+
new TextAnalyticsInput()
54+
{
55+
Id = "2",
56+
57+
Text = "It's incredibly sunny outside! I'm so happy."
58+
},
59+
60+
new TextAnalyticsInput()
61+
{
62+
Id = "3",
63+
64+
Text = "Pike place market is my favorite Seattle attraction."
65+
}
66+
}
67+
};
68+
//If you’re using C# 7.1 or greater, you can use an async main() method to await the function
69+
var sentimentV3Prediction = TextAnalyticsSentimentV3Client.SentimentV3PreviewPredictAsync(inputDocuments).Result;
70+
71+
// Replace with whatever you wish to print or simply consume the sentiment v3 prediction
72+
Console.WriteLine("Document ID=" + sentimentV3Prediction.Documents[0].Id + " : Sentiment=" + sentimentV3Prediction.Documents[0].Sentiment);
73+
Console.WriteLine("Document ID=" + sentimentV3Prediction.Documents[1].Id + " : Sentiment=" + sentimentV3Prediction.Documents[1].Sentiment);
74+
Console.WriteLine("Document ID=" + sentimentV3Prediction.Documents[2].Id + " : Sentiment=" + sentimentV3Prediction.Documents[2].Sentiment);
75+
Console.ReadKey();
76+
}
77+
78+
public class SentimentV3Response
79+
{
80+
public IList<DocumentSentiment> Documents { get; set; }
81+
82+
public IList<ErrorRecord> Errors { get; set; }
83+
84+
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
85+
public RequestStatistics Statistics { get; set; }
86+
}
87+
88+
public class TextAnalyticsBatchInput
89+
{
90+
public IList<TextAnalyticsInput> Documents { get; set; }
91+
}
92+
93+
public class TextAnalyticsInput
94+
{
95+
/// <summary>
96+
/// A unique, non-empty document identifier.
97+
/// </summary>
98+
public string Id { get; set; }
99+
100+
/// <summary>
101+
/// The input text to process.
102+
/// </summary>
103+
public string Text { get; set; }
104+
105+
/// <summary>
106+
/// The language code. Default is english ("en").
107+
/// </summary>
108+
public string LanguageCode { get; set; } = "en";
109+
}
110+
111+
public class DocumentSentiment
112+
{
113+
public DocumentSentiment(
114+
string id,
115+
DocumentSentimentLabel sentiment,
116+
SentimentConfidenceScoreLabel documentSentimentScores,
117+
IEnumerable<SentenceSentiment> sentencesSentiment)
118+
{
119+
Id = id;
120+
121+
Sentiment = sentiment;
122+
123+
DocumentScores = documentSentimentScores;
124+
125+
Sentences = sentencesSentiment;
126+
}
127+
128+
/// <summary>
129+
/// A unique, non-empty document identifier.
130+
/// </summary>
131+
public string Id { get; set; }
132+
133+
/// <summary>
134+
/// Predicted sentiment for document (Negative, Neutral, Positive, or Mixed).
135+
/// </summary>
136+
public DocumentSentimentLabel Sentiment { get; set; }
137+
138+
/// <summary>
139+
/// Document level sentiment confidence scores for each sentiment class.
140+
/// </summary>
141+
public SentimentConfidenceScoreLabel DocumentScores { get; set; }
142+
143+
/// <summary>
144+
/// Sentence level sentiment analysis.
145+
/// </summary>
146+
public IEnumerable<SentenceSentiment> Sentences { get; set; }
147+
}
148+
149+
public enum DocumentSentimentLabel
150+
{
151+
Positive,
152+
153+
Neutral,
154+
155+
Negative,
156+
157+
Mixed
158+
}
159+
160+
public enum SentenceSentimentLabel
161+
{
162+
Positive,
163+
164+
Neutral,
165+
166+
Negative
167+
}
168+
169+
public class SentimentConfidenceScoreLabel
170+
{
171+
public double Positive { get; set; }
172+
173+
public double Neutral { get; set; }
174+
175+
public double Negative { get; set; }
176+
}
177+
178+
public class ErrorRecord
179+
{
180+
/// <summary>
181+
/// The input document unique identifier that this error refers to.
182+
/// </summary>
183+
public string Id { get; set; }
184+
185+
/// <summary>
186+
/// The actual error message.
187+
/// </summary>
188+
public string Message { get; set; }
189+
}
190+
191+
public class SentenceSentiment
192+
{
193+
/// <summary>
194+
/// The predicted Sentiment for the sentence.
195+
/// </summary>
196+
public SentenceSentimentLabel Sentiment { get; set; }
197+
198+
/// <summary>
199+
/// The sentiment confidence score for the sentence for all classes.
200+
/// </summary>
201+
public SentimentConfidenceScoreLabel SentenceScores { get; set; }
202+
203+
/// <summary>
204+
/// The sentence offset from the start of the document.
205+
/// </summary>
206+
public int Offset { get; set; }
207+
208+
/// <summary>
209+
/// The sentence length as given by StringInfo's LengthInTextElements property.
210+
/// </summary>
211+
public int Length { get; set; }
212+
213+
/// <summary>
214+
/// The warnings generated for the sentence.
215+
/// </summary>
216+
public string[] Warnings { get; set; }
217+
}
218+
219+
public class RequestStatistics
220+
{
221+
/// <summary>
222+
/// Number of documents submitted in the request.
223+
/// </summary>
224+
public int DocumentsCount { get; set; }
225+
226+
/// <summary>
227+
/// Number of valid documents. This excludes empty, over-size limit or non-supported languages documents.
228+
/// </summary>
229+
public int ValidDocumentsCount { get; set; }
230+
231+
/// <summary>
232+
/// Number of invalid documents. This includes empty, over-size limit or non-supported languages documents.
233+
/// </summary>
234+
public int ErroneousDocumentsCount { get; set; }
235+
236+
/// <summary>
237+
/// Number of transactions for the request.
238+
/// </summary>
239+
public long TransactionsCount { get; set; }
240+
}
241+
}
242+
}

0 commit comments

Comments
 (0)