0 1 00:00:00,640 --> 00:00:00,990 All right. 1 2 00:00:01,020 --> 00:00:08,760 So in the last lesson, we looked at how we could use any search term that we can come up with to search 2 3 00:00:08,760 --> 00:00:17,580 Twitter using the Twitter API and Swifter to get a 100 tweets back that match or contain this particular 3 4 00:00:17,580 --> 00:00:18,750 search term. 4 5 00:00:18,930 --> 00:00:26,120 And then, we ran it through our sentimentClassifier to get a sentiment for each of the 100 tweets. 5 6 00:00:26,430 --> 00:00:34,080 And then, we created a score depending on how many positive or how many negative sentiments there were 6 7 00:00:34,140 --> 00:00:36,480 in that set of 100 tweets. 7 8 00:00:36,480 --> 00:00:42,210 Now, of course, the user can't go into our code and change a string manually. 8 9 00:00:42,210 --> 00:00:49,680 So in this lesson, we're going to make our app interface work and integrate our code so that the user 9 10 00:00:49,680 --> 00:00:56,730 will be able to type in a particular hashtag or a particular handle and press the predict button to 10 11 00:00:56,730 --> 00:01:02,840 generate an emoticon that represents how people feel about that particular topic 11 12 00:01:02,850 --> 00:01:04,370 over 100 tweets. 12 13 00:01:04,590 --> 00:01:06,610 So let's get started doing that. 13 14 00:01:06,690 --> 00:01:12,590 And the first thing we have to do is we need to tap into the text that's inside the textField. 14 15 00:01:12,840 --> 00:01:21,690 So that is, of course, simply denoted by something like, let's say, let's searchText = textField 15 16 00:01:21,780 --> 00:01:22,480 .text. 16 17 00:01:22,860 --> 00:01:28,880 And, remember, this is a optional string because the user might have left it blank. 17 18 00:01:28,920 --> 00:01:33,260 So let's go ahead and add this as a "if let" 18 19 00:01:33,600 --> 00:01:39,600 so that we can unwrap the optional only when the textField is not empty because we don't want to be 19 20 00:01:39,600 --> 00:01:43,910 making API requests with an empty searchText. 20 21 00:01:43,950 --> 00:01:50,740 And now, let's go ahead and include all of this code up to, but not including the last brackets, 21 22 00:01:50,940 --> 00:01:55,370 and let's put it inside our brand-new "if let." 22 23 00:01:55,620 --> 00:02:03,570 And now instead of using a hardcoded string, we can change it to our searchText. 23 24 00:02:03,600 --> 00:02:03,870 All right. 24 25 00:02:03,900 --> 00:02:10,890 So the next thing that we need to do is we need to be able to activate this search for tweets pinging 25 26 00:02:11,120 --> 00:02:19,890 the Twitter API as well as making the predictions all when the user presses the predict button. 26 27 00:02:19,890 --> 00:02:26,310 So we need to take all of this code that's currently inside viewDidLoad and we need to move it out 27 28 00:02:26,310 --> 00:02:30,730 of viewDidLoad and into our IBAction here. 28 29 00:02:30,930 --> 00:02:34,210 So we don't actually need anything to happen on 29 30 00:02:34,230 --> 00:02:35,480 viewDidLoad anymore. 30 31 00:02:35,490 --> 00:02:41,550 We only had it there in order to test our code so that every time we run it, it would carry it out and 31 32 00:02:41,550 --> 00:02:45,210 it would print the results into our debug console. 32 33 00:02:45,240 --> 00:02:50,890 So, now all of this gets carried out when we press the predict button instead. 33 34 00:02:50,940 --> 00:02:57,090 So when we press the predict button, we want to create a sentimentScore and we don't want to see it 34 35 00:02:57,180 --> 00:03:01,830 in the debug console because, remember, the user can't see any of that. 35 36 00:03:01,830 --> 00:03:10,110 Instead, we want to represent that sentimentScore as an emoji so that when the user hits predict 36 37 00:03:10,350 --> 00:03:17,280 and gets the results back, it should update this emoji to represent how positive or negative the sentiment 37 38 00:03:17,280 --> 00:03:18,570 was. 38 39 00:03:18,570 --> 00:03:22,800 So in order to do that, we can just go ahead and add an "if" statement. 39 40 00:03:22,860 --> 00:03:31,160 So let's say if sentimentScore is greater than 20, so that's pretty much overwhelmingly positive, 40 41 00:03:31,320 --> 00:03:41,130 then the sentimentLabel, it's text property, which, remember, has to be a string, and emojis are actually 41 42 00:03:41,130 --> 00:03:42,910 unicode characters. 42 43 00:03:43,020 --> 00:03:49,290 So all we need to do here is to pull up our emoticon chooser which you can do by using the shortcut 43 44 00:03:49,320 --> 00:03:56,910 controller command and space, or you can simply go into edit and emoji and symbols, it does exactly the same 44 45 00:03:56,910 --> 00:03:57,720 thing. 45 46 00:03:57,840 --> 00:04:05,490 And inside here, if it's really positive, I'm going to choose to show this emoji and, again, remember that 46 47 00:04:05,490 --> 00:04:12,130 the sentimentLabel is something that is at class level and we're currently inside a closure, 47 48 00:04:12,300 --> 00:04:19,710 so we have to mark this property with a "self." And then, I'm just gonna go through this and add a whole 48 49 00:04:19,710 --> 00:04:28,650 bunch of cases where if instead of the sentimentScore being greater than 20, what if it was simply greater 49 50 00:04:28,650 --> 00:04:29,470 than 10, 50 51 00:04:29,550 --> 00:04:34,320 so between 10 and 20. Because, remember, at this point, this check would have already been done, 51 52 00:04:34,320 --> 00:04:36,390 so it's definitely not greater than 20. 52 53 00:04:36,780 --> 00:04:44,070 But if it's greater than 10, then in that case, maybe I'll want the sentimentLabel to have an emoji 53 54 00:04:44,100 --> 00:04:50,100 that looks kind of happy but, obviously, less happy than the previous one. 54 55 00:04:50,100 --> 00:04:58,060 And I'm just going to go through this and add an emoji for most of the ranges. 55 56 00:04:58,260 --> 00:05:04,920 So, now I have a whole bunch of conditions for, say, if the sentimentScore was above 20, or if it's between 56 57 00:05:04,980 --> 00:05:15,060 10 and 20, between 0 and 10, or at--or equal to zero, or between minus 10 and zero, between minus 20 and 57 58 00:05:15,060 --> 00:05:16,320 minus 10. 58 59 00:05:16,320 --> 00:05:20,280 And finally, if it's even less than minus 20. 59 60 00:05:20,340 --> 00:05:22,590 So, now let's give our app a try. 60 61 00:05:25,620 --> 00:05:31,710 So let's try something like @CocaCola. 61 62 00:05:31,720 --> 00:05:34,470 Okay, so people are kind of happy about it. 62 63 00:05:34,470 --> 00:05:39,280 So this is gonna be something between zero and 10. 63 64 00:05:39,570 --> 00:05:41,790 Now, let's try their competitor. 64 65 00:05:41,790 --> 00:05:47,570 How do people feel about Pepsi? Interesting. 65 66 00:05:47,590 --> 00:05:52,710 So people are not so happy with Pepsi, but they seem to like Coca-Cola more. 66 67 00:05:52,720 --> 00:05:54,940 That's interesting. 67 68 00:05:54,940 --> 00:06:03,190 Now, what if we try some stock symbols? Let's say, AAPL for Apple. Ooh, not so happy. 68 69 00:06:03,190 --> 00:06:13,260 What about something like Twitter? TWTR. Okay, people are feeling kind of mad about it. 69 70 00:06:13,270 --> 00:06:17,060 What about Facebook? 70 71 00:06:17,120 --> 00:06:22,570 What about Microsoft? 71 72 00:06:22,620 --> 00:06:23,610 Interesting. 72 73 00:06:23,610 --> 00:06:26,490 So you could play with this all day long. 73 74 00:06:26,490 --> 00:06:29,430 And believe me, I've been playing with it all day long. 74 75 00:06:29,640 --> 00:06:38,250 But now, essentially, we have a very simple interface and our app is able to do a lot of heavy lifting 75 76 00:06:38,490 --> 00:06:45,870 all in the background using the powers of CoreML 2 and the Natural Language processing that it brings 76 77 00:06:45,870 --> 00:06:50,590 us, as well as the machine learning model that we build ourselves 77 78 00:06:50,610 --> 00:06:58,960 of only a thousand tweets, and their sentiment analysis. And we've managed to use the Twitter API to pull 78 79 00:06:58,960 --> 00:07:07,470 live tweet data and filter it by a variety of means in order to generate this hundred tweet 79 80 00:07:07,800 --> 00:07:12,390 sentimentScore which we're using to power our little app. 80 81 00:07:12,390 --> 00:07:18,990 So I hope you can see that the possibilities for this app are absolutely endless and you can extend 81 82 00:07:18,990 --> 00:07:20,850 this in a variety ways, 82 83 00:07:20,880 --> 00:07:23,660 and as far as your imagination carries you really. 83 84 00:07:24,120 --> 00:07:28,440 But the skills are now yours for you to experiment and play with 84 85 00:07:28,440 --> 00:07:33,630 and I look forward to all of the awesome apps that you'll be building using these techniques that you've 85 86 00:07:33,630 --> 00:07:35,700 learned in this and in previous modules. 86 87 00:07:36,510 --> 00:07:38,620 So if there's anything cool that you've built, 87 88 00:07:38,640 --> 00:07:44,070 be sure to post it in the Q & A section so that myself and other students can check it out and we 88 89 00:07:44,070 --> 00:07:48,910 can congratulate you on your great work and also be your first beta testers. 89 90 00:07:48,930 --> 00:07:51,260 So I wish you all the best of luck. 90 91 00:07:51,360 --> 00:07:56,850 And in the next lesson, we're going to refactor our code to make it a little bit tidier and a little 91 92 00:07:56,850 --> 00:07:57,930 bit prettier. 92 93 00:07:57,960 --> 00:08:00,510 So if you want to watch along for that, 93 94 00:08:00,510 --> 00:08:02,520 be sure to head over to the next lesson. 94 95 00:08:02,670 --> 00:08:04,800 Otherwise, I'll see you on the next module.