forked from lichapibot/lichess-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.py
68 lines (47 loc) · 1.34 KB
/
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
59
60
61
62
63
64
65
66
67
68
import argparse
import chess
import chess.pgn
MAX_VISIT_GAMES = 100000
parser = argparse.ArgumentParser(description='Filter PGN file')
parser.add_argument('--inf', help="Specify input file")
parser.add_argument('--outf', help="Specify output file")
parser.add_argument('--minrtg', help="Specify minimum rating")
args = parser.parse_args()
print(args)
MIN_RTG = 1500
if args.minrtg:
MIN_RTG = int(args.minrtg)
pgn = open(args.inf)
cnt=0
found=0
pgns = []
class HeaderVisitor(chess.pgn.BaseVisitor):
def __init__(self):
self.game = chess.pgn.Game()
self.variation_stack = [self.game]
self.starting_comment = ""
self.in_variation = False
def visit_header(self, tagname, tagvalue):
self.game.headers[tagname] = tagvalue
def result(self):
return self.game
while cnt<MAX_VISIT_GAMES:
rawgame = chess.pgn.read_game(pgn)
if rawgame==None:
break
white_elo = 1500
black_elo = 1500
try:
white_elo = int(rawgame.headers["WhiteElo"])
black_elo = int(rawgame.headers["BlackElo"])
except:
pass
cnt+=1
if white_elo >= MIN_RTG and black_elo >= MIN_RTG:
found+=1
print("{:5d} {:5d}".format(cnt,found))
exporter = chess.pgn.StringExporter(headers=True, variations=True, comments=True)
pgn_string = rawgame.accept(exporter)
pgns.append(pgn_string)
with open(args.outf,"w") as outfile:
outfile.write("\n\n\n".join(pgns)+"\n\n\n")