-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcreate_table.py
More file actions
336 lines (262 loc) · 9.59 KB
/
Copy pathcreate_table.py
File metadata and controls
336 lines (262 loc) · 9.59 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import psycopg2
from server_postgres import execute_sql
from server_postgres import close_db_connection
import server_postgres as sp
import time
def connect_db():
try:
conn = psycopg2.connect(database='Milestone2', user='postgres',
password='123', host='127.0.0.1', port=5432)
except Exception as e:
print("fail")
else:
return conn
return None
def execute_select(sql):
conn = connect_db()
cur = conn.cursor()
cur.execute(sql)
list_header = [row[0] for row in cur.description]
list_result = [[str(item) for item in row] for row in cur.fetchall()]
res = [dict(zip(list_header, row)) for row in list_result]
# print(list_header)
# print(list_result)
# print(res)
return res
def word_table():
#Creating a table
sql = "drop table if exists words;"
execute_sql(sql)
sql = "CREATE TABLE WORDS(word_id SERIAL PRIMARY KEY,timestamp TIMESTAMP NOT NULL, tweet_id NUMERIC NOT NULL," \
" word varchar(1000) NOT NULL, phrase_yn NUMERIC NOT NULL);"
execute_sql(sql)
print("Table created successfully........")
#Preparing query to create a database
conn = connect_db()
cur = conn.cursor()
sql="select * from phrases"
tweet_table = execute_select(sql)
# print(ret)
#print(tweet_table[0]['timestamp'])
j= 0
conn = connect_db()
cur = conn.cursor()
for row in tweet_table:
minute_timestamp = str(row['timestamp'])
#minute_timestamp = minute_timestamp[:16]
tweet_id = str(row['tweet_id'])
#row['text'] = row['text'].lower()
#minute_timestamp = 1
tweet_text = row['text'].split()
phrases = [tweet_text[i] + ' ' + tweet_text[i+1] for i in range(len(tweet_text)-1)]
j += 100
for i in tweet_text:
#print(tweet_id)
cur.execute("""insert into words(timestamp, tweet_id, word, phrase_yn) values(%s,%s,%s,0); """, (minute_timestamp, tweet_id, i))
sql = ("insert into words(timestamp, tweet_id, word_id, word, phrase_yn) " + "values(%s,%s,%s,0)" % (minute_timestamp,tweet_id, i) + ";")
#print(sql)
#execute_sql(sql)
j += 1
for k in phrases:
cur.execute("""insert into words(timestamp, tweet_id, word, phrase_yn) values(%s,%s,%s,1); """,
(minute_timestamp, tweet_id, k))
sql = ("insert into words(timestamp, tweet_id, word, phrase_yn) " + "values(%s,%s,%s,1)" % (
minute_timestamp, tweet_id, k) + ";")
# print(sql)
# execute_sql(sql)
j += 1
close_db_connection(conn)
def word_frequency_in_minute( single_word):
conn = connect_db()
cur = conn.cursor()
sql = """with occur as(select
count(word) Occurence
from words
where timestamp >= date_trunc('minute', localtimestamp) + interval '6 hours'
and timestamp <= localtimestamp + interval '6 hours'
and word LIKE %s)
,total as( select count(*) total_words
from words
where phrase_yn = 0)
select round(Occurence::decimal/total_words, 8) as Frequency
from occur, total"""
cur.execute(sql , ( single_word,))
list_header = [row[0] for row in cur.description]
list_result = [[str(item) for item in row] for row in cur.fetchall()]
res = [dict(zip(list_header, row)) for row in list_result]
close_db_connection(conn)
print('word_frequency_in_minute:')
print(int(res[1]))
return res
#word_frequency_in_minute('the')
def distinct_words_in_minute():
conn = connect_db()
cur = conn.cursor()
sql = """select count(distinct word)
from words
where
timestamp >= date_trunc('minute', localtimestamp) + interval '6 hours'
and timestamp <= localtimestamp + interval '6 hours'
and phrase_yn = 0 """
cur.execute(sql)
list_header = [row[0] for row in cur.description]
list_result = [[str(item) for item in row] for row in cur.fetchall()]
res = [dict(zip(list_header, row)) for row in list_result]
close_db_connection(conn)
print('distinct_words_in_minute:')
print(int(res[1]))
return res
#distinct_words_in_minute()
def phrases_in_current_minute():
conn = connect_db()
cur = conn.cursor()
sql = """select count(word)
from words
where
timestamp >= date_trunc('minute', localtimestamp) + interval '6 hours'
and timestamp <= localtimestamp + interval '6 hours'
"""
cur.execute(sql)
list_header = [row[0] for row in cur.description][0]
list_result = [[str(item) for item in row] for row in cur.fetchall()][0][0]
res = [list_header, list_result]
close_db_connection(conn)
print('phrases_in_current_minute:')
print(int(res[1]))
return res
#phrases_in_current_minute()
def phrases_in_prior_minute():
conn = connect_db()
cur = conn.cursor()
sql = """select count(word)
from words
where
timestamp >= date_trunc('minute', localtimestamp) + interval '6 hours' - interval '1 minutes'
and timestamp <= date_trunc('minute', localtimestamp) + interval '6 hours'
"""
cur.execute(sql)
list_header = [row[0] for row in cur.description][0]
list_result = [[str(item) for item in row] for row in cur.fetchall()][0][0]
res = [list_header, list_result]
close_db_connection(conn)
print('phrases_in_prior_minute:')
print(int(res[1]))
return res
#phrases_in_prior_minute()
def distinct_phrases_in_current_minute():
conn = connect_db()
cur = conn.cursor()
sql = """select count(distinct word)
from words
where
timestamp >= date_trunc('minute', localtimestamp) + interval '6 hours'
and timestamp <= localtimestamp + interval '6 hours'
"""
cur.execute(sql)
list_header = [row[0] for row in cur.description][0]
list_result = [[str(item) for item in row] for row in cur.fetchall()][0][0]
res = [list_header, list_result]
close_db_connection(conn)
print('distinct_phrases_in_current_minute:')
print(int(res[1]))
return res
#distinct_phrases_in_current_minute()
def distinct_phrases_in_prior_minute():
conn = connect_db()
cur = conn.cursor()
sql = """select count(distinct word)
from words
where
timestamp >= date_trunc('minute', localtimestamp) + interval '6 hours' - interval '1 minutes'
and timestamp <= date_trunc('minute', localtimestamp) + interval '6 hours'
"""
cur.execute(sql)
list_header = [row[0] for row in cur.description][0]
list_result = [[str(item) for item in row] for row in cur.fetchall()][0][0]
res = [list_header, list_result]
close_db_connection(conn)
print('distinct_phrases_in_prior_minute:')
print(int(res[1]))
return res
#distinct_phrases_in_prior_minute()
def word_count_in_current_minute(single_word):
conn = connect_db()
cur = conn.cursor()
sql = """select
count(word) from words
where
timestamp >= date_trunc('minute', localtimestamp) + interval '6 hours'
and timestamp < localtimestamp + interval '6 hours'
and word LIKE %s """
cur.execute(sql , (single_word,))
list_header = [row[0] for row in cur.description][0]
list_result = [[str(item) for item in row] for row in cur.fetchall()][0][0]
res = [list_header, list_result]
close_db_connection(conn)
print('word_count_in_current_minute:')
print(int(res[1]))
return res
#word_count_in_current_minute('year')
def word_count_in_prior_minute(single_word):
conn = connect_db()
cur = conn.cursor()
sql = """select
count(word) from words
where
timestamp >= date_trunc('minute', localtimestamp) + interval '6 hours' - interval '1 minutes'
and timestamp < date_trunc('minute', localtimestamp) + interval '6 hours'
and word LIKE %s """
cur.execute(sql , (single_word,))
list_header = [row[0] for row in cur.description][0]
list_result = [[str(item) for item in row] for row in cur.fetchall()][0][0]
res = [list_header, list_result]
close_db_connection(conn)
print('word_count_in_prior_minute:')
print(int(res[1]))
return res
#word_count_in_prior_minute('year')
import math
def trendiness_calc(phrase):
prob_current_minute = (1 + int(word_count_in_current_minute(phrase)[1]))/(int(phrases_in_current_minute()[1]) + int(distinct_phrases_in_current_minute()[1]))
prob_prior_minute = (1 + int(word_count_in_prior_minute(phrase)[1]))/ (int(phrases_in_prior_minute()[1]) + int(distinct_phrases_in_prior_minute()[1]))
trendiness = math.log10(prob_current_minute) - math.log10(prob_prior_minute)
print('The Trendines Score of ' + str(phrase) + ' is ' + str(trendiness))
#trendiness_calc('the')
# select
# count(distinct
# word) from words
#
# select
# count(distinct
# word) from words
#
# where
# timestamp >= date_trunc('minute', localtimestamp) + interval '5 hours'
# and timestamp <= localtimestamp + interval '6 hours' ;
# select
# date_trunc('minute', localtimestamp)
# union
# select
# date_trunc('second', localtimestamp)
def main():
word_table()
try:
trendiness_calc('the')
except ZeroDivisionError:
print("Not enough tweets in the current minute")
#sp.main(500)
try:
trendiness_calc('like')
except ZeroDivisionError:
print("Not enough tweets in the current minute")
#sp.main(500)
try:
trendiness_calc('love it')
except ZeroDivisionError:
print("Not enough tweets in the current minute")
try:
trendiness_calc('good')
except ZeroDivisionError:
print("Not enough tweets in the current minute")
if __name__ == "__main__":
main()