Now, in the last lesson, we used the Swifter framework to authenticate ourselves to work with the Twitter 1
API. 2
We authenticated ourselves using the application that we set up on Twitter and we performed a method 3
called searchTweet using a particular search query specifying that we only wanted the tweets that were 4
in English, and that we wanted the maximum 100 tweets to come back, and also that we want the full 280 5
characters 6
if there are in fact 280 characters in the tweet. And if it was successful, then we basically just print 7
the results of that search inside our debug console. 8
But if there was an error, then we'll print the error. 9
So, now that that's done, we can delete this reference and we can get on with the work of classifying 10
and predicting the sentiment in each of these tweets. 11
Now, in order to use our classifier, we've already previously dragged and dropped it into our project. 12
And you can see that it's automatically created a class called TweetSentimentClassifier. And this is 13
the class that we're going to be using if we want to create a new object to do our classification. 14
And, remember, this file gets created automatically so that we can tap into that class and we can use 15
all of these associated methods for prediction. 16
So let's go over to our ViewController and let's create a new object of our sentiment classifier and 17
I'm just going to call it sentimentClassifier and it's going to be a new object created from the class 18
that is TweetSentimentClassifier. And now, of course, because we're using CoreML, we have to import 19
that framework as well. 20
So, now that we've got our new sentimentClassifier object set up and ready to go, it's time to use it 21
and perform some methods to actually classify our data. 22
So if we head back over to this class that was created again, we can see the methods that are available 23
for our use. 24
Now, one of them says that we can make a prediction using the convenience interface and the parameters 25
only include text which is the input text as a string value. 26
And it could potentially throw an error, but if it was successful, then it'll return the result of the 27
prediction as a TweetSentimentClassifierOutput object. So let's try and use our sentimentClassifier 28
with the simplest type of prediction where we're just using a piece of text. 29
So we're going to use our sentimentClassifier and we're going to use the method called prediction. 30
And you can see that there's several prediction methods, each getting more and more complex. But the simplest 31
type just takes a single String and can throw an error, but if it was successful, then we get a prediction. 32
So let's hit enter. And the text that I'm going to test it with is just something like, say, "@Apple is 33
a terrible company!" And in order to satisfy the part where it can throw, let's mark this with a "try" and 34
force it to go through. Currently, because I'm just testing our code, I don't really care so much about 35
handling the error. And then we're going to save the output of that method to a constant 36
and I'll just call it prediction. Now, if we, again, command and click on this method called prediction and 37
we jumped to the definition, 38
you can see that the output that it gives you is something called a TweetSentimentClassifierOutput. 39
Now, once we've gotten that prediction, then we can go ahead and print it out. 40
Now, instead of just printing out the prediction which, as you can see, is something of type 41
Tweet SentimentClassifierOutput, we actually want to tap into the property that is called label, and this is the text 42
label that is either positive, negative, or neutral. And that's a String, so piece of text, 43
and that's going to be far more useful to us than the actual prediction object. 44
So, now that's done, 45
let's go ahead and comment out this large JSON that we're going to have cluttering our debug console 46
and we can go ahead and just print the prediction label. 47
So you can see somewhere amongst all of this clutter is the word "Negative." Now, because I'm using the 48
beta version of Xcode 10, 49
often, you see more debug messages in here. But when you're using it, there should be less. 50
But if there isn't, then don't worry about it. It's nothing to do with your app. 51
These are not errors. They're just for your information. But you can see that the prediction that we got 52
back is negative for the text "@Apple is a terrible company!" 53
Now, let's try it with something else. 54
And you can see, this one, we're getting a positive sentiment when it analyzes this new piece of text. 55
So, as you can see, we've now managed to use the sentimentClassifier that we created inside a live app 56
on a live piece of text. 57
So the next step is only a little bit of a jump away where we have to try and use the text that we're 58
getting back from our API call and to pass it through our sentiment classifier. 59
So that is what we're gonna be doing in the next lesson. 60
So for all of that and more, I'll see you on the next lesson.