-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo.py
executable file
·149 lines (141 loc) · 4.24 KB
/
go.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
#!/usr/bin/env python3
import getopt
import sys
import support
import glob
import numpy as np
import sklearn
import sklearn.linear_model
import sklearn.neighbors
import warnings
def unmapProba(probs,labelmap):
label_unmap = [None] * len(labelmap.keys())
for l in labelmap.keys():
li = labelmap[l]
label_unmap[li] = l
probaArray = []
i = 0
for prob_l in probs:
this_letter_array = []
top_args = prob_l.argsort()[::-1][:3]
print("Extracting character probability for slot %d" % i)
i += 1
for ti in top_args:
print("%s : %f" % (label_unmap[ti],prob_l[ti]))
this_letter_array.append(label_unmap[ti])
probaArray.append(this_letter_array)
return probaArray
def unmapLabel(labels,labelmap):
label_unmap = {}
for l in labelmap.keys():
label_unmap[labelmap[l]] = l
x = [label_unmap[i] for i in labels]
return x
def usage():
print("-t / --train: train and test a single folder")
print("-f / --format: specify a feature extraction helper")
print("-h / --holdout: specify a holdout dataset")
print("-w / --wordlist: specify wordlist for closest match guessing")
print("-d / --demo: run with extra bells and whistles")
CFG_MODEL = None
CFG_FOLDER = None
CFG_HOLDOUT = None
CFG_WORDLIST = None
CFG_DEMO = False
i = 0
labelMap = {}
labelArray = []
featureArray = []
def closestMatch(proba_top3,wordlist):
print("Searching closest words from '%s'" % wordlist)
f = open(wordlist)
len_proba_top3 = len(proba_top3)
for wl in f.readlines():
wl_ = wl.rstrip()
if len(wl_) != len_proba_top3:
continue
skipFlag = False
for i in range(0,len(wl_)):
if wl_[i] not in proba_top3[i]:
skipFlag = True
break
if skipFlag:
continue
else:
print(wl_)
f.close()
if __name__ == "__main__":
if len(sys.argv) < 2:
usage()
sys.exit(0)
sw = np.testing.suppress_warnings()
opts,args = getopt.getopt(sys.argv[1:],"m:f:h:w:d",["model=","folder=","holdout=","wordlist=","demo"])
for arg,val in opts:
if arg in ("-m","--model"):
CFG_MODEL = val
elif arg in ("-h","--holdout"):
CFG_HOLDOUT = val
elif arg in ("-f","--folder"):
CFG_FOLDER = val
elif arg in ("-w","--wordlist"):
CFG_WORDLIST = val
elif arg in ("-d","--demo"):
import matplotlib.pyplot as plt
CFG_DEMO = True
# todo: implement mode selector
if CFG_MODEL is None:
print("go.py: missing -m/--model argument. must be one of wave,em")
sys.exit(0)
if CFG_FOLDER is None:
print("go.py: missing -f/--format argument.")
sys.exit(0)
try:
exec("import support.%s" % CFG_MODEL)
__wave_model = eval("support.%s.WaveHelper" % CFG_MODEL)
except Exception as e:
print("go.py: could not import WaveHelper from support.%s" % CFG_MODEL)
print(e)
sys.exit(0)
print("Constructing ML Classifier...")
for fn in glob.glob("%s/*" % CFG_FOLDER):
wh = __wave_model(fn)
with warnings.catch_warnings():
if CFG_DEMO:
warnings.simplefilter("ignore")
f = wh.extractFeatures() # returns: an array of features
l = wh.getLabel() # returns: a single label per file
if l in labelMap.keys():
l_a = labelMap[l]
else:
labelMap[l] = i
l_a = i
i += 1
featureArray += f
labelArray += [l_a] * len(f)
clf = sklearn.linear_model.LogisticRegression()
if CFG_HOLDOUT is None:
print("Using train_test_split approach")
feature_train,feature_test,label_train,label_test = sklearn.model_selection.train_test_split(featureArray,labelArray,test_size=0.2)
else:
print("Using holdout data approach")
feature_train = featureArray
label_train = labelArray
wh = __wave_model(CFG_HOLDOUT)
f = wh.extractFeatures()
feature_test = f
with warnings.catch_warnings():
if CFG_DEMO:
warnings.simplefilter("ignore")
clf.fit(feature_train,label_train)
print("PREDICTION:")
l = list(clf.predict(feature_test))
lf = clf.predict_proba(feature_test)
unmapped = unmapLabel(l,labelMap)
print(unmapped)
prob_array = unmapProba(lf,labelMap)
if CFG_WORDLIST is not None and CFG_HOLDOUT is not None:
closestMatch(prob_array,CFG_WORDLIST)
if CFG_HOLDOUT is None:
print("LABEL_TEST:")
unmapped_test = unmapLabel(label_test,labelMap)
print(unmapped_test)