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