Skip to content

Instantly share code, notes, and snippets.

@fletcher
Created March 19, 2010 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fletcher/337641 to your computer and use it in GitHub Desktop.
Save fletcher/337641 to your computer and use it in GitHub Desktop.
# Automate annotation of chess games
games := $(wildcard *.pgn)
annotated := $(patsubst %.pgn, %.pgn.can, $(games))
html := $(patsubst %.pgn, %.pgn.html, $(games))
tex := $(patsubst %.pgn, %.pgn.tex, $(games))
user := Fletcher
margin := 0.5
time := 10
moves := 3
all: $(annotated)
both: user = wb
both: $(annotated)
# For my online games, use my login
online: user = someloginname
online: $(annotated)
%.pgn.can: %.pgn.base.can %.pgn.base %.pgn.clean
cp $*.pgn.base.can $*.pgn.can
merge $*.pgn.can $*.pgn.base $*.pgn.clean
%.pgn.base.can: %.pgn.base
echo "annotate $*.pgn.base ${user} 1-9999 ${margin} ${time} ${moves}" | crafty
rm game.00*
rm log.00*
# rm position.bin
# rm position.lrn
# rm book.lrn
# Create a cleaned copy for crafty
%.pgn.clean: %.pgn
~/bin/pgnclean.pl $*.pgn > $*.pgn.clean
%.pgn.base: %.pgn.clean
~/bin/pgnstrip.pl $*.pgn > $*.pgn.base
html: $(html) bitmaps
tex: $(tex)
%.pgn.html: %.pgn bitmaps
echo "annotateh $*.pgn ${user} 1-9999 ${margin} ${time} ${moves}" | crafty
rm game.00*
rm log.00*
rm position.bin
rm position.lrn
rm book.lrn
bitmaps::
cp -r /Users/fletcher/Documents/Dropbox/Stuff/Chess/Chess-HTML/bitmaps .
%.pgn.tex: %.pgn
echo "annotatet $*.pgn ${user} 1-9999 ${margin} ${time} ${moves}" | crafty
rm game.00*
rm log.00*
rm position.bin
rm position.lrn
rm book.lrn
%.pgn.base: %.pgn.clean
pgnstrip.pl $*.pgn.clean > $*.pgn.base
%.pgn.clean: %.pgn
pgnclean.pl $*.pgn > $*.pgn.clean
#!/usr/bin/env perl
# Clean up for easier reading, goal is to match crafty's output
# Also, fix inconsistencies
my $move = qr{
(?:[RKNBQ]?
[a-h]?
[1-8]?
x?
[a-h]
[1-8]
(?:=[QNBR])?
[\+\#]?)
|O\-O|O\-O\-O|\.\.\.?
}mx;
my $result = qr{
0-1|
1-0|
1/2-1/2|
\*
}mx;
local $/;
$text = <>;
# Clean up 6... continuations
$text =~ s{
(\d+)\.\.\.
}{
"$1. ... ";
}egsxi;
# Red Hot Pawn adds headers that mess up crafty
$text =~ s{
# Strip ratings - leave ELO
\[(white|black)rating.*?\]\n
}{
"";
}egsxi;
$text =~ s{
\[(white|black)elo
}{
"[" . $1 . "Elo";
}egmxi;
# tChess puts ELO in wrong spot
$text =~ s{
\[(white|black)\s\"tChess\s\((\d+)\)\"\]
}{
"[$1 \"tChess\"]\n[$1Elo \"$2\"]";
}egmxi;
# Organize player names and ELO to match crafty
$text =~ s{
(\[white.*?\])\n
(.*?)
(\[whiteelo.*?\])\n
}{
"$1\n$3\n$2";
}egsxi;
$text =~ s{
(\[black.*?\])\n
(.*?)
(\[blackelo.*?\])\n
}{
"$1\n$3\n$2";
}egsxi;
# Move Result to end of header
$text =~ s{
(\[Result.*?\])\n
(\[.*\])\n
}{
"$2\n$1\n";
}egsxi;
# One move per line
$text =~ s{
\s+(\d+)\.\s*($move)\s+($move)
}{
$num = $1;
$move1 = $2;
$move2 = $3;
$move1 =~ s/^\.\.$/.../;
$move2 =~ s/^\.\.$/.../;
"\n" . sprintf("%3d",$num) . "." . sprintf("%8s",$move1) . sprintf("%8s",$move2)
}egsxi;
# Same for continued moves
$text =~ s{
\s+(\d+)\.\s*($move)\s+(\{.*?\}|$result)
}{
"\n" . sprintf("%3d",$1) . "." . sprintf("%8s",$2) . " " . $3
}egsxi;
# And for comments
$text =~ s{
\s+(\{.*?\})
}{
"\n" . " " . $1
}egsxi;
# And variations
$text =~ s{
\s+(\(.*?\))
}{
my $line = $1;
$line =~ s/\s+/ /g;
"\n" . " " . $line
}egsxi;
# Skip line after comments/variations
$text =~ s{
(\)|\})[ \t]*\n([ ]*(\d+)\.)
}{
"$1\n\n$2"
}egsxi;
# Result to its own line
$text =~ s{
\s+($result)
}{
"\n $1\n";
}egsxi;
# Fix ".." to "..." for continued moves
$text =~ s{
(\s)\.\.(\s)
}{
"$1...$2"
}egsxi;
# Strip trailing lines
$text =~ s/\n+\Z/\n/;
print $text;
#!/usr/bin/env perl
# Strip comments and annotations
# Also, fix inconsistencies
my $move = qr{
(?:[RKNBQ]?
[a-h]?
[1-8]?
x?
[a-h]
[1-8]
(?:=[QNBR])?
[\+\#]?)
|O\-O|O\-O\-O|\.\.\.?
}mx;
my $result = qr{
0-1|
1-0|
1/2-1/2|
\*
}mx;
local $/;
$text = <>;
# Strip some header tags
$text =~ s{
\[(ECO|endDate|GameId).*?\]\n
}{
}egsxi;
# Strip comments\annotations
$text =~ s{
\{.*?\}
}{
}egsxi;
$text =~ s{
\(.*?\)
}{}egsxi;
# Fix split moves
#$text =~ s{
# (\d+)(\.\s*$move)\s*(\d+)\.\s*\.\.\.?
#}{
# "$1$2";
#}egxsi;
# Or force all moves to be split... ?
$text =~ s{
\n([ \t]*\d+)(\.\s*$move)(\s+$move)
}{
"\n$1$2\n$1. ...$3";
}egsxi;
# And then strip lines just with "..." as it's a duplicate
$text =~ s{
\n[ \t]*(\d+)\.[ ]+\.\.\.[ ]*\n
}{
"\n";
}egsxi;
# Strip empty lines
$text =~ s/\n\s*\n/\n/g;
print $text;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment