-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_forest.py
244 lines (197 loc) · 10.5 KB
/
random_forest.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
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
235
236
237
238
239
240
241
242
243
import nltk
nltk.download('stopwords')
from eval import parse, preprocessing
from emotion_extraction import extract_emo, average_emo
from turn_level_sentiment import get_extracted_features
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import precision_score, recall_score, f1_score, accuracy_score
from sklearn.preprocessing import normalize
from text_embedding.features import *
from text_embedding.vectors import *
from utils import *
def main():
args = parse()
if args.dataset.lower() == 'pol':
SARC = SARC_POL
elif args.dataset.lower() == 'main':
SARC = SARC_MAIN
SARC = SARC_POL
train_file = SARC+'train-balanced.csv'
test_file = SARC+'test-balanced.csv'
comment_file = SARC+'comments.json'
results = []
results.append(extract_bong(train_file, test_file, comment_file, args))
results.append(extract_emotion_features(train_file, test_file, comment_file, args))
results.append(extract_sentiment(train_file, test_file, comment_file))
print("Feature\t\t|Accuracy\t\t|Precision\t\t|Recall\t\t\t|F1-Score\t")
for row in range(len(results)):
if row == 0:
model = "BONG\t\t|"
elif row == 1:
model = "Emotion\t\t|"
else:
model="Sentiment\t|"
print(model, results[row][0], "\t", results[row][1], "\t", results[row][2], "\t", results[row][3], "\t")
print("\t\t\tFeature Importance")
for row in range(len(results)):
if row == 0:
model = "BONG\t\t|"
elif row == 1:
model = "Emotion\t\t|"
else:
model="Sentiment\t|"
print(model, results[row][4])
def extract_bong(train_file, test_file, comment_file, args):
# Load SARC pol/main sequences with labels.
train_seqs, test_seqs, train_labels, test_labels = \
load_sarc_responses(train_file, test_file, comment_file, lower=args.lower)
# Only use responses for this method. Ignore ancestors.
train_resp = train_seqs['responses']
test_resp = test_seqs['responses']
# Split into first and second responses and their labels.
# {0: list_of_first_responses, 1: list_of_second_responses}
train_docs = {i: [l[i] for l in train_resp] for i in range(2)}
test_docs = {i: [l[i] for l in test_resp] for i in range(2)}
train_labels = {i: [2*int(l[i])-1 for l in train_labels] for i in range(2)}
test_labels = {i: [2*int(l[i])-1 for l in test_labels] for i in range(2)}
# Train a classifier on all responses in training data. We will later use this
# classifier to determine for every sequence which of the 2 responses is more sarcastic.
train_all_docs_tok = preprocessing(tokenize(train_docs[0] + train_docs[1]))
test_all_docs_tok = preprocessing(tokenize(test_docs[0] + test_docs[1]))
train_all_labels = np.array(train_labels[0] + train_labels[1])
test_all_labels = np.array(test_labels[0] + test_labels[1])
# Bongs or embeddings.
if args.embed:
#print('Create embeddings')
weights = None
if args.weights == 'sif':
weights = sif_weights(train_all_docs_tok, 1E-3)
if args.weights == 'snif':
weights = sif_weights(train_all_docs_tok, 1E-3)
weights = {f: 1-w for f, w in weights.items()}
w2v = vocab2vecs({word for doc in train_all_docs_tok+test_all_docs_tok for word in doc}, vectorfile=args.embedding)
train_all_vecs = docs2vecs(train_all_docs_tok, f2v=w2v, weights=weights)
test_all_vecs = docs2vecs(test_all_docs_tok, f2v=w2v, weights=weights)
else:
#print('Create bongs')
n = args.n
min_count = args.min_count
train_ngrams = [sum((list(nltk.ngrams(doc, k)) for k in range(1, n+1)), []) for doc in train_all_docs_tok]
test_ngrams = [sum((list(nltk.ngrams(doc, k)) for k in range(1, n+1)), []) for doc in test_all_docs_tok]
vocabulary = feature_vocab(train_ngrams, min_count=min_count)
train_all_vecs = docs2bofs(train_ngrams, vocabulary)
test_all_vecs = docs2bofs(test_ngrams, vocabulary)
# Normalize?
if args.normalize:
normalize(train_all_vecs, copy=False)
normalize(test_all_vecs, copy=False)
#print('Dimension of representation: %d'%train_all_vecs.shape[1])
clf = RandomForestClassifier(n_estimators=100)
clf.fit(train_all_vecs, train_all_labels)
predict = clf.predict(test_all_vecs)
feature_importance = clf.feature_importances_
precision = precision_score(test_all_labels, predict)
recall = recall_score(test_all_labels, predict)
f1 = f1_score(test_all_labels, predict)
accuracy = accuracy_score(test_all_labels, predict)
return accuracy, precision, recall, f1, feature_importance
def extract_emotion_features(train_file, test_file, comment_file, args):
# Load SARC pol/main sequences with labels.
train_seqs, test_seqs, train_labels, test_labels = \
load_sarc_responses(train_file, test_file, comment_file, lower=args.lower)
# Ancestor/prior statements that form the context of the sarcasm statements
train_ancestor = train_seqs['ancestors']
test_ancestor = test_seqs['ancestors']
# Only use responses for this method. Ignore ancestors.
train_resp = train_seqs['responses']
test_resp = test_seqs['responses']
# Split into first and second responses and their labels.
# {0: list_of_first_responses, 1: list_of_second_responses}
train_docs = {i: [l[i] for l in train_resp] for i in range(2)}
test_docs = {i: [l[i] for l in test_resp] for i in range(2)}
train_labels = {i: [2*int(l[i])-1 for l in train_labels] for i in range(2)}
test_labels = {i: [2*int(l[i])-1 for l in test_labels] for i in range(2)}
train_anc_docs = {0: [l[0] for l in train_ancestor]}
test_anc_docs = {0: [l[0] for l in test_ancestor]}
# Train a classifier on all responses in training data. We will later use this
# classifier to determine for every sequence which of the 2 responses is more sarcastic.
train_all_ancs_tok = preprocessing(tokenize(train_anc_docs[0]))
test_all_ancs_tok = preprocessing(tokenize(test_anc_docs[0]))
train_all_docs_tok = preprocessing(tokenize(train_docs[0] + train_docs[1]))
test_all_docs_tok = preprocessing(tokenize(test_docs[0] + test_docs[1]))
train_all_labels = np.array(train_labels[0] + train_labels[1])
test_all_labels = np.array(test_labels[0] + test_labels[1])
train_all_docs_emo = []
test_all_docs_emo = []
# Measuring Emotions in each sentence
for idx, sentence in enumerate(train_ancestor):
previous_statement = train_all_ancs_tok[idx]
next_statement = preprocessing(tokenize([train_docs[0][idx], train_docs[1][idx]]))
first_response = next_statement[0]
second_response = next_statement[1]
emo_prev = extract_emo(previous_statement)
emo_first = extract_emo(first_response)
emo_second = extract_emo(second_response)
train_all_docs_emo.append(average_emo(emo_prev, emo_first, len(previous_statement), len(first_response)))
train_all_docs_emo.append(average_emo(emo_prev, emo_second, len(previous_statement), len(second_response)))
train_all_docs_emo = np.array(train_all_docs_emo)
for idx, sentence in enumerate(test_ancestor):
previous_statement = test_all_ancs_tok[idx]
next_statement = preprocessing(tokenize([test_docs[0][idx], test_docs[1][idx]]))
first_response = next_statement[0]
second_response = next_statement[1]
emo_prev = extract_emo(previous_statement)
emo_first = extract_emo(first_response)
emo_second = extract_emo(second_response)
test_all_docs_emo.append(average_emo(emo_prev, emo_first, len(previous_statement), len(first_response)))
test_all_docs_emo.append(average_emo(emo_prev, emo_second, len(previous_statement), len(second_response)))
test_all_docs_emo = np.array(test_all_docs_emo)
# Evaluate this classifier on all responses.
clf = RandomForestClassifier(n_estimators=100)
clf.fit(train_all_docs_emo, train_all_labels)
predict = clf.predict(test_all_docs_emo)
feature_importance = clf.feature_importances_
# Measure Performance
precision = precision_score(test_all_labels, predict)
recall = recall_score(test_all_labels, predict)
f1 = f1_score(test_all_labels, predict)
accuracy = accuracy_score(test_all_labels, predict)
return accuracy, precision, recall, f1, feature_importance
def extract_sentiment(train_file, test_file, comment_file):
# Load SARC pol/main sequences with labels.
train_seqs, test_seqs, train_labels, test_labels = \
load_sarc_responses(train_file, test_file, comment_file, lower=False)
# Ancestor/prior statements that form the context of the sarcasm statements
train_ancestor = train_seqs['ancestors']
test_ancestor = test_seqs['ancestors']
# Responses of the ancestor statements
train_resp = train_seqs['responses']
test_resp = test_seqs['responses']
# Split into first and second responses and their labels.
# {0: list_of_first_responses, 1: list_of_second_responses}
train_docs = {i: [l[i] for l in train_resp] for i in range(2)}
test_docs = {i: [l[i] for l in test_resp] for i in range(2)}
# Convert label values, from {0,1} to {-1,1}
train_labels = {i: [2*int(l[i])-1 for l in train_labels] for i in range(2)}
test_labels = {i: [2*int(l[i])-1 for l in test_labels] for i in range(2)}
# Combine all labels into one array, both in train and test data
train_all_labels = np.array(train_labels[0] + train_labels[1])
test_all_labels = np.array(test_labels[0] + test_labels[1])
# Feature extraction for train and test data, using sentiment analysis (VADER) and emotional affect (NRCLex)
train_all_docs_sentiment = get_extracted_features(train_ancestor, train_docs)
test_all_docs_sentiment = get_extracted_features(test_ancestor, test_docs)
# Evaluate this classifier on all responses.
clf = RandomForestClassifier(n_estimators=100)
clf.fit(train_all_docs_sentiment, train_all_labels)
predict = clf.predict(test_all_docs_sentiment)
feature_importance = clf.feature_importances_
# Measure Performance
precision = precision_score(test_all_labels, predict)
recall = recall_score(test_all_labels, predict)
f1 = f1_score(test_all_labels, predict)
accuracy = accuracy_score(test_all_labels, predict)
return accuracy, precision, recall, f1, feature_importance
if __name__ == '__main__':
main()