Python 使用IMDb JSON评论计算情感极性

Python 使用IMDb JSON评论计算情感极性,python,json,sentiment-analysis,Python,Json,Sentiment Analysis,我试图使用python的JSON评论来计算平均情绪极性。然而,我的代码要么无限循环以获得所有极性,要么使用avg=sum(blob.polarity)/len(blob.polarity)得到一个错误“float object is not iterable” JSON审查数据是一个目录列表,如: [ { "date": "xx/xx/xxxx", "rate": "8.0", "

我试图使用python的JSON评论来计算平均情绪极性。然而,我的代码要么无限循环以获得所有极性,要么使用avg=sum(blob.polarity)/len(blob.polarity)得到一个错误“float object is not iterable”

JSON审查数据是一个目录列表,如:

[
  {
    "date": "xx/xx/xxxx",
    "rate": "8.0",
    "Review text": "its such an awesome movie! the actions are intense!"
  },
  ...
]
我的代码:

response = requests.get("https://imdb.com/reviews/ironman.json") #not an actual URL 

if response:

   data = json.loads(response.text)
   content = ""

   for line in data:
       review = line["Review text"]
       content = content + review + " "
    
       blob = textblob.TextBlob(content)
       print("The average review polarity:", blob.polarity)

else:
   print("404 error.")
输出:

The average review polarity: 0.21027251552795012
The average review polarity: 0.20845378442639165
The average review polarity: 0.20358391721811314
……………… looping forever...

我试图得到所有评论的平均极性。基本上,我只需要得到一个极性输出,它是平均值,而不是倍数。我试过做
sum(blob.polarity)/len(blob.polarity),
这样做会给我一个错误。我被卡住了。

我认为你应该多做一些事情,比如:

response = requests.get("https://imdb.com/reviews/ironman.json")
if response.ok:
   reviews = json.loads(response.text)
   content = ' '.join([review["Review text"] for review in reviews])
   blob = textblob.TextBlob(content)
   print("The average review polarity:", blob.polarity)

else:
   print("404 error.")

我看不出在每次查看文本连接后计算极性的意义。好的,如果响应,请显示您的尝试`sum(blob.polarity)/len(blob.polarity)`以便我们可以向您展示如何正确地进行操作(因为这是计算平均值的正确方法)。你的代码中也没有任何东西可以让它永远循环。@martineau-我刚刚在blob=textblob下面添加了avg=float(sum/blob.polarity)/float(len(blob.polarity)。然后打印(“平均审查极性:”,avg)试试调试器。你会惊讶于使用这个基本工具所能学到的东西。@TomServo-如果我的教授教我们如何在colab上使用这个工具,那就太好了,我在任何地方都看不到它。嗨,DevLounge,这太棒了!不过,我们需要计算所有评论的平均值。感谢你的帮助,谢谢!很公平!欢迎你