简体   繁体   English

将数据发布到Sentiment140进行情感分析

[英]Posting data to Sentiment140 for Sentiment Analysis

My aim is to perform at least 3 different types of sentiment analysis on data collected from twitter. 我的目标是对从Twitter收集的数据进行至少3种不同类型的情绪分析。

I'm attempting to perform sentiment analysis on tweets I've gathered using Python and Twitter API and stored in a database (MySQL & PhpMyAdmin). 我正在尝试使用Python和Twitter API收集并存储在数据库(MySQL&PhpMyAdmin)中的推文上进行情感分析。 There are 3 problems I'm having: 我遇到3个问题:

  1. There aren't many API's that perform sentiment analysis (any suggestions would be helpful) 执行情感分析的API并不多(任何建议都将有所帮助)
  2. I'm attempting to connect to the Sentiment140 API but my code is returning the following error 我正在尝试连接到Sentiment140 API,但是我的代码返回了以下错误

TypeError: POST data should be bytes, an iterable of bytes, or a file object. TypeError:POST数据应为字节,字节可迭代或文件对象。 It cannot be of type str. 它不能是str类型。

  1. Is there a general way to connect the Sentiment140 API (or any other sentiment analysis API) with my MySQL database for analysis of the text column in the database? 是否有将Sentiment140 API(或任何其他情感分析API)与MySQL数据库连接以分析数据库中文本列的一般方法?

Python 蟒蛇

import urllib.request
import json

url = 'http://www.sentiment140.com/api/bulkClassifyJson'
values = {'data': [{'text': 'I love Titanic.'}, {'text': 'I hate Titanic.'}]} 

data = json.dumps(values) # instead of urllib.urlencode(values)
response = urllib.request.urlopen(url, data)
page = response.read()

All help will be greatly appreciated, thank you! 所有帮助将不胜感激,谢谢!

As stated in the urllib docs , you have to convert your strs to bytes before feeding it to urlopen: urllib docs中所述,您必须先将strs转换为字节,然后再将其提供给urlopen:

Note that params output from urlencode is encoded to bytes before it is sent to urlopen as data. 请注意,在将urlencode输出的参数作为数据发送到urlopen之前,已将其编码为字节。

You can do so by calling .encode() on data : 您可以通过对data调用.encode()来实现:

import urllib.request
import json

url = 'http://www.sentiment140.com/api/bulkClassifyJson'
values = {'data': [{'text': 'I love Titanic.'}, {'text': 'I hate Titanic.'}]} 

data = json.dumps(values)
response = urllib.request.urlopen(url, data=data.encode("utf-8"))
page = response.read()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM