-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
234 lines (191 loc) · 8.48 KB
/
app.py
File metadata and controls
234 lines (191 loc) · 8.48 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
import mysql.connector as mariadb
from SentimentProvider import AlchemyProvider, SemantriaProvider
import time
import sys
# Uncomment when config file is present
from config import semantria_key, semantria_secret, german_conf_twitter_active, german_conf, db_host, db_name, db_user, \
db_password, db_port
def error_handling(statuscode, message):
print message
# Function to update the Db
def update_db(sentiment = True, keywords = True, picture_keywords = True):
print "Updating Db entries..."
print "Connection to DB."
try:
# connect to db
mariadb_connection = mariadb.connect(host=db_host, port=db_port, user=db_user, password=db_password,
database=db_name)
cursor = mariadb_connection.cursor(buffered=True)
print "Execute Select Statement"
# providers = [#SemantriaProvider()] # , AlchemyProvider()]
providers = [AlchemyProvider()]
for provider in providers:
flag = True
while flag:
flag = False
if sentiment is True:
sentiment_comments = update_sentiment_for_comments(provider, cursor)
if flag is False:
flag = sentiment_comments
if keywords is True:
keywords_comments = update_keywords_for_comments(provider, cursor)
if flag is False:
flag = keywords_comments
if picture_keywords is True:
keywords_pictures = update_keywords_for_pictures(provider, cursor)
if flag is False:
flag = keywords_pictures
mariadb_connection.commit()
mariadb_connection.close()
except mariadb.Error as error:
print "Error: {}".format(error)
return
def update_keywords_for_pictures(provider, cursor):
provider_id = provider.provider_id()
# get data
query_stmt = "SELECT id, full_picture FROM post p LEFT JOIN post_has_class ON p.id = post_has_class.post_id" + \
" WHERE p.full_picture IS NOT NULL AND post_has_class.class_id is NULL ORDER BY rand() LIMIT 10"
print(query_stmt)
cursor.execute(query_stmt)
if cursor.rowcount == 0:
print("No values to be updated. Terminating update process.")
return False
else:
print("Rows to process: " + str(cursor.rowcount))
# prepare data
input_urls = []
for id, full_picture in cursor:
input_urls.append({"id": id, "picture": full_picture})
# parse for keywords
docs = provider.parse_picture_keywords(input_urls)
if docs is not None and len(docs) > 0:
print(len(docs))
# update database
for doc in docs:
post_id = doc.postid()
if len(doc.classes()) == 0:
# insert dummy class
insert_class(cursor, '', post_id, provider_id, '')
else:
for i in range(len(doc.classes())):
# find wether keyword already exists
current_class = doc.classes()[i]
score = doc.score()[i]
insert_class(cursor, current_class, post_id, provider_id, score)
print "Updated " + str(len(docs)) + " entries in the database."
else:
return False
return True
def update_keywords_for_comments(provider, cursor):
provider_id = provider.provider_id()
# get data
query_stmt = "SELECT id,text FROM post p LEFT JOIN post_has_keyword ON p.id = post_has_keyword.post_id" + \
" WHERE p.text IS NOT NULL AND length(trim(text)) != 0 AND post_has_keyword.keyword_id is NULL LIMIT 100"
print(query_stmt)
cursor.execute(query_stmt)
if cursor.rowcount == 0:
print("No values to be updated. Terminating update process.")
return False
else:
print("Rows to process: " + str(cursor.rowcount))
# prepare data
input_texts = []
for id, text in cursor:
input_texts.append({"id": id, "text": text})
# parse for keywords
docs = provider.parse_keywords(input_texts, 'German')
if docs is not None:
print(len(docs))
# update database
for doc in docs:
post_id = doc.postid()
if len(doc.keywords()) == 0:
# insert dummy keyword
insert_keyword(cursor, '', post_id, provider_id, '')
else:
for i in range(len(doc.keywords())):
# find wether keyword already exists
keyword = doc.keywords()[i]
relevance = doc.relevance()[i]
insert_keyword(cursor, keyword, post_id, provider_id, relevance)
print "Updated " + str(len(docs)) + " entries in the database."
return True
def insert_class(cursor, class_name, post_id, provider_id, score):
class_id = None
find_stmt = "SELECT id FROM class WHERE class.class = '" + class_name + "'"
while class_id is None:
cursor.execute(find_stmt)
if cursor.rowcount == 0:
keyword_stmt = 'INSERT INTO class(`class`) VALUES("' + class_name + '")'
print(keyword_stmt)
cursor.execute(keyword_stmt)
else:
result = cursor.fetchall()
class_id = result[0][0]
post_has_class_statement = 'INSERT INTO post_has_class(`sentimentProvider_id`, `post_id`, `class_id`, `score`) VALUES("' + str(
provider_id) + '", "' + str(
post_id) + '", "' + str(
class_id) + '", "' + str(
score) + '")'
print(post_has_class_statement)
cursor.execute(post_has_class_statement)
def insert_keyword(cursor, keyword, post_id, provider_id, relevance):
keyword_id = None
find_stmt = "SELECT id FROM keyword WHERE keyword.keyword = '" + keyword + "'"
while keyword_id is None:
cursor.execute(find_stmt)
if cursor.rowcount == 0:
keyword_stmt = 'INSERT INTO keyword(`keyword`) VALUES("' + keyword + '")'
# print(keyword_stmt)
cursor.execute(keyword_stmt)
else:
result = cursor.fetchall()
keyword_id = result[0][0]
post_has_keyword_statement = 'INSERT INTO post_has_keyword(`sentimentProvider_id`, `post_id`, `keyword_id`, `relevance`) VALUES("' + str(
provider_id) + '", "' + str(
post_id) + '", "' + str(
keyword_id) + '", "' + str(
relevance) + '") ON DUPLICATE KEY UPDATE keyword_id=keyword_id'
print(post_has_keyword_statement)
cursor.execute(post_has_keyword_statement)
def update_sentiment_for_comments(provider, cursor):
# get data
query_stmt = "SELECT id, text FROM comment c WHERE id NOT IN (SELECT comment_id FROM sentiment s WHERE sentimentProvider_id = " \
+ str(provider.provider_id()) + " AND s.comment_id = c.id) AND text IS NOT NULL AND length(trim(text)) != 0 LIMIT 100"
print(query_stmt)
cursor.execute(query_stmt)
if cursor.rowcount == 0:
print("No values to be updated. Terminating update process.")
return False
else:
print("Rows to process: " + str(cursor.rowcount))
# prepare data
input_texts = []
for id, text in cursor:
input_texts.append({"id": id, "text": text})
# parse for sentiment
docs = provider.parse_sentiment(input_texts, 'German')
if docs is not None:
print(len(docs))
# update database
for doc in docs:
stmt = 'INSERT INTO sentiment(`sentimentProvider_id`, `comment_id`, `sentiment`, `mixed`) VALUES("' + str(
provider.provider_id()) + '", "' + str(
doc.id()) + '", "' + str(
doc.sentiment_score()) + '", "' + str(
doc.mixed()) + '")'
print(stmt)
cursor.execute(stmt)
print "Updated " + str(len(docs)) + " entries in the database."
return True
def is_true(string):
return string == 'True'
if __name__ == "__main__":
while True:
if len(sys.argv) >= 4:
update_db(is_true(sys.argv[1]), is_true(sys.argv[2]), is_true(sys.argv[3]))
else:
update_db(True, True, True)
seconds = 6000
print 'Waiting ' + str(seconds) + ' seconds!'
time.sleep(seconds)