PGN to FEN (with Evaluation)?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
Steve Maughan
Posts: 1221
Joined: Wed Mar 08, 2006 8:28 pm
Location: Florida, USA

PGN to FEN (with Evaluation)?

Post by Steve Maughan »

I'd like to convert a (large) PGN database to a FEN / EPD text file, with one FEN string for each position. And I'd like to append each move's comment / evaluation onto the end of each FEN string.

What's the easiest way to do this?

I've looked at SCID PC and PGN Extract and neither seem to do this. Is there any utility which will fit this purpose?

Thanks - Steve

P.S. Ideally I'd also like it to be able to handle FRC / Chess960 games as well
http://www.chessprogramming.net - Maverick Chess Engine
Ferdy
Posts: 4833
Joined: Sun Aug 10, 2008 3:15 pm
Location: Philippines

Re: PGN to FEN (with Evaluation)?

Post by Ferdy »

Could you post a sample game? let me see its format.
elcabesa
Posts: 855
Joined: Sun May 23, 2010 1:32 pm

Re: PGN to FEN (with Evaluation)?

Post by elcabesa »

I think you could do it in 2 step.
First use pgn2epd to create the big list of fen position.
second write a little scripts that read a line, execute a small search with an engine and then record the result

i remember i did something like that in vajolet, but I didn't create the scritp, I modified vajolet to do the second step
matthewlai
Posts: 793
Joined: Sun Aug 03, 2014 4:48 am
Location: London, UK

Re: PGN to FEN (with Evaluation)?

Post by matthewlai »

Steve Maughan wrote:I'd like to convert a (large) PGN database to a FEN / EPD text file, with one FEN string for each position. And I'd like to append each move's comment / evaluation onto the end of each FEN string.

What's the easiest way to do this?

I've looked at SCID PC and PGN Extract and neither seem to do this. Is there any utility which will fit this purpose?

Thanks - Steve

P.S. Ideally I'd also like it to be able to handle FRC / Chess960 games as well
If you know Python, the python-chess library should work. It would be something like 10 lines of code.

Actually, since I'm learning Python right now and need some practice, I can write it for you, in a few hours.
Disclosure: I work for DeepMind on the AlphaZero project, but everything I say here is personal opinion and does not reflect the views of DeepMind / Alphabet.
matthewlai
Posts: 793
Joined: Sun Aug 03, 2014 4:48 am
Location: London, UK

Re: PGN to FEN (with Evaluation)?

Post by matthewlai »

This should do it:

Code: Select all

#!/usr/bin/env python3

import chess
import sys

from chess import pgn

def PrintNode(gameNode):
	if len(gameNode.variations) > 0:
		print(gameNode.board().epd(sm = gameNode.variations[0].move, c0 = gameNode.variations[0].comment))
	else:
		print(gameNode.board().epd())

if len(sys.argv) != 2:
	print&#40;"Usage&#58; " + sys.argv&#91;0&#93; + " <PGN file>")
	sys.exit&#40;1&#41;

if sys.argv&#91;1&#93; == '-'&#58;
    pgn = sys.stdin
else&#58;
    pgn = open&#40;sys.argv&#91;1&#93;)

game = chess.pgn.read_game&#40;pgn&#41;
while game != None&#58;
	
	gameNode = game
	while len&#40;gameNode.variations&#41;&#58;
		PrintNode&#40;gameNode&#41;
		gameNode = gameNode.variations&#91;0&#93;
    
	# for the last node &#40;no variations&#41;
	PrintNode&#40;gameNode&#41;
        
	game = chess.pgn.read_game&#40;pgn&#41;

Code: Select all

sudo pip3 install python-chess
python3 pgn2epd.py input.pgn > output.epd
If you are processing very large files and performance matters, you'll probably want to grab the latest version of the python-chess library from github. I submitted a patch last week that makes it much faster.
Disclosure: I work for DeepMind on the AlphaZero project, but everything I say here is personal opinion and does not reflect the views of DeepMind / Alphabet.
User avatar
Steve Maughan
Posts: 1221
Joined: Wed Mar 08, 2006 8:28 pm
Location: Florida, USA

Re: PGN to FEN (with Evaluation)?

Post by Steve Maughan »

HI Matthew,

Python Chess Library - it sounds fantastic! It may just be a good enough reason to dip my toe in the waters of Python.

Many thanks!

Steve
http://www.chessprogramming.net - Maverick Chess Engine
Jesse Gersenson
Posts: 593
Joined: Sat Aug 20, 2011 9:43 am

Re: PGN to FEN (with Evaluation)?

Post by Jesse Gersenson »

pgn-extract can convert a pgn to epd/fen. Here is the help file:
http://www.cs.kent.ac.uk/people/staff/d ... /help.html

Here's a start in bash:

Code: Select all

~$ pgn-extract -Wepd test.pgn |cut -d' ' -f1,2,3,4>tmp

Code: Select all

#!/bin/bash
while read p; do
motor=maverick
fen="position fen $p" 
echo $p
&#40;echo "$fen";sleep 0.01;echo 'go depth 47'; sleep 1;echo 'quit';)|./$motor|grep "cp "|tail -1|sed 's/.*cp //g'|sed 's/ nps.*//g';
done <tmp
Outputs the fen and score in cp:
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -
37
rnbqkbnr/pppppppp/8/8/8/5N2/PPPPPPPP/RNBQKB1R b KQkq -
-22
rnbqkbnr/pp1ppppp/8/2p5/8/5N2/PPPPPPPP/RNBQKB1R w KQkq c6
38
...