-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge.py
156 lines (144 loc) · 5.43 KB
/
merge.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
import sys
import string
import re
import os
import platform
def get_path():
path = sys.path[0]
if os.path.isdir(path):
return path
elif os.path.isfile(path):
return os.path.dirname(path)
filter = None
if len(sys.argv) >= 2:
filter = sys.argv[1]
nickmap = {}
fnick = open('nickname.txt', 'r')
while True:
reads = fnick.readline()
if not reads:
break
reads = reads.strip()
if len(reads) == 0:
break
rec = re.split('\s+', reads)
if len(rec) == 3:
nickmap[str.upper(rec[0])] = (rec[1], rec[2])
else:
nickmap[str.upper(rec[0])] = (rec[1], None)
fnick.close()
blacklist = {}
fblacklist = open('blacklist.txt', 'r')
while True:
reads = fblacklist.readline()
if not reads:
break
reads = reads.strip()
if len(reads) == 0:
break
rec = re.split('\s+', reads)
blacklist[rec[0] + '\x01' + rec[1]] = 1
fblacklist.close()
path = get_path()
if platform.system() == 'Windows':
path = path + '\\' + 'records' + '\\'
else:
path = path + '/' + 'records' + '/'
scoremap = {}
allengines = {}
engine_year_map = {}
for root, dirs, files in os.walk(path):
for file in files:
if filter:
if not file.split('.')[0].split('_')[1] in filter:
continue
curscoremap = {}
maxscore = 0
fin = open(path + file, 'r')
reads = fin.readline().strip()
reads = str.upper(reads)
names = re.split('\s+', reads)[1:]
for i in range(len(names)):
if blacklist.has_key(names[i] + '\x01' + file.split('.')[0]):
names[i] = 'BLACKLIST'
elif nickmap.has_key(names[i] + file.split('_')[0]):
names[i] = nickmap[names[i] + file.split('_')[0]]
if names[i][1]:
names[i] = names[i][0] + ' ' + names[i][1]
else:
names[i] = names[i][0]
elif nickmap.has_key(names[i]):
names[i] = nickmap[names[i]]
if names[i][1]:
names[i] = names[i][0] + ' ' + names[i][1]
else:
names[i] = names[i][0]
else:
match = re.match(r'([A-Z_\\-]+)([0-9]*)', names[i])
if match:
name = match.group(1)
version = match.group(2)
if len(version) == 2 and (version[0] == '0' or version[0] == '1'):
version = '20' + version
if len(version) > 0:
names[i] = name + ' ' + version
allengines[names[i]] = 1
for i in range(len(names)):
year = string.atoi(file[:4])
engine_year_map.setdefault(names[i], set())
engine_year_map[names[i]].add(year)
reads = fin.readline().strip()
points = re.split('\s+', reads)[1:]
j = 0
winscore = None
losscore = None
for each in points:
if each == '-':
j += 1
elif each == ':':
pass
elif losscore == None:
losscore = string.atoi(each)
else:
winscore = string.atoi(each)
if string.atof(file[:4]) <= 2005 or string.atof(file[:4]) >= 2017:
winscore, losscore = losscore, winscore
engines = (names[i], names[j])
if names[i] < names[j]:
engines = (names[i], names[j])
scores = (winscore, losscore)
if not curscoremap.has_key(engines):
curscoremap[engines] = [0, 0]
curscoremap[engines] = [curscoremap[engines][0] + scores[0], curscoremap[engines][1] + scores[1]]
if scores[0] + scores[1] > maxscore:
maxscore = scores[0] + scores[1]
j += 1
winscore = None
losscore = None
for engines, scores in curscoremap.iteritems():
if not scoremap.has_key(engines):
scoremap[engines] = [0, 0, 0]
scoremap[engines] = [scoremap[engines][0] + scores[0], scoremap[engines][1] + maxscore - scores[0] - scores[1], scoremap[engines][2] + scores[1]]
fin.close()
fname = open('engines.txt', 'w')
allengines = sorted(allengines.keys())
for each in allengines:
fname.write(each + '\n')
fname.close()
fout = open('score.txt', 'w')
scoremap = sorted(scoremap.iteritems())
for each in scoremap:
engines = each[0]
scores = each[1]
if engines[0].split(' ')[0] != engines[1].split(' ')[0] and engines[0] != 'BLACKLIST' and engines[1] != 'BLACKLIST':
fout.write('\"' + engines[0] + '\"' + '\t' + str(scores[0]) + '\t' + str(scores[1]) + '\t' + str(scores[2]) + '\t' + '\"' + engines[1] + '\"' + '\n')
fout.close()
sorted_engine_year_map = sorted(engine_year_map.iteritems(), key = lambda x: x[0])
if not filter:
fname = 'engine_year_map'
else:
fname = 'engine_year_map_' + filter
fout = open(fname + '.txt', 'w')
for engine, years in sorted_engine_year_map:
fout.write(engine + '\t' + ','.join(map(str, sorted(years))) + '\n')
fout.close()