I am working on a script using Twitter’s API and I am trying to find matches to exact phrases.
The API however doesn’t allow queries for exact phrases so I’ve been trying to find a workaround however I am getting results that contain words from the phrases but not in the exact match as the phrase.
var search_terms = "buy now, look at this meme, how's the weather?";
let termSplit = search_terms.toLowerCase();
let termArray = termSplit.split(', ');
//["buy now", "look at this meme", "how's the weather?"];
client.stream('statuses/filter', { track: search_terms }, function (stream) {
console.log("Searching for tweets...");
stream.on('data', function (tweet) {
if(termArray.some(v => tweet.text.toLowerCase().includes(v.toLowerCase()) )){
//if(tweet.text.indexOf(termArray) > 0 )
console.log(tweet);
}
});
});
Expected results should be a tweet with any text as long as it contains the exact phrase somewhere.
The results I am getting returns tweets that have an array value present but not an exact phrase match of the value.
Example results being returned –
"I don’t know why now my question has a close request but I don’t buy it."
Example results I am expecting –
"If you like it then buy now."
What am I doing wrong?
2
Answers
You could try using regular expressions. Here’s an example of a regular expression search for a phrase. It returns a positive number (the character where the match started) if there is a match, and -1 otherwise. I return the whole phrase if there is a match.
You can use quite sophisticated grammar’s for matching particular phrases of interest, I’m just using simple words in this example.
First, toward the future:
Twitter is planning to deprecate the
statuses/filter
v1.1
endpoint:So, now is a great time to start using the equivalent v
2
API, Filtered Stream, which supports exact phrase matching, helping you avoid this entire scenario in your application code.With that out of the way, below I’ve included a minimal, reproducible example for you to consider which demonstrates how to match exact phrases in streamed tweets, and even extract additional useful information (like which phrase was used to match it and at what index within the tweet text). It includes inline comments explaining things line-by-line: