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