使用IMDb JSON评论计算情绪极性



我正在尝试使用python的JSON评论来计算平均情绪极性。然而,我的代码要么无限循环以获得所有的极性,要么我会得到一个错误";float对象不可迭代";使用avg=sum(blob.polarity(/len(blob.polarity(

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

[
{
"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.")

我看不出在每次审查文本连接后计算极性的意义

最新更新