-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogreg_filter.py
58 lines (45 loc) · 1.85 KB
/
logreg_filter.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
import levenshtein
import senlen_ratio
import networkx_GED
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score
from sklearn.preprocessing import label_binarize
input_file = '../Data/de-en.train'
udM1 = '../Data/UDPipe_models/german-ud-2.0-conll17-170315.udpipe' #a UDPipe model
udM2 = '../Data/UDPipe_models/english-ud-2.0-conll17-170315.udpipe' #a UDPipe model
output_file = '../Output/de-en.raw.filtered'
'''senlen parameters'''
use_sentence_length = True
senlen_removestopwords = True
senlen_stopwords = ['ADP']
'''levenshtein parameters'''
use_levenshtein = True
lev_removestopwords = True
lev_stopwords = ['CCONJ', 'NUM', 'PART']
lev_transposition = False
'''GED parameters'''
use_GED = False
ged_removestopwords = True
ged_stopwords = ['NUM', 'SCONJ']
methods = []
if use_sentence_length:
senlen = senlen_ratio.Senlen_ratio((input_file, senlen_removestopwords, senlen_stopwords))
methods.append(senlen)
if use_levenshtein:
lev = levenshtein.Levenshtein((input_file, lev_removestopwords, lev_stopwords, lev_transposition))
methods.append(lev)
if use_GED:
ged = networkx_GED.Networkx_GED((input_file, udM1, udM2, ged_removestopwords, ged_stopwords))
methods.append(ged)
label = tuple(m[0] for m in methods)
full_data_values = np.array(list(zip(*[[s[0] for s in m[1]] for m in methods])))
full_data_target = np.array([l[1] for l in methods[0][1]])
full_data_target = label_binarize(full_data_target, classes=['Y', 'N'])
clf = LogisticRegression().fit(full_data_values, full_data_target)
pred = clf.predict(full_data_values)
with open(input_file, 'r') as input_file, open(output_file, 'w') as output_file:
for sentence_pair, prediction in zip(input_file, pred):
if prediction == 0:
sentence_pair = '\t'.split(sentence_pair, 1)
output_file.write(sentence_pair)