From e6cab1e6d437e7ca15b32795c14a49baf08df5dc Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Thu, 11 Dec 2014 01:31:18 +0100 Subject: [PATCH] Use the new opening data from pgn-extract. --- ECO.pm | 82 -------------------------------------------- binlookup.cpp | 2 +- count.h | 2 +- eco-list.pl | 8 ----- www/opening-stats.pl | 21 ++++++++++-- 5 files changed, 20 insertions(+), 95 deletions(-) delete mode 100755 ECO.pm delete mode 100644 eco-list.pl diff --git a/ECO.pm b/ECO.pm deleted file mode 100755 index c1a3833..0000000 --- a/ECO.pm +++ /dev/null @@ -1,82 +0,0 @@ -#! /usr/bin/perl -# -# Get eco.pgn from ftp://ftp.cs.kent.ac.uk/pub/djb/pgn-extract/eco.pgn, -# or any other opening database you might want to use as a base. -# -use strict; -use warnings; -use Chess::PGN::Parse; - -require 'Position.pm'; - -package ECO; - -our %fen_to_opening = (); -our @openings = (); - -sub init { - { - my $pos = Position->start_pos("white", "black"); - my $key = _key_for_pos($pos); - push @openings, { eco => 'A00', name => 'Start position' }; - $fen_to_opening{$key} = $#openings; - } - - my $pgn = Chess::PGN::Parse->new("eco.pgn") - or die "can't open eco.pgn\n"; - while ($pgn->read_game()) { - my $tags = $pgn->tags(); - $pgn->quick_parse_game; - my $pos = Position->start_pos("white", "black"); - my $moves = $pgn->moves // []; - my $eco = $pgn->eco; - next if (!defined($eco)); - my $name = $tags->{'Opening'}; - if (exists($tags->{'Variation'}) && $tags->{'Variation'} ne '') { - $name .= ": " . $tags->{'Variation'}; - } - for (my $i = 0; $i < scalar @$moves; ++$i) { - my ($from_row, $from_col, $to_row, $to_col, $promo) = $pos->parse_pretty_move($moves->[$i]); - $pos = $pos->make_move($from_row, $from_col, $to_row, $to_col, $promo, $moves->[$i]); - } - my $key = _key_for_pos($pos); - push @openings, { eco => $pgn->eco(), name => $name }; - $fen_to_opening{$key} = $#openings; - } -} - -sub persist { - my $filename = shift; - open my $fh, ">", $filename - or die "openings.txt: $!"; - for my $opening (@openings) { - print $fh $opening->{'eco'}, " ", $opening->{'name'}, "\n"; - } - close $fh; -} - -sub unpersist { - my $filename = shift; - open my $fh, "<", $filename - or die "openings.txt: $!"; - while (<$fh>) { - chomp; - push @openings, $_; - } - close $fh; -} - -sub get_opening_num { # May return undef. - my $pos = shift; - return $fen_to_opening{_key_for_pos($pos)}; -} - -sub _key_for_pos { - my $pos = shift; - my $key = $pos->fen; - # Remove the move clocks. - $key =~ s/ \d+ \d+$//; - return $key; -} - -1; diff --git a/binlookup.cpp b/binlookup.cpp index 5b5fd40..488a8e3 100644 --- a/binlookup.cpp +++ b/binlookup.cpp @@ -36,7 +36,7 @@ int main(int argc, char **argv) while (mtbl_iter_next(it, &key, &len_key, &val, &len_val)) { string move((char *)(key + prefix_len), len_key - prefix_len); const Count* c = (Count *)val; - printf("%s %d %d %d %d %f %f %d\n", move.c_str(), + printf("%s %d %d %d %u %f %f %d\n", move.c_str(), c->white, c->draw, c->black, c->opening_num, float(c->sum_white_elo) / c->num_elo, float(c->sum_black_elo) / c->num_elo, diff --git a/count.h b/count.h index 3024b89..8d843c7 100644 --- a/count.h +++ b/count.h @@ -3,7 +3,7 @@ struct Count { int white = 0; int draw = 0; int black = 0; - int opening_num = -1; + unsigned int opening_num = 0; unsigned long long sum_white_elo = 0; unsigned long long sum_black_elo = 0; int num_elo = 0; diff --git a/eco-list.pl b/eco-list.pl deleted file mode 100644 index 52e3546..0000000 --- a/eco-list.pl +++ /dev/null @@ -1,8 +0,0 @@ -#! /usr/bin/perl -use strict; -use warnings; -require 'ECO.pm'; - -ECO::init(); -ECO::persist(); - diff --git a/www/opening-stats.pl b/www/opening-stats.pl index 06f67b2..d74f1ad 100755 --- a/www/opening-stats.pl +++ b/www/opening-stats.pl @@ -5,9 +5,9 @@ use CGI; use JSON::XS; use lib '..'; use Position; -use ECO; -ECO::unpersist("../openings.txt"); +our %openings = (); +read_openings(); my $cgi = CGI->new; my $fen = $cgi->param('fen'); @@ -31,7 +31,7 @@ while (<$fh>) { black_avg_elo => $black_avg_elo * 1, num_elo => $num_elo * 1 }; - $opening = $ECO::openings[$opening_num]; + $opening = $openings{$opening_num}; } close $fh; @@ -44,3 +44,18 @@ sub num { my $x = shift; return $x->{'white'} + $x->{'draw'} + $x->{'black'}; } + +sub read_openings { + open my $fh, "../openings.txt" + or die "../openings.txt: $!"; + for my $line (<$fh>) { + chomp $line; + my ($hash, $eco, $opening, $variation, $subvariation) = split /\t/, $line; + if ($variation eq '') { + $openings{$hash} = $eco . ": " . $opening; + } else { + $openings{$hash} = $eco . ": " . $opening . ": " . $variation; + } + } + close $fh; +} -- 2.39.2