如何找到一个单词在 tweepy 流中重复的次数?

How do I find the number of times a word has been repeated in a tweepy stream?

我最近一直在 python 中使用 tweepy 库。我写了一个通过推文流式传输的程序。我使用字符串作为过滤器。 我已经根据字符串代表的情绪为列表中的每个字符串分配了一个特定的变量。

    class listener(StreamListener):

    def on_status(self, status):
        try:
            print status
            return True
        except BaseException, e:
            print 'failed on status, ', str(e)
            time.sleep(5)
            if '' in status.text.lower() and 'retweeted_status' not in status:
                print status.text
                print status.coordinates

    def on_error(self, status):
        print status

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
twitterStream = Stream(auth, listener())

api = tweepy.API(auth)


happy=["I love","I'm so happy","feeling joyful","feeling awesome"]
sad=["I'm depressed","I'm very sad","It is painful","feeling terible"]
angry=["I'm furious","wtf!","I'm so pissed off","I'm angry"]
shocked=["rip","omg","I can't believe it","I'm shocked"]
romantic=["I love you","today is my anniversary","feeling romantic","I'm dating"]

twitterStream.filter(track=sad)(track=happy)(track=angry)(track=shocked)(track=romantic)

mood_happy=0
mood_sad=0
mood_angry=0
mood_shocked=0
mood_romantic=0

我想做的是,每次来自任何变量的字符串出现在推文行中时,我希望 mood_n 的值加起来为 1。例如,如果短语 'Feeling Joyful' 在流中出现 5 次。我想要 mood_happy=5 的值。我该怎么做?

抱歉,我知道我不应该 post 此类查询,但我已经在 google 上搜索解决方案好几个小时了,但一直没有找到找到了一点关于这个的信息。 :(

有优化的地方,但基本上是这样的:

import time
from tweepy import StreamListener, OAuthHandler, Stream


twitter_access = {
    "consumer_key"       : "enter_consumer_key",
    "consumer_secret"    : "enter_consumer_secret",
    "access_token"       : "enter_access_token",
    "access_token_secret": "enter_access_token_secret",
}

happy = ["I love", "I'm so happy", "feeling joyful", "feeling awesome"]
sad = ["I'm depressed", "I'm very sad", "It is painful", "feeling terible"]
angry = ["I'm furious", "wtf!", "I'm so pissed off", "I'm angry"]
shocked = ["rip", "omg", "I can't believe it", "I'm shocked"]
romantic = ["I love you", "today is my anniversary", "feeling romantic", "I'm dating"]

mood_happy = 0
mood_sad = 0
mood_angry = 0
mood_shocked = 0
mood_romantic = 0


class Listener(StreamListener):

    def on_status(self, status):
        global mood_happy, mood_sad, mood_angry, mood_shocked, mood_romantic

        try:
            # print status
            tweet_text = status.text
            for mood_n_score in [[happy, 'mood_happy'], [sad, 'mood_sad'], [angry, 'mood_angry'],
                                 [shocked, 'mood_shocked'], [romantic, 'mood_romantic']]:
                lst_mood = mood_n_score[0]
                type_mood = mood_n_score[1]

                for mood in lst_mood:
                    if mood in tweet_text:
                        if type_mood == 'mood_happy':
                            mood_happy += 1
                        elif type_mood == 'mood_sad':
                            mood_sad += 1
                        elif type_mood == 'mood_angry':
                            mood_angry += 1
                        elif type_mood == 'mood_shocked':
                            mood_shocked += 1
                        else:
                            mood_romantic += 1
                        break

            print('\n----------------')
            print 'mood_happy:', mood_happy
            print 'mood_sad:', mood_sad
            print 'mood_angry:', mood_angry
            print 'mood_shocked:', mood_shocked
            print 'mood_romantic:', mood_romantic

            # return True

        except BaseException, e:
            print 'failed on status, ', str(e)
            time.sleep(5)
            if '' in status.text.lower() and 'retweeted_status' not in status:
                print status.text
                print status.coordinates

    def on_error(self, status):
        print status

auth = OAuthHandler(twitter_access["consumer_key"], twitter_access["consumer_secret"])
auth.set_access_token(twitter_access["access_token"], twitter_access["access_token_secret"])
# api = API(auth)
twitterStream = Stream(auth, Listener(), timeout=None)

for list_of_mood in [happy, sad, angry, shocked, romantic]:
    twitterStream.filter(track=list_of_mood)