如何将分析的Twitter流数据从生产者发送到消费者

时间:2019-04-11 02:54:22

标签: python pyspark tweepy sentiment-analysis twitter-streaming-api

我正在尝试对Twitter进行实时情绪分析。我能够将Twitter流数据发送给消费者。目前,我可以在将Twitter数据流传输到消费者时进行情感分析。因此,基本上我可以立即对流数据进行分析。但是现在我不知道如何将原始的Twitter数据和分析结果同时发送给消费者。

我正在使用kafka-python和pyspark。我尝试创建在生产者代码中完成的分析结果的数据框

python: This is how i get the streaming data and do the sentiment 
analysis


class StdOutListener(StreamListener):
def __init__(self, producer):
    self.producer_obj = producer

def on_data(self, data):

    try:
        self.producer_obj.send("twitterstreamingdata", data.encode('utf-8'))
        global initime
        t = int(calctime(initime))
        all_data = json.loads(data)
        tweet = all_data["text"]
        # username=all_data["user"]["screen_name"]
        tweet = " ".join(re.findall("[a-zA-Z]+", tweet))
        blob = TextBlob(tweet.strip())

        global positive
        global negative
        global compound
        global count

        count = count + 1
        senti = 0
        for sen in blob.sentences:
            senti = senti + sen.sentiment.polarity
            if sen.sentiment.polarity >= 0:
                positive = positive + sen.sentiment.polarity
            else:
                negative = negative + sen.sentiment.polarity
        compound = compound + senti
        print(count)
        print(tweet)
        print(senti)
        print(t)
        print(str(positive) + ' ' + str(negative) + ' ' + str(compound))



    except BaseException as e:
        print("Error on_data: %s" % str(e))
    return True

由于我不知道如何发送经过分析的数据,所以我只能设法获取原始的Twitter数据。

0 个答案:

没有答案