Giter VIP home page Giter VIP logo

pigeon's Introduction

pigeon

Parse Portable Game Notation (‘PGN’) Files

Description

‘Portable Game Notation’ (‘PGN’) is a plain text computer-processible format for recording chess games (both the moves and related data), supported by many chess programs. It was was devised around 1993, by Steven J. Edwards, and was first popularized via the ‘Usenet’ newsgroup ‘rec.games.chess’. ‘PGN’ is structured “for easy reading and writing by human users and for easy parsing and generation by computer programs.” The chess moves themselves are given in algebraic chess notation. Tools are provided to parse ‘PGN’ files into a data frame.

What’s in the tin?

The following functions are implemented:

  • pgn2sql: Convert a PGN file to a SQLite DB
  • pgn_count: Count number of games in a PGN file
  • read_pgn: Read in a PGN file

The following built-in data sets are included:

  • system.file("extdata", "r7.pgn", package="pigeon"): 2017 FIDE World Cup extract

Installation

devtools::install_github("hrbrmstr/pigeon")

Usage

library(pigeon)
library(tidyverse)

# current verison
packageVersion("pigeon")
## [1] '0.2.0'

Built-in example

fide <- read_pgn(system.file("extdata", "r7.pgn", package="pigeon"))

fide
## # A tibble: 2 x 13
##            Event    Site       Date Round               White               Black  Result BlackElo WhiteElo
##            <chr>   <chr>      <chr> <chr>               <chr>               <chr>   <chr>    <chr>    <chr>
## 1 World Cup 2017 Tbilisi 2017.09.23  44.1 Aronian Levon (ARM)    Ding Liren (CHN) 1/2-1/2     2777     2799
## 2 World Cup 2017 Tbilisi 2017.09.24  45.1    Ding Liren (CHN) Aronian Levon (ARM) 1/2-1/2     2799     2777
## # ... with 4 more variables: LiveChessVersion <chr>, ECO <chr>, moves_raw <list>, Moves <list>
glimpse(fide)
## Observations: 2
## Variables: 13
## $ Event            <chr> "World Cup 2017", "World Cup 2017"
## $ Site             <chr> "Tbilisi", "Tbilisi"
## $ Date             <chr> "2017.09.23", "2017.09.24"
## $ Round            <chr> "44.1", "45.1"
## $ White            <chr> "Aronian Levon (ARM)", "Ding Liren (CHN)"
## $ Black            <chr> "Ding Liren (CHN)", "Aronian Levon (ARM)"
## $ Result           <chr> "1/2-1/2", "1/2-1/2"
## $ BlackElo         <chr> "2777", "2799"
## $ WhiteElo         <chr> "2799", "2777"
## $ LiveChessVersion <chr> "1.4.8", "1.4.8"
## $ ECO              <chr> "A18", "E06"
## $ moves_raw        <list> ["1. c4 {[%clk 1:30:45]} Nf6 {[%clk 1:30:26]} 2. Nc3 {[%clk 1:31:06]} e6 {[%clk 1:30:50]}...
## $ Moves            <list> [<"c4", "Nf6", "Nc3", "e6", "e4", "d5", "cxd5", "exd5", "e5", "Ne4", "Nf3", "Bf5", "Be2",...

Bigger example

tf <- tempfile(fileext = ".zip")
td <- tempdir()
download.file("https://www.pgnmentor.com/players/Adams.zip",  tf)
fil <- unzip(tf, exdir = td)

adams <- read_pgn(fil)

adams
## # A tibble: 2,982 x 12
##             Event      Site       Date Round              White              Black  Result WhiteElo BlackElo   ECO
##             <chr>     <chr>      <chr> <chr>              <chr>              <chr>   <chr>    <chr>    <chr> <chr>
##  1 Lloyds Bank op    London 1984.??.??     1     Adams, Michael    Sedgwick, David     1-0                     C05
##  2 Lloyds Bank op    London 1984.??.??     3     Adams, Michael  Dickenson, Neil F     1-0              2230   C07
##  3 Lloyds Bank op    London 1984.??.??     4       Hebden, Mark     Adams, Michael     1-0     2480            B10
##  4 Lloyds Bank op    London 1984.??.??     5    Pasman, Michael     Adams, Michael     0-1     2310            D42
##  5 Lloyds Bank op    London 1984.??.??     6     Adams, Michael   Levitt, Jonathan 1/2-1/2              2370   B99
##  6 Lloyds Bank op    London 1984.??.??     9     Adams, Michael Saeed, Saeed Ahmed     1-0              2430   B56
##  7         BCF-ch Edinburgh 1985.??.??     1     Adams, Michael   Singh, Sukh Dave 1/2-1/2     2360     2080   B70
##  8         BCF-ch Edinburgh 1985.??.??     2 Abayasekera, Roger     Adams, Michael     1-0     2200     2360   B13
##  9         BCF-ch Edinburgh 1985.??.??     3     Adams, Michael    Jackson, Sheila 1/2-1/2     2360     2225   C85
## 10         BCF-ch Edinburgh 1985.??.??     4     Muir, Andrew J     Adams, Michael 1/2-1/2     2285     2360   E45
## # ... with 2,972 more rows, and 2 more variables: moves_raw <list>, Moves <list>
glimpse(adams)
## Observations: 2,982
## Variables: 12
## $ Event     <chr> "Lloyds Bank op", "Lloyds Bank op", "Lloyds Bank op", "Lloyds Bank op", "Lloyds Bank op", "Lloyds...
## $ Site      <chr> "London", "London", "London", "London", "London", "London", "Edinburgh", "Edinburgh", "Edinburgh"...
## $ Date      <chr> "1984.??.??", "1984.??.??", "1984.??.??", "1984.??.??", "1984.??.??", "1984.??.??", "1985.??.??",...
## $ Round     <chr> "1", "3", "4", "5", "6", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "?", "1", ...
## $ White     <chr> "Adams, Michael", "Adams, Michael", "Hebden, Mark", "Pasman, Michael", "Adams, Michael", "Adams, ...
## $ Black     <chr> "Sedgwick, David", "Dickenson, Neil F", "Adams, Michael", "Adams, Michael", "Levitt, Jonathan", "...
## $ Result    <chr> "1-0", "1-0", "1-0", "0-1", "1/2-1/2", "1-0", "1/2-1/2", "1-0", "1/2-1/2", "1/2-1/2", "1-0", "1/2...
## $ WhiteElo  <chr> "", "", "2480", "2310", "", "", "2360", "2200", "2360", "2285", "2360", "2250", "2360", "2225", "...
## $ BlackElo  <chr> "", "2230", "", "", "2370", "2430", "2080", "2360", "2225", "2360", "2245", "2360", "2260", "2360...
## $ ECO       <chr> "C05", "C07", "B10", "D42", "B99", "B56", "B70", "B13", "C85", "E45", "C84", "B10", "C85", "A22",...
## $ moves_raw <list> ["1.e4 e6 2.d4 d5 3.Nd2 Nf6 4.e5 Nfd7 5.f4 c5 6.c3 Nc6 7.Ndf3 cxd4 8.cxd4 f6 9.Bd3 Bb4+ 10.Bd2 Q...
## $ Moves     <list> [<"e4", "e6", "d4", "d5", "Nd2", "Nf6", "e5", "Nfd7", "f4", "c5", "c3", "Nc6", "Ndf3", "cxd4", "...
unlink(tf)
unlink(fil)

pigeon's People

Contributors

hrbrmstr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

mpascariu

pigeon's Issues

I'm getting errors while reading in certain databases

Using nearly any file in the most recent KingBase 2017 PGN Format (http://www.kingbase-chess.net/) throws an error due to invalid escape characters (I believe).

For example, trying to read in KingBase2017-A00-A39.pgn throws the following error after a while:

Error: lexical error: invalid character inside string. : "Bxh6" }] }, { "Event" : "Mnster Open", "Site" : "Mnste (right here) ------^

Trying to read in the millionbases.pgn also throws an error (although I don't think it's related to this framework)

Error in paste0(readLines(json, warn = FALSE), collapse = "\n") : result would exceed 2^31-1 bytes

Feature Requests: Custom Tags, Filter by Elo, Extract Clock and Time data,

I'm now using this library thanks to the MySQL database functionality. Having a hard time with extracting custom tags (e.g. BlackElo and WhiteElo).

LiChess also embeds clk and eval tags within moves (as seen in your readme file) which would be nice to put into a dataframe column. Also It'd would be nice to skip import of games where Black or White Elo doesn't exceed some value (e.g. 2200).

TODO

ASAN/UBSAN/Valgrind

There are likely a few memory leaks given how the standalone C program was hacked into a library.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.