0 1 00:00:00,390 --> 00:00:09,690 Now, in the last lesson, we used our TweetSentimentClassifier which we created inside our app by creating 1 2 00:00:09,720 --> 00:00:12,500 a new object from that class. 2 3 00:00:12,750 --> 00:00:20,160 And then we used a very simple prediction based off some text that we come up with on the spot and we 3 4 00:00:20,160 --> 00:00:22,160 got our predictions back 4 5 00:00:22,260 --> 00:00:24,890 that seemed to be working pretty well. 5 6 00:00:24,960 --> 00:00:32,430 The next step, instead of classifying pieces of text that we come up with, is to use the tweets that we 6 7 00:00:32,430 --> 00:00:40,380 get back from our API call to Twitter using our particular search terms such as @Apple or @Google 7 8 00:00:40,420 --> 00:00:46,850 or whatever it is that we want to look for. And in order to do that, we have to be able to parse this JSON 8 9 00:00:46,860 --> 00:00:54,600 that we get back, so that we can pull out this, the value of the property ful_text. And the easiest way 9 10 00:00:54,660 --> 00:01:00,760 of passing JSONs is if you remember, of course, using SwiftyJSON. 10 11 00:01:01,020 --> 00:01:08,430 Now, as I mentioned in previous modules, you can also use a native Swift feature which is decoding the 11 12 00:01:08,430 --> 00:01:11,510 JSON using the encoder and decoder. 12 13 00:01:12,090 --> 00:01:18,450 But in this case, because there are so many properties here that I would have to create a struct for, 13 14 00:01:18,810 --> 00:01:24,450 it's actually quite painful if all I want to use is just one piece of information. 14 15 00:01:24,540 --> 00:01:30,240 And in these cases, I still favor using something like SwiftyJSON because it just makes life so much 15 16 00:01:30,270 --> 00:01:31,390 easier. 16 17 00:01:31,830 --> 00:01:40,620 So let's go ahead and close down our Xcode project and we're going to open up Terminal. And I'm going 17 18 00:01:40,620 --> 00:01:47,840 to cd over to my Twittermenti container folder, and then I'm going to say "pod init," 18 19 00:01:50,810 --> 00:01:56,620 then open Podfile -a Xcode. 19 20 00:01:56,650 --> 00:02:01,900 Now, I have to write Xcode-beta because I'm currently using the Xcode beta. 20 21 00:02:01,930 --> 00:02:07,720 Now, when you're viewing this tutorial, hopefully, the official version of Xcode 10 should have come 21 22 00:02:07,720 --> 00:02:11,120 out, and that will happen around the end of September. 22 23 00:02:11,200 --> 00:02:16,630 So if you downloaded the official version of Xcode 10 from the AppStore, then make sure that you use just 23 24 00:02:16,690 --> 00:02:17,330 Xcode. 24 25 00:02:17,470 --> 00:02:24,940 But if you are doing this tutorial before October, then you'll have to open it inside Xcode beta and 25 26 00:02:24,940 --> 00:02:34,600 the part that I want to add is simply pod "SwiftyJSON" and make sure that you don't have any typos 26 27 00:02:34,630 --> 00:02:36,050 in there. 27 28 00:02:36,130 --> 00:02:40,960 Let's hit Save and close this down, and then let's do "pod install." 28 29 00:02:45,390 --> 00:02:45,760 All right. 29 30 00:02:45,780 --> 00:02:47,880 So the installation is complete. 30 31 00:02:47,880 --> 00:02:50,040 There's one dependency that was added. 31 32 00:02:50,220 --> 00:02:59,030 And I can now open my Twittermenti.xcworkspace file. So, now if I expand my Twittermenti folder 32 33 00:02:59,150 --> 00:03:07,390 and head back to my ViewController, I can use that framework by importing SwiftyJSON. 33 34 00:03:07,430 --> 00:03:14,990 And just because it has trouble the first time of integrating those links, always make sure you do a 34 35 00:03:14,990 --> 00:03:24,120 command B just to build and ensure that build succeeds. In case your pods have things that need to be 35 36 00:03:24,150 --> 00:03:25,930 updated to the latest settings, 36 37 00:03:25,950 --> 00:03:33,220 just click on this link and click perform changes, and that should get rid of all of your warnings. 37 38 00:03:33,330 --> 00:03:33,630 All right. 38 39 00:03:33,660 --> 00:03:38,730 So, now back over here, we're going to try, instead of printing the results, 39 40 00:03:38,880 --> 00:03:41,590 we're going to pass the results. 40 41 00:03:41,910 --> 00:03:48,750 Now, if you need a reminder on how SwiftyJSON works, make sure you head back to the Clima module and, 41 42 00:03:48,780 --> 00:03:55,680 equally, if you don't want to use SwiftyJSON and you prefer using the Swift encoder-decoder, again, check 42 43 00:03:55,680 --> 00:04:02,010 out the lesson when we talked about that in the Clima module. But in my case, I'm simply going to say, 43 44 00:04:02,670 --> 00:04:12,000 "let tweet = results" which is the JSON object that we get back from our API call, 44 45 00:04:12,660 --> 00:04:19,830 and I'm going to tap into the result at the index zero to get the first result, and then I'm going to 45 46 00:04:19,830 --> 00:04:24,180 tap into the property that is full underscore text. 46 47 00:04:24,210 --> 00:04:25,890 So let's try that. 47 48 00:04:25,920 --> 00:04:33,060 So in here, I'm going to put in the string full_text. And now I have to try and get the 48 49 00:04:33,060 --> 00:04:36,540 string result from this by writing .string. 49 50 00:04:37,320 --> 00:04:45,690 Now, remember, this .string is a optional string because it might not be able to get anything using 50 51 00:04:45,690 --> 00:04:47,460 something that I've just typed in. 51 52 00:04:47,460 --> 00:04:52,770 So, for example, I could have had a typo in here and I was trying to find an object that had a property 52 53 00:04:52,770 --> 00:04:55,830 of "fulle_text" which, obviously, doesn't exist, 53 54 00:04:55,860 --> 00:04:58,020 so this would return nil. 54 55 00:04:58,020 --> 00:05:01,110 And that's why this whole thing is an optional. 55 56 00:05:01,110 --> 00:05:04,920 So in order to keep everything safe, let's create an "if let." 56 57 00:05:04,950 --> 00:05:10,710 So if let tweet = results at index zero of the property that is 57 58 00:05:10,710 --> 00:05:13,470 full_text as a string, 58 59 00:05:13,590 --> 00:05:22,440 then let's just go ahead and print out this particular tweet. And now I'm going to go ahead and comment 59 60 00:05:22,530 --> 00:05:24,070 out these two lines. 60 61 00:05:24,270 --> 00:05:30,570 And at this stage, if you hit run, you might not see anything happening as in your app being launched, 61 62 00:05:31,170 --> 00:05:39,960 and it might be because Xcode somehow changed the project that you want to run to the Swifter iOS 62 63 00:05:39,960 --> 00:05:41,180 framework. 63 64 00:05:41,190 --> 00:05:45,540 Of course, running the framework doesn't launch an app or do anything, 64 65 00:05:45,540 --> 00:05:52,590 instead, we have to launch the Twittermenti app, so make sure that this is selected up here. And then 65 66 00:05:52,740 --> 00:05:54,270 click Run. 66 67 00:05:54,390 --> 00:06:00,090 And don't worry if you have some temporary red errors or warnings come up here, 67 68 00:06:00,330 --> 00:06:04,640 as long as you get build succeeded, then those will go away. 68 69 00:06:07,330 --> 00:06:07,660 All right. 69 70 00:06:07,690 --> 00:06:14,300 So our app has now been launched and we've got some text that has been printed in here. 70 71 00:06:14,550 --> 00:06:18,180 "Yo @Apple! Running iOS 12b4 71 72 00:06:18,340 --> 00:06:24,160 and I keep having to reboot my phone. My phone almost called the cops!" 72 73 00:06:24,190 --> 00:06:30,280 Man, this is the reason why you don't want to work with betas. But that is the tweet that we manage again 73 74 00:06:30,460 --> 00:06:33,280 and it's probably negative. 74 75 00:06:33,280 --> 00:06:34,710 This is live results. 75 76 00:06:34,720 --> 00:06:42,400 So if we search for @Apple on Twitter and try to find the first English tweet, you can see we've got 76 77 00:06:42,400 --> 00:06:44,580 this tweet from Malcolm 77 78 00:06:44,740 --> 00:06:53,450 that is exactly what we found over there. And we've now parsed it into a string from our JSON object. 78 79 00:06:53,680 --> 00:06:56,070 So that's working really well. 79 80 00:06:56,290 --> 00:07:00,350 But now we need to parse all of our tweets out of the JSON 80 81 00:07:00,520 --> 00:07:06,760 and we want to add it to an array so that we can make a prediction for all the items in the array. 81 82 00:07:07,900 --> 00:07:10,150 So for that, we'll need a "for loop." 82 83 00:07:10,750 --> 00:07:19,990 So we're going to say, "for i in" starting from the index zero and going up to, but not including index 83 84 00:07:19,990 --> 00:07:27,280 100. Because if we have a look over here at our JSON editor, you can see we have 100 items, 84 85 00:07:27,280 --> 00:07:30,040 but the last one has an index of 99. 85 86 00:07:30,070 --> 00:07:36,560 So our "for loop" is going to stop at 99 just before it reaches 100. 86 87 00:07:36,730 --> 00:07:46,990 And inside this "for loop," we're going to run our "if let" statement to parse our tweet out of the results. 87 88 00:07:47,350 --> 00:07:55,690 And instead of using zero, we're going to use "i" so that each time this loop runs, we increment the number 88 89 00:07:55,690 --> 00:08:03,940 here and we get the next tweet. And instead of printing the tweet, we're going to append this tweet to 89 90 00:08:04,030 --> 00:08:14,590 an array, and that array is simply going to be called tweets, and it's gonna be an array of Strings. 90 91 00:08:14,930 --> 00:08:24,890 So let's initialize it up here and then let's say, tweets.append, new element which is tweet. Tweets 91 92 00:08:24,950 --> 00:08:27,230 append tweet. 92 93 00:08:27,260 --> 00:08:34,700 We create a new array of strings called tweets and we run a loop a hundred times and we parse the 93 94 00:08:34,700 --> 00:08:41,480 "full_text" as a string out of the JSON for every single one of those 100 tweets. 94 95 00:08:41,900 --> 00:08:46,550 And for each tweet that we parse, we add it to our tweets array. 95 96 00:08:46,640 --> 00:08:50,880 So by the end here, we should have an array of tweets. 96 97 00:08:50,960 --> 00:08:52,000 So let's check it out. 97 98 00:08:57,530 --> 00:08:57,800 Now, 98 99 00:08:57,800 --> 00:09:02,450 down here, you can see here's our array without any of the other parts. 99 100 00:09:02,450 --> 00:09:04,720 We've got all of our tweets here, 100 101 00:09:04,820 --> 00:09:09,740 not really having much structure separated by commas in our array. 101 102 00:09:10,490 --> 00:09:12,650 So that's working perfectly. 102 103 00:09:12,670 --> 00:09:15,290 So we can go ahead and delete that. 103 104 00:09:15,320 --> 00:09:15,620 All right. 104 105 00:09:15,650 --> 00:09:23,480 So the next thing we need to do is to classify all of those tweets. Now, running a prediction is something 105 106 00:09:23,480 --> 00:09:30,170 that uses resources on the--either in this case, is my mac, or if this app was running on the phone, then 106 107 00:09:30,170 --> 00:09:31,660 it would be the phone. 107 108 00:09:31,880 --> 00:09:38,440 And it tries to use the CPU or the GPU to try and make that prediction. 108 109 00:09:38,440 --> 00:09:41,020 Now, we want to try and make this prediction in batch, 109 110 00:09:41,030 --> 00:09:48,680 so we want to be able to feed it our entire array of tweets and try to get a prediction for each and 110 111 00:09:48,710 --> 00:09:49,700 every one of them. 111 112 00:09:49,730 --> 00:09:54,780 So an array of predictions out, instead of running it one by one. 112 113 00:09:54,780 --> 00:10:03,050 So the next thing we want to do is to be able to parse this array of tweets to our TweetSentimentClassifier 113 114 00:10:03,470 --> 00:10:07,840 and try to get an array of sentiment classifications. 114 115 00:10:07,970 --> 00:10:12,700 So that is what we're gonna do on the next lesson. For all of that and more, 115 116 00:10:12,770 --> 00:10:13,880 I'll see you on the next lesson.