首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NLTK VADER情绪分析-无法解决此错误

NLTK VADER情绪分析-无法解决此错误
EN

Stack Overflow用户
提问于 2020-05-25 12:26:28
回答 1查看 397关注 0票数 0

我删除了所有我认为可能导致错误的特殊字符。仍然得到这个我不能理解的错误:

代码语言:javascript
复制
python3 sentiment.py
Traceback (most recent call last):
  File "sentiment.py", line 14, in <module>
    score = sentiment.polarity_scores(headline)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/nltk/sentiment/vader.py", line 360, in polarity_scores
    sentitext = SentiText(text, self.constants.PUNC_LIST,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/nltk/sentiment/vader.py", line 270, in __init__
    text = str(text.encode("utf-8"))
AttributeError: 'float' object has no attribute 'encode'

这就是我所拥有的:

代码语言:javascript
复制
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
import pandas as pd
from pandas import DataFrame
import numpy as np

sentiment = SentimentIntensityAnalyzer()

testset = pd.read_csv("CNNs.csv")

Result = { 'pos':[] , 'neg':[], 'compound':[], 'neu':[] }

for headline in testset['Title']:
    score = sentiment.polarity_scores(headline)
    Result['pos'].append(score['pos'])
    Result['neu'].append(score['neu'])
    Result['neg'].append(score['neg'])
    Result['compound'].append(score['compound'])

testset['pos'] = pd.DataFrame(Result)['pos']
testset['neu'] = pd.DataFrame(Result)['neu']
testset['neg'] = pd.DataFrame(Result)['neg']
testset['compound'] = pd.DataFrame(Result)['compound']

testset.to_csv('CNN.csv', index=False)

如果有人知道如何解决或解决这个问题,我们将非常非常感激。提前谢谢你!

.csv文件示例:

代码语言:javascript
复制
Date,Title
04/21/2012,Tao Girls Taobao Chinese Shopping Site To Offer Model Delivery Service
04/21/2012,Giant George The Biggest Dog In The World PHOTOS
04/21/2012,Tiny Hands at Work Before Their Time
04/21/2012,James Van Der Beek I Would Never Do Dancing With The Stars
04/21/2012,Supernatural Recap Bobby Returns in Of Grave Importance
04/21/2012,Nikita Recap Of Wrath Nikita Embraces Her Dark Side
04/21/2012,Celebrity Photos Of The Week Coachella And Baby Bumps PHOTOS
04/21/2012,Hilary Duff Post Baby Body Actress Looks Great One Month After Giving Birth PHOTOS
04/21/2012,Kim Kardashian Without Makeup Kris Jenner Tweets Creepy Photo Of Kim Sleeping PHOTOS
04/22/2012,International Stolen Asset Recovery As a Development Issue   A World Bank Presidents Legacy
04/22/2012,Poetry Helps the Healing After the Fukushima Disaster
04/22/2012,France Election Results 2012 Hollande Sarkozy Advance To Runoff Far Right Gets 20 Percent
04/22/2012,Jennifer Connelly Blonde For New Movie Virginia WATCH
04/22/2012,Cybill Shepherds Polka Dot Tie 1971 A Look Back
04/22/2012,American Idol What They Wore 4/18/12 Top 7 Redux
EN

回答 1

Stack Overflow用户

发布于 2020-05-25 19:12:33

不要紧,知道了。需要在应用polarity_scores函数之前将标题(标题)转换为字符串

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61995779

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档