{
"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 13\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 14\u001b[0m count \u001b[39m+\u001b[39m\u001b[39m=\u001b[39m \u001b[39m1\u001b[39m\n\u001b[1;32m---> 15\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 16\u001b[0m pbar\u001b[39m.\u001b[39mupdate(\u001b[39m1\u001b[39m)\n\u001b[0;32m 17\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: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
}