-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneration.py
More file actions
379 lines (316 loc) · 15.3 KB
/
generation.py
File metadata and controls
379 lines (316 loc) · 15.3 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import pickle
from random import seed
from nltk import AlignedSent, IBMModel2
import pandas as pd
from scipy.sparse import vstack, hstack
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression, Perceptron
from sklearn.metrics import roc_auc_score
from cleaning import entity_and_reference, condense_ref
from inference import get_w2v
__author__ = 'jamesgin'
from features import *
import spacy
# nlp = spacy.load('en')
from nltk.translate.ibm3 import IBMModel3
censored = ['it meets the following conditions']
def semi_sup():
pairs = session.query(Question, RawClause).filter((Question.related_clause.isnot(None)) &
(Question.related_clause != 54488))\
.join(RawClause)
for q, r in pairs:
print('')
print(q.body)
print(q.get_correct())
print(r.cleaned)
@memory.cache
def get_ibm_model3():
X, y, tfidf = generate_clause_set(get_clause_id, 1)
nn = NearestNeighbors(1, algorithm='brute', metric='cosine')
nn.fit(X)
question_bodies = session.query(Question.body).all()
question_bodies = [q[0] for q in question_bodies]
q_vec = tfidf.transform(question_bodies)
neighs = nn.kneighbors(q_vec, return_distance=False)
ids = y[neighs].ravel()
bodies = []
for i in ids:
body = session.query(RawClause.cleaned).filter(RawClause.id == i).first()
bodies.append(tokenise(body[0]))
question_toks = [tokenise(q) for q in question_bodies]
matched = zip(question_toks, bodies)
aligned = [AlignedSent(*m) for m in matched]
ibm = IBMModel2(aligned, 1)
return ibm
def question_cluster():
questions = session.query(Question.body).all()
cv = CountVectorizer()
freq_mat = cv.fit_transform([q[0] for q in questions])
freqs = [(word, freq_mat.getcol(idx).sum()) for word, idx in cv.vocabulary_.items()]
print sorted(freqs, key = lambda x: -x[1])
fq = freq_mat.toarray()
fq = fq / np.linalg.norm(fq, axis=1)[:, None]
pass
def questions_checker(output_file_name, for_parser):
questions = session.query(Question).order_by(Question.id)
with open(output_file_name, 'wb') as file_with_reference:
with open(for_parser, 'wb') as file_for_parser:
lines = []
parser_lines = []
for q in questions:
lines.append('{},{}\n'.format(q.id, q.body.encode('ascii', 'ignore')))
parser_lines.append(q.body + '\n')
# print(clause.header)
# print(section.name)
file_with_reference.writelines(lines)
file_for_parser.writelines(parser_lines)
def statement_checker(output_file_name, for_parser, extractor):
nlp = spacy.load('en')
statements = session.query(StatementPart, RawClause, Section).join(RawClause).join(Section).filter((StatementPart.parent_id.is_(None)))
# & (Section.docpath.like('COBS%'))
# & (Section.id == 2926) # .in_(range(2961, 2969))) #
# & (RawClause.content_html.notilike('%<table%'))).order_by(StatementPart.id)
with open(output_file_name, 'wb') as file_with_reference:
with open(for_parser, 'wb') as file_for_parser:
lines = []
parser_lines = []
for statement, clause, section in statements:
# print(clause.name)
# shortest = statement.shortest_sentence()
for shortest in statement.all_sentences():
sent = fix_multipart_sentence(shortest)
sent = sent.replace(u'\u2019', "'")
if 'should present the information referred to in' in sent:
pass
sent = clean_sentence(sent, extractor)
doc = nlp(sent)
for s in doc.sents:
cleaned = str(s).replace(':', '')
if 'deleted' not in cleaned.lower() and 'Note ]' not in cleaned:
# if cleaned.startswith("If a communication relates to a firm's business that is not"):
# cleaned = clean_sentence(str(s), extractor)
print(cleaned)
lines.append('{},{}\n'.format(statement.id, cleaned))
parser_lines.append(cleaned + '\n')
# print(clause.header)
# print(section.name)
file_with_reference.writelines(lines)
file_for_parser.writelines(parser_lines)
def generate_clause_sentences(statements):
"""
For multi sentence clauses, ascertain contextual information and then obligation / guidance
For list based sentences, convert these into a single grammatical sentence.
:param statements:
:return:
"""
pass
def clip_end(part):
for i in [';', ' ;', '; and', '; or']:
if part.endswith(i):
part = part[:-len(i)]
return part
def generate_distractors():
pass
def load_additional_context():
context = session.query(StatementPart.id, RawClause.header, Section.name).join(RawClause).join(Section)\
.filter(Section.name.like('%COBS%')).all()
return pd.DataFrame(context).set_index('id')
def remove_multiple_statements(part):
for c in censored:
part = part.replace(c, '')
return part
def fix_multipart_sentence(sentence):
sentence = [clip_end(p).strip() for p in sentence]
sent_string = ' '.join(sentence)
if not sent_string.endswith('.'):
sent_string += '.'
return sent_string
def get_test_vecs(tfidf, questions, concatenated):
X = []
y = []
true_ans = []
for q in questions:
# print(q.body)
if concatenated:
q_vec = tfidf.transform([q.body])
a_vecs = [tfidf.transform([a]) for a in q.all_answers()]
vecs = [hstack([q_vec, a]) for a in a_vecs]
else:
docs = [q.body + '. ' + a for a in q.all_answers()]
vecs = tfidf.transform(docs)
X.extend(vecs)
ys = np.zeros(4)
ys[q.correct] = 1
y.append(ys)
true_ans.append(q.correct)
X = vstack(X)
y = np.concatenate(y)
return X, y
# @memory.cache
def tfidf_test(tfidf, train_x, train_y, questions, concatenated):
correct = 0
# total = float(questions.count())
X = []
y = []
true_ans = []
for q in questions:
# print(q.body)
if concatenated:
q_vec = tfidf.transform([q.body])
a_vecs = [tfidf.transform([a]) for a in q.all_answers()]
vecs = [hstack([q_vec, a]) for a in a_vecs]
else:
docs = [q.body + '. ' + a for a in q.all_answers()]
vecs = tfidf.transform(docs)
X.extend(vecs)
ys = np.zeros(4)
ys[q.correct] = 1
y.append(ys)
true_ans.append(q.correct)
X = vstack(X)
y = np.concatenate(y)
print('Training')
lr = RandomForestClassifier(n_estimators=100)
# lr = ExtraTreesClassifier(1000)
# lr.fit(train_x, train_y)
# print('Scoring')
# lr = ExtraTreesClassifier(n_estimators=1000, n_jobs=8, verbose=3)
# lr = LogisticRegressionCV()
# lr = Perceptron('l1', n_jobs=-1)
lr.fit(train_x, train_y)
# print('Scoring')
# print(roc_auc_score(train_y, lr.predict_proba(train_x)[:,1]))
proba = lr.predict_proba(X)
# print(proba)
pred_ans = proba[:,1].reshape(len(questions),4).argmax(axis=1)
# print pred_ans
# print true_ans
# print(y)
# print(roc_auc_score(y, lr.predict_proba(X)[:,1]))
print((pred_ans == true_ans).mean())
return (pred_ans == true_ans).mean()
def get_vector(tfidf, q, a, concatenate):
if concatenate:
q_vec = tfidf.transform([q])
a_vec = tfidf.transform([a])
return hstack([q_vec, a_vec])
else:
return tfidf.transform([q + ' ' + a])
# @memory.cache()
def get_vecs(no_wrong, qs, answers, concatenated, header_context):
all_vecs = []
tf = []
allqs = []
allas = []
for idx, rows in qs.iterrows():
try:
# add_cont = 'Regarding {}, '.format(context.ix[rows.ix[1]]['header'].lower().encode('ascii', 'ignore'))
q = rows.ix[1]
# if len(q) < 100:
# if header_context:
# q = add_cont + q
a = rows.ix[2]
allqs.append(q)
allas.append(a)
rand_indexs = np.random.permutation(len(answers))[:no_wrong]
rnds = answers.as_matrix()[rand_indexs]
tf.append(1)
for i in range(no_wrong):
allqs.append(q)
allas.append(rnds[i])
tf.append(0)
except:
pass
q_vecs = tfidf.transform(allqs)
a_vecs = tfidf.transform(allas)
X = hstack([q_vecs, a_vecs])
y = np.array(tf)
return X, y
def get_vecs_no_gen(qs, concatenated, header_context):
q_vecs = tfidf.transform(qs['0'].as_matrix())
a_vecs = tfidf.transform(qs['1'].as_matrix())
X = hstack((q_vecs, a_vecs))
y = qs['3']
return X, y
def sanity_check(no_wrong, qs, answers, concatenated, header_context):
all_vecs = []
tf = []
q = "A firm must ensure that information that contains an indication of past performance of relevant business , a relevant investment or a financial index , satisfies the following conditions : the information includes appropriate performance information which covers at least the immediately preceding how many years , or the whole period for which the investment has been offered , the financial index has been established , or the service has been provided if less than five years , or such longer period as the firm may decide , and in every case that performance information must be based on and show complete 12-month periods ?"
t_a = "at least the immediately preceding five years , or the whole period for which the investment has been offered"
f_a = "at least the immediately preceding ten years , or the whole period for which the investment has been offered"
q2 = 'A firm must ensure that information that contains an indication of past performance of relevant business , a relevant investment , a structured deposit or a financial index , satisfies the following conditions : it discloses the effect of commissions , fees or other charges if the indication is based on which of the following ?'
t2_a = 'gross performance'
f2_a = 'net performance'
q3 = "A firm must ensure that information that contains an indication of past performance of relevant business , a relevant investment or a financial index , satisfies which of the following?"
t3_a = 'the following conditions : the reference period and the source of information are clearly stated'
f3_a = 'the following conditions : the reference period and the source of information are unclearly stated'
vec1 = get_vector(tfidf, q, t_a, True)
vec2 = get_vector(tfidf, q, f_a, True)
vec3 = get_vector(tfidf, q2, t2_a, True)
vec4 = get_vector(tfidf, q2, f2_a, True)
vec5 = get_vector(tfidf, q3, t3_a, True)
vec6 = get_vector(tfidf, q3, f3_a, True)
X = vstack([vec1, vec2, vec3, vec4, vec5, vec6])
y = np.array([1,0,1,0,1,0])
return X, y
if __name__ == '__main__':
# statement_checker('ref_perf.txt', 'to_parser_perf.txt', condense_ref)
# statement_checker('ref_suitability.txt', 'to_parser_suitability.txt', condense_ref)
# statement_checker('ref_colon.txt', 'to_parser_colon.txt', condense_ref)
# statement_checker('ref_perf_colon.txt', 'to_parser_perf_colon.txt', condense_ref)
# statement_checker('ref_everything.txt', 'to_parser_everything.txt', condense_ref)
# context = load_additional_context()
_, _, tfidf = generate_clause_set(get_clause_id, 1)
#
# q = "A firm must ensure that information that contains an indication of past performance of relevant business , a relevant investment or a financial index , satisfies the following conditions : the information includes appropriate performance information which covers at least the immediately preceding how many years , or the whole period for which the investment has been offered , the financial index has been established , or the service has been provided if less than five years , or such longer period as the firm may decide , and in every case that performance information must be based on and show complete 12-month periods ?"
# t_a = "at least the immediately preceding five years , or the whole period for which the investment has been offered"
# f_a = "at least the immediately preceding ten years , or the whole period for which the investment has been offered"
#
# q2 = 'A firm must ensure that information that contains an indication of past performance of relevant business , a relevant investment , a structured deposit or a financial index , satisfies the following conditions : it discloses the effect of commissions , fees or other charges if the indication is based on which of the following ?'
# t2_a = 'gross performance'
# f2_a = 'net performance'
#
# q3 = "A firm must ensure that information that contains an indication of past performance of relevant business , a relevant investment or a financial index , satisfies which of the following?"
# t3_a = 'the following conditions : the reference period and the source of information are clearly stated'
# f3_a = 'the following conditions : the reference period and the source of information are unclearly stated'
#
# tfidf.fit_transform([q + ' ' + t_a, q + ' ' + f_a,
# # q2 + ' ' + t2_a, q2 + ' ' + f2_a,
# # q3 + ' ' + t3_a, q3 + ' ' + f3_a])
# #
#
#
#
#
factor = 'suitability'
qs = pd.read_csv('generated_perf.csv')
# for a in qs['2']:
# print(a)
# qs = qs[(qs['2'].str.split().apply(len) > 1)]
tfidf.fit(qs['0'] + ' ' + qs['1'])
# tfidf.transform((qs['0'] + ' ' + qs['1']).as_matrix())
seed(0)
res = []
for i in range(100):
answers_1 = qs['1'].fillna('')
for no_wrong in [4]:
X, y = get_vecs(no_wrong, qs, answers_1, concatenated=True, header_context=False)
# X, y = get_vecs_no_gen(qs, concatenated=True, header_context=False)
# X, y = sanity_check(no_wrong, qs, answers_1, concatenated=True, header_context=False)
# questions = session.query(Question).filter((Question.id.in_([695, 724, 659, 413, 332])))
# tfidf_test(tfidf, X, y, questions, True)
questions = session.query(Question).filter(Question.type.in_([0,1])).all()
questions = session.query(Question).filter(Question.body.ilike('%suitability%')).all()
et = tfidf_test(tfidf, X, y, questions, True)
res.append(et)
# et = pickle.load(open('et.p', 'rb'))
# X_test, y_test = get_test_vecs(tfidf, questions, True)
# proba = et.predict_proba(X_test)
pass
print np.mean(res)
# statement_checker('ref_all.txt', 'to_parser_all.txt')
# questions_checker('q_ref.txt', 'q_to_parser.txt')
# question_cluster()
# ibm = get_ibm_model3()
# semi_sup()