-
Notifications
You must be signed in to change notification settings - Fork 1
/
ICAAD_classification_use.py
266 lines (197 loc) · 10 KB
/
ICAAD_classification_use.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
from __future__ import division
import sys
sys.path.append('../InferSent')
from models import InferSent
import torch
from lda import Evaluation, Viewer
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import torch
import time
import re
import pdb
import fastText
PATH = '../data/ICAAD/ICAAD_labeledSentences.csv'
STOPWORDS = ['of', 'and', 'then', 'by', 'for', 'a', 'the', 'from', 'that', 'to', 'with', 'within', 'this', 'so', 'as', 'on', 'in', 'therefore', 'is', 'it', 'that', 'at']
FREQUENT_WORDS = ['court', 'judge', 'counsel', 'offence', 'plaintiff', 'accused', 'defendant', 'sentence', 'appeal', 'commit', 'committed', 'charge', 'charged', 'unlawful', 'record']
EXCLUDE = STOPWORDS + FREQUENT_WORDS
ENCODING = 'fasttext'
#ENCODING = 'USE'
#ENCODING = 'InferSent'
THRESHOLD = 0.45
logit = 0
a = 30.0
rm_stopwords = 0
InferSent_path = '../InferSent/encoder/infersent1.pkl'
W2V_PATH = '../InferSent/dataset/fastText/crawl-300d-2M.vec'
W2V_PATH = '../InferSent/dataset/GloVe/glove.840B.300d.txt'
W2V_PATH = '../fastText/model.bin'
W2V_PATH = '../WordEmbedding/FastText_wiki-news-300d-40000-subword.bin'
params_model = {'bsize': 64, 'word_emb_dim':300, 'enc_lstm_dim':2048, 'pool_type':'max', 'dpout_model':0.0, 'version':2}
model = fastText.load_model(W2V_PATH)
#model = InferSent(params_model)
#model.load_state_dict(torch.load(InferSent_path))
#model.set_w2v_path(W2V_PATH)
#model.build_vocab_k_words(K=100000)
#pdb.set_trace()
sent_encoder_graph = tf.get_default_graph()
#sentenceEncoder = hub.Module("https://tfhub.dev/google/universal-sentence-encoder/2")
# sentenceEncoder = hub.Module("https://tfhub.dev/google/universal-sentence-encoder/1")
#sentenceEncoder = hub.Module("https://tfhub.dev/google/universal-sentence-encoder-large/3")
#sentences = tf.placeholder(dtype=tf.string, shape=[None])
#embedding = sentenceEncoder(sentences)
def cosine(u, v):
return np.dot(u,v)/(np.linalg.norm(u) * np.linalg.norm(v))
def preprocess(sentence):
result = sentence.replace(",", " , ")
result = result.replace(":", " : ")
result = result.replace(";", " ; ")
return result
def removeStopwords(sentence):
tokens = sentence.split()
tokens = [word for word in tokens if word not in STOPWORDS]
return ' '.join(tokens)
def transformProbability(p,a):
return p**a/(p**a + (1-p)**a)
def relu(p):
if p<0:
return 0
else:
return p
def avg_word_vectors(sentence):
vec = [model.get_word_vector(word) for word in sentence.split()]
weights = [1 if word not in EXCLUDE else 0.5 for word in sentence.split()]
return np.average(np.array(vec), weights=weights, axis=0)
def ICAAD_classification_use(data, category, config, evidences, method='max'):
print('** EMBEDDING **')
t0 = time.time()
test_sentences = data.sentence.tolist()
data['targetLabel'] = data.category == category
if ENCODING=='USE':
with sent_encoder_graph.as_default():
with tf.Session(graph=sent_encoder_graph) as session:
session.run([tf.global_variables_initializer(), tf.tables_initializer()])
evidence_embedding = session.run(embedding, feed_dict={sentences: evidences})
if rm_stopwords:
test_sentences = [removeStopwords(sentence) for sentence in test_sentences]
data['sentence'] = test_sentences
sentence_embedding = session.run(embedding, feed_dict={sentences: test_sentences})
similarity = np.inner(evidence_embedding, sentence_embedding)
#cos = tf.reduce_sum(tf.multiply(evidence_embedding, sentence_embedding), axis=1)
#clip_cos = tf.clip_by_value(cos, -1.0, 1.0)
#scores = 1.0 - tf.acos(clip_cos)
#if logit:
# pos = np.vectorize(relu)
# similarity = pos(similarity)
# f = np.vectorize(transformProbability)
# similarity = f(similarity, a)
#if method=='max':
# similarity = np.max(similarity, axis=0)
# similarity = similarity.reshape(1, similarity.shape[0])
#if method=='avg':
# similarity = np.average(similarity, axis=0)
# similarity = similarity.reshape(1, similarity.shape[0])
#data['similarity'] = similarity.reshape(len(data))
#data['similarity'] = data.similarity.apply(round, ndigits=4)
session.close()
elif ENCODING=='fasttext':
test_sentences = [preprocess(sentence) for sentence in test_sentences]
evidences = [preprocess(evidence) for evidence in evidences]
sentence_embedding = [avg_word_vectors(sentence) for sentence in test_sentences]
evidence_embedding = [avg_word_vectors(evidence) for evidence in evidences]
#evidence_embedding = [model.get_sentence_vector(preprocess(evidence)) for evidence in evidences]
#sentence_embedding = [model.get_sentence_vector(preprocess(sentence)) for sentence in test_sentences]
#similarity2 = np.inner(evidence_embedding, sentence_embedding)
similarity = []
for evd_embed in evidence_embedding:
for sent_emb in sentence_embedding:
similarity.append(cosine(evd_embed, sent_emb))
similarity = np.array(similarity).reshape((len(evidence_embedding), len(sentence_embedding)))
#pdb.set_trace()
elif ENCODING=='InferSent':
#pdb.set_trace()
#model.encode(test_sentences)
#test_sentences = test_sentences[:20]
model.build_vocab(test_sentences, tokenize=True)
evidence_embedding = model.encode(evidences, bsize=128, tokenize=True, verbose=True)
sentence_embedding = model.encode(test_sentences, bsize=128, tokenize=True, verbose=True)
similarity = []
for evd_embed in evidence_embedding:
for sent_emb in sentence_embedding:
similarity.append(cosine(evd_embed, sent_emb))
similarity = np.array(similarity).reshape((len(evidence_embedding), len(sentence_embedding)))
if method=='logreg':
for num in range(similarity.shape[0]):
data['sim_' + str(num)] = similarity[num, :]
data['avg'] = data[['sim_0', 'sim_1']].mean(axis=1)
data['predictedLabel'] = data['avg'] >= THRESHOLD
certainCases = (data['avg']>=0.7) | (data['avg']<0.2)
train = data[certainCases]
test = data[~certainCases]
from sklearn.linear_model import LogisticRegression
logreg = LogisticRegression()
logreg.fit(train[['sim_0', 'sim_1']], train['predictedLabel'])
test['predictedLabel'] = logreg.predict(test[['sim_0', 'sim_1']])
test['similarity'] = logreg.predict_log_proba(test[['sim_0', 'sim_1']])[:,1]
data = test
if logit:
pos = np.vectorize(relu)
similarity = pos(similarity)
f = np.vectorize(transformProbability)
similarity = f(similarity, a)
if method=='max':
similarity = np.max(similarity, axis=0)
similarity = similarity.reshape(1, similarity.shape[0])
if method=='avg':
similarity = np.average(similarity, axis=0)
similarity = similarity.reshape(1, similarity.shape[0])
if method=='max' or method=='avg':
data['similarity'] = similarity.reshape(len(data))
data['similarity'] = data.similarity.apply(round, ndigits=4)
print('USE Time: {}'.format(time.time()-t0))
print('** EVALUATION **')
t0 = time.time()
data['predictedLabel'] = data.similarity >= THRESHOLD
evaluation = Evaluation(target=data.targetLabel.tolist(), prediction=data.predictedLabel.tolist())
evaluation.computeMeasures()
evaluation.confusionMatrix()
print('Evaluation Time: {}'.format(time.time()-t0))
print('Accuracy: ' + str(evaluation.accuracy))
print('Recall: ' + str(evaluation.recall))
print('Precision: ' + str(evaluation.precision))
print(evaluation.confusionMatrix)
evaluation.createTags()
data['tags'] = evaluation.tags
data.sort_values(['tags', 'similarity'], inplace=True, ascending=[True, False])
pd.set_option('display.max_colwidth', 500)
html_name = config + '_' + ENCODING
if len(evidences)>1:
html_name = html_name + '_' + method
if rm_stopwords:
html_name = html_name + '_rmStopwords'
if logit:
html_name = html_name + '_logit'
axes = data.hist('similarity')
plt.savefig(html_name + '_prediction.png')
axes = data.hist('similarity', by='tags')
plt.savefig(html_name + '_tags.png')
viewer = Viewer('use')
viewer.use_classificationResults(html_name, evidences, data.drop(columns=['id', 'category', 'predictedLabel', 'targetLabel']), THRESHOLD, evaluation, a)
if __name__=='__main__':
data = pd.read_csv(PATH, encoding='utf8')
#data = data.sample(1000, random_state=42)
category = 'Evidence.of.SA'
negCategory = 'Evidence.no.SADV'
data = data[(data.category==category) | (data.category==negCategory)]
#data.sentence = data.sentence[:21]
#data = data.sample(100, random_state=42)
#set_evidences = [('keyword_rape', ['rape']), ('keyword_sexual_assault', ['sexual assault']), ('multiple_keywords', ['rape', 'sexual assault']), ('multiple_keywords_string', ['rape sexual assault']), ('multiple_keywords_string2', ['sexual assault rape'])]
# set_evidences = [('keyword_rape', ['rape']), ('phrase_rape', ['rape is committed when having sexual intercourse without consent']), ('combined', ['rape', 'rape is committed when having sexual intercourse without consent'])]
# set_evidences = [('combined', ['rape', 'rape is committed when having sexual intercourse without consent', 'sexual assault', 'incest', 'indecent assault', 'carnal knowledge'])]
#set_evidences = [('phrase_rape', ['rape is committed when having sexual intercourse without consent'])]
set_evidences = [('keyword_rape', ['rape'])]
for config, evidences in set_evidences:
ICAAD_classification_use(data, category, config, evidences, method='max')