1 00:00:00,340 --> 00:00:03,520 We need to serialize our request data into JSON and 2 00:00:03,520 --> 00:00:07,090 then send it to the text analytics API URL. 3 00:00:07,090 --> 00:00:11,230 I wanna to show you a quicker way to use JSON.net to serialize objects 4 00:00:11,230 --> 00:00:12,941 using the JSON convert class. 5 00:00:13,980 --> 00:00:17,048 You might have seen it earlier in the JSON.net documentation. 6 00:00:17,048 --> 00:00:20,768 String request Json = 7 00:00:20,768 --> 00:00:26,841 JsonConvert.SerializeObject and 8 00:00:26,841 --> 00:00:32,920 will send it the sentimentRequest. 9 00:00:34,560 --> 00:00:38,100 There's one small bug I found with our sentiment request class. 10 00:00:38,100 --> 00:00:40,120 Let's go fix it before we build our request. 11 00:00:41,300 --> 00:00:43,780 This JSON property needs to be documents. 12 00:00:44,820 --> 00:00:47,920 Let's go check the JSON response in the docs to make sure it matches. 13 00:00:49,870 --> 00:00:56,110 There it is, okay, let's get back to our method. 14 00:00:56,110 --> 00:00:59,570 We need to convert our request based on into a byte array so 15 00:00:59,570 --> 00:01:02,490 we can pass it to the upload data method. 16 00:01:02,490 --> 00:01:04,600 Remember when we talked about encoding? 17 00:01:04,600 --> 00:01:08,260 It's a good practice to use UTFA for sending data over the Web. 18 00:01:08,260 --> 00:01:11,940 UTFA is the most compatible encoding. 19 00:01:11,940 --> 00:01:16,150 Let's convert our string into a byte array using UTFA. 20 00:01:16,150 --> 00:01:21,999 To do that, we can use the Encoding 21 00:01:21,999 --> 00:01:26,237 class by RequestBytes = 22 00:01:26,237 --> 00:01:31,284 Encoding utf8.GetBytes, 23 00:01:31,284 --> 00:01:34,810 and we'll pass it. 24 00:01:34,810 --> 00:01:37,330 Our requestJson. 25 00:01:37,330 --> 00:01:40,563 Then we can send it to the URL in our upload data method. 26 00:01:43,410 --> 00:01:46,550 But our URL is still the old one. 27 00:01:46,550 --> 00:01:47,300 Let's delete that. 28 00:01:48,400 --> 00:01:52,120 And we'll put a string placeholder and paste in request bytes. 29 00:01:53,200 --> 00:01:55,633 We need to go grab the URL from the documentation and 30 00:01:55,633 --> 00:01:57,451 paste it into the upload data method. 31 00:02:01,360 --> 00:02:02,218 Here it is. 32 00:02:02,218 --> 00:02:08,350 Copy and paste. 33 00:02:08,350 --> 00:02:13,120 Now, we can use the json convert class to deserialize the response. 34 00:02:13,120 --> 00:02:14,930 But we need a string for that. 35 00:02:14,930 --> 00:02:17,031 Json convert only deals with strings. 36 00:02:19,264 --> 00:02:21,556 String sentiments. 37 00:02:21,556 --> 00:02:27,590 equals encoding dot UTF8 dot Get String. 38 00:02:29,140 --> 00:02:31,060 And we'll pass it our response. 39 00:02:31,060 --> 00:02:34,740 And now we can use the deserialize object method on Json convert. 40 00:02:36,220 --> 00:02:43,370 Sentiment response equals Json convert dot deserialize 41 00:02:45,440 --> 00:02:49,690 And our type parameter is sentiment response, 42 00:02:51,700 --> 00:02:55,070 and then we need to pass it the string of sentiments. 43 00:02:56,920 --> 00:02:57,750 All right looking good. 44 00:02:58,760 --> 00:03:02,410 Now we need to do something with the sentiments from the news headlines. 45 00:03:02,410 --> 00:03:05,260 We can add a property to the NewsResult class 46 00:03:05,260 --> 00:03:09,080 to store the sentiment scores from our text analytics response. 47 00:03:09,080 --> 00:03:11,168 Let's take a look at the response JSON again. 48 00:03:14,266 --> 00:03:18,810 The score field has decimals, so which type do you think we should use? 49 00:03:18,810 --> 00:03:20,000 How about a double? 50 00:03:20,000 --> 00:03:21,542 It doesn't need to be super precise. 51 00:03:25,195 --> 00:03:28,948 New search class and 52 00:03:28,948 --> 00:03:33,807 NewsResult public double 53 00:03:33,807 --> 00:03:38,680 SentimentScore get set. 54 00:03:38,680 --> 00:03:43,150 Now let's go back up to main In our program class. 55 00:03:44,780 --> 00:03:46,770 Let's see where we can call this method. 56 00:03:46,770 --> 00:03:51,216 How about right after we call GetNewsForPlayer? 57 00:03:51,216 --> 00:03:58,082 SentimentResponse sentimentResponse 58 00:03:58,082 --> 00:04:03,520 = GetSentimentResponse And 59 00:04:03,520 --> 00:04:06,370 we'll pass it our list of news results. 60 00:04:07,990 --> 00:04:11,310 Then we can loop through the sentiment responses. 61 00:04:11,310 --> 00:04:15,604 So for each var sentiment in 62 00:04:15,604 --> 00:04:23,180 sentimentResponse.Sentiments Cause it's a list, 63 00:04:25,440 --> 00:04:30,080 and then let's add another loop to find the correct news result and 64 00:04:30,080 --> 00:04:39,331 update the score, foreach (var newsResult in newsResults). 65 00:04:41,820 --> 00:04:46,250 Will add a check to see if the headline matches the ID of the sentiment and 66 00:04:46,250 --> 00:04:48,340 then we'll add the score to the news result object. 67 00:04:49,690 --> 00:04:54,960 So if the news result headline. 68 00:04:55,960 --> 00:05:03,770 because if you remember we put the Sentiment.id equal to the headline. 69 00:05:04,980 --> 00:05:14,110 Then we'll add the score to the news result object equals sentiment.score. 70 00:05:14,110 --> 00:05:17,970 But we need to parse the string from sentiment into a double 71 00:05:17,970 --> 00:05:20,670 We can use good old Double.TryParse. 72 00:05:20,670 --> 00:05:23,685 We'll need a double to hold the value. 73 00:05:23,685 --> 00:05:29,380 If Double.TryParse, and 74 00:05:29,380 --> 00:05:36,011 in we'll pass it sentiment.Score, and out score. 75 00:05:38,600 --> 00:05:41,638 And so if that's successful, 76 00:05:41,638 --> 00:05:47,700 we'll assign newsresult.sentiment score equals score. 77 00:05:49,340 --> 00:05:56,290 We can get rid of this line right here Okay, and since 78 00:05:56,290 --> 00:06:00,460 we found a match we can break out of this loop to continue to the next news result. 79 00:06:02,420 --> 00:06:06,000 Now, let's add the sentiment score to the text that we write to the console, 80 00:06:07,040 --> 00:06:12,930 Sentiment Score and we'll pass it a 0. 81 00:06:12,930 --> 00:06:16,679 Since it's at the beginning, and then we'll need to change all these that 82 00:06:16,679 --> 00:06:22,683 follow, One more 83 00:06:22,683 --> 00:06:27,930 in the list here. 84 00:06:27,930 --> 00:06:32,620 So we've got 1, 2, 3, 4, and then we'll need to add the sentiment score On 85 00:06:32,620 --> 00:06:38,050 the result object, result.SentimentScore, 86 00:06:38,050 --> 00:06:41,520 and I like to double check the numbers, we go one, two, three, four. 87 00:06:42,950 --> 00:06:46,070 One, two, three, four, great. 88 00:06:47,380 --> 00:06:53,760 Let's run it and see what we get All right. 89 00:06:53,760 --> 00:06:55,320 It looks like our sentiments are there, 90 00:06:55,320 --> 00:06:59,060 but do you think we should format them a little better? 91 00:06:59,060 --> 00:07:02,040 The scores are supposed to represent a percentage. 92 00:07:02,040 --> 00:07:04,740 We can use a numeric format or in our string. 93 00:07:04,740 --> 00:07:05,810 format method. 94 00:07:05,810 --> 00:07:06,720 The letter P. 95 00:07:06,720 --> 00:07:09,450 will format the numeric value into a percentage. 96 00:07:10,940 --> 00:07:11,940 Stop debugging. 97 00:07:13,180 --> 00:07:17,560 So a lot like our date formatter, right here we just do a :P. 98 00:07:17,560 --> 00:07:20,152 Now let's see what we get. 99 00:07:23,981 --> 00:07:25,860 All right, great. 100 00:07:25,860 --> 00:07:27,600 It's a percentage. 101 00:07:27,600 --> 00:07:30,630 You can find more ways to format numbers with the link in the notes. 102 00:07:31,854 --> 00:07:34,290 It's a lot easier to read. 103 00:07:34,290 --> 00:07:38,130 Let's see, this is two percent, missing the injured Marco Verratti and 104 00:07:38,130 --> 00:07:43,250 Claudio [LAUGH] Struggled, 105 00:07:43,250 --> 00:07:45,630 yeah, I guess that's a low sentiment. 106 00:07:45,630 --> 00:07:50,504 Let's look at some other ones here, Looking for low ones 107 00:07:54,786 --> 00:07:57,430 These are all pretty high, 9%. 108 00:07:57,430 --> 00:08:00,120 Looks like somebody had hand surgery. 109 00:08:02,491 --> 00:08:04,160 Suffered the injury. 110 00:08:04,160 --> 00:08:05,030 Yeah, that's pretty low. 111 00:08:05,030 --> 00:08:11,340 All right, great Fantastic job. 112 00:08:11,340 --> 00:08:13,490 You learned a lot in this course. 113 00:08:13,490 --> 00:08:17,840 We learned about file IO and how to read and write files to your computer. 114 00:08:17,840 --> 00:08:21,198 Then we explored how to parse text data into dates and numbers. 115 00:08:21,198 --> 00:08:24,680 We use Json.net for serialization and then send 116 00:08:24,680 --> 00:08:29,690 it over the Web Now you can take what you've learned and build some projects. 117 00:08:29,690 --> 00:08:33,450 Go find some JSON data that interests you and build an app around it or 118 00:08:33,450 --> 00:08:37,640 better yet find an API and use it to grab data from the web. 119 00:08:37,640 --> 00:08:40,080 I've put some project ideas for you in the notes. 120 00:08:40,080 --> 00:08:41,950 Go forth and build things.