-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_data_to_db.py
223 lines (211 loc) · 8.54 KB
/
push_data_to_db.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import json
import csv
from datetime import datetime
import numpy as np
import MySQLdb
def generateFeature(topicName, filename, label):
data = json.load(open(filename))
numberItem = len(data)
if (numberItem <= 0):
return None
vector =[]
def increaseBag(key, bag):
if key in bag.keys():
bag[key]+=1
else:
bag[key]=1
def calShannon(bag):
sum=0
for item in bag:
temp = bag[item]/ numberItem
sum += temp*np.log(temp)
return -1*sum
#Average number of retweet levels in tweets.
depth_retweets=0
#Ratio of tweets that contain a retweet.
ratio_retweets=0
#Average number of hashtags in tweets.
hashtags=0
#Average length of tweets
length=0
#Number of tweets with exclamation signs.
exclamations=0
#Number of question signs in tweets.
questions=0
#Average number of links in tweets
links=0
#Average number of uses of the trending topic in tweets.
topicRepetition=0
#Average number of tweets that are replies to others
replies=0
#Average number of tweets per second in the trend
spreadVelocity=0 # 2
#Shannon’s diversity index of users who posted tweets
user_diversity={}
#Shannon’s diversity index of users who were retweeted in the trend (not the user who retweets).
retweeted_user_diversity={}
#Shannon’s diversity index of hashtags included in the trend.
hashtag_diversity={}
#Shannon’s diversity index of languages used in the trend.
language_diversity={}
#Shannon’s diversity index of terms contained in the trend.
vocabulary_diversity={}
#every json tweet
for tweetJson in data:
tweetJson = data[tweetJson]
#print (tweetJson)
if tweetJson['retweet_count']>0 : depth_retweets+=1
if tweetJson['retweet_count']>0 : ratio_retweets+=1
hashtags+=len(tweetJson['arr_hashtags'])
length+=len(tweetJson['tweet'])
if '!' in tweetJson['tweet']: exclamations+=1
if '?' in tweetJson['tweet']: questions+=1
links+=tweetJson['links']
topicRepetition += tweetJson['tweet'].lower().count(topicName.lower())
if tweetJson['isReplies']>0:replies+=1
#time = tweetJson['created']
#stringTime = time[4:7]+' '+time[8:10]+' '+time[-4:]+' '+time[11:13]+':'+time[14:16]+':'+time[17:19]
#print (stringTime)
#datetime_object = datetime.strptime(stringTime, '%b %d %Y %H:%M:%S')
#print (datetime_object)
increaseBag(tweetJson['userId'], user_diversity)
if tweetJson['retweet_count']>0:
increaseBag(tweetJson['userId'], retweeted_user_diversity)
for hashtag in tweetJson['arr_hashtags']:
increaseBag(hashtag['text'], hashtag_diversity)
increaseBag(tweetJson['lang'], language_diversity)
newBag = [w.lower() for w in tweetJson['tweet'].split()]
if len(newBag)>0:
for word in newBag:
increaseBag(word, vocabulary_diversity)
#Average number of tweets per second in the trend
time = data[next(iter(data))]['created']
stringTime = time[4:7]+' '+time[8:10]+' '+time[-4:]+' '+time[11:13]+':'+time[14:16]+':'+time[17:19]
datetime_objectBegin = datetime.strptime(stringTime, '%b %d %Y %H:%M:%S')
temp = data.popitem()
time = temp[1]['created']
stringTime = time[4:7]+' '+time[8:10]+' '+time[-4:]+' '+time[11:13]+':'+time[14:16]+':'+time[17:19]
datetime_objectEnd = datetime.strptime(stringTime, '%b %d %Y %H:%M:%S')
spreadVelocity = datetime_objectBegin - datetime_objectEnd
#fill value to vector
vector.append(depth_retweets/numberItem)
vector.append(ratio_retweets/numberItem)
vector.append(hashtags/numberItem)
vector.append(length/numberItem)
vector.append(exclamations/numberItem)
vector.append(questions/numberItem)
vector.append(links/numberItem)
vector.append(topicRepetition/numberItem)
vector.append(replies/numberItem)
if spreadVelocity.total_seconds() > 0:
vector.append(numberItem/spreadVelocity.total_seconds())
else:
vector.append(0)
vector.append(calShannon(user_diversity))
vector.append(calShannon(retweeted_user_diversity))
vector.append(calShannon(hashtag_diversity))
vector.append(calShannon(language_diversity))
vector.append(calShannon(vocabulary_diversity))
if label =='ongoing-event':
vector.append(0)
elif label =='news':
vector.append(1)
elif label =='meme':
vector.append(2)
elif label =='commemorative':
vector.append(3)
return vector
arrVectors = []
# Open database connection
db = MySQLdb.connect("localhost", "htduongdl96", "motorola", "DBtweet")
# prepare a cursor object using cursor() method
cursor = db.cursor()
try:
sql = "use DBtweet"
cursor.execute(sql)
sql = """CREATE TABLE ALL_TWEET_VECTOR (
ID INT NOT NULL AUTO_INCREMENT,
TREND CHAR(200) CHARACTER SET utf8 COLLATE utf8_general_ci,
TIME VARCHAR(30),
DEPTH_RETWEETS FLOAT ,
RATIO_RETWEETS FLOAT ,
HASHTAGS FLOAT ,
LENGTH FLOAT ,
EXCLAMATIONS FLOAT ,
QUESTIONS FLOAT ,
LINKS FLOAT ,
TOPICREPETITION FLOAT ,
REPLIES FLOAT ,
SPREADVELOCITY FLOAT ,
USER_DIVERSITY FLOAT ,
RETWEETED_USER_DIVERSITY FLOAT ,
HASHTAG_DIVERSITY FLOAT ,
LANGUAGE_DIVERSITY FLOAT ,
VOCABULARY_DIVERSITY FLOAT ,
CLASS INT,
CONFIRMED TINYINT(1),
PRIMARY KEY (ID)
)"""
cursor.execute(sql)
sql = """CREATE TABLE TWEET_VECTOR_TRAIN (
ID INT NOT NULL AUTO_INCREMENT,
TREND CHAR(200) CHARACTER SET utf8 COLLATE utf8_general_ci,
TIME VARCHAR(30),
DEPTH_RETWEETS FLOAT ,
RATIO_RETWEETS FLOAT ,
HASHTAGS FLOAT ,
LENGTH FLOAT ,
EXCLAMATIONS FLOAT ,
QUESTIONS FLOAT ,
LINKS FLOAT ,
TOPICREPETITION FLOAT ,
REPLIES FLOAT ,
SPREADVELOCITY FLOAT ,
USER_DIVERSITY FLOAT ,
RETWEETED_USER_DIVERSITY FLOAT ,
HASHTAG_DIVERSITY FLOAT ,
LANGUAGE_DIVERSITY FLOAT ,
VOCABULARY_DIVERSITY FLOAT ,
CLASS INT,
CONFIRMED TINYINT(1),
PRIMARY KEY (ID)
)"""
# cursor.execute(sql)
cursor.execute(sql)
sql = """CREATE TABLE DETAIL_TWEET (
ID INT NOT NULL,
ID_TWEET INT)"""
cursor.execute(sql)
except:
pass
with open('TT-annotations.csv', newline='', encoding="utf8") as csvfile:
trendingTopicArr = csv.reader(csvfile, delimiter=';')
i = 0
db.set_character_set('utf8mb4')
for trendingTopic in trendingTopicArr:
path = '../features/' + trendingTopic[0] + '.json'
temp = generateFeature(trendingTopic[2], path, trendingTopic[3])
# print(trendingTopic[3])
if temp != None:
arrVectors.append(temp)
print (arrVectors[i])
trends = trendingTopic[2]
print(trends)
sql = "INSERT INTO TWEET_VECTOR_TRAIN(\
TREND, DEPTH_RETWEETS,RATIO_RETWEETS,HASHTAGS, \
LENGTH, EXCLAMATIONS, QUESTIONS,LINKS ,TOPICREPETITION ,REPLIES ,\
SPREADVELOCITY ,USER_DIVERSITY ,RETWEETED_USER_DIVERSITY ,HASHTAG_DIVERSITY ,\
LANGUAGE_DIVERSITY, VOCABULARY_DIVERSITY, CLASS)\
VALUES ('%s', %f, %f, %f,\
%f, %f, %f,%f, %f, %f," \
"%f, %f, %f, %f, \
%f, %f, %d)" % \
(trends, arrVectors[i][0], arrVectors[i][1], arrVectors[i][2],
arrVectors[i][3], arrVectors[i][4], arrVectors[i][5],
arrVectors[i][6], arrVectors[i][7], arrVectors[i][8],
arrVectors[i][9], arrVectors[i][10],
arrVectors[i][11], arrVectors[i][12],
arrVectors[i][13], arrVectors[i][14], arrVectors[i][15])
cursor.execute(sql)
db.commit()
i = i + 1