我正在按照这里的教程 https://github.com/amueller/introduction_to_ml_with_python/blob/master/07-working-with-text-data.ipynb 了解机器学习和文本。
就我而言,我正在使用我下载的推文,正面和负面推文在他们使用的完全相同的目录结构中(试图学习情绪分析)。
在 iPython Notebook 中,我像他们一样加载我的数据:
tweets_train =load_files('Path to my training Tweets')
然后我尝试用 CountVectorizer 来拟合它们
vect = CountVectorizer().fit(text_train)
我明白了
UnicodeDecodeError:“utf-8”编解码器无法解码位置 561 中的字节 0xd8:无效的继续字节
这是因为我的推文中有各种非标准文本吗?我没有对我的推文进行任何清理(我假设有一些库可以帮助解决这个问题,以便让一袋单词起作用?)
编辑:我使用 Twython 下载推文的代码:
def get_tweets(user):
twitter = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET)
user_timeline = twitter.get_user_timeline(screen_name=user,count=1)
lis = user_timeline[0]['id']
lis = [lis]
for i in range(0, 16): ## iterate through all tweets
## tweet extract method with the last list item as the max_id
user_timeline = twitter.get_user_timeline(screen_name=user,
count=200, include_retweets=False, max_id=lis[-1])
for tweet in user_timeline:
lis.append(tweet['id']) ## append tweet id's
text = str(tweet['text']).replace("'", "")
text_file = open(user, "a")
text_file.write(text)
text_file.close()