-
Notifications
You must be signed in to change notification settings - Fork 0
/
get sentiments.py
66 lines (53 loc) · 2.18 KB
/
get sentiments.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
## PURPOSE
# analyze sentiment in my abyss entries
## NOTES
## import libraries
import pandas as pd
import pyodbc
import time
from textblob import TextBlob
from textblob.sentiments import NaiveBayesAnalyzer
import os
## start stopwatch and connect
start_time = time.time()
connection = pyodbc.connect('Driver={SQL Server};''Server=abyss.database.windows.net;''Database=Abyss;''UID=rickcole;''PWD=TURNMUSCIOFF;');
cursor = connection.cursor()
## delete from staging tables
sql = "{call [Abyss].[dbo].[SP_DeleteStagingTables_SentimentAnalysis]}"
cursor.execute(sql)
connection.commit()
## insert registry into Sentiment Analysis iteration table
sql = "{call [Abyss].[dbo].[SP_InsertInto_SentimentAnalysis_Iteration]}"
cursor.execute(sql)
connection.commit()
## get data
sql = 'select pk_id_usersessioninput, input from tbl_void_usersessioninput'
connection.commit();
data = pd.read_sql_query(sql, connection);
ids = data['pk_id_usersessioninput'].tolist()
user_inputs = data['input'].tolist()
## create vectors
positive_sentiments = []
negative_sentiments = []
## get user sentiments and compile into df
for index, user_input in enumerate(user_inputs):
blob = TextBlob(str(user_input), analyzer = NaiveBayesAnalyzer())
positive_sentiment = blob.sentiment[1]
negative_sentiment = blob.sentiment[2]
positive_sentiments.append(positive_sentiment)
negative_sentiments.append(negative_sentiment)
print(index)
df_sentiment_ratings = pd.DataFrame({'fk_id_usersessioninput':ids, 'Positive_Sentiment':positive_sentiments, 'Negative_Sentiment':negative_sentiments})
## write df to csv file
if os.path.exists('pos_and_neg_sent.csv'):
os.remove('pos_and_neg_sent.csv')
df_sentiment_ratings.to_csv('pos_and_neg_sent.csv', mode = 'a', header=False)
## write csv to db
os.system('cd C:\\Program Files (x86)\\Microsoft SQL Server\\140\\DTS\\Binn & dtexec /f "C:\\Users\\ricks\\OneDrive\\Tabs\\upload sentiments.dtsx"')
## insert from staging table into sentiment analysis table
sql = "{call [Abyss].[dbo].[SP_InsertInto_SentimentAnalysis]}"
cursor.execute(sql)
connection.commit()
## close connection
cursor.close();
connection.close();