正在开发一个使用 nltk 进行股票情绪分析的项目。我在 GH 中进行了搜索,发现没有类似的 sentimaent_analysisr 或popularity_scores 调用。
我还查看了Python 3.4 - 'bytes' object has no attribute 'encode'它不是重复的,因为我没有调用 bcrypt.gensalt().encode('utf-8')。尽管它确实暗示了某些类型错误的问题。
谁能帮忙解决这个错误吗?
我收到错误:
/lib/python3.5/site-packages/nltk/sentiment/vader.py in init(self, text) 154 def init(self, text): 155 if not isinstance(text, str): --> 156 text = str(text.encode('utf-8')) 157 self.text = text 158 self.words_and_emoticons = self._words_and_emoticons()
AttributeError: 'bytes' object has no attribute 'encode'
数据帧 df_stocks.head(5) 是:
prices articles
2007-01-01 12469 What Sticks from '06. Somalia Orders Islamist...
2007-01-02 12472 Heart Health: Vitamin Does Not Prevent Death ...
2007-01-03 12474 Google Answer to Filling Jobs Is an Algorithm...
2007-01-04 12480 Helping Make the Shift From Combat to Commerc...
2007-01-05 12398 Rise in Ethanol Raises Concerns About Corn as...
代码如下,最后一行发生错误:
import numpy as np
import pandas as pd
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import subjectivity
from nltk.sentiment import SentimentAnalyzer
from nltk.sentiment.util import *from nltk.sentiment.vader import SentimentIntensityAnalyzer
import unicodedata
for date, row in df_stocks.T.iteritems():
sentence = unicodedata.normalize('NFKD', df_stocks.loc[date, 'articles']).encode('ascii','ignore')
ss = sid.polarity_scores(sentence)
谢谢
最佳答案
来自 unicodedata.normalize() docs ,方法是将UNICODE字符串转换为通用格式字符串。
import unicodedata
print(unicodedata.normalize('NFKD', u'abcdあäasc').encode('ascii', 'ignore'))
它将得到:
b'abcdaasc'
所以,问题就在这里:df_stocks.loc[date, 'articles']
不是 UNICODE 字符串。
关于python - 情感分析器错误: 'bytes' object has no attribute 'encode' using,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46231574/