0
1
00:00:00,300 --> 00:00:08,670
Now, in the last lesson, we used the Swifter framework to authenticate ourselves to work with the Twitter
1

2
00:00:08,670 --> 00:00:09,830
API.
2

3
00:00:09,930 --> 00:00:16,260
We authenticated ourselves using the application that we set up on Twitter and we performed a method
3

4
00:00:16,260 --> 00:00:23,130
called searchTweet using a particular search query specifying that we only wanted the tweets that were
4

5
00:00:23,130 --> 00:00:31,290
in English, and that we wanted the maximum 100 tweets to come back, and also that we want the full 280
5

6
00:00:31,350 --> 00:00:32,130
characters
6

7
00:00:32,160 --> 00:00:39,240
if there are in fact 280 characters in the tweet. And if it was successful, then we basically just print
7

8
00:00:39,270 --> 00:00:43,590
the results of that search inside our debug console.
8

9
00:00:44,010 --> 00:00:46,560
But if there was an error, then we'll print the error.
9

10
00:00:47,790 --> 00:00:55,770
So, now that that's done, we can delete this reference and we can get on with the work of classifying
10

11
00:00:56,280 --> 00:01:01,580
and predicting the sentiment in each of these tweets.
11

12
00:01:01,770 --> 00:01:09,390
Now, in order to use our classifier, we've already previously dragged and dropped it into our project.
12

13
00:01:09,900 --> 00:01:16,290
And you can see that it's automatically created a class called TweetSentimentClassifier. And this is
13

14
00:01:16,290 --> 00:01:23,350
the class that we're going to be using if we want to create a new object to do our classification.
14

15
00:01:23,550 --> 00:01:32,070
And, remember, this file gets created automatically so that we can tap into that class and we can use
15

16
00:01:32,160 --> 00:01:36,270
all of these associated methods for prediction.
16

17
00:01:36,270 --> 00:01:45,210
So let's go over to our ViewController and let's create a new object of our sentiment classifier and
17

18
00:01:45,210 --> 00:01:53,370
I'm just going to call it sentimentClassifier and it's going to be a new object created from the class
18

19
00:01:53,430 --> 00:02:04,230
that is TweetSentimentClassifier. And now, of course, because we're using CoreML, we have to import
19

20
00:02:04,650 --> 00:02:06,000
that framework as well.
20

21
00:02:07,870 --> 00:02:14,020
So, now that we've got our new sentimentClassifier object set up and ready to go, it's time to use it
21

22
00:02:14,080 --> 00:02:18,010
and perform some methods to actually classify our data.
22

23
00:02:18,430 --> 00:02:24,940
So if we head back over to this class that was created again, we can see the methods that are available
23

24
00:02:24,970 --> 00:02:26,210
for our use.
24

25
00:02:26,260 --> 00:02:33,040
Now, one of them says that we can make a prediction using the convenience interface and the parameters
25

26
00:02:33,340 --> 00:02:37,960
only include text which is the input text as a string value.
26

27
00:02:38,230 --> 00:02:43,720
And it could potentially throw an error, but if it was successful, then it'll return the result of the
27

28
00:02:43,720 --> 00:02:52,370
prediction as a TweetSentimentClassifierOutput object. So let's try and use our sentimentClassifier
28

29
00:02:52,670 --> 00:02:58,030
with the simplest type of prediction where we're just using a piece of text.
29

30
00:02:58,190 --> 00:03:04,810
So we're going to use our sentimentClassifier and we're going to use the method called prediction.
30

31
00:03:04,820 --> 00:03:10,760
And you can see that there's several prediction methods, each getting more and more complex. But the simplest
31

32
00:03:10,760 --> 00:03:19,040
type just takes a single String and can throw an error, but if it was successful, then we get a prediction.
32

33
00:03:19,040 --> 00:03:27,620
So let's hit enter. And the text that I'm going to test it with is just something like, say, "@Apple is
33

34
00:03:27,680 --> 00:03:37,600
a terrible company!" And in order to satisfy the part where it can throw, let's mark this with a "try" and
34

35
00:03:37,600 --> 00:03:44,050
force it to go through. Currently, because I'm just testing our code, I don't really care so much about
35

36
00:03:44,050 --> 00:03:50,280
handling the error. And then we're going to save the output of that method to a constant
36

37
00:03:50,320 --> 00:03:59,160
and I'll just call it prediction. Now, if we, again, command and click on this method called prediction and
37

38
00:03:59,160 --> 00:04:00,810
we jumped to the definition,
38

39
00:04:00,810 --> 00:04:07,380
you can see that the output that it gives you is something called a TweetSentimentClassifierOutput.
39

40
00:04:07,380 --> 00:04:12,480
Now, once we've gotten that prediction, then we can go ahead and print it out.
40

41
00:04:12,480 --> 00:04:18,540
Now, instead of just printing out the prediction which, as you can see, is something of type 
41

42
00:04:18,540 --> 00:04:26,700
Tweet SentimentClassifierOutput, we actually want to tap into the property that is called label, and this is the text
42

43
00:04:26,790 --> 00:04:33,860
label that is either positive, negative, or neutral. And that's a String, so piece of text,
43

44
00:04:34,020 --> 00:04:38,130
and that's going to be far more useful to us than the actual prediction object.
44

45
00:04:39,420 --> 00:04:40,830
So, now that's done,
45

46
00:04:40,920 --> 00:04:46,980
let's go ahead and comment out this large JSON that we're going to have cluttering our debug console
46

47
00:04:47,280 --> 00:04:51,210
and we can go ahead and just print the prediction label.
47

48
00:04:57,260 --> 00:05:04,220
So you can see somewhere amongst all of this clutter is the word "Negative." Now, because I'm using the
48

49
00:05:04,220 --> 00:05:06,020
beta version of Xcode 10,
49

50
00:05:06,170 --> 00:05:12,220
often, you see more debug messages in here. But when you're using it, there should be less.
50

51
00:05:12,260 --> 00:05:16,540
But if there isn't, then don't worry about it. It's nothing to do with your app.
51

52
00:05:16,550 --> 00:05:22,250
These are not errors. They're just for your information. But you can see that the prediction that we got
52

53
00:05:22,250 --> 00:05:27,400
back is negative for the text "@Apple is a terrible company!"
53

54
00:05:27,440 --> 00:05:29,270
Now, let's try it with something else.
54

55
00:05:36,950 --> 00:05:43,370
And you can see, this one, we're getting a positive sentiment when it analyzes this new piece of text.
55

56
00:05:43,910 --> 00:05:52,010
So, as you can see, we've now managed to use the sentimentClassifier that we created inside a live app
56

57
00:05:52,370 --> 00:05:55,110
on a live piece of text.
57

58
00:05:55,160 --> 00:06:03,680
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

59
00:06:03,680 --> 00:06:09,620
getting back from our API call and to pass it through our sentiment classifier.
59

60
00:06:10,190 --> 00:06:13,470
So that is what we're gonna be doing in the next lesson.
60

61
00:06:13,490 --> 00:06:16,460
So for all of that and more, I'll see you on the next lesson.
