-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
194 lines (156 loc) · 5.72 KB
/
database.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
from sqlalchemy import create_engine
from sqlalchemy.engine import URL
import pandas as pd
from tqdm.notebook import tqdm
from nltk.sentiment import SentimentIntensityAnalyzer
from transformers import AutoTokenizer
from transformers import AutoModelForSequenceClassification
from transformers import pipeline
from scipy.special import softmax
import seaborn as sns
import text2emotion as te
#Connessione al database
url = URL.create(
drivername="postgresql",
username="postgres",
host="/tmp/postgresql/socket",
database="Tirocinio"
)
engine = create_engine("postgresql://postgres:PASSWORD@localhost:5432/TIROCINIO")
connection = engine.connect()
df1 = pd.read_sql_query('Select * from tirocinio.db_tweet2', con=engine)
#VADER
sia = SentimentIntensityAnalyzer()
res = {}
for i, row in tqdm(df1.iterrows(), total=len(df1)):
text= row['text']
myid = row['id']
res[myid] = sia.polarity_scores(text)
vaders = pd.DataFrame(res).T
vaders = vaders.reset_index().rename(columns={'index' : 'id'})
vaders = vaders.merge(df1, how='left')
print(vaders)
#ROBERTA
MODEL = f"cardiffnlp/twitter-roberta-base-sentiment"
tokenizer = AutoTokenizer.from_pretrained(MODEL)
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
def polarity_scores_roberta(example):
encoded_text = tokenizer(example, return_tensors='pt')
output = model(**encoded_text)
scores = output[0][0].detach().numpy()
scores = softmax(scores)
scores_dict = {
'roberta_neg' : scores[0],
'roberta_neu' : scores[1],
'roberta_pos' : scores[2]
}
return scores_dict
res = {}
for i, row in tqdm(df1.iterrows(), total=len(df1)):
text = row['text']
myid = row['id']
res[myid] = polarity_scores_roberta(text)
roberta= pd.DataFrame(res).T
roberta = roberta.reset_index().rename(columns={'index' : 'id'})
roberta = roberta.merge(df1, how='left')
print(roberta)
#Vader + Roberta
MODEL = f"cardiffnlp/twitter-roberta-base-sentiment"
tokenizer = AutoTokenizer.from_pretrained(MODEL)
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
def polarity_scores_roberta(example):
encoded_text = tokenizer(example, return_tensors='pt')
output = model(**encoded_text)
scores = output[0][0].detach().numpy()
scores = softmax(scores)
scores_dict = {
'roberta_neg' : scores[0],
'roberta_neu' : scores[1],
'roberta_pos' : scores[2]
}
return scores_dict
sia = SentimentIntensityAnalyzer()
res = {}
for i, row in tqdm(df1.iterrows(), total=len(df1)):
try:
text = row['text']
myid = row['id']
vader_result = sia.polarity_scores(text)
vader_result_rename = {}
for key, value in vader_result.items():
vader_result_rename[f"vader_{key}"] = value
roberta_result = polarity_scores_roberta(text)
both = {**vader_result_rename, **roberta_result}
res[myid] = both
except RuntimeError:
print(f'Broke for id {myid}')
results_df = pd.DataFrame(res).T
results_df = results_df.reset_index().rename(columns={'index': 'id'})
results_df = results_df.merge(df1, how='left')
results_df.to_sql('db_vader_roberta',con=engine ,schema='tirocinio',if_exists="replace",index=False)
#Text2Emotion
res = {}
for i, row in tqdm(df1.iterrows(), total=len(df1)):
text= row['text']
myid = row['id']
res[myid] = te.get_emotion(text)
textemo = pd.DataFrame(res).T
textemo = textemo.reset_index().rename(columns={'index' : 'id'})
textemo = textemo.merge(df1, how='left')
print(textemo)
#Distilbert
tokenizer = AutoTokenizer.from_pretrained("joeddav/distilbert-base-uncased-go-emotions-student")
model = AutoModelForSequenceClassification.from_pretrained("joeddav/distilbert-base-uncased-go-emotions-student")
emotion = pipeline('sentiment-analysis', model='joeddav/distilbert-base-uncased-go-emotions-student')
def distilbert_28(example):
encoded_text = tokenizer(example, return_tensors='pt')
output = model(**encoded_text)
scores = output[0][0].detach().numpy()
scores = softmax(scores)
scores_dict = {
"admiration" : scores[0],
"amusement": scores[1],
"anger" : scores[2],
"annoyance" : scores[3],
"approval" : scores[4],
"caring" : scores[5],
"confusion" : scores[6],
"curiosity" : scores[7],
"desire" : scores[8],
"disappointment" : scores[9],
"disapproval" : scores[10],
"disgust" : scores[11],
"embarrassment" : scores[12],
"excitement" : scores[13],
"fear" : scores[14],
"gratitude" : scores[15],
"grief" : scores[16],
"joy" : scores[17],
"love" : scores[18],
"nervousness" : scores[19],
"optimism" : scores[20],
"pride" : scores[21],
"realization" : scores[22],
"relief" : scores[23],
"remorse" : scores[24],
"sadness" : scores[25],
"surprise" : scores[26],
"neutral" : scores[27]
}
return scores_dict
def distilbert_first(text):
scores_dict = {
"emotion" : emotion(text)[0]['label'],
"score" : emotion(text)[0]['score']
}
return scores_dict
res = {}
for i, row in tqdm(df1.iterrows(), total=len(df1)):
text = row['text']
myid = row['id']
res[myid] = distilbert_first(text)
distilbert = pd.DataFrame(res).T
distilbert = distilbert.reset_index().rename(columns={'index' : 'id'})
distilbert = distilbert.merge(df1, how='left')
print(distilbert)
distilbert.to_sql('db_distilbert',con=engine ,schema='tirocinio',if_exists="replace",index=False)