将TextBlob情感分析应用于Twitter Stream

时间:2018-06-06 17:42:19

标签: twitter pyspark nlp streaming textblob

我目前正在发送推文,并希望对每条推文应用情感分析,同时映射推文和价值。我一直收到错误:“NoneType”对象没有属性'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'

1 个答案:

答案 0 :(得分:0)

试试这个:

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