skip to Main Content

This is for tweepy. It says

TwitterClient not defined.

import re 
import tweepy 
from tweepy import OAuthHandler 
from textblob import TextBlob 
class TwitterClient(object):
    '''
   Generic Twitter Class for sentiment analysis. 
    '''
    def __init__(self): 
        ''' 
    Class constructor or initialization method. 
        '''
        # keys and tokens from the Twitter Dev Console 
        consumer_key = 'remove'
        consumer_secret = 'remove'
        access_token = 'remove-remove'
        access_token_secret = 'remove'

        # attempt authentication 
        try: 
        # create OAuthHandler object 
            self.auth = OAuthHandler(consumer_key, consumer_secret) 
        # set access token and secret 
            self.auth.set_access_token(access_token, access_token_secret) 
        # create tweepy API object to fetch tweets 
            self.api = tweepy.API(self.auth) 
        except: 
            print("Error: Authentication Failed") 

    def clean_tweet(self, tweet): 
        ''' 
        Utility function to clean tweet text by removing links, special characters 
        using simple regex statements. 
        '''
        return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z t])|(w+://S+)", " ", tweet).split())
    def get_tweet_sentiment(self, tweet): 
        ''' 
        Utility function to classify sentiment of passed tweet 
        using textblob's sentiment method 
        '''
        # create TextBlob object of passed tweet text 
        analysis = TextBlob(self.clean_tweet(tweet)) 
        # set sentiment 
        if analysis.sentiment.polarity > 0: 
            return 'positive'
        elif analysis.sentiment.polarity == 0: 
            return 'neutral'
        else: 
            return 'negative'
    def get_tweets(self, query, count = 10): 
        ''' 
        Main function to fetch tweets and parse them. 
        '''
        # empty list to store parsed tweets 
        tweets = [] 
        try: 
            # call twitter api to fetch tweets 
                fetched_tweets = self.api.search(q = query, count = count) 

            # parsing tweets one by one 
                for tweet in fetched_tweets: 
                # empty dictionary to store required params of a tweet 
                    parsed_tweet = {} 

                # saving text of tweet 
                    parsed_tweet['text'] = tweet.text 
                # saving sentiment of tweet 
                    parsed_tweet['sentiment'] = self.get_tweet_sentiment(tweet.text)
                # appending parsed tweet to tweets list 
                    if tweet.retweet_count > 0: 
                    # if tweet has retweets, ensure that it is appended only once 
                        if parsed_tweet not in tweets: 
                            tweets.append(parsed_tweet) 
                        else: 
                            tweets.append(parsed_tweet) 

                # return parsed tweets 
                return tweets 

        except tweepy.TweepError as e: 
            #print error (if any) 
                print("Error : " + str(e)) 

    def main():
            #creating object of TwitterClient Class 
        api = TwitterClient()
            #calling function to get tweets 
        tweets = api.get_tweets(query = 'ADF', count = 200) 

        #picking positive tweets from tweets 
        ptweets = [tweet for  tweet in tweets if tweet['sentiment'] == 'positive'] 
    #       percentage of positive tweets 
        print("Positive tweets percentage: {} %".format(100*len(ptweets)/len(tweets))) 
            #picking negative tweets from tweets 
        ntweets = [tweet for tweet in tweets if tweet['sentiment'] == 'negative'] 
            #percentage of negative tweets 
        print("Negative tweets percentage: {} %".format(100*len(ntweets)/len(tweets))) 
            #percentage of neutral tweets 
        netweets = [tweet for tweet in tweets if tweet['sentiment'] == 'neutral']
        print("Neutral tweets percentage: {} %".format(100*(len(netweets)/len(tweets))))
            #printing first 5 positive tweets 
        print("nnPositive tweets:")
        for tweet in ptweets[:10]:
            print(tweet['text'])
            #printing first 5 negative tweets  
        print("nnNegative tweets:") 
        for tweet in ntweets[:10]: 
            print(tweet['text']) 
    if __name__ == "__main__": 
                #calling main function 
            main()

2

Answers


  1. Here is a cut-down version of your code which demonstrates the problem.

    class TwitterClient(object):
        None
    
        def main():
            api = TwitterClient()
            print("main()")
    
    
        if __name__ == "__main__":
            main()
    

    Note the indentation of both main() and if __name__ == "__main__" place them under the definition of TwitterClient itself. Hence the error, in Python 3:

    Traceback (most recent call last):
      File "twitter-55610165.py", line 2, in <module>
        class TwitterClient(object):
      File "twitter-55610165.py", line 11, in TwitterClient
        main()
      File "twitter-55610165.py", line 6, in main
        api = TwitterClient()
    NameError: name 'TwitterClient' is not defined
    

    TwitterClient is not defined because the class definition of TwitterClient has not finished – you’re still inside it. The if is at the class scope level, so runs at time of definition of the class. Indentation determines a lot about scope in Python.

    With small but important changes in whitespace, to take main() and if __name__ ... out of the TwitterClient scope and put them back at the main scope, the problem goes away.

    class TwitterClient(object):
        None
    
    def main():
        api = TwitterClient()
        print("main()")
    
    
    if __name__ == "__main__":
        main()
    

    ie these constructs are now at the same indent level as TwitterClient, further left by one level of indentation.

    $ python3 twitter-55610165.py
    main()
    
    Login or Signup to reply.
  2. An easy solution is to remove main() from the TwitterClient() definition.
    The exact problem is that main() is inside the TwitterClient(), so in other words you haven’t finished defining TwitterClient(), so python is throwing errors.

    How to fix

    The easiest solution is to move main() and if __name__... lines from the TwitterClient() definition. That will get rid of your current error. This code should work:

    import re
    import tweepy
    from tweepy import OAuthHandler
    from textblob import TextBlob
    class TwitterClient(object):
    ”’
    Generic Twitter Class for sentiment analysis.
    ”’
    def init(self):
    ”’
    Class constructor or initialization method.
    ”’
    # keys and tokens from the Twitter Dev Console
    consumer_key = ‘remove’
    consumer_secret = ‘remove’
    access_token = ‘remove-remove’
    access_token_secret = ‘remove’

            # attempt authentication 
            try: 
            # create OAuthHandler object 
                self.auth = OAuthHandler(consumer_key, consumer_secret) 
            # set access token and secret 
                self.auth.set_access_token(access_token, access_token_secret) 
            # create tweepy API object to fetch tweets 
                self.api = tweepy.API(self.auth) 
            except: 
                print("Error: Authentication Failed") 
    
        def clean_tweet(self, tweet): 
            ''' 
            Utility function to clean tweet text by removing links, special characters 
            using simple regex statements. 
            '''
            return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z t])|(w+://S+)", " ", tweet).split())
        def get_tweet_sentiment(self, tweet): 
            ''' 
            Utility function to classify sentiment of passed tweet 
            using textblob's sentiment method 
            '''
            # create TextBlob object of passed tweet text 
            analysis = TextBlob(self.clean_tweet(tweet)) 
            # set sentiment 
            if analysis.sentiment.polarity > 0: 
                return 'positive'
            elif analysis.sentiment.polarity == 0: 
                return 'neutral'
            else: 
                return 'negative'
        def get_tweets(self, query, count = 10): 
            ''' 
            Main function to fetch tweets and parse them. 
            '''
            # empty list to store parsed tweets 
            tweets = [] 
            try: 
                # call twitter api to fetch tweets 
                    fetched_tweets = self.api.search(q = query, count = count) 
    
                # parsing tweets one by one 
                    for tweet in fetched_tweets: 
                    # empty dictionary to store required params of a tweet 
                        parsed_tweet = {} 
    
                    # saving text of tweet 
                        parsed_tweet['text'] = tweet.text 
                    # saving sentiment of tweet 
                        parsed_tweet['sentiment'] = self.get_tweet_sentiment(tweet.text)
                    # appending parsed tweet to tweets list 
                        if tweet.retweet_count > 0: 
                        # if tweet has retweets, ensure that it is appended only once 
                            if parsed_tweet not in tweets: 
                                tweets.append(parsed_tweet) 
                            else: 
                                tweets.append(parsed_tweet) 
    
                    # return parsed tweets 
                    return tweets 
    
            except tweepy.TweepError as e: 
                #print error (if any) 
                    print("Error : " + str(e)) 
    
    def main():
            #creating object of TwitterClient Class 
        api = TwitterClient()
            #calling function to get tweets 
        tweets = api.get_tweets(query = 'ADF', count = 200) 
    
            #picking positive tweets from tweets 
        ptweets = [tweet for  tweet in tweets if tweet['sentiment'] == 'positive'] 
            #percentage of positive tweets 
        print("Positive tweets percentage: {} %".format(100*len(ptweets)/len(tweets))) 
            #picking negative tweets from tweets 
        ntweets = [tweet for tweet in tweets if tweet['sentiment'] == 'negative'] 
            #percentage of negative tweets 
        print("Negative tweets percentage: {} %".format(100*len(ntweets)/len(tweets))) 
            #percentage of neutral tweets 
        netweets = [tweet for tweet in tweets if tweet['sentiment'] == 'neutral']
        print("Neutral tweets percentage: {} %".format(100*(len(netweets)/len(tweets))))
            #printing first 5 positive tweets 
        print("nnPositive tweets:")
        for tweet in ptweets[:10]:
            print(tweet['text'])
            #printing first 5 negative tweets  
        print("nnNegative tweets:") 
        for tweet in ntweets[:10]: 
                print(tweet['text']) 
    if __name__ == "__main__": 
        #calling main function 
        main()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search