Download openings.py from AngeloWolff/snail-chess-data: direct link, hf CLI and curl.
- Browser
- Download file 2.17 kB
-
https://huggingface.co/datasets/AngeloWolff/snail-chess-data/resolve/main/openings.py
- Command line
-
hf download hf://datasets/AngeloWolff/snail-chess-data/openings.py
-
curl -L -o openings.py https://huggingface.co/datasets/AngeloWolff/snail-chess-data/resolve/main/openings.py
2.17 kB
| import argparse | |
| import pathlib | |
| import random | |
| import chess | |
| parser = argparse.ArgumentParser(description="Opening FENs from Snail's own books: weighted book walk plus random plies") | |
| parser.add_argument("out") | |
| parser.add_argument("--count", type=int, default=200000) | |
| parser.add_argument("--books", nargs="+", default=[str(pathlib.Path(__file__).resolve().parent / "books" / b) for b in ("master.txt", "neutral.txt", "gambit.txt", "passive.txt")]) | |
| parser.add_argument("--min-plies", type=int, default=4) | |
| parser.add_argument("--max-plies", type=int, default=12) | |
| parser.add_argument("--random-plies", type=int, default=1) | |
| parser.add_argument("--seed", type=int, default=1) | |
| args = parser.parse_args() | |
| books = [] | |
| for path in args.books: | |
| lines = {} | |
| for line in pathlib.Path(path).read_text().splitlines(): | |
| if " : " not in line and not line.startswith(" :"): | |
| continue | |
| key, _, rest = line.partition(":") | |
| words = rest.split() | |
| lines[key.strip()] = [(words[i], int(words[i + 1])) for i in range(0, len(words) - 1, 2)] | |
| books.append(lines) | |
| rng = random.Random(args.seed) | |
| seen = set() | |
| out = [] | |
| attempts = 0 | |
| while len(out) < args.count and attempts < args.count * 20: | |
| attempts += 1 | |
| book = rng.choice(books) | |
| board = chess.Board() | |
| played = [] | |
| target = rng.randint(args.min_plies, args.max_plies) | |
| while len(played) < target: | |
| choices = book.get(" ".join(played)) | |
| if not choices: | |
| break | |
| moves, weights = zip(*choices) | |
| move = rng.choices(moves, weights=weights)[0] | |
| if chess.Move.from_uci(move) not in board.legal_moves: | |
| break | |
| board.push_uci(move) | |
| played.append(move) | |
| for _ in range(args.random_plies): | |
| legal = list(board.legal_moves) | |
| if not legal: | |
| break | |
| board.push(rng.choice(legal)) | |
| if board.is_game_over(): | |
| continue | |
| fen = board.fen() | |
| key = " ".join(fen.split()[:4]) | |
| if key in seen: | |
| continue | |
| seen.add(key) | |
| out.append(fen) | |
| rng.shuffle(out) | |
| pathlib.Path(args.out).write_text("\n".join(out) + "\n") | |
| print(f"{len(out)} distinct openings ({attempts} walks)") | |