将 TextBlob 情感分析应用于 Twitter Stream

Applying TextBlob sentimental analysis to Twitter Stream

我目前正在流式传输推文,并希望对每条推文应用情感分析,映射推文和价值。我不断收到错误消息:"NoneType' object has no attribute 'limit'"。不确定我做错了什么。

from textblob import TextBlob
from textblob import Blobber
from textblob.sentiments import NaiveBayesAnalyzer

tb = Blobber(analyzer=NaiveBayesAnalyzer())
def tweet_sentiment(tweet):
'''function for polarity'''
  sentiment = tb(tweet)
  if analysis.sentiment.polarity > .5:
      return 1
  elif analysis.sentiment.polarity < .5:
      return -1
  else:
      return 0


(lines.flatMap(lambda x: (x, tweet_sentiment))
.map(lambda rec: Tweet(rec[0], rec[1]))
.foreachRDD(lambda rdd: rdd.toDF())
.limit(20).registerTempTable("tweets"))


    ------------------------------------------------------------------- 
    --------
    AttributeError                            Traceback (most recent 
    call last)
    <ipython-input-8-d939b88ef526> in <module>()
          2 (lines.flatMap(lambda x: (x, tweet_sentiment))
          3 .map(lambda rec: Tweet(rec[0], rec[1]))
    ----> 4 .foreachRDD(lambda rdd: rdd.toDF())
          5 .limit(20).registerTempTable("tweets"))

    AttributeError: 'NoneType' object has no attribute 'limit'

试试这个:

(lines.flatMap(lambda x: (x, tweet_sentiment))
.map(lambda rec: Tweet(rec[0], rec[1]))
.foreachRDD(lambda rdd: rdd.toDF())
.registerTempTable("tweets"))