How to search a sentence in tweets

95 Views Asked by At

I am trying to gather dataset from Twitter accounts who posted a status update in the form of a statement of diagnosis, such as “I was diagnosed with X today”, where X would represent either depression.

I was being able to use TwitterSearch library but it only searches for the keyword not a full sentence.

    from TwitterSearch import *
    try:
        tso = TwitterSearchOrder() # create a TwitterSearchOrder object
        tso.set_keywords(['depression', 'diagnosed']) # let's define all words we would like to have a look for
        tso.set_language('en') # we want to see English tweets only
        tso.set_include_entities(False) # and don't give us all those entity information
    ts = TwitterSearch(
            consumer_key = 'x',
            consumer_secret = 'y',
            access_token = 'z',
            access_token_secret = 't'
 )
    print( tweet['user']['screen_name'], tweet['text'] )

However I would like to use regular expression to get tweets that match the sentence.

1

There are 1 best solutions below

1
On

You can search full sentences - not only keywords - with set_keywords

from TwitterSearch import *
try:
    tso = TwitterSearchOrder() # create a TwitterSearchOrder object
    tso.set_keywords(['I was diagnosed with depression today'])
    tso.set_language('en') # we want to see English tweets only
    tso.set_include_entities(False)

    ts = TwitterSearch(
        consumer_key = 'c',
        consumer_secret = 's',
        access_token = 'at',
        access_token_secret = 'ats'
     )

     # this is where the fun actually starts :)
    for tweet in ts.search_tweets_iterable(tso):
        print( '@%s tweeted: %s' % ( tweet['user']['screen_name'], tweet['text'] ) )

except TwitterSearchException as e: # take care of all those ugly errors if there are some
    print(e)

So, no need to filter the result with regex.