decompress .pgn.zst dataset
Browse files- ChessBot-Dataset.py +24 -16
ChessBot-Dataset.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import logging
|
| 2 |
from datasets import (
|
| 3 |
GeneratorBasedBuilder,
|
|
@@ -10,6 +11,9 @@ from datasets import (
|
|
| 10 |
Array2D,
|
| 11 |
)
|
| 12 |
from adversarial_gym.chess_env import ChessEnv
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
class ChessPGNDataset(GeneratorBasedBuilder):
|
| 15 |
VERSION = "0.0.1"
|
|
@@ -55,19 +59,23 @@ class ChessPGNDataset(GeneratorBasedBuilder):
|
|
| 55 |
uid = 0
|
| 56 |
|
| 57 |
for path in shards:
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io
|
| 2 |
import logging
|
| 3 |
from datasets import (
|
| 4 |
GeneratorBasedBuilder,
|
|
|
|
| 11 |
Array2D,
|
| 12 |
)
|
| 13 |
from adversarial_gym.chess_env import ChessEnv
|
| 14 |
+
import zstandard as zstd
|
| 15 |
+
|
| 16 |
+
|
| 17 |
|
| 18 |
class ChessPGNDataset(GeneratorBasedBuilder):
|
| 19 |
VERSION = "0.0.1"
|
|
|
|
| 59 |
uid = 0
|
| 60 |
|
| 61 |
for path in shards:
|
| 62 |
+
decompressor = zstd.ZstdDecompressor()
|
| 63 |
+
with open(path, "rb") as compressed:
|
| 64 |
+
with decompressor.stream_reader(compressed) as reader:
|
| 65 |
+
text_stream = io.TextIOWrapper(reader, encoding='utf-8')
|
| 66 |
+
while (game := chess.pgn.read_game(text_stream)) is not None:
|
| 67 |
+
# while (game := chess.pgn.read_game(f)) is not None:
|
| 68 |
+
board = game.board()
|
| 69 |
+
base = {"1-0":1,"0-1":-1}.get(game.headers["Result"], 0)
|
| 70 |
+
for move in game.mainline_moves():
|
| 71 |
+
state = ChessEnv.get_piece_configuration(board)
|
| 72 |
+
state = state if board.turn else -state
|
| 73 |
+
action = ChessEnv.move_to_action(move)
|
| 74 |
+
result = base * (-1 if board.turn == 0 else 1)
|
| 75 |
+
yield uid, {
|
| 76 |
+
"state": state.astype("int8"),
|
| 77 |
+
"action": int(action),
|
| 78 |
+
"result": int(result),
|
| 79 |
+
}
|
| 80 |
+
uid += 1
|
| 81 |
+
board.push(move)
|