/
parse_file.py
executable file
·284 lines (202 loc) · 7.67 KB
/
parse_file.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env python
# encoding: utf-8
##
# @file parse_file.py
# @brief
# @author Zhijie Xie
# @version 1.0.0
# @date 2014-11-19
import re
import os
import json
import operator
import cmath
from scipy import io
from numpy import dot
from numpy import loadtxt
from numpy import array
from optparse import OptionParser
USAGE = "usage: %prog [options] arg1 arg2"
VERSION = 'v1.0.0'
class Argument(object):
"""Docstring for Argument. """
def __init__(self, weight_file, config_file):
"""TODO: to be defined1.
:weight_file: TODO
:config_file: TODO
"""
self.weight_file = weight_file
self.config_file = config_file
self.input_layer_size = io.loadmat(config_file)['input_layer_size']
self.hidden_layer_size = io.loadmat(config_file)['hidden_layer_size']
self.num_labels = io.loadmat(config_file)['num_labels']
self.theta1 = io.loadmat(weight_file)['Theta1']
self.theta2 = io.loadmat(weight_file)['Theta2']
def sigmoid(z):
"""TODO: Docstring for sigmoid.
:z: TODO
:returns: TODO
"""
return 1 / (1 + cmath.e**(-z))
def rand_initialize_weights(L_in, L_out):
"""TODO: Docstring for randInitializeWeights.
:L_in: TODO
:L_out: TODO
:returns: TODO
"""
pass
def load_argument(weight_file, config_file):
"""TODO: Docstring for load_weight.
:weight_file: TODO
:config_file: TODO
:returns: TODO
"""
argument = Argument(weight_file, config_file)
return argument
def get_options():
"""TODO: Docstring for get_options.
:returns: TODO
"""
parser = OptionParser(usage=USAGE, version=VERSION)
parser.add_option('-w', '--weight', action='store', type='string',
help='The weight file.',
dest='weight_file')
parser.add_option('-c', '--config', action='store', type='string',
help='The config file.',
dest='config_file')
parser.add_option('-f', '--file', action='store', type='string',
help='the file you want to make a prediction.',
dest='filename')
parser.add_option('-m', '--multi', action='store_true', dest='multi',
help='If this symbol is set, train the algorithem by all file \
in this directory.')
parser.add_option('-t', '--test', action='store_true', dest='test',
help='Test.')
return parser.parse_args()
def predict(options, args):
"""TODO: Docstring for main.
:returns: TODO
"""
argument = load_argument(options.weight_file, options.config_file)
input_data = loadtxt('unknow.txt', delimiter=',')
x = input_data[0:-1]
y = input_data[-1]
with open('all_extension.txt', 'r') as extension_file:
all_extension = json.loads(extension_file.read())
result = sigmoid(dot(argument.theta2, sigmoid(array([1] + list(dot(argument.theta1, array([1] + list(x))))))))
rate = max(list(result))
print 'The correct rate is: %f' %(rate)
print 'The extension of the file should be \"%s\"' %(all_extension[operator.indexOf(result, rate)])
def create_predict_file(filename, all_extension, all_keywords, exclude_file):
"""TODO: Docstring for predict.
:filename: TODO
:exclude_file: TODO
:returns: TODO
"""
keywords = {}
single_file = [filename]
index = operator.indexOf(all_extension, os.path.splitext(filename)[1]) + 1
keywords = fill_feature_dict(single_file, './unknow/')
result = keyword_to_feature(keywords, all_keywords, index)
with open('unknow.txt', 'w') as unknow_file:
unknow_file.write(result + os.linesep)
def test(all_extension, all_keywords, exclude_file=[]):
"""TODO: Docstring for parse_multi_file.
:returns: TODO
"""
all_files = [x for x in os.listdir('./test') if x[0] != '.']
with open('test.txt', 'w') as test_file:
for one in all_files:
single_list = [one]
index = operator.indexOf(all_extension, os.path.splitext(one)[1]) + 1
keywords = fill_feature_dict(single_list, './test/')
result = keyword_to_feature(keywords, all_keywords, index)
test_file.write(result + os.linesep)
def fill_feature_dict(all_files, folder, exclude_file=[]):
"""TODO: Docstring for fill_feature_dict.
:all_files: TODO
:exclude_file: TODO
:returns: TODO
"""
keywords = {}
for filename in all_files:
if os.path.splitext(filename)[1] not in exclude_file:
filename = folder + filename
with open(filename, 'r') as code_file:
# remove the string and word in the right-hand side of [!=<>:;]
processed = re.sub('\w+\s*[!=<>:;]', '', code_file.read())
processed = re.sub('\'.*\'', '', processed)
processed = re.sub('\".*\"', '', processed)
all_words = [x for x in re.findall('[A-Za-z]+', processed) if len(x) > 1 and len(x) <= 10]
for word in all_words:
keywords.setdefault(word, 1)
return keywords
def keyword_to_feature(keywords, all_keywords, index):
"""TODO: Docstring for keyword_to_feature.
:keywords: TODO
:returns: TODO
"""
result = ''
all_keywords_order = sorted(all_keywords.iteritems(), key=lambda x: x[0], reverse=False)
for key_tuple in all_keywords_order:
result += str(keywords.setdefault(key_tuple[0], 0)) + ','
result += str(index)
return result
def count_one_type(extension):
"""TODO: Docstring for count_one_type.
:extension: TODO
:returns: TODO
"""
keywords = {}
all_files = [x for x in os.listdir('./train') if os.path.splitext(x)[1] == extension]
keywords = fill_feature_dict(all_files, './train/')
return keywords
def parse_multi_file(output_file, exclude_file):
"""TODO: Docstring for parse_multi_file.
:returns: TODO
"""
all_keywords = {}
all_files = [x for x in os.listdir('./train') if x[0] != '.']
all_keywords = fill_feature_dict(all_files, './train/', exclude_file)
temp = [os.path.splitext(x)[1] for x in all_files if x[0] != '.']
all_extension = list(set(temp))
for one in all_files:
single_list = [one]
index = operator.indexOf(all_extension, os.path.splitext(one)[1]) + 1
keywords = fill_feature_dict(single_list, './train/')
result = keyword_to_feature(keywords, all_keywords, index)
output_file.write(result + os.linesep)
with open('all_extension.txt', 'w') as extension_file:
extension_file.write(json.dumps(all_extension))
with open('all_keywords.txt', 'w') as keyword_file:
keyword_file.write(json.dumps(all_keywords))
with open('oct_extension.txt', 'w') as oct_extension:
for extension in all_extension:
oct_extension.write(extension + os.linesep)
def main():
"""TODO: Docstring for main.
:returns: TODO
"""
(options, args) = get_options()
exclude_file = ['.swp', '.mat', '']
try:
with open('all_extension.txt', 'r') as extension_file:
all_extension = json.loads(extension_file.read())
with open('all_keywords.txt', 'r') as keyword_file:
all_keywords = json.loads(keyword_file.read())
except IOError, e:
pass
if options.multi:
with open('data.txt', 'w') as output_file:
parse_multi_file(output_file,
exclude_file)
elif options.test:
test(all_extension, all_keywords)
else:
create_predict_file(options.filename,
all_extension,
all_keywords,
exclude_file)
predict(options, args)
if __name__ == '__main__':
main()