Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
We'll start to build our request to send to the Text Analytics API.
Text Analytics API
The Text Analytics API demo that's shown in the video (https://text-analytics-demo.azurewebsites.net/) is no longer available. The home page for the Microsoft Cognitive Services Text Analytics API (including an interactive demo) can be found at https://azure.microsoft.com/en-us/services/cognitive-services/text-analytics/.
To get started with using the Text Analytics API (including how to get your API key), follow the instructions at https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/quick-start.
-
0:00
We now know how to receive data from a web resource.
-
0:03
Let's learn about how to send data to a web resource.
-
0:07
We'll be using another Microsoft API called text analytics.
-
0:12
I've got a demo up of it here.
-
0:14
This web service analyzes a piece of text for sentiment, topics and language.
-
0:19
All we do is send it some text.
-
0:21
I love C#.
-
0:28
And it comes back with analysis of the text we sent.
-
0:32
It says our sentence is 82% positive.
-
0:35
I'd agree with that.
-
0:36
Let's see what it says If I type in something a little less positive.
-
0:41
I hate lima beans.
-
0:46
Analyze, it detected our text was much less positive 10%.
-
0:53
We can use this service to analyze the sentiment in each of our news headlines.
-
0:58
Let's click on getting started.
-
1:02
Let's see to follow along you need to follow the steps and
-
1:07
task one here in order to get an API key.
-
1:10
It's a different key from the one we used earlier for a new search.
-
1:14
Feel free to posit video and get your API key ready.
-
1:19
Okay here's task two, says we'll need to use these headers.
-
1:25
We'll get back to that in a bit.
-
1:29
Let's see, we're going to need two new classes.
-
1:32
One for our request data and one for response data.
-
1:35
Here, it's telling us how to format our request.
-
1:39
Let's copy and paste this into a new class in our application.
-
1:44
Add class, we'll call it, SentimentRequest.
-
1:54
Then, edit, Paste Special, Paste JSON As Classes.
-
2:01
No, it didn't like that.
-
2:05
This sample has some invalid JSON,
-
2:08
we can use a JSON validation service to try and figure out what's wrong.
-
2:13
Head to google and JSON validation
-
2:18
service, we'll use this one JSONLint.
-
2:23
Let's paste our JSON here and then click validate.
-
2:29
It's got line five highlighted, but
-
2:31
I think the problem is these ellipses right here.
-
2:35
Let's delete that and try again.
-
2:37
Validate, all right, valid JSON.
-
2:42
Let's copy this and go back over and try to paste it into our class again.
-
2:45
Edit, Paste Special, Paste JSON as Classes.
-
2:52
All right, let's rename Rootobject to SentimentRequest and
-
2:57
we'll change this array to a list, List documents and
-
3:05
I'd like to change the properties to capitals.
-
3:15
And we'll need our JsonProperty again.
-
3:20
JsonProperty all right,
-
3:27
PropertyName=document.
-
3:33
Copy that, that's an id,
-
3:38
and then our text field.
-
3:46
Okay, now we need a class for our response data.
-
3:50
We can go back and get the response JSON.
-
3:55
Here it is, sentiment response.
-
3:59
Copy and paste that, it looks like we've got our ellipses again.
-
4:03
Let's use JSONLint, take out this ellipsis and validate.
-
4:09
Uh-oh, think we've got an extra comma here, good thing we checked.
-
4:16
Validate and valid Json.
-
4:20
So we'll do the same thing back in our code,
-
4:25
add another class, add class,
-
4:28
then this is going to be called SentimentResponse.
-
4:38
And one more time Paste Special, paste JSON as classes.
-
4:42
All right, great.
-
4:45
Rename the root object to SentimentResponse
-
4:50
change our array to a list.
-
4:57
And, we've got a little red squiggly line right here.
-
5:03
I think it's because we've got two document classes defined, and
-
5:06
they're conflicting.
-
5:08
Well there are a lot of things we could do to fix this,
-
5:10
but the simplest I can think of is to rename one of these classes.
-
5:15
Well the response is giving us an ID and a score.
-
5:19
That's not really a document is it?
-
5:21
Let's rename this to sentiment and
-
5:25
then we'll need to change the list up here and the name to sentiments.
-
5:30
Then we'll need our JSON property.
-
5:33
JSON property, you'll be an expert at the quick actions by now.
-
5:38
And property name equals documents,
-
5:44
and we'll capitalize our ID and
-
5:50
score down here.
-
5:53
ID capital I, and
-
5:56
one more time for score, Score.
-
6:02
Okay, that looks good.
-
6:08
Now we can get our method set up to make the request to the web service.
-
6:13
Since the request can have more than one piece to analyze, we can give the method
-
6:17
a list of news results and then have it return the sentiment response.
-
6:22
All the way down here, public static SentimentResponse,
-
6:31
and we'll call it GetSentimentResponse.
-
6:36
It'll take a list of NewsResults,
-
6:39
and we'll call those newsResults.
-
6:46
And we'll go ahead and var SentimentResponse=
-
6:54
new SentimentResponse and return it,
-
6:59
so that visual studio is happy.
-
7:04
Okay, now we'll also need a SentimentRequest,
-
7:10
SentimentRequest = new SentimentRequest.
-
7:19
Okay but we need to add data to this SentimentRequest object.
-
7:24
We can use a foreach loop to iterate through our news results and
-
7:27
add data to our SentimentRequest object.
-
7:29
foreach(var result in
-
7:34
newsResults), and then inside the foreach loop,
-
7:39
we'll add a new document to the list that contains the text from each news result.
-
7:44
sentimentRequest.Documents.Add,we can
-
7:51
use object initialization here to create an instance of a document
-
7:55
as we add it to the list.
-
7:57
New document, in a curly brace.
-
8:02
I can use a space here to get it.
-
8:04
Okay, we've got ID equals, we'll l use the headline as the ID,
-
8:10
result.Headline, and then Text equals,
-
8:17
it should be the summary, result.Summary.
-
8:22
There's one thing we need to do,
-
8:24
we need to instantiate this Documents to a new list of documents.
-
8:29
So sentimentRequest.Documents = new list of document.
-
8:38
If we didn't do this and
-
8:39
tried to add a document to the list, we'd end up with a runtime error.
-
8:44
Now we can create our web client.
-
8:46
We can copy and paste from our news requests to start
-
9:01
But this download data isn't the method we're going to use.
-
9:05
We need to send data.
-
9:07
There's a different method on web client called upload data .UploadData,
-
9:15
we can see that it wants a byte array in addition to the url.
-
9:18
I think we can handle that.
-
9:20
We've already got our API key header but we'll need to insert our new API key.
-
9:27
Make sure to use your own, and there was some additional headers it wanted.
-
9:34
Let's go back to the documentation.
-
9:36
I think it was up here, there they are.
-
9:42
We need two more content type and accept.
-
9:45
We saw accept earlier on those standard headers.
-
9:49
We'll copy this value since it's the same for both, head back over the code.
-
9:56
Okay, webClient.Headers.Add.
-
10:04
And we'll use this enum here, Accept and
-
10:09
we'll pass in the value which is application/JSON.
-
10:14
And web client.headers.Add,
-
10:18
one more content type there it is at the bottom and
-
10:23
we'll pass application/JSON.
-
10:26
We'll go ahead and change this search results to response.
-
10:33
We're almost ready to make this request.
You need to sign up for Treehouse in order to download course files.
Sign up