Do we able to get keyword searched from the tweets by using Search.SearchTweets()?

Jun 25, 2014 at 3:56 PM
Edited Jun 25, 2014 at 3:57 PM
So, basically i have a list of keywords that are strings. I added them in one string. For instance: I have "apple", "obama" and "pencil" words. Thus my newly created string variable will look like this: "apple OR obama OR pencil". This type of search works in both of the code and the search box of the twitter account. Then, I sent that string as a parameter to Search.SearchTweets(). Now, i want to get the keyword from the tweets that returns. Thus, i will know which keyword caused that tweet to return as a result.

Thanks.
Coordinator
Jun 30, 2014 at 7:59 PM
Hi there,

Sorry for taking so much time to reply to your post. I usually respond within hours but I have been working a lot lately.

This feature is something that you have to implement by doing a simple regex/search over the Tweet.Text foreach of your keyworkds.
// This code can be improved for performances
var keywords = new[] { "obama", "apple" };
var search = keywords[0];
keywords.ForEach(x => search += "OR" + x);

var tweets = Search.SearchTweets(search);

foreach (var tweet in tweets)
{
    foreach (var keyword in keywords)
    {
        if (tweet.Text.Contains(keyword))
        {
            // Your code here to manage it
        }
    }
}
Though, if you are willing to use the Stream API, Tweetinvi does that for you.
var keywords = new[] { "obama", "apple" };
var stream = Stream.CreateFilteredStream();
            
foreach (var keyword in keywords)
{
    stream.AddTrack(keyword);
}
            
stream.MatchingTweetReceived += (sender, args) =>
{
    var matchingKeywords = args.MatchingTracks;
    var tweet = args.Tweet;
};

stream.StartStreamMatchingAllConditions();
Kind Regards,
Linvi