【问题标题】:Applying TextBlob sentimental analysis to Twitter Stream将 TextBlob 情感分析应用于 Twitter 流
【发布时间】:2018-06-06 17:42:18
【问题描述】:

我目前正在流式传输推文,并希望对每条推文应用情感分析,映射推文和价值。我不断收到错误消息:“NoneType”对象没有属性“限制””。不知道我做错了什么。

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 是我的套接字流:lines = socket_stream.window(30, 10)

标签: twitter pyspark nlp streaming textblob


【解决方案1】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 2020-08-14
    • 2023-04-04
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 2017-10-07
    • 1970-01-01
    相关资源
    最近更新 更多