-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathshortenpvs.py
More file actions
51 lines (46 loc) · 1.66 KB
/
Copy pathshortenpvs.py
File metadata and controls
51 lines (46 loc) · 1.66 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
import argparse, chess, random, re
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Shorten existing PVs by removing moves from the end.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--epdFile",
default="matetrackpv.epd",
help="file containing the positions, their mate scores and their PVs",
)
parser.add_argument(
"--outFile",
default="shortpvs.epd",
help="output file with shortened PVs",
)
parser.add_argument(
"--plies",
type=int,
default=1,
help="number of plies to remove, if possible",
)
parser.add_argument(
"--random",
action="store_true",
help="remove random number X of plies, with X in [0, PLIES].",
)
args = parser.parse_args()
p = re.compile(r"([0-9a-zA-Z/\- ]*) bm #([0-9\-]*);")
fens = count = 0
with open(args.epdFile) as fin, open(args.outFile, "w") as fout:
for line in fin:
m = p.match(line)
assert m, f"error for line '{line[:-1]}' in file {args.epdFile}"
fen, bm = m.group(1), int(m.group(2))
_, _, pv = line.partition("; PV: ")
pv, _, _ = pv[:-1].partition(";") # remove '\n'
pv = pv.split()
plies = random.randint(0, args.plies) if args.random else args.plies
if plies == 0 or plies >= len(pv):
fout.write(line)
else:
fout.write(f"{fen} bm #{bm}; PV: {' '.join(pv[:-plies])};\n")
count += 1
fens += 1
print(f"Loaded {fens} FENs, shortened {count} PVs.")