mauricett commited on
Commit
2855028
1 Parent(s): 5416689

Upload lichess_sf_hf.py

Browse files
Files changed (1) hide show
  1. lichess_sf_hf.py +106 -0
lichess_sf_hf.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #%%
2
+ import datasets
3
+ import zstandard as zstd
4
+ import io
5
+
6
+ #%%
7
+ class LichessConfig(datasets.BuilderConfig):
8
+ def __init__(self, features, **kwargs):
9
+ super(LichessConfig, self).__init__(**kwargs)
10
+ self.features = features
11
+
12
+
13
+ class Lichess(datasets.GeneratorBasedBuilder):
14
+ BUILDER_CONFIG_CLASS = LichessConfig
15
+ BUILDER_CONFIGS = [LichessConfig(name="pgn",
16
+ features=["WhiteElo",
17
+ "BlackElo",
18
+ "pgn"]),
19
+
20
+ LichessConfig(name="fen",
21
+ features=["WhiteElo",
22
+ "BlackElo",
23
+ "fens",
24
+ "moves",
25
+ "scores"]),]
26
+
27
+ def _info(self):
28
+ features_dict = {feature: datasets.Value("uint16") for feature in self.config.features}
29
+
30
+ if self.config.name == "pgn":
31
+ features_dict["pgn"] = datasets.Value("string")
32
+
33
+
34
+ if self.config.name == "fen":
35
+ features_dict["fens"] = datasets.Value("string")
36
+ features_dict["moves"] = datasets.Value("string")
37
+ features_dict["scores"] = datasets.Value("string")
38
+
39
+ info = datasets.DatasetInfo(datasets.Features(features_dict))
40
+ return info
41
+
42
+ def _get_filepaths(self):
43
+ #months = ["01", "02", "03", "04", "05", "06",
44
+ # "07", "08", "09", "10", "11", "12"]
45
+ months = ["04"]
46
+ shards = ["0", "1", "2", "3"]
47
+ filepaths = ["lichess/" + self.config.name + "/2023/" + m for m in months]
48
+
49
+ if self.config.name == "pgn":
50
+ paths = []
51
+ for shard in shards:
52
+ paths.extend([filepath + "/" + shard + ".zst" for filepath in filepaths])
53
+
54
+ if self.config.name == "fen":
55
+ paths = []
56
+ for shard in shards:
57
+ paths.extend([filepath + "/" + shard + "_fen.zst" for filepath in filepaths])
58
+
59
+ return paths
60
+
61
+ def _split_generators(self, dl_manager: datasets.DownloadManager):
62
+ filepaths = self._get_filepaths()
63
+ downloaded_files = dl_manager.download(filepaths)
64
+ generator = datasets.SplitGenerator(name=datasets.Split.TRAIN,
65
+ gen_kwargs={'filepaths': downloaded_files})
66
+ return [generator]
67
+
68
+ def _generate_examples(self, filepaths):
69
+ dctx = zstd.ZstdDecompressor()
70
+ for filepath in filepaths:
71
+ n = 0
72
+ with open(filepath, "rb") as file:
73
+ with dctx.stream_reader(file) as sr:
74
+ pgn = io.TextIOWrapper(sr)
75
+
76
+ if self.config.name == "pgn":
77
+ while True:
78
+ white_elo, black_elo = pgn.readline().split(" ")
79
+ game_pgn = pgn.readline()
80
+ next_line = pgn.readline()
81
+
82
+ if game_pgn:
83
+ _id = n
84
+ n += 1
85
+ yield _id, {"WhiteElo": int(white_elo),
86
+ "BlackElo": int(black_elo),
87
+ "pgn": game_pgn}
88
+ else:
89
+ break
90
+
91
+ elif self.config.name == "fen":
92
+ while True:
93
+ white_elo, black_elo = pgn.readline().split(" ")
94
+ game = pgn.readline()
95
+ fens, moves, scores = game.split(";")
96
+
97
+ if game:
98
+ _id = n
99
+ n += 1
100
+ yield _id, {"WhiteElo": white_elo,
101
+ "BlackElo": black_elo,
102
+ "fens": fens,
103
+ "moves": moves,
104
+ "scores": scores}
105
+ else:
106
+ break