-
Notifications
You must be signed in to change notification settings - Fork 7
/
SenticrTest.py
177 lines (135 loc) · 4.86 KB
/
SenticrTest.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
# Created by happygirlzt
from SentiCR import SentiCR
import pandas as pd
import numpy as np
from sklearn.metrics import classification_report
import time
sentiment_analyzer=SentiCR()
from pathlib import Path
data_folder=Path('/sa4se/data') # your data folder
api_train=data_folder/'api-train.pkl'
api_test=data_folder/'api-test.pkl'
gh_train=data_folder/'gh-train.pkl'
gh_test=data_folder/'gh-test.pkl'
jira_train=data_folder/'jira-train.pkl'
jira_test=data_folder/'jira-test.pkl'
so_train=data_folder/'so-train.pkl'
so_test=data_folder/'so-test.pkl'
app_train=data_folder/'app-train.pkl'
app_test=data_folder/'app-test.pkl'
cr_train=data_folder/'cr-train.pkl'
cr_test=data_folder/'cr-test.pkl'
def predict_jira():
begin=time.time()
df=pd.read_pickle(jira_test)
df['label']=df['label'].replace(-1, 0)
sentences=df['sentence']
y_test=df['label']
pred=[]
for sent in sentences:
score=sentiment_analyzer.get_sentiment_polarity(sent)
pred.append(score)
end=time.time()
print('Prediction used {:.2f} seconds'.format(end-begin))
y_pred=pd.DataFrame(pred, columns=['pred_label'])
print(classification_report(y_test, y_pred))
# report = classification_report(y_test, y_pred, output_dict=True)
# df = pd.DataFrame(report).transpose()
# df.to_csv('./SentiCR_jira.csv')
def predict_so():
begin=time.time()
df=pd.read_pickle(so_test)
df['label']=df['label'].replace(-1, 2)
sentences=df['sentence']
y_test=df['label']
pred=[]
for sent in sentences:
score=sentiment_analyzer.get_sentiment_polarity(sent)
pred.append(score)
end=time.time()
print('Prediction used {:.2f} seconds'.format(end-begin))
y_pred=pd.DataFrame(pred, columns=['pred_label'])
print(classification_report(y_test, y_pred))
#results = confusion_matrix(y_test, y_pred, labels=[1,0,2])
#print(results)
#report = classification_report(y_test, y_pred, output_dict=True)
#df = pd.DataFrame(report).transpose()
#df.to_csv('./SentiCR_so.csv')
def predict_gh():
begin=time.time()
df=pd.read_pickle(gh_test)
sentences=df['sentence']
y_test=df['label']
pred=[]
for sent in sentences:
score=sentiment_analyzer.get_sentiment_polarity(sent)
pred.append(score)
end=time.time()
print('Prediction used {:.2f} seconds'.format(end-begin))
y_pred=pd.DataFrame(pred, columns=['pred_label'])
# new_df=pd.DataFrame(columns=['Text', 'SentiCR_predicted'])
# new_df['Text'] = sentences.copy
# new_df['SentiCR_predicted'] = y_pred.copy
# new_df.to_csv('./senticr_preditied.csv', header=True)
print(classification_report(y_test, y_pred))
# report = classification_report(y_test, y_pred, output_dict=True)
# df = pd.DataFrame(report).transpose()
# df.to_csv('./SentiCR_gh.csv')
def predict_app():
begin=time.time()
df=pd.read_pickle(app_test)
df['label']=df['label'].replace(-1,2)
sentences=df['sentence']
y_test=df['label']
print(sentences.shape[0]==y_test.shape[0])
pred=[]
for sent in sentences:
score=sentiment_analyzer.get_sentiment_polarity(sent)
pred.append(score)
end=time.time()
print('Prediction used {:.2f} seconds'.format(end-begin))
y_pred=pd.DataFrame(pred, columns=['pred_label'])
print(classification_report(y_test, y_pred))
# report = classification_report(y_test, y_pred, output_dict=True)
# df = pd.DataFrame(report).transpose()
# df.to_csv('./SentiCR_app.csv')
def predict_cr():
begin=time.time()
df=pd.read_pickle(cr_test)
df['label']=df['label'].replace(-1,1)
sentences=df['sentence']
y_test=df['label']
pred=[]
for sent in sentences:
score=sentiment_analyzer.get_sentiment_polarity(sent)
pred.append(score)
end=time.time()
print('Prediction used {:.2f} seconds'.format(end-begin))
y_pred=pd.DataFrame(pred, columns=['pred_label'])
print(classification_report(y_test, y_pred))
# report = classification_report(y_test, y_pred, output_dict=True)
# df = pd.DataFrame(report).transpose()
# df.to_csv('./SentiCR_cr1.csv')
def predict_api():
begin=time.time()
df=pd.read_pickle(api_test)
df['label']=df['label'].replace(-1,2)
sentences=df['sentence']
y_test=df['label']
pred=[]
for sent in sentences:
score=sentiment_analyzer.get_sentiment_polarity(sent)
pred.append(score)
end=time.time()
print('Prediction used {:.2f} seconds'.format(end-begin))
y_pred=pd.DataFrame(pred, columns=['pred_label'])
print(classification_report(y_test, y_pred))
#report = classification_report(y_test, y_pred, output_dict=True)
#df = pd.DataFrame(report).transpose()
#df.to_csv('./SentiCR_api.csv')
#predict_jira()
#predict_api()
#predict_gh()
predict_so()
#predict_cr()
#predict_app()