-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathpgn_parser.py
More file actions
346 lines (281 loc) Β· 12.3 KB
/
pgn_parser.py
File metadata and controls
346 lines (281 loc) Β· 12.3 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import chess
import chess.pgn
import json
from typing import List, Dict, Any, Optional
from dataclasses import dataclass
import re
@dataclass
class GameChunk:
"""Represents a 4-move sequence from a chess game"""
game_metadata: dict
move_sequence: dict
position_data: dict
position_facts: dict
annotations: dict
embedding_content: str
def extract_game_metadata(game) -> dict:
"""Extract basic metadata from PGN headers"""
headers = game.headers
return {
"white_player": headers.get("White", "Unknown"),
"black_player": headers.get("Black", "Unknown"),
"white_elo": int(headers.get("WhiteElo", 0)) if headers.get("WhiteElo") else None,
"black_elo": int(headers.get("BlackElo", 0)) if headers.get("BlackElo") else None,
"result": headers.get("Result", "*"),
"date": headers.get("Date", ""),
"event": headers.get("Event", ""),
"site": headers.get("Site", ""),
"round": headers.get("Round", ""),
"eco": headers.get("ECO", ""),
}
def extract_position_facts(board: chess.Board) -> dict:
"""Extract deterministic facts about the current position"""
facts = {}
# Basic material balance
material_balance = 0
piece_values = {chess.PAWN: 1, chess.KNIGHT: 3, chess.BISHOP: 3,
chess.ROOK: 5, chess.QUEEN: 9}
for square in chess.SQUARES:
piece = board.piece_at(square)
if piece:
value = piece_values[piece.piece_type]
if piece.color == chess.WHITE:
material_balance += value
else:
material_balance -= value
facts["material_balance"] = material_balance
# Castling rights
castling = {"white": [], "black": []}
if board.has_kingside_castling_rights(chess.WHITE):
castling["white"].append("k")
if board.has_queenside_castling_rights(chess.WHITE):
castling["white"].append("q")
if board.has_kingside_castling_rights(chess.BLACK):
castling["black"].append("k")
if board.has_queenside_castling_rights(chess.BLACK):
castling["black"].append("q")
facts["castling_rights"] = castling
# Open files (simple version)
open_files = []
for file_idx in range(8):
has_pawn = False
for rank in range(8):
square = chess.square(file_idx, rank)
piece = board.piece_at(square)
if piece and piece.piece_type == chess.PAWN:
has_pawn = True
break
if not has_pawn:
open_files.append(chess.FILE_NAMES[file_idx])
facts["open_files"] = open_files
# Game phase estimation
piece_count = len(board.piece_map())
if piece_count > 28:
game_phase = "opening"
elif piece_count > 12:
game_phase = "middlegame"
else:
game_phase = "endgame"
facts["game_phase"] = game_phase
return facts
def extract_move_comments(game, move_numbers: List[int]) -> dict:
"""Extract comments for moves in the chunk"""
comments = {}
variations = []
# This is simplified - full comment extraction is complex in python-chess
# For now, just extract basic move comments if available
# TODO: Implement proper comment extraction from game tree
# This would involve walking the game tree and collecting comments
# for the specific moves in our chunk
return {
"move_comments": comments,
"variations": variations
}
def format_embedding_content(moves: List[str], position_facts: dict,
fen: str, eco: str, comments: dict) -> str:
"""Format chunk data into text suitable for embedding"""
content_parts = []
# Core move sequence
content_parts.append(f"Moves: {' '.join(moves)}")
# Position description
content_parts.append(f"Position: {fen}")
# Opening if available
if eco:
content_parts.append(f"Opening: {eco}")
# Game phase
game_phase = position_facts.get("game_phase", "")
if game_phase:
content_parts.append(f"Game phase: {game_phase}")
# Material situation
material = position_facts.get("material_balance", 0)
if material == 0:
content_parts.append("Material: equal")
elif material > 0:
content_parts.append(f"Material: White ahead by {material} points")
else:
content_parts.append(f"Material: Black ahead by {abs(material)} points")
# Castling status
castling = position_facts.get("castling_rights", {})
white_castling = castling.get("white", [])
black_castling = castling.get("black", [])
if not white_castling and not black_castling:
content_parts.append("Castling: both sides completed")
elif white_castling and black_castling:
content_parts.append("Castling: both sides retain rights")
# Open files
open_files = position_facts.get("open_files", [])
if open_files:
content_parts.append(f"Open files: {', '.join(open_files)}")
# Add any human commentary
move_comments = comments.get("move_comments", {})
if move_comments:
for move_num, comment in move_comments.items():
content_parts.append(f"Commentary: {comment}")
return ". ".join(content_parts)
def create_overlapping_chunks(game) -> List[GameChunk]:
"""Create overlapping 4-move chunks from a game"""
chunks = []
metadata = extract_game_metadata(game)
# Collect ALL moves once to avoid repeated generator calls
all_moves = list(game.mainline_moves())
# Skip games with fewer than 4 moves
if len(all_moves) < 4:
return chunks
# Play through the game and collect moves in SAN notation
board = game.board()
moves = []
move_numbers = []
# Convert to standard algebraic notation
for move_count, move in enumerate(all_moves):
san_move = board.san(move)
moves.append(san_move)
move_numbers.append(move_count + 1)
board.push(move)
# Create overlapping 4-move chunks
for start_idx in range(len(moves) - 3):
try:
print(f"π Creating chunk {start_idx + 1}, start_idx={start_idx}")
chunk_moves = moves[start_idx:start_idx + 4]
chunk_move_numbers = move_numbers[start_idx:start_idx + 4]
print(f"β
Chunk moves extracted: {chunk_moves}")
# Replay to get position after these 4 moves
chunk_board = game.board()
print(f"β
Fresh board created")
for i in range(start_idx + 4):
print(f"Debug: chunk {start_idx + 1}, accessing all_moves[{i}] (max valid: {len(all_moves)-1})")
chunk_board.push(all_moves[i])
print(f"β
Board position replayed successfully")
# Determine whose turn it is after this sequence
to_move = "white" if chunk_board.turn == chess.WHITE else "black"
print(f"β
Turn determined: {to_move}")
# Extract position facts
position_facts = extract_position_facts(chunk_board)
print(f"β
Position facts extracted")
# Extract comments (simplified for now)
comments = extract_move_comments(game, chunk_move_numbers)
print(f"β
Comments extracted")
# Create the chunk
chunk = GameChunk(
game_metadata=metadata,
move_sequence={
"moves": chunk_moves,
"move_numbers": chunk_move_numbers,
"colors": ["white" if i % 2 == 0 else "black" for i in range(4)],
"to_move_after": to_move
},
position_data={
"fen": chunk_board.fen(),
"move_count": start_idx + 4,
},
position_facts=position_facts,
annotations=comments,
embedding_content=format_embedding_content(
chunk_moves, position_facts, chunk_board.fen(),
metadata.get("eco", ""), comments
)
)
print(f"β
Chunk {start_idx + 1} created successfully")
chunks.append(chunk)
print(f"β
Chunk {start_idx + 1} added to chunks list")
except Exception as chunk_error:
print(f"β Error in chunk {start_idx + 1}: {type(chunk_error).__name__}: {chunk_error}")
import traceback
traceback.print_exc()
raise # Re-raise to see full stack trace
return chunks
def parse_pgn_file(pgn_file_path: str, max_games: Optional[int] = None) -> List[dict]:
"""Parse PGN file and return list of chunks"""
chunks = []
games_processed = 0
print(f"π Starting to parse {pgn_file_path}")
with open(pgn_file_path, 'r', encoding='utf-8') as pgn_file:
while True:
game = chess.pgn.read_game(pgn_file)
if game is None:
break
try:
game_chunks = create_overlapping_chunks(game)
# Convert chunks to dictionaries for JSON serialization
for chunk in game_chunks:
chunk_dict = {
"game_metadata": chunk.game_metadata,
"move_sequence": chunk.move_sequence,
"position_data": chunk.position_data,
"position_facts": chunk.position_facts,
"annotations": chunk.annotations,
"embedding_content": chunk.embedding_content
}
chunks.append(chunk_dict)
games_processed += 1
if games_processed % 100 == 0:
print(f" Processed {games_processed} games, created {len(chunks)} chunks")
if max_games and games_processed >= max_games:
break
except Exception as e:
print(f"β οΈ Error processing game {games_processed + 1}: {type(e).__name__}: {str(e)}")
print(f" Game headers: {game.headers if game else 'No game object'}")
if game:
try:
moves = list(game.mainline_moves())
print(f" Move count: {len(moves)}")
if len(moves) < 4:
print(f" β€ Game too short for 4-move chunks")
except Exception as move_error:
print(f" β€ Could not extract moves: {move_error}")
continue
print(f"β
Completed parsing: {games_processed} games β {len(chunks)} chunks")
return chunks
def save_chunks_to_json(chunks: List[dict], output_file: str):
"""Save chunks to JSON file"""
print(f"πΎ Saving {len(chunks)} chunks to {output_file}")
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(chunks, f, indent=2, ensure_ascii=False)
print(f"β
Saved to {output_file}")
if __name__ == "__main__":
# Test with small file
input_file = "mega-2025-small.pgn"
output_file = "chunks-small.json"
# Parse PGN file
chunks = parse_pgn_file(input_file, max_games=50) # Limit for testing
# Save to JSON
save_chunks_to_json(chunks, output_file)
# Show sample output
if chunks:
print("\nπ Sample chunk:")
sample_chunk = chunks[0]
print(f"Moves: {sample_chunk['move_sequence']['moves']}")
print(f"FEN: {sample_chunk['position_data']['fen']}")
print(f"Embedding content: {sample_chunk['embedding_content'][:200]}...")