Tool to reverse a fen string

Discussion of anything and everything relating to chess playing software and machines.

Moderators: hgm, Dann Corbit, Harvey Williamson

glav
Posts: 38
Joined: Sun Apr 07, 2019 1:10 am
Full name: Giovanni Lavorgna

Re: Tool to reverse a fen string

Post by glav »

Norm Pollock wrote: Fri Dec 25, 2020 8:14 pm Interesting project. I gave it a try and I came up with my version of the tool. It is pretty fast considering it is written in Java. I estimate it flips "epd" records at about 10,000 records per second on a typical home PC using I5. No opcodes or blank lines in output. Outputs are "epd" files.

Consider this to be a beta version, so any comments, bug finds, suggestions appreciated. I will probably put it into my "40H-EPD" toolbox next week after I write up a readme and if no bugs are found (hopefully).

usage for command line execution:

epdFlip filename.epd

output:
outZ.epd : the flipped records
outZ2.epd: the original records and the flipped records alternating

link:
https://www.mediafire.com/file/l9jjdfsm ... p.exe/file
Thanks Norman.
Ferdy
Posts: 4833
Joined: Sun Aug 10, 2008 3:15 pm
Location: Philippines

Re: Tool to reverse a fen string

Post by Ferdy »

glav wrote: Mon Dec 28, 2020 9:10 am Yes, it is indeed what I need. My intended use would be to study the opening repertoire of players I like the play of style of. For istance, say that I understood and liked a few plans of Kasparov in the Sicilian with black. If I reproduce them with white they should be even better. Right? In this perspective, it would be great if I could gather some more information from the database to see if other players have already employed the same idea. So I need a tool to quickly reverse the game in order to have "probes" to scan the database with. Such a tool would be a great step in that direction. May be I could reverse all Kasparov games with black at once and try to mine the games for other plans I liked.
OK to reverse names, elo, results, etct.
Thanks again.
Giovanni
There is a file called flip_pgn.py in this repo.

Usage:

Code: Select all

python flip_pgn.py --input mygames.pgn --output 1.pgn
You need to install python-chess.

Try if it works.

Sample:

Code: Select all

flip_pgn.py --input WorldChamp1960.pgn
[pgn][Event "World Championship 23th"]
[Site "Moscow"]
[Date "1960.??.??"]
[Round "1"]
[White "Botvinnik, Mikhail"]
[Black "Tal, Mihail"]
[Result "0-1"]
[BlackElo ""]
[BlackFideId "?"]
[BlackTitle "?"]
[ECO "C18"]
[FEN "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1"]
[SetUp "1"]
[WhiteElo ""]
[WhiteFideId "?"]
[WhiteTitle "?"]

1... e5 2. e3 d5 3. d4 Nc6 4. Bb5 e4 5. c4 a6 6. Bxc6+ bxc6 7. Qc2 Qg5 8. f4 Qg6 9. Ne2 Qxg2 10. Rg1 Qxh2 11. cxd5 Kd8 12. Bd2 Qh4+ 13. Ng3 Ne7 14. d6 cxd6 15. Ba5+ Ke8 16. Qxe4 Bg4 17. Nc3 d5 18. Qc2 h5 19. e4 Rh6 20. Qf2 dxe4 21. Ncxe4 Re6 22. Kd2 Rb8 23. b3 Nf5 24. Rae1 Rb5 25. Bc3 Qd8 26. Nxf5 Rxf5 27. Ng3 Rd5 28. Rxe6+ fxe6 29. Kc2 c5 30. dxc5 Bxc5 31. Qg2 Bxg1 32. Qxg1 h4 0-1

[Event "World Championship 23th"]
[Site "Moscow"]
[Date "1960.??.??"]
[Round "2"]
[White "Tal, Mihail"]
[Black "Botvinnik, Mikhail"]
[Result "1/2-1/2"]
[BlackElo ""]
[BlackFideId "?"]
[BlackTitle "?"]
[ECO "A61"]
[FEN "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1"]
[SetUp "1"]
[WhiteElo ""]
[WhiteFideId "?"]
[WhiteTitle "?"]

1... d5 2. Nf3 c5 3. c4 d4 4. e3 Nc6 5. exd4 cxd4 6. d3 Nf6 7. g3 Bg4 8. Bg2 Nd7 9. h3 Bh5 10. g4 Bg6 11. Nh4 Nc5 12. Nxg6 hxg6 13. O-O e6 14. Qe2 Be7 15. Rd1 O-O 16. Nd2 a5 17. Ne4 Nxe4 18. Qxe4 a4 19. Rb1 Ra7 20. Bd2 Nb4 21. Bxb4 Bxb4 22. b3 a3 23. Rbc1 Qd6 24. Rc2 b6 25. Qc6 Qxc6 26. Bxc6 Rc7 27. Bf3 g5 28. Re2 Rc5 29. Rc1 g6 30. Bg2 Rd8 31. Rf1 Rd6 32. Kh2 Kg7 33. Kg3 Rd8 34. h4 gxh4+ 35. Kxh4 g5+ 36. Kg3 Rc7 37. Rh1 Bd6+ 38. Kf3 Kg6 39. Ree1 Bb4 40. Re5 Rc5 41. Rxc5 bxc5 42. Ke2 Ba5 43. Be4+ Kf6 44. Rh5 Rg8 45. f4 1/2-1/2[/pgn]
glav
Posts: 38
Joined: Sun Apr 07, 2019 1:10 am
Full name: Giovanni Lavorgna

Re: Tool to reverse a fen string

Post by glav »

Ferdy wrote: Mon Dec 28, 2020 10:25 am
glav wrote: Mon Dec 28, 2020 9:10 am Yes, it is indeed what I need. My intended use would be to study the opening repertoire of players I like the play of style of. For istance, say that I understood and liked a few plans of Kasparov in the Sicilian with black. If I reproduce them with white they should be even better. Right? In this perspective, it would be great if I could gather some more information from the database to see if other players have already employed the same idea. So I need a tool to quickly reverse the game in order to have "probes" to scan the database with. Such a tool would be a great step in that direction. May be I could reverse all Kasparov games with black at once and try to mine the games for other plans I liked.
OK to reverse names, elo, results, etct.
Thanks again.
Giovanni
There is a file called flip_pgn.py in this repo.

Usage:

Code: Select all

python flip_pgn.py --input mygames.pgn --output 1.pgn
You need to install python-chess.

Try if it works.

Sample:

Code: Select all

flip_pgn.py --input WorldChamp1960.pgn
[pgn][Event "World Championship 23th"]
[Site "Moscow"]
[Date "1960.??.??"]
[Round "1"]
[White "Botvinnik, Mikhail"]
[Black "Tal, Mihail"]
[Result "0-1"]
[BlackElo ""]
[BlackFideId "?"]
[BlackTitle "?"]
[ECO "C18"]
[FEN "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1"]
[SetUp "1"]
[WhiteElo ""]
[WhiteFideId "?"]
[WhiteTitle "?"]

1... e5 2. e3 d5 3. d4 Nc6 4. Bb5 e4 5. c4 a6 6. Bxc6+ bxc6 7. Qc2 Qg5 8. f4 Qg6 9. Ne2 Qxg2 10. Rg1 Qxh2 11. cxd5 Kd8 12. Bd2 Qh4+ 13. Ng3 Ne7 14. d6 cxd6 15. Ba5+ Ke8 16. Qxe4 Bg4 17. Nc3 d5 18. Qc2 h5 19. e4 Rh6 20. Qf2 dxe4 21. Ncxe4 Re6 22. Kd2 Rb8 23. b3 Nf5 24. Rae1 Rb5 25. Bc3 Qd8 26. Nxf5 Rxf5 27. Ng3 Rd5 28. Rxe6+ fxe6 29. Kc2 c5 30. dxc5 Bxc5 31. Qg2 Bxg1 32. Qxg1 h4 0-1

[Event "World Championship 23th"]
[Site "Moscow"]
[Date "1960.??.??"]
[Round "2"]
[White "Tal, Mihail"]
[Black "Botvinnik, Mikhail"]
[Result "1/2-1/2"]
[BlackElo ""]
[BlackFideId "?"]
[BlackTitle "?"]
[ECO "A61"]
[FEN "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1"]
[SetUp "1"]
[WhiteElo ""]
[WhiteFideId "?"]
[WhiteTitle "?"]

1... d5 2. Nf3 c5 3. c4 d4 4. e3 Nc6 5. exd4 cxd4 6. d3 Nf6 7. g3 Bg4 8. Bg2 Nd7 9. h3 Bh5 10. g4 Bg6 11. Nh4 Nc5 12. Nxg6 hxg6 13. O-O e6 14. Qe2 Be7 15. Rd1 O-O 16. Nd2 a5 17. Ne4 Nxe4 18. Qxe4 a4 19. Rb1 Ra7 20. Bd2 Nb4 21. Bxb4 Bxb4 22. b3 a3 23. Rbc1 Qd6 24. Rc2 b6 25. Qc6 Qxc6 26. Bxc6 Rc7 27. Bf3 g5 28. Re2 Rc5 29. Rc1 g6 30. Bg2 Rd8 31. Rf1 Rd6 32. Kh2 Kg7 33. Kg3 Rd8 34. h4 gxh4+ 35. Kxh4 g5+ 36. Kg3 Rc7 37. Rh1 Bd6+ 38. Kf3 Kg6 39. Ree1 Bb4 40. Re5 Rc5 41. Rxc5 bxc5 42. Ke2 Ba5 43. Be4+ Kf6 44. Rh5 Rg8 45. f4 1/2-1/2[/pgn]
Works great. Even with a multi-pgn file. Thank you so much!
Giovanni