Now, you can see that if I collapse this, we're only getting 15 items back, so 15 tweets back from the API 1
requests. 2
Now, why is that and how can we increase it? 3
So let's head back to our Twitter documentation because all queries relating to any sort of API we're 4
using, we have to try and find the answer in the API docs. And if you read through all of these parameters, 5
then you come across one called count. 6
You can see this is an optional parameter so you don't have to specify a value for it. But if you do, 7
then you can specify the number of tweets to return per page up to a maximum of 100, and it defaults 8
to 15. 9
That's why we're getting 15 results back. So we can increase it to 100 so that we can reach the maximum. 10
And, obviously, the more tweets that we use and we process through our sentiment analysis, the more accurate 11
our overall feel of the topic is, right? 12
If we've got the sentiment of how people feel about Apple using our search of @Apple and it's only 13
based on 15 tweets, then it's obviously not going to be as accurate as if we did it on 100. 14
So let's go ahead and add this count parameter to our search tweet method. 15
So I'm going to add it just after the part where it says "using," and I'm going to say "count," and I'm going 16
to increase it to 100. 17
So, now if we rerun our app, and you can see that it was printing for a little bit longer, 18
but let's go ahead and copy all of that and, again, place it into our JSON editor. 19
And again, of course, go right to the top and delete these parts from the debugger and put it over here. 20
And then let's hit the right button and let's try and turn it into a JSON, and we can see we now have 21
a 100 results in our array. 22
So we've now managed to successfully increase to the maximum number of tweets we can get back from Twitter. 23
And, of course, if you needed even more tweets to come back from your search, you can either perform the 24
search multiple times increasing the date range, for example, or you can go to one of the premium plans 25
or the enterprise plans, and they'll allow you to fetch back more results as well. 26
But for our purposes, this should be more than sufficient. 27
Now, the other thing that you might notice when you have a look at the text of the tweets that we're 28
getting back is that some of them are not in English, right? 29
For example, this one looks like Turkish, I think. 30
And remember that our sentiment analysis, analyzer rather, was trained on English tweets, so we can only 31
interpret the sentiment in English. 32
So instead of getting back, you know, lots of tweets in different languages, we can try and only ask for 33
the tweets that are in English. And that, of course, is yet another parameter that we can include which 34
is called language. 35
And this will restrict the tweets we get back to the given language given by an ISO code. 36
So if you go ahead and click on here, you can see the ISO codes for all of the countries, and English 37
is usually across the board denoted by "en." 38
So let's go ahead and add that parameter to our list of parameters. 39
Now, the place where you add it kind of matters because if you place it behind the "count"--let's see. 40
You'll see that you get an error here from Xcode, And it says that the argument language must precede 41
the argument count and it is giving you an automatic suggested fix. 42
So let's go ahead and fix that. And you can see it's basically moved this parameter and its value to 43
before the one that says count. 44
And that's because in the method that is searchTweet, they've specified the order that all these parameters 45
must come in, and the order follows the order that you see them in this table, 46
so geocode comes before language, language comes before locale, et cetera. But Xcode's quite helpful in 47
this case, actually, pointing it out to you and fixing it for you. 48
So, now if we run this again, specifying that we only want the tweets that are in English, then you can 49
see if I go again over to JSON editor 50
and I delete all of that, and here I'm actually getting a parse error here. And this is because the location 51
of a particular tweet is in this format, it's actually Halifax Nova Scotia. 52
But in this particular JSON editor, it doesn't like that format. 53
So we can go ahead and try to locate it line 5188. 54
So let's try and find that 5188. 55
You can see it's got a little cross next to it. 56
So let's just go ahead and delete that because we don't really care for it anyways. 57
Let's go ahead and click right button. 58
So, now we have our 100 tweets. 59
And if you look at them, they should all have text 60
that's in English, right? 61
So this will make it much easier for our sentiment analyzer to be able to process the tweets that we 62
got here. 63
Now, the next problem that you might notice if you look through a lot of these tweets is that the text 64
that we're getting back is occasionally truncated, and it gets cut off at 140 characters. 65
So if you don't remember this dramatic piece of news, don't worry, it wasn't that dramatic. 66
But, recently, Twitter lifted the count for the number of characters from 140 to 280. So when you make 67
a standard search request to the Twitter API, it gives you all the text back in 140 characters. It truncates 68
at the 140th character. But, of course, because people are able to tweet at 280 characters, 69
occasionally, you're getting missing bits of text. 70
So what can we do? 71
Well, we can either trawl through this documentation and, eventually, you'll find a guarantee, the part 72
that tells you why this is and how you can fix it. 73
But as a developer, you're going to come across these situations that you've never encountered before 74
and you have to remember that your best friend is Google and Stack Overflow. 75
So let's try and see if we can come up with a succinct and concise description of our problem and give 76
it to Mr. Google to try and figure out. 77
So "twitter api search tweets truncated." 78
Let's see what we get from that. 79
So the first result we get back is a Stack Overflow question that says, "Twitter api text field value 80
is truncated," which is pretty much exactly our problem. 81
And you have to remember at all times that it's very, very rare that you will ever come across a problem 82
that nobody else has encountered before. 83
So it's just a matter of getting to the right keywords or finding where somebody has posed that question 84
or answered that question for you. 85
So they have exactly the problem that we're describing. Over here, you see their tweet in the text property 86
is truncated and they want it to be the full text. 87
So the first answer with 28 upvotes is saying that the Twitter API has been recently changed to support 88
the new rule regarding 280 character limit. To get the full text, 89
we have to add the parameter tweet_mode with a value of extended to your request parameters. And instead 90
of looking for text in the JSON response, we look for something called full_text. And there's a link to 91
more info. 92
If we scroll down here, you can see that there is a parameter called result_type and it's, again, an optional 93
one. But it allows you to specify what type of search results you would prefer to receive. 94
So we need to add this parameter tweet_mode. 95
If we go back to our searchTweet method and if we, again, pull up that full method by going to swifter 96
searchTweet and we automatically insert all of that, and I'm just going to comment it out because I'm 97
not going to use it as code, I'm just gonna use it as a reference, and you can see that one of these parameters 98
is something called tweetMode. And this comes just before the success callback. 99
So we can add it right here. 100
So it's called tweetMode. And it's expecting something with the data type of tweetMode. 101
So here, what we need is to say .extended. And you can see as you start typing, you're getting that 102
tweetMode being suggested to you. 103
Now, at this point, you might wonder, how did I know to use an enum for the tweet mode using .extended 104
because I can see that it's expecting something of class TweetMode, 105
but I don't know what it is and what are the options. 106
So one of the things that you can do inside Xcode is, instead, of using the option key where we try to 107
look at the Discussion and Summary and Definition, one of the other things you can do is hold down the 108
command key and click on a method. 109
And this gives you a number of things, such as change the name of the method, or go and find the place 110
where it was called, or show quick help. 111
But there's another one called Jump to Definition, and this takes you to the place where that method 112
was created. 113
So in this case, this is a file in the Swifter framework called SwifterSearch.swift 114
and this is where the definition of the searchTweet function is declared. And this function has a parameter 115
called tweetMode and it's normally set to default. 116
Now we can see this thing called TweetMode. 117
If you hold down the command key again and jump to its definition, then you can see where that enum was 118
created. 119
And you can see all of the cases that you can use, for example, .default, .extended, .compat, and 120
the one that we want is, of course, extended so that we send this to the Twitter API and we get back the 121
full text of the tweet. 122
So, now let's head back to our ViewController and try and run our app now with the tweetMode set as 123
extended. 124
And if you remember previously, we had the 29th tweet showing a truncated text. 125
So, now if we copy the new result that we got from that running and paste it in here 126
and, again, deleting that part, and fixing the error on 8026, 127
and we look through these tweets, you can see that we're now getting a much longer number of characters 128
up to 280, and it's now under a property called full_text rather than the previous one which was text. 129
So, now that we're able to get all the tweets we want in the format that we want, it's time to run our 130
machine learning model on it to start predicting what is the sentiment in each of these tweets. 131
And you can see, as a human, I'm reading this tweet and I feel like it's a tad negative talking about 132
how Apple never tweets, 133
they just promote their ads. 134
And although you could also argue that it's neutral, but let's see what our model thinks, and let's run 135
it through to see what we get. 136
So in the next lesson, 137
that's exactly what we're gonna be doing. 138
We're going to be applying our prediction to these tweets. 139
So for all of that and more, I'll see you on the next lesson.