Opening books format

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Dann Corbit
Posts: 12540
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: Opening books format

Post by Dann Corbit »

I made a few small changes for my own purposes (mostly for post processing, since the PGN standard demands the 7 mandatory tags):

Code: Select all

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <io.h>

struct BOOK &#123;
    unsigned char   move_from;
    unsigned char   move_to;
    unsigned char   move_promo;
    unsigned char   priority;
    unsigned int    games;
    unsigned int    won_games;
    unsigned int    lost_games;
    unsigned int    hz;
    int             first_child;
    int             next_sibling;
&#125;   *book;

FILE           *ft;
int             linelength = 0;

void            print_head&#40;)
&#123;
    fprintf&#40;ft, "*\n\n");
    fprintf&#40;ft, "&#91;Event \"?\"&#93;\n");
    fprintf&#40;ft, "&#91;Site \"?\"&#93;\n");
    fprintf&#40;ft, "&#91;Date \"????.??.??\"&#93;\n");
    fprintf&#40;ft, "&#91;Round \"?\"&#93;\n");
    fprintf&#40;ft, "&#91;Result \"*\"&#93;\n");
    fprintf&#40;ft, "&#91;White \"?\"&#93;\n");
    fprintf&#40;ft, "&#91;Black \"?\"&#93;\n\n");
&#125;

void print_move&#40;unsigned char move_from, unsigned char move_to, unsigned char move_promo, int ply&#41;
&#123;
    if (&#40;move_from >> 3&#41; + 1 == 32 && &#40;move_to >> 3&#41; + 1 == 32&#41; return;
    if &#40;linelength > 80&#41; &#123;
        fprintf&#40;ft, "\n");
        linelength = 0;
    &#125;
    if (&#40;ply & 1&#41; == 0&#41; &#123;
        fprintf&#40;ft, "%d.", ply / 2 + 1&#41;;
        linelength += 3;
    &#125;
    fprintf&#40;ft, "%c%d%c%d",
            &#40;move_from & 7&#41; + 'a', &#40;move_from >> 3&#41; + 1,
            &#40;move_to & 7&#41; + 'a', &#40;move_to >> 3&#41; + 1&#41;;
    if &#40;move_promo&#41;
        fprintf&#40;ft, "Q");       // allways queen&#58; this is incorrect

    fprintf&#40;ft, " ");
    linelength += 5;
&#125;

int main&#40;int argc, char *argv&#91;&#93;)
&#123;
    int             i,
                    ply;
    size_t          filesize;
    int             node&#91;1000&#93;;
    struct MOVE &#123;
        unsigned char   move_from;
        unsigned char   move_to;
        unsigned char   move_promo;
    &#125;   moves&#91;1000&#93;;

    if &#40;argc < 2&#41; &#123;
        printf&#40;"Simple command-line converter from ABK to PGN\n");
        printf&#40;"Usage&#58; abk.exe <abk_file>\n");
        printf&#40;"Result PGN-file&#58; out.pgn\n");
        exit&#40;0&#41;;
    &#125;
    ft = fopen&#40;argv&#91;1&#93;, "rb");
    if &#40;ft == NULL&#41; &#123;
        printf&#40;"Can't open file %s\n", argv&#91;1&#93;);
        exit&#40;0&#41;;
    &#125;
    filesize = filelength&#40;fileno&#40;ft&#41;);
    book = malloc&#40;filesize&#41;;
    fread&#40;book, filesize / sizeof&#40;struct BOOK&#41;, sizeof&#40;struct BOOK&#41;, ft&#41;;
    fclose&#40;ft&#41;;
    ft = fopen&#40;"out.pgn", "w");
    print_head&#40;);
    ply = 0;
    node&#91;0&#93; = 900;  // offset to first node in abk-file

    for (;;) &#123;
        print_move&#40;book&#91;node&#91;ply&#93;&#93;.move_from, book&#91;node&#91;ply&#93;&#93;.move_to,
                   book&#91;node&#91;ply&#93;&#93;.move_promo, ply&#41;;
        if &#40;book&#91;node&#91;ply&#93;&#93;.first_child > 0&#41; &#123;  // current game

            moves&#91;ply&#93;.move_from = book&#91;node&#91;ply&#93;&#93;.move_from;
            moves&#91;ply&#93;.move_to = book&#91;node&#91;ply&#93;&#93;.move_to;
            moves&#91;ply&#93;.move_promo = book&#91;node&#91;ply&#93;&#93;.move_promo;
            node&#91;ply + 1&#93; = book&#91;node&#91;ply&#93;&#93;.first_child;
            ply++;
        &#125; else &#123; // new game
            fprintf&#40;ft, "\n");
            node&#91;ply&#93; = book&#91;node&#91;ply&#93;&#93;.next_sibling;
            while &#40;node&#91;ply&#93; < 0&#41; &#123;
                ply--;
                if &#40;ply < 0&#41; &#123;
                    fclose&#40;ft&#41;;
                    exit&#40;0&#41;;
                &#125;
                node&#91;ply&#93; = book&#91;node&#91;ply&#93;&#93;.next_sibling;
            &#125;
            print_head&#40;);
            linelength = 0;
            for &#40;i = 0; i < ply; i++) &#123;
                print_move&#40;moves&#91;i&#93;.move_from, moves&#91;i&#93;.move_to, moves&#91;i&#93;.move_promo, i&#41;;
            &#125;
        &#125;
        if &#40;node&#91;ply&#93; >= filesize / &#40;int&#41; sizeof&#40;struct BOOK&#41;) &#123;
            printf&#40;"Error&#58; out of file\n");
            fclose&#40;ft&#41;;
            exit&#40;0&#41;;
        &#125;
    &#125;
    return 0;
&#125;
User avatar
beachknight
Posts: 3533
Joined: Tue Jan 09, 2007 8:33 pm
Location: Antalya, Turkey

Re: Opening books format

Post by beachknight »

Is there any executable that takes *.abk book as input
and produce *.pgn as output.

I'd be glad to have such a tool.

Best to you,
hi, merhaba, hallo HT
User avatar
beachknight
Posts: 3533
Joined: Tue Jan 09, 2007 8:33 pm
Location: Antalya, Turkey

Re: Opening books format

Post by beachknight »

OS: WinXPSP2

Best!
hi, merhaba, hallo HT
User avatar
Jim Ablett
Posts: 1383
Joined: Fri Jul 14, 2006 7:56 am
Location: London, England
Full name: Jim Ablett

Re: Opening books format

Post by Jim Ablett »

beachknight wrote:Is there any executable that takes *.abk book as input
and produce *.pgn as output.

I'd be glad to have such a tool.

Best to you,
Here's the compiled windows exe 'abk2pgn.exe' from Dann's modified src.
Works fine, but output still needs a little bit of post-processing
with pgn-extract/pgntrim.

http://www.zshare.net/download/10653257fcb8f902/

Jim.
CThinker
Posts: 388
Joined: Wed Mar 08, 2006 10:08 pm

Re: Opening books format

Post by CThinker »

One more modification...

Replace this:

Code: Select all

    if &#40;move_promo&#41;
        fprintf&#40;ft, "Q");       // allways queen&#58; this is incorrect 
with this:

Code: Select all

    if &#40;move_promo&#41; &#123;
        static const char prm&#91;&#93; = "RNBQ";
        fprintf&#40;ft, "%c", prm&#91;abs&#40;move_promo&#41;-1&#93;);
    &#125;
User avatar
Jim Ablett
Posts: 1383
Joined: Fri Jul 14, 2006 7:56 am
Location: London, England
Full name: Jim Ablett

Re: Opening books format

Post by Jim Ablett »

CThinker wrote:One more modification...

Replace this:

Code: Select all

    if &#40;move_promo&#41;
        fprintf&#40;ft, "Q");       // allways queen&#58; this is incorrect 
with this:

Code: Select all

    if &#40;move_promo&#41; &#123;
        static const char prm&#91;&#93; = "RNBQ";
        fprintf&#40;ft, "%c", prm&#91;abs&#40;move_promo&#41;-1&#93;);
    &#125;
abk2pgn with Lance Perkins mod

http://www.zshare.net/download/106703053e39962e/

For cleaning up pgns I created a little batch script for my personal use
which run a series of pgn tools one after the other automatically (saves a lot of typing on the command line).

http://www.zshare.net/download/1067058909ff22e0/

This what it does automatically >
To clean/normalize and process a pgn file

Step 1: Backup original pgn file.
Step 2: 'Pgn-Extract' to remove duplicates.
Step 3: 'CleanTag' to remove erroneous tags.
Step 4: 'Trim' to correctly re-format pgn file.
Step 5: 'PgnMan' to add ECO to openings.
Step 6: 'AddElo' to fill in missing Elo tags.
Step 7: 'CleanUp' to remove incorrect null values.
Step 8: 'NameList' to produce player stats file.
Step 9: 'Ratelist' to produce player elo stats file

Step 10: Output completed/processed pgn 'processed.pgn' and stats files.


On the command line type:
-----------------------------------------------

process <name of pgn file>

EXAMPLE:

process anand.pgn


When completed directory will additionally contain >

1. processed.pgn - the fully cleaned/normalized/processed pgn file.
2. Ratelist.txt - Ratings list of all players contained in the pgn.
3. Namelist.txt - List of all players contained in the pgn.
Jim.
User avatar
Jim Ablett
Posts: 1383
Joined: Fri Jul 14, 2006 7:56 am
Location: London, England
Full name: Jim Ablett

Re: Opening books format

Post by Jim Ablett »

On the command line type:
-----------------------------------------------

process <name of pgn file>

EXAMPLE:

process anand.pgn
Small amendment to that. The don't have to type the name of the pgn.
Just drop the pgn into the folder and type process

Jim.
User avatar
beachknight
Posts: 3533
Joined: Tue Jan 09, 2007 8:33 pm
Location: Antalya, Turkey

Re: Opening books format

Post by beachknight »

Thanks Jim.

I'll try them out.

Best to yoU!
hi, merhaba, hallo HT
Jimbo I
Posts: 149
Joined: Thu Feb 15, 2007 4:34 am
Location: USA

Re: Opening books format

Post by Jimbo I »

Jim Ablett wrote:
CThinker wrote:One more modification...

Replace this:

Code: Select all

    if &#40;move_promo&#41;
        fprintf&#40;ft, "Q");       // allways queen&#58; this is incorrect 
with this:

Code: Select all

    if &#40;move_promo&#41; &#123;
        static const char prm&#91;&#93; = "RNBQ";
        fprintf&#40;ft, "%c", prm&#91;abs&#40;move_promo&#41;-1&#93;);
    &#125;
abk2pgn with Lance Perkins mod

http://www.zshare.net/download/106703053e39962e/

For cleaning up pgns I created a little batch script for my personal use
which run a series of pgn tools one after the other automatically (saves a lot of typing on the command line).

http://www.zshare.net/download/1067058909ff22e0/

This what it does automatically >
To clean/normalize and process a pgn file

Step 1: Backup original pgn file.
Step 2: 'Pgn-Extract' to remove duplicates.
Step 3: 'CleanTag' to remove erroneous tags.
Step 4: 'Trim' to correctly re-format pgn file.
Step 5: 'PgnMan' to add ECO to openings.
Step 6: 'AddElo' to fill in missing Elo tags.
Step 7: 'CleanUp' to remove incorrect null values.
Step 8: 'NameList' to produce player stats file.
Step 9: 'Ratelist' to produce player elo stats file

Step 10: Output completed/processed pgn 'processed.pgn' and stats files.


On the command line type:
-----------------------------------------------

process <name of pgn file>

EXAMPLE:

process anand.pgn


When completed directory will additionally contain >

1. processed.pgn - the fully cleaned/normalized/processed pgn file.
2. Ratelist.txt - Ratings list of all players contained in the pgn.
3. Namelist.txt - List of all players contained in the pgn.
Jim.
Hi Jim,
I stumbled across this thread and was wanting to try out your abk2pgn compile with the Lance Perkins mod, but it seems the link has expired. Would it be possible for you to repost a new link so I can download it to play with? Thanks.
User avatar
Jim Ablett
Posts: 1383
Joined: Fri Jul 14, 2006 7:56 am
Location: London, England
Full name: Jim Ablett

Re: Opening books format

Post by Jim Ablett »

Jimbo I wrote:
Jim Ablett wrote:
CThinker wrote:One more modification...

Replace this:

Code: Select all

    if &#40;move_promo&#41;
        fprintf&#40;ft, "Q");       // allways queen&#58; this is incorrect 
with this:

Code: Select all

    if &#40;move_promo&#41; &#123;
        static const char prm&#91;&#93; = "RNBQ";
        fprintf&#40;ft, "%c", prm&#91;abs&#40;move_promo&#41;-1&#93;);
    &#125;
abk2pgn with Lance Perkins mod

http://www.zshare.net/download/106703053e39962e/

For cleaning up pgns I created a little batch script for my personal use
which run a series of pgn tools one after the other automatically (saves a lot of typing on the command line).

http://www.zshare.net/download/1067058909ff22e0/

This what it does automatically >
To clean/normalize and process a pgn file

Step 1: Backup original pgn file.
Step 2: 'Pgn-Extract' to remove duplicates.
Step 3: 'CleanTag' to remove erroneous tags.
Step 4: 'Trim' to correctly re-format pgn file.
Step 5: 'PgnMan' to add ECO to openings.
Step 6: 'AddElo' to fill in missing Elo tags.
Step 7: 'CleanUp' to remove incorrect null values.
Step 8: 'NameList' to produce player stats file.
Step 9: 'Ratelist' to produce player elo stats file

Step 10: Output completed/processed pgn 'processed.pgn' and stats files.


On the command line type:
-----------------------------------------------

process <name of pgn file>

EXAMPLE:

process anand.pgn


When completed directory will additionally contain >

1. processed.pgn - the fully cleaned/normalized/processed pgn file.
2. Ratelist.txt - Ratings list of all players contained in the pgn.
3. Namelist.txt - List of all players contained in the pgn.
Jim.
Hi Jim,
I stumbled across this thread and was wanting to try out your abk2pgn compile with the Lance Perkins mod, but it seems the link has expired. Would it be possible for you to repost a new link so I can download it to play with? Thanks.
New link - abk2pgn (Lance Perkins mod)
Download:
http://www.mediafire.com/?nt2jvxzmttz

Jim.