Skip to content

Commit

Permalink
Fixed issue with 'BlackElo' not being in the PGN file. Added further …
Browse files Browse the repository at this point in the history
…tests to check Basic formats are handled by the library.
  • Loading branch information
zq99 committed Jun 24, 2022
1 parent 785e3ab commit 141cd00
Show file tree
Hide file tree
Showing 16 changed files with 19,379 additions and 19,069 deletions.
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pgn2data library
# pgn2data

A library that converts chess pgn files into CSV tabulated data sets.
This library converts chess pgn files into CSV tabulated data sets.

A pgn file can contain one or multiple chess games. The library parses the pgn file and creates two csv files:

Expand All @@ -11,15 +11,15 @@ A pgn file can contain one or multiple chess games. The library parses the pgn f
The two files can be mapped together using a GUID which the process inserts into both files.



## Installation

Run the following command on the python terminal:
The library requires Python 3.7 or later.

To install, type the following command on the python terminal:

pip install pgn2data




## Implementation

Here is a basic example of how to convert a PGN file:
Expand All @@ -38,13 +38,20 @@ To group multiple files into the same output file you can do the following:
result = pgn_data.export()
result.print_summary()

This processes the two pgn files in the specified list and exports them to file called "output.csv".
The above code will process the two pgn files (file1 and file2) and export them to a file called "output.csv".


## Examples

The folder 'samples' in this repository, has some examples of the output from the library.

You can also go [here](https://www.kaggle.com/datasets/zq1200/magnus-carlsen-lichess-games-dataset) to see a Kaggle project that converted all of Magnus Carlsen's online Bullet games
into CSV format.


## Columns

This is a list of the columns in each dataset:
This is a full list of the columns in each output file:

### Games File

Expand Down
23 changes: 15 additions & 8 deletions converter/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,21 @@ def __get_game_row_data(self, game, game_id, order, file_name):
"""

winner = self.__get_winner(game)
loser = game.headers["White"] if winner == game.headers["Black"] else (
game.headers["Black"] if winner == game.headers["White"] else winner)
winner_elo = game.headers["WhiteElo"] if winner == game.headers["White"] else (
game.headers["BlackElo"] if winner == game.headers["Black"] else "")
loser_elo = game.headers["WhiteElo"] if winner == game.headers["Black"] else (
game.headers["BlackElo"] if winner == game.headers["White"] else "")
winner_loser_elo_diff = 0 if not (str(winner_elo).isnumeric() and str(loser_elo).isnumeric()) else int(
winner_elo) - int(loser_elo)
loser = ""
winner_elo = ""
loser_elo = ""
winner_loser_elo_diff = ""

if "White" in game.headers and "Black" in game.headers:
loser = game.headers["White"] if winner == game.headers["Black"] else (
game.headers["Black"] if winner == game.headers["White"] else winner)
if "WhiteElo" in game.headers and "BlackElo" in game.headers:
winner_elo = game.headers["WhiteElo"] if winner == game.headers["White"] else (
game.headers["BlackElo"] if winner == game.headers["Black"] else "")
loser_elo = game.headers["WhiteElo"] if winner == game.headers["Black"] else (
game.headers["BlackElo"] if winner == game.headers["White"] else "")
winner_loser_elo_diff = 0 if not (str(winner_elo).isnumeric() and str(loser_elo).isnumeric()) else int(
winner_elo) - int(loser_elo)

return [game_id, order,
game.headers["Event"] if "Event" in game.headers else "",
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name='pgn2data',
version='0.0.4',
version='0.0.5',
packages=['converter', 'common', 'testing'],
url='',
classifiers=classifiers,
Expand Down
2 changes: 2 additions & 0 deletions testing/exports/basic_format_game_info.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
game_id,game_order,event,site,date_played,round,white,black,result,white_elo,white_rating_diff,black_elo,black_rating_diff,white_title,black_title,winner,winner_elo,loser,loser_elo,winner_loser_elo_diff,eco,termination,time_control,utc_date,utc_time,variant,ply_count,date_created,file_name
a5ab6da8-88d4-4552-acf3-45d7388a845a,1,F/S Return Match,"Belgrade, Serbia JUG",1992.11.04,29,"Fischer, Robert J.","Spassky, Boris V.",1/2-1/2,,,,,,,draw,,draw,,,,,,,,,,2022-06-24T20:39:21+0000,basic_format.pgn
86 changes: 86 additions & 0 deletions testing/exports/basic_format_moves.csv

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions testing/exports/github_issue_02_game_info.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
game_id,game_order,event,site,date_played,round,white,black,result,white_elo,white_rating_diff,black_elo,black_rating_diff,white_title,black_title,winner,winner_elo,loser,loser_elo,winner_loser_elo_diff,eco,termination,time_control,utc_date,utc_time,variant,ply_count,date_created,file_name
652ef17f-4eb7-4133-89f8-4a83bc6cc933,1,?,?,2022.6.16,?,Dr. Wolf - Intermediate,James,0-1,,,,,,,James,,Dr. Wolf - Intermediate,,,,,,,,,,2022-06-24T20:39:29+0000,github_issue_02.pgn
129 changes: 129 additions & 0 deletions testing/exports/github_issue_02_moves.csv

Large diffs are not rendered by default.

0 comments on commit 141cd00

Please sign in to comment.