File size: 9,956 Bytes
81aa0bd |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import chess\n",
"import chess.pgn\n",
"import os\n",
"from tqdm import tqdm\n",
"import json"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
" 98%|ββββββββββ| 172350/175000 [38:39<00:35, 74.29it/s] \n"
]
},
{
"ename": "UnicodeDecodeError",
"evalue": "'utf-8' codec can't decode byte 0xfc in position 3540: invalid start byte",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mUnicodeDecodeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32ms:\\Documents\\chess_engine\\clean_data.ipynb Cell 2\u001b[0m line \u001b[0;36m1\n\u001b[0;32m <a href='vscode-notebook-cell:/s%3A/Documents/chess_engine/clean_data.ipynb#W1sZmlsZQ%3D%3D?line=12'>13</a>\u001b[0m games\u001b[39m.\u001b[39mappend(game\u001b[39m.\u001b[39maccept(exporter)\u001b[39m.\u001b[39mreplace(\u001b[39m\"\u001b[39m\u001b[39m\\n\u001b[39;00m\u001b[39m\"\u001b[39m, \u001b[39m\"\u001b[39m\u001b[39m \u001b[39m\u001b[39m\"\u001b[39m) \u001b[39m+\u001b[39m \u001b[39m'\u001b[39m\u001b[39m\\n\u001b[39;00m\u001b[39m'\u001b[39m)\n\u001b[0;32m <a href='vscode-notebook-cell:/s%3A/Documents/chess_engine/clean_data.ipynb#W1sZmlsZQ%3D%3D?line=13'>14</a>\u001b[0m count \u001b[39m+\u001b[39m\u001b[39m=\u001b[39m \u001b[39m1\u001b[39m\n\u001b[1;32m---> <a href='vscode-notebook-cell:/s%3A/Documents/chess_engine/clean_data.ipynb#W1sZmlsZQ%3D%3D?line=14'>15</a>\u001b[0m game \u001b[39m=\u001b[39m chess\u001b[39m.\u001b[39;49mpgn\u001b[39m.\u001b[39;49mread_game(game_file)\n\u001b[0;32m <a href='vscode-notebook-cell:/s%3A/Documents/chess_engine/clean_data.ipynb#W1sZmlsZQ%3D%3D?line=15'>16</a>\u001b[0m pbar\u001b[39m.\u001b[39mupdate(\u001b[39m1\u001b[39m)\n\u001b[0;32m <a href='vscode-notebook-cell:/s%3A/Documents/chess_engine/clean_data.ipynb#W1sZmlsZQ%3D%3D?line=16'>17</a>\u001b[0m \u001b[39mwith\u001b[39;00m \u001b[39mopen\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mchess_game_data.txt\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39m'\u001b[39m\u001b[39ma\u001b[39m\u001b[39m'\u001b[39m) \u001b[39mas\u001b[39;00m data_file:\n",
"File \u001b[1;32ms:\\packages\\envs\\chess\\Lib\\site-packages\\chess\\pgn.py:1673\u001b[0m, in \u001b[0;36mread_game\u001b[1;34m(handle, Visitor)\u001b[0m\n\u001b[0;32m 1671\u001b[0m \u001b[39mwhile\u001b[39;00m line \u001b[39mand\u001b[39;00m \u001b[39m\"\u001b[39m\u001b[39m}\u001b[39m\u001b[39m\"\u001b[39m \u001b[39mnot\u001b[39;00m \u001b[39min\u001b[39;00m line:\n\u001b[0;32m 1672\u001b[0m comment_lines\u001b[39m.\u001b[39mappend(line)\n\u001b[1;32m-> 1673\u001b[0m line \u001b[39m=\u001b[39m handle\u001b[39m.\u001b[39mreadline()\n\u001b[0;32m 1675\u001b[0m \u001b[39mif\u001b[39;00m line:\n\u001b[0;32m 1676\u001b[0m close_index \u001b[39m=\u001b[39m line\u001b[39m.\u001b[39mfind(\u001b[39m\"\u001b[39m\u001b[39m}\u001b[39m\u001b[39m\"\u001b[39m)\n",
"File \u001b[1;32m<frozen codecs>:322\u001b[0m, in \u001b[0;36mdecode\u001b[1;34m(self, input, final)\u001b[0m\n",
"\u001b[1;31mUnicodeDecodeError\u001b[0m: 'utf-8' codec can't decode byte 0xfc in position 3540: invalid start byte"
]
}
],
"source": [
"count = 0\n",
"last = 0\n",
"with tqdm(total=175000) as pbar:\n",
" for root, d_name, files in os.walk(\"S:\\Documents\\ccc-data\\ccc-data\"):\n",
" for file in files:\n",
" if os.path.splitext(file)[1] == '.pgn':\n",
" pgn_path = os.path.join(root, file)\n",
" with open(pgn_path) as game_file:\n",
" games = []\n",
" game = chess.pgn.read_game(game_file)\n",
" while game:\n",
" exporter = chess.pgn.StringExporter(headers=False, variations=False, comments=False)\n",
" games.append(game.accept(exporter).replace(\"\\n\", \" \") + '\\n')\n",
" count += 1\n",
" game = chess.pgn.read_game(game_file)\n",
" pbar.update(1)\n",
" with open(\"chess_game_data.txt\", 'a') as data_file:\n",
" data_file.writelines(games)\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
" 0%| | 28/1000000 [00:00<2:01:47, 136.85it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"lichess_elite_2021-12.pgn\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 49%|βββββ | 490442/1000000 [51:38<2:35:36, 54.58it/s] "
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"lichess_elite_2022-01.pgn\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 97%|ββββββββββ| 970547/1000000 [1:39:46<09:18, 52.71it/s] "
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"lichess_elite_2022-02.pgn\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"1345189it [2:17:35, 63.03it/s] "
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"lichess_elite_2022-03.pgn\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"1711576it [2:54:32, 51.88it/s] "
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"lichess_elite_2022-04.pgn\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"2050605it [3:28:36, 60.10it/s] "
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"lichess_elite_2022-05.pgn\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"2406133it [4:04:30, 54.89it/s] "
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"lichess_elite_2022-06.pgn\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"2741369it [4:38:51, 53.44it/s] "
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"lichess_elite_2022-08.pgn\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"3087892it [5:15:18, 50.95it/s] "
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"lichess_elite_2022-09.pgn\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"3434038it [5:53:10, 51.47it/s] "
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"lichess_elite_2022-10.pgn\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"3761579it [6:28:19, 67.38it/s] "
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"lichess_elite_2022-11.pgn\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"4064728it [7:00:58, 61.31it/s] "
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"lichess_elite_2022-12.pgn\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"4343360it [7:29:28, 161.05it/s]\n"
]
}
],
"source": [
"count = 0\n",
"last = 0\n",
"with tqdm(total=1000000) as pbar:\n",
" for root, d_name, files in os.walk(\"S:\\Documents\\Lichess Elite Database\\Latest\"):\n",
" for file in files:\n",
" print(file)\n",
" if os.path.splitext(file)[1] == '.pgn':\n",
" pgn_path = os.path.join(root, file)\n",
" with open(pgn_path) as game_file:\n",
" games = []\n",
" try:\n",
" game = chess.pgn.read_game(game_file)\n",
" while game:\n",
" exporter = chess.pgn.StringExporter(headers=False, variations=False, comments=False)\n",
" games.append(game.accept(exporter).replace(\"\\n\", \" \") + '\\n')\n",
" count += 1\n",
" game = chess.pgn.read_game(game_file)\n",
" pbar.update(1)\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" with open(\"lichess_game_data.txt\", 'a') as data_file:\n",
" data_file.writelines(games)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "chess",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
|