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