当前位置:   article > 正文

python中int对象不可迭代_python - 情感分析接收错误:'int'对象不可迭代_python-3.x_酷徒编程知识库...

int迭代不了

我在csv文件上运行情感分析,并且收到这个错误消息,

这是我的代码:def sentimentAFINN(text):

words = pattern_split.split(text.lower())

sentiments = len(list(map(lambda word: afinn.get(word, 0), words)))

if sentiments:

sentiment = float(sum(sentiments))/math.sqrt(len(sentiments))

else:

sentiment = 0

return sentiment

def sentimentDisplayValue(sentimentScore):

if sentimentScore > 0.1:

return"Positive"

elif sentimentScore < -0.1:

return"Negative"

else:

return"Neutral"

totals = defaultdict(int)

for (index, row) in data.iterrows():

text = row['comment']

text_munged = munger(text)

sentimentScore = sentimentAFINN(text_munged)

sentimentDisplay = sentimentDisplayValue(sentimentScore)

totals[sentimentDisplay] = totals[sentimentDisplay] + 1

pt.add_row([text_munged, sentimentScore, sentimentDisplay])

print (pt)

print (totals)

这是错误消息:TypeError Traceback (most recent call last)

in

4 text = row['LikelyToReferComment']

5 text_munged = munger(text)

----> 6 sentimentScore = sentimentAFINN(text_munged)

7 sentimentDisplay = sentimentDisplayValue(sentimentScore)

8 totals[sentimentDisplay] = totals[sentimentDisplay] + 1

in sentimentAFINN(text)

29 sentiments = len(list(map(lambda word: afinn.get(word, 0), words)))

30 if sentiments:

---> 31 sentiment = float(sum(sentiments))/math.sqrt(len(sentiments))

32

33 else:

TypeError: 'int' object is not iterable

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/692415
推荐阅读
相关标签
  

闽ICP备14008679号