File size: 2,624 Bytes
813df99 |
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 |
import os
import re
def load_positions_mapping(positions_file_path):
"""
Reads a report where entries look like:
File: chesspgn_877.pgn
Positions: 702430, Games: 9808
and returns a dict { 'chesspgn_877.pgn': 702430, ... }.
"""
mapping = {}
file_pattern = re.compile(r'^\s*File:\s*(\S+)\s*$')
pos_pattern = re.compile(r'^\s*Positions:\s*(\d+)')
with open(positions_file_path, 'r') as f:
lines = f.readlines()
i = 0
while i < len(lines):
m_file = file_pattern.match(lines[i])
if m_file:
fname = m_file.group(1)
# look ahead up to 3 lines for Positions:
found = False
for j in range(i+1, min(i+4, len(lines))):
m_pos = pos_pattern.match(lines[j])
if m_pos:
mapping[fname] = int(m_pos.group(1))
found = True
break
if not found:
print(f"Warning: no Positions line found for {fname!r}")
i += 1
return mapping
def sum_positions_in_dir(mapping, split_dir):
"""
Looks for all .pgn files in split_dir, sums up mapping[filename].
Returns (count_of_files, total_positions).
"""
pgns = [fn for fn in os.listdir(split_dir) if fn.endswith('.pgn')]
total_positions = 0
missing = []
for fn in pgns:
if fn in mapping:
total_positions += mapping[fn]
else:
missing.append(fn)
if missing:
print(f"Warning: {len(missing)} files in {split_dir!r} not found in positions file:")
for fn in missing:
print(" ", fn)
return len(pgns), total_positions
if __name__ == "__main__":
# ←── adjust these paths to your environment ──→
positions_txt = '/home/kage/chess_workspace/ChessBot-Battleground/dataset/ChessBot-Dataset/analysis_results.txt'
input_dir = '/home/kage/chess_workspace/ChessBot-Battleground/dataset/ChessBot-Dataset' # contains subfolders 'train/' and 'test/'
train_dir = os.path.join(input_dir, 'train')
test_dir = os.path.join(input_dir, 'test')
# load the per‐file positions
mapping = load_positions_mapping(positions_txt)
# compute for train
n_train_files, train_positions = sum_positions_in_dir(mapping, train_dir)
# compute for test
n_test_files, test_positions = sum_positions_in_dir(mapping, test_dir)
print()
print(f"TRAIN → {n_train_files} files, {train_positions:,} total positions")
print(f" TEST → {n_test_files} files, {test_positions:,} total positions")
|