-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4-sentiment_analysis.py
44 lines (31 loc) · 1.05 KB
/
4-sentiment_analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from textblob import TextBlob
import pymysql
from googletrans import Translator
# db config
host = "####"
user = "####"
password = "####"
database = "####"
# mysql connection
conn = pymysql.connect(host=host, port=3306, user=user, password=password, database=database)
# mysql cursor
cursor = conn.cursor()
sqlTweets = f"-- Select id, full_text, sentiment from tweets where sentiment is null"
sqlTweets = f"Select id, full_text, sentiment from tweets"
sqlUpdateTweet = f"update tweets set sentiment=%s where id=%s"
sqlUsers = f"Select * from twitter_users where id_str ='%s'"
translator = Translator()
cursor.execute(sqlTweets)
resultTweets = cursor.fetchall()
if resultTweets:
for item in resultTweets:
# todo must check
id = item[0]
comment = item[1]
comment = translator.translate(str(comment), dest='en').text
blob = TextBlob(comment)
polarity = blob.sentiment.polarity
# update tweet
values = (str(polarity), str(id))
print(values)
cursor.execute(sqlUpdateTweet, values)