forked from CCWI/sentiment-analyser-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
73 lines (59 loc) · 2.75 KB
/
app.py
File metadata and controls
73 lines (59 loc) · 2.75 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
import mysql.connector as mariadb
from SentimentProvider import AlchemyProvider, SemantriaProvider
import time
# 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():
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 = []
#providers.append(SemantriaProvider())
providers.append(AlchemyProvider())
for provider in providers:
flag = True
while flag:
# 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) LIMIT 100"
print(query_stmt)
cursor.execute(query_stmt)
if cursor.rowcount == 0:
print("No values to be updated. Terminating update process.")
flag = 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(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."
mariadb_connection.commit()
mariadb_connection.close()
except mariadb.Error as error:
print "Error: {}".format(error)
return
while True:
update_db()
time.sleep(6000)