-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathbuild_database.py
More file actions
729 lines (601 loc) · 26.4 KB
/
build_database.py
File metadata and controls
729 lines (601 loc) · 26.4 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
# 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 asyncio
import json
from pathlib import Path
import sys
import chess.engine
import concurrent.futures
import chess.pgn
import io
import re
from typing import Optional
from parse_pgn import parse_pgn_to_positions
from position_features import extract_position_features
from stockfish_pool import create_deep_analysis_pool
from chess_description_generator import ChessDescriptionGenerator, enhance_single_position_with_retry
def analyze_single_position_with_pool(pos_with_idx, analysis_pool):
"""Analyze a single position using the provided engine pool"""
position, idx = pos_with_idx
engine = analysis_pool.get_engine() # Blocks until engine available
try:
board = chess.Board(position["fen"])
analysis = engine.analyse(board, chess.engine.Limit(depth=18))
# Extract evaluation score
score = analysis["score"].white()
if score.is_mate():
eval_score = 10000 if score.mate() > 0 else -10000
eval_type = "mate"
mate_in = abs(score.mate())
else:
eval_score = score.score() / 100.0
eval_type = "cp"
mate_in = None
best_move = analysis["pv"][0] if analysis.get("pv") else None
best_move_san = board.san(best_move) if best_move else None
return (idx, {
"evaluation": eval_score,
"evaluation_type": eval_type,
"mate_in": mate_in,
"best_move": str(best_move) if best_move else None,
"best_move_san": best_move_san,
"principal_variation": [str(move) for move in analysis.get("pv", [])[:5]],
"depth": analysis.get("depth"),
"nodes": analysis.get("nodes"),
"time": analysis.get("time"),
})
except Exception as e:
return (idx, {
"evaluation": 0.0,
"evaluation_type": "error",
"error": str(e),
})
finally:
analysis_pool.return_engine(engine)
async def build_chess_database(
pgn_file: str,
output_file: str,
max_positions: int = 5000,
resume: bool = True,
):
"""Complete pipeline: PGN -> Features -> Descriptions -> Database"""
print("🏁 CHESS DATABASE BUILDER")
print("=" * 50)
# Check for existing intermediate files to resume from
intermediate_file = output_file.replace(
".json", "_with_features_and_engine.json"
)
features_only_file = output_file.replace(".json", "_with_features.json")
positions = None
# Try to resume from most recent intermediate file
if resume and Path(intermediate_file).exists():
print(f"\n🔄 Resuming from {intermediate_file}")
with open(intermediate_file, "r") as f:
positions = json.load(f)
print(
f" ✓ Loaded {len(positions)} positions with features and engine"
" analysis"
)
skip_to_step = 4 # Skip to descriptions
elif resume and Path(features_only_file).exists():
print(f"\n🔄 Resuming from {features_only_file}")
with open(features_only_file, "r") as f:
positions = json.load(f)
print(f" ✓ Loaded {len(positions)} positions with features")
skip_to_step = 2.5 # Skip to engine analysis
else:
# Check if input file exists
if not Path(pgn_file).exists():
print(f"❌ Error: PGN file '{pgn_file}' not found!")
print(
f" Make sure your mega-2025-small.pgn is in the current directory"
)
return
# Step 1: Parse PGN file
print(f"\n📖 Step 1: Parsing {pgn_file}")
positions = parse_pgn_to_positions(pgn_file, max_positions)
if not positions:
print(f"❌ No positions extracted from {pgn_file}")
return
print(f" ✓ Extracted {len(positions)} positions")
skip_to_step = 2 # Start from feature extraction
# Step 2: Extract features for each position (skip if resuming with features)
if skip_to_step <= 2:
print(f"\n🔍 Step 2: Extracting position features")
print(f" Processing {len(positions)} positions...")
for i, position in enumerate(positions):
try:
position["position_features"] = extract_position_features(
position["fen"]
)
except Exception as e:
print(f" ⚠️ Error processing position {i+1}: {e}")
# Skip this position or use basic features
position["position_features"] = {"error": str(e)}
if (i + 1) % 500 == 0:
print(
f" Progress: {i+1}/{len(positions)} positions"
f" ({((i+1)/len(positions)*100):.1f}%)"
)
print(f" ✓ Features extracted for all positions")
# Save intermediate checkpoint
features_checkpoint = output_file.replace(".json", "_with_features.json")
with open(features_checkpoint, "w") as f:
json.dump(positions, f, indent=2)
print(f" ✓ Checkpoint saved to {features_checkpoint}")
# Step 2.5: Pool-based Stockfish analysis of all positions
if skip_to_step <= 2.5:
print(f"\n🔍 Step 2.5: Pool-based Stockfish analysis")
print(f" Analyzing {len(positions)} positions with 4 engines...")
# Initialize pool ONCE before threading to avoid race condition
analysis_pool = create_deep_analysis_pool(pool_size=4)
try:
# Create position list with indices
positions_with_idx = [(pos, i) for i, pos in enumerate(positions)]
# Process all positions with thread pool + engine pool
completed = 0
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
# Submit all positions with the shared pool
future_to_pos = {executor.submit(analyze_single_position_with_pool, pos_data, analysis_pool): pos_data
for pos_data in positions_with_idx}
# Collect results as they complete
for future in concurrent.futures.as_completed(future_to_pos):
idx, analysis = future.result()
positions[idx]["stockfish_analysis"] = analysis
completed += 1
if completed % 25 == 0: # More frequent progress updates
print(f" Progress: {completed}/{len(positions)} positions analyzed "
f"({(completed/len(positions)*100):.1f}%)")
print(f" ✓ Pool-based Stockfish analysis completed for all positions")
except Exception as e:
print(f" ❌ Error in pool-based analysis: {e}")
print(f" Adding fallback analysis...")
# Add empty analysis for positions that failed
for position in positions:
if "stockfish_analysis" not in position:
position["stockfish_analysis"] = {
"evaluation": 0.0,
"evaluation_type": "error",
"error": "Pool analysis failed",
}
finally:
# Clean up the single analysis pool
print(" Cleaning up analysis engine pool...")
analysis_pool.cleanup()
# Save intermediate checkpoint with engine analysis
with open(intermediate_file, "w") as f:
json.dump(positions, f, indent=2)
print(f" ✓ Checkpoint saved to {intermediate_file}")
# Step 3: Save intermediate result (positions with features and engine analysis)
intermediate_file = output_file.replace(
".json", "_with_features_and_engine.json"
)
print(f"\n💾 Step 3: Saving intermediate results")
try:
with open(intermediate_file, "w") as f:
json.dump(positions, f, indent=2)
print(
" ✓ Saved positions with features and engine analysis to"
f" {intermediate_file}"
)
except Exception as e:
print(f" ❌ Error saving intermediate file: {e}")
return
# Step 4: Dual enhancement - Human commentary + LLM analysis
print(f"\n📝 Step 4: Dual enhancement - Human commentary + LLM analysis")
# Load PGN games for commentary extraction
print(" Loading PGN games for commentary lookup...")
original_pgn_games = load_pgn_games(pgn_file)
print(f" ✓ Loaded {len(original_pgn_games)} games for commentary extraction")
# Initialize LLM generator for systematic analysis
print(" Initializing LLM generator for systematic analysis...")
generator = ChessDescriptionGenerator()
chain = generator.create_description_chain()
# Process each position: extract commentary + generate LLM description
enhanced_count = 0
commentary_count = 0
llm_count = 0
errors = 0
# Prepare positions with indices for async LLM processing
positions_with_idx = [(pos, i) for i, pos in enumerate(positions)]
print(f" Processing {len(positions)} positions with dual enhancement...")
# Step 4a: Extract human commentary for all positions
print(f"\n 📝 Step 4a: Extracting human commentary...")
for idx, position in enumerate(positions):
try:
# Extract human commentary when available
commentary_desc = extract_pgn_commentary(position, original_pgn_games)
if commentary_desc:
position["human_commentary"] = commentary_desc
commentary_count += 1
if idx % 50 == 0:
print(f"\n 💬 Commentary sample (position {idx+1}):")
print(f" Game: {position['game_context']['white_player']} vs {position['game_context']['black_player']}")
print(f" Move: {position['move_number']} {position['last_move']}")
print(f" Expert: {commentary_desc['description'][:150]}...")
print(f" Raw: {commentary_desc.get('raw_commentary', '')[:100]}...")
if (idx + 1) % 200 == 0:
print(f" Commentary progress: {idx+1}/{len(positions)} positions ({commentary_count} with commentary)")
except Exception as e:
print(f" ⚠️ Commentary extraction error for position {idx+1}: {e}")
print(f" ✓ Commentary extraction completed: {commentary_count}/{len(positions)} positions have expert commentary")
# Step 4b: Generate LLM descriptions for ALL positions (incorporating expert commentary)
print(f"\n 🤖 Step 4b: Generating comprehensive LLM analysis...")
print(f" LLM will incorporate expert commentary when available")
print(f" Using Gemini 2.0 Flash-Lite with 10 concurrent workers...")
# Create semaphore for rate limiting
semaphore = asyncio.Semaphore(10)
# Create all LLM enhancement tasks
tasks = [
enhance_single_position_with_retry(pos_data, generator, chain, semaphore)
for pos_data in positions_with_idx
]
# Process LLM tasks with progress tracking
for task in asyncio.as_completed(tasks):
try:
idx, enhanced_desc, error = await task
if error:
print(f" ❌ LLM error for position {idx+1}: {error}")
# Use template fallback for LLM description
positions[idx]["enhanced_description"] = {
"description": generate_quick_description(positions[idx]),
"strategic_themes": [],
"tactical_elements": [],
"key_squares": [],
"source": "template_fallback"
}
errors += 1
else:
positions[idx]["enhanced_description"] = enhanced_desc
llm_count += 1
# Show combined sample every 25th completion
if llm_count % 25 == 0:
pos = positions[idx]
print(f"\n 📝 Comprehensive analysis sample (position {idx+1}):")
print(f" Game: {pos['game_context']['white_player']} vs {pos['game_context']['black_player']}")
print(f" Move: {pos['move_number']} {pos['last_move']}")
print(f" LLM synthesis: {enhanced_desc['description'][:150]}...")
print(f" LLM themes: {', '.join(enhanced_desc['strategic_themes'])}")
if pos.get('human_commentary'):
print(f" ✓ Incorporated expert commentary: {pos['human_commentary']['description'][:100]}...")
else:
print(f" ○ No expert commentary to incorporate")
enhanced_count += 1
if enhanced_count % 50 == 0:
print(f" LLM progress: {enhanced_count}/{len(positions)} positions processed ({llm_count} success, {errors} errors)")
except Exception as e:
print(f" ❌ Task execution error: {e}")
errors += 1
print(f" ✓ Dual enhancement completed:")
print(f" - Positions with human commentary: {commentary_count}")
print(f" - Positions with LLM analysis: {llm_count}")
print(f" - Positions with BOTH: {sum(1 for pos in positions if pos.get('human_commentary') and pos.get('enhanced_description'))}")
print(f" - Errors: {errors}")
# Step 5: Analysis summary and LLM cost estimation
print(f"\n💰 Step 5: Analysis and Cost Summary")
# Count positions by evaluation range
eval_ranges = {
"winning": 0,
"advantage": 0,
"equal": 0,
"disadvantage": 0,
"losing": 0,
"mate": 0,
"error": 0,
}
tactical_positions = 0
for pos in positions:
analysis = pos.get("stockfish_analysis", {})
eval_type = analysis.get("evaluation_type", "error")
if eval_type == "mate":
eval_ranges["mate"] += 1
tactical_positions += 1
elif eval_type == "error" or eval_type == "unavailable":
eval_ranges["error"] += 1
else:
eval_score = analysis.get("evaluation", 0)
if abs(eval_score) >= 3.0:
eval_ranges["winning" if eval_score > 0 else "losing"] += 1
tactical_positions += 1
elif abs(eval_score) >= 1.0:
eval_ranges["advantage" if eval_score > 0 else "disadvantage"] += 1
else:
eval_ranges["equal"] += 1
print(f" Engine Analysis Summary:")
print(
" - Winning/Losing positions:"
f" {eval_ranges['winning'] + eval_ranges['losing']}"
)
print(
" - Advantage/Disadvantage:"
f" {eval_ranges['advantage'] + eval_ranges['disadvantage']}"
)
print(f" - Roughly equal: {eval_ranges['equal']}")
print(f" - Checkmate positions: {eval_ranges['mate']}")
print(f" - Tactical positions (|eval| > 1.0): {tactical_positions}")
# Calculate actual LLM costs
avg_input_tokens = 500 # Estimated
avg_output_tokens = 250 # Estimated
total_input_tokens = enhanced_count * avg_input_tokens
total_output_tokens = enhanced_count * avg_output_tokens
input_cost = (total_input_tokens / 1_000_000) * 0.075 # Gemini 2.0 Flash-Lite pricing
output_cost = (total_output_tokens / 1_000_000) * 0.30
total_llm_cost = input_cost + output_cost
print(f"\n LLM Enhancement Results:")
print(f" - Enhanced positions: {enhanced_count}/{len(positions)}")
print(f" - Template fallbacks: {errors}")
print(f" - Estimated cost: ${total_llm_cost:.2f}")
print(f" - Input tokens: {total_input_tokens:,} (${input_cost:.2f})")
print(f" - Output tokens: {total_output_tokens:,} (${output_cost:.2f})")
# Step 6: Save final database
print(f"\n💾 Step 6: Saving final database")
try:
with open(output_file, "w") as f:
json.dump(positions, f, indent=2)
print(f" ✓ Database saved to {output_file}")
except Exception as e:
print(f" ❌ Error saving final database: {e}")
return
# Step 7: Database summary
print(f"\n📊 DATABASE SUMMARY")
print("=" * 30)
# Analyze the database content
game_phases = {"opening": 0, "middlegame": 0, "endgame": 0}
players = set()
years = set()
for pos in positions:
features = pos.get("position_features", {})
if isinstance(features, dict) and "game_phase" in features:
phase = features["game_phase"]
if phase in game_phases:
game_phases[phase] += 1
context = pos.get("game_context", {})
if "white_player" in context:
players.add(context["white_player"])
if "black_player" in context:
players.add(context["black_player"])
if "date" in context and context["date"]:
year = context["date"][:4] if len(context["date"]) >= 4 else None
if year and year.isdigit():
years.add(year)
print(f"Total positions: {len(positions)}")
print(
f"Game phases: Opening({game_phases['opening']}),"
f" Middlegame({game_phases['middlegame']}),"
f" Endgame({game_phases['endgame']})"
)
print(f"Unique players: {len(players)}")
print(
f"Year range: {min(years) if years else 'N/A'} -"
f" {max(years) if years else 'N/A'}"
)
print(f"\n🎉 SUCCESS!")
print(f"✓ Database built: {output_file}")
print(f"✓ Ready for similarity search implementation")
print(f"✓ Ready for LLM enhancement (optional)")
# Show a few sample positions
print(f"\n📋 SAMPLE POSITIONS:")
for i, pos in enumerate(positions[:3]):
print(
f"\n{i+1}. {pos['game_context']['white_player']} vs"
f" {pos['game_context']['black_player']}"
)
print(f" Move {pos['move_number']}: {pos['last_move']}")
print(f" {pos.get('quick_description', 'No description')}")
print(f" FEN: {pos['fen'][:50]}...")
def extract_pgn_commentary(position: dict, original_pgn_games: dict) -> dict:
"""Extract human commentary from PGN for this position"""
try:
# Find the game this position came from
game_context = position.get("game_context", {})
game_key = f"{game_context.get('white_player')}_{game_context.get('black_player')}_{game_context.get('date')}"
if game_key not in original_pgn_games:
return None
game = original_pgn_games[game_key]
move_number = position.get("move_number", 0)
# Walk through the game to find commentary at this move
board = game.board()
current_move = 1
for node in game.mainline():
if current_move == move_number:
# Extract commentary from this move
commentary_text = node.comment if hasattr(node, 'comment') else ""
if commentary_text.strip():
return {
"description": parse_commentary_to_description(commentary_text),
"strategic_themes": extract_strategic_themes(commentary_text),
"tactical_elements": extract_tactical_elements(commentary_text),
"key_squares": extract_key_squares(commentary_text),
"source": "human_expert",
"raw_commentary": commentary_text[:200] + "..." if len(commentary_text) > 200 else commentary_text
}
board.push(node.move)
current_move += 1
except Exception as e:
print(f" ⚠️ Commentary extraction error: {e}")
return None
def parse_commentary_to_description(commentary: str) -> str:
"""Convert raw PGN commentary to natural description"""
# Clean up PGN annotations
cleaned = re.sub(r'\[%[^\]]*\]', '', commentary) # Remove evaluation annotations
cleaned = re.sub(r'\$\d+', '', cleaned) # Remove NAG codes
cleaned = re.sub(r'\s+', ' ', cleaned).strip() # Clean whitespace
if len(cleaned) > 300:
cleaned = cleaned[:300] + "..."
return cleaned if cleaned else "Position with expert commentary"
def extract_strategic_themes(commentary: str) -> list:
"""Extract strategic themes from commentary text"""
themes = []
commentary_lower = commentary.lower()
# Look for common strategic keywords
strategic_keywords = {
'attack': ['attack', 'attacking', 'offensive'],
'defense': ['defense', 'defensive', 'defending'],
'control': ['control', 'domination', 'pressure'],
'development': ['development', 'developing', 'piece activity'],
'king safety': ['king safety', 'castling', 'king position'],
'pawn structure': ['pawn structure', 'pawn chain', 'pawn weakness'],
'space advantage': ['space', 'territory', 'expansion'],
'piece coordination': ['coordination', 'cooperation', 'harmony'],
'counterplay': ['counterplay', 'counter-attack', 'initiative']
}
for theme, keywords in strategic_keywords.items():
if any(keyword in commentary_lower for keyword in keywords):
themes.append(theme)
return themes[:5] # Limit to 5 themes
def extract_tactical_elements(commentary: str) -> list:
"""Extract tactical motifs from commentary"""
elements = []
commentary_lower = commentary.lower()
tactical_keywords = {
'pin': ['pin', 'pinned'],
'fork': ['fork', 'double attack'],
'skewer': ['skewer'],
'discovered attack': ['discovered', 'discovery'],
'sacrifice': ['sacrifice', 'sacrificial'],
'combination': ['combination', 'tactical sequence'],
'mate threat': ['mate', 'checkmate', 'mating'],
'blunder': ['blunder', 'mistake', 'error']
}
for element, keywords in tactical_keywords.items():
if any(keyword in commentary_lower for keyword in keywords):
elements.append(element)
return elements[:3] # Limit to 3 elements
def extract_key_squares(commentary: str) -> list:
"""Extract square references from commentary"""
# Look for chess square notation (e4, d5, etc.)
squares = re.findall(r'\b[a-h][1-8]\b', commentary)
return list(set(squares))[:5] # Unique squares, limit to 5
def load_pgn_games(pgn_file: str) -> dict:
"""Load and index PGN games for commentary lookup"""
games = {}
try:
with open(pgn_file, 'r', encoding='utf-8') as f:
while True:
game = chess.pgn.read_game(f)
if game is None:
break
# Create unique key for game
white = game.headers.get("White", "Unknown")
black = game.headers.get("Black", "Unknown")
date = game.headers.get("Date", "")
game_key = f"{white}_{black}_{date}"
games[game_key] = game
except Exception as e:
print(f" ⚠️ Error loading PGN games: {e}")
return games
def generate_quick_description(position: dict) -> str:
"""Generate engine-enhanced template-based description"""
try:
features = position.get("position_features", {})
context = position.get("game_context", {})
engine_analysis = position.get("stockfish_analysis", {})
description_parts = []
# Game phase
game_phase = features.get("game_phase", "unknown")
description_parts.append(f"{game_phase.title()} position")
# Engine evaluation
eval_score = engine_analysis.get("evaluation", 0)
eval_type = engine_analysis.get("evaluation_type", "cp")
if eval_type == "mate":
mate_in = engine_analysis.get("mate_in", "?")
if eval_score > 0:
description_parts.append(f"White mates in {mate_in}")
else:
description_parts.append(f"Black mates in {mate_in}")
elif eval_type == "cp":
if abs(eval_score) >= 3.0:
if eval_score > 0:
description_parts.append(f"White winning (+{eval_score:.1f})")
else:
description_parts.append(f"Black winning ({eval_score:.1f})")
elif abs(eval_score) >= 1.0:
if eval_score > 0:
description_parts.append(f"White advantage (+{eval_score:.1f})")
else:
description_parts.append(f"Black advantage ({eval_score:.1f})")
else:
description_parts.append(f"roughly equal ({eval_score:+.1f})")
else:
# Fallback to material balance
material = features.get("material", {})
balance = material.get("balance", 0)
if abs(balance) > 1.5:
if balance > 0:
description_parts.append(f"White ahead by {abs(balance):.1f} points")
else:
description_parts.append(f"Black ahead by {abs(balance):.1f} points")
else:
description_parts.append("material roughly equal")
# Best move from engine
best_move = engine_analysis.get("best_move_san")
if best_move:
description_parts.append(f"best: {best_move}")
# Board control
board_control = features.get("board_control", {})
open_files = board_control.get("open_files", [])
if open_files and len(open_files) <= 2: # Don't clutter if many open files
files_str = ",".join(open_files)
description_parts.append(
f"open {files_str} file{'s' if len(open_files) > 1 else ''}"
)
# Special situations
special = features.get("special", {})
if special.get("in_check"):
description_parts.append("king in check")
elif special.get("is_checkmate"):
description_parts.append("checkmate")
# King safety
king_safety = features.get("king_safety", {})
castling = king_safety.get("castling_status", {})
if castling.get("white_has_castled") and castling.get("black_has_castled"):
description_parts.append("both sides castled")
# Last move context
last_move = position.get("last_move", "")
if last_move:
description_parts.append(f"after {last_move}")
return ". ".join(description_parts).capitalize()
except Exception as e:
return (
f"Position from move {position.get('move_number', '?')} - analysis"
f" error: {str(e)}"
)
def main():
"""Main function with command line argument handling"""
# Default values
pgn_file = "mega-2025-small.pgn"
output_file = "chess_database.json"
max_positions = 5000
resume = True
# Handle command line arguments
if len(sys.argv) > 1:
pgn_file = sys.argv[1]
if len(sys.argv) > 2:
max_positions = int(sys.argv[2])
if len(sys.argv) > 3:
output_file = sys.argv[3]
if len(sys.argv) > 4:
resume = sys.argv[4].lower() != "false"
print(f"Configuration:")
print(f" PGN file: {pgn_file}")
print(f" Max positions: {max_positions}")
print(f" Output file: {output_file}")
print(f" Resume from checkpoints: {resume}")
print(f" LLM enhancement: Gemini 2.0 Flash-Lite (10 concurrent)")
# Run the database builder
asyncio.run(
build_chess_database(pgn_file, output_file, max_positions, resume)
)
if __name__ == "__main__":
main()