-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAnalyzer.py
More file actions
219 lines (186 loc) · 5.82 KB
/
Analyzer.py
File metadata and controls
219 lines (186 loc) · 5.82 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
from fileDB import FileDB
from userDB import UserDB
from problemDB import ProblemDB
from subprocess import *
import random
import json
def getUsers():
udb = UserDB()
users = udb.getAllUser()
udb.close()
return users
def getFilenames(fdb, username, limit, where=None):
file_filter = {'user_name': username}
if not where is None:
file_filter.update(where)
return fdb.getFiles(file_filter, limit)
def chooseSampleFiles(user_num=1000, file_num_each=1):
users = getUsers()
sample_list = random.sample(users, user_num)
fdb = FileDB()
files = []
failed = 0
file_filter = {'lang': 'GNU C++11', 'verdict': 'OK'}
for i, user in enumerate(sample_list):
if (i+1)%1000 == 0:
print(i+1)
file_list = getFilenames(fdb, user, file_num_each, file_filter)
if len(file_list) == 0:
failed += 1
continue
files += file_list
print('failed num: '+str(failed))
return files
def getFileFromProbId(contest_id, prob_index, limit=-1):
file_filter = {
'lang': 'GNU C++11',
'verdict': 'OK',
'contestId': contest_id,
'prob_index': prob_index
}
fdb = FileDB()
return fdb.getFiles(file_filter, limit)
def writeToFileList(files):
with open('data/filelist.txt', 'w', encoding='utf-8') as f:
for data in files:
f.write('%s %s\n' % ('src/' + data['file_name'], data['lang'].replace(' ', '_')))
def analyze(jar_path, files):
cmd = ['java', '-jar', jar_path, '-e', '-j', '-s', 'filelist.txt']
pipe = Popen(cmd, cwd='data', stdout=PIPE, stderr=None).stdout
idx = 0
# print(files)
result = []
ok = 0
for line in pipe.readlines():
data = json.loads(line.decode('utf-8').strip())
# print(data)
while idx < len(files) and files[idx]['file_name'] != data['src_name']:
# print('not found '+files[idx]['file_name'])
idx += 1
if idx < len(files):
ok += 1
file_data = files[idx]
del data['src_name']
file_data['data'] = data
result.append(file_data)
# print(file_data)
else:
break
print('finish analyze. %d/%d files were analyzed.' % (ok, len(files)))
return result
def getMode(multiset):
max_value = 0
max_idx = 0
for k, v in multiset.items():
if v > max_value:
max_value = v
max_idx = k
return max_idx
def normalization_mode(result, keywords):
mode = {}
for kw in keywords:
multiset = {}
for data in result:
d = data['data']
if not kw in d:
d[kw] = 0
continue
num = d[kw]
if not num in multiset:
multiset[num] = 0
multiset[num] += 1
mode[kw] = getMode(multiset)
print(mode)
for kw in keywords:
if mode[kw] == 0:
continue
for data in result:
data['data'][kw] /= mode[kw]
def normalization_median(result, keywords):
median = {}
for kw in keywords:
count = []
for data in result:
d = data['data']
if not kw in d:
d[kw] = 0
continue
count.append(d[kw])
if len(count) == 0:
median[kw] = 0
continue
median[kw] = sorted(count)[len(count)//2]
# print(median)
for kw in keywords:
if median[kw] == 0:
continue
for data in result:
data['data'][kw] /= median[kw]
def getDictUserToRating():
udb = UserDB()
users = udb.getAllUserWithRating()
utor = {}
for user in users:
utor[user['user_name']] = user['rating']
return utor
def writeData(result, keywords):
utor = getDictUserToRating()
normalization_median(result, keywords)
with open('data/analysis_result_norm.csv', 'w') as f:
f.write('user,rating,'+','.join(keywords)+'\n')
for data in result:
f.write('"%s",%d' % (data['user_name'], utor[data['user_name']]))
for key in keywords:
d = 0
if key in data['data']:
d = data['data'][key]
f.write(',%f' % d)
f.write('\n')
def getKeywords():
keywords = []
with open('data/keyword') as f:
for line in f:
keywords.append(line.strip())
return keywords
def setData(result, data, keywords):
for res in result:
name = res['user_name']
if not name in data:
data[name] = {}
for key in keywords:
data[name][key] = 0
data[name]['prob_num'] = 0
d = res['data']
for key in keywords:
data[name][key] += d[key]
data[name]['prob_num'] += 1
def statistics(border=0):
pdb = ProblemDB()
problems = pdb.getProblems('points>100')
keywords = getKeywords()
data = {}
for prob in problems:
print(prob['id'])
files = getFileFromProbId(prob['contestId'], prob['prob_index'])
writeToFileList(files)
result = analyze('analyze.jar', files)
if len(result) < border:
continue
normalization_median(result, keywords)
setData(result, data, keywords)
utor = getDictUserToRating()
with open('data/statistics_nonfiltered.csv', 'w') as f:
for key, each in data.items():
f.write('"%s",%d' % (key, utor[key]))
for kw in keywords:
f.write(',%f' % (each[kw]/each['prob_num']))
f.write('\n')
def testAnalysis():
files = getFileFromProbId(712, 'B')
print('finish getting file list')
writeToFileList(files)
result = analyze('analyze.jar', files)
keywords = getKeywords()
writeData(result, keywords)
if __name__ == '__main__':
statistics()