Linux program to convert computer algebraic in text file to PGN in 2nd text file

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

smcracraft
Posts: 737
Joined: Wed Mar 08, 2006 8:08 pm
Location: Orange County California
Full name: Stuart Cracraft

Linux program to convert computer algebraic in text file to PGN in 2nd text file

Post by smcracraft »

Anyone have the converter?

It could just receive this kind of thing

e2e4 e7e5
g1f3 g8f6

and output in PGN.

...and so forth, all communication being done with the input physical file of CA and output file of PGN.
jdart
Posts: 4366
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

Re: Linux program to convert computer algebraic in text file to PGN in 2nd text file

Post by jdart »

Pretty sure you could do that with python-chess, without a lot of trouble.

--Jon
Daniel Shawul
Posts: 4185
Joined: Tue Mar 14, 2006 11:34 am
Location: Ethiopia

Re: Linux program to convert computer algebraic in text file to PGN in 2nd text file

Post by Daniel Shawul »

smcracraft wrote: Mon Dec 10, 2018 2:40 am Anyone have the converter?

It could just receive this kind of thing

e2e4 e7e5
g1f3 g8f6

and output in PGN.

...and so forth, all communication being done with the input physical file of CA and output file of PGN.
I had a need for that kind of tool and didn't want to write code for implementing SAN strictly.
So what I did is use an acceptable SAN notation that is processed by pgn tools. e2e4, Nc3xd5, e7e8=Q
{Piece}from{x}to[=promotion]
You would still need to identify if the move is a capture or promotion move, but you don't have to do all the complicated
SAN stuff for resolving ambiguities.

This kind of PGN is accepted by ChessTempo graphics (and probably other tools) and is easier to generate.

1. g2g3 d7d6 2. e2e3 f7f5 3. Nb1a3 g7g6 4. Na3c4 a7a6 5. d2d4 Bf8h6 6. Bf1g2 b7b5 7. Nc4d2 d6d5 8. Bg2h3 f5f4 9. Qd1e2 e7e6 10. Nd2b3 Qd8f6 11. e3xf4 Ke8d8 12. Bc1d2 Kd8e7 13. Qe2e5 Qf6xe5 14. f4xe5 Bc8b7 15. Nb3a5 Bh6xd2 16. Ke1xd2 Bb7c6 17. Ng1f3 Ng8f6 18. Bh3f5 e6xf5 19. e5xf6 Ke7xf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d7e6 24. Kc1d1 Rf8c8 25. Na5b7 b5b4 26. Ra1b1 Ra8a7 27. Nb7c5 c7c6 28. Nc5xe6 Kf6xe6 29. Ne5xc6 Ke6f6 30. Nc6xa7 Rc8g8 31. Rb1c1 Nb8d7 32. Na7c6 Rg8c8 33. Nc6xb4 Nd7b6 34. Nb4xa6 Nb6c4 35. Re1e5 Nc4xe5 36. Na6b4 Ne5c4 37. Nb4xd5 Kf6e6 38. Nd5b4 Rc8d8 39. Kd1e2 Rd8xd4 40. Nb4d3 Rd4e4 41. Ke2f3 Re4e3 42. Kf3f2 Ke6d5 43. Rc1e1 Re3xe1 44. Nd3b4 Kd5c5 45. Nb4a6 Kc5b6 46. Kf2xe1 Kb6xa6 47. b2b4 Nc4b2 48. a2a4 Ka6b6 49. h2h4 Kb6c7 50. a4a5 Nb2c4 51. h4h5 Nc4a3 52. h5xg6 Kc7d8 53. Ke1f2 Kd8e8 54. a5a6 Na3b1 55. a6a7 Ke8f8 56. g6g7 Kf8g8 57. a7a8=Q Kg8f7 58. Qa8f8 Kf7e6 59. g7g8=Q Ke6d7 60. Qf8xh6 Kd7c7 61. Qg8f7 Kc7d8 62. Qh6f8 1-0 [/pgn]
User avatar
stegemma
Posts: 859
Joined: Mon Aug 10, 2009 10:05 pm
Location: Italy
Full name: Stefano Gemma

Re: Linux program to convert computer algebraic in text file to PGN in 2nd text file

Post by stegemma »

In WinBoard, you just can copy the text and paste it with Ctrl-V (or Edit->Paste game from clipboard), then you can save the PGN.

Maybe you can do the same with some other software in Linux?
Author of Drago, Raffaela, Freccia, Satana, Sabrina.
http://www.linformatica.com
User avatar
hgm
Posts: 27789
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Linux program to convert computer algebraic in text file to PGN in 2nd text file

Post by hgm »

What you can do with WinBoard on Windows, you can do with XBoard on Linux or OSX...

And with Ctrl-C (or Edit->Copy Game) you can then put it back on the clipboard in PGN format. You could even specify the tags before doing that, through Edit -> Edit Tags.

E.g. a game posted elsewhere in this section in coordinate notation would look like this, after Ctrl-V, Ctrl-C:
[Event "Game from non-PGN file"]
[Site "hgm-xboard"]
[Date "2018.12.10"]
[Round "-"]
[White "?"]
[Black "?"]
[Result "0-1"]

1. Nf3 d5 2. c4 d4 3. d3 c5 4. e4 Nc6 5. Be2 e5 6. Qa4 Bd6 7. Bg5 Ne7 8.
Bxe7 Qxe7 9. Nbd2 h5 10. Rc1 h4 11. O-O g5 12. h3 g4 13. hxg4 Bxg4 14. Nxd4
exd4 15. Bxg4 Qg5 16. e5 Qxg4 17. exd6 Rg8 18. g3 hxg3 19. Ne4 Qh3 20. fxg3
Rxg3+ 21. Nxg3 Qxg3+ 22. Kh1 O-O-O 23. d7+ Kb8 24. Qxa7+ Kxa7 25. Rf5 Rh8+
26. Rh5 Rxh5#
{Black mates} 0-1
BTW, WinBoard/XBoard would accept almost anything that remotely looks like a collection of chess moves as input for a game. You can for instance directly copy-paste a HTML table that in some of its colums has the moves into it. As long as other columns do not have things that look like moves too. (I once encountered a problem that it interpreted the 00 in thinking times like 2:00 as an alternative notation for castling, but I now suppress that if there is an adjacent ':'.)
User avatar
MikeB
Posts: 4889
Joined: Thu Mar 09, 2006 6:34 am
Location: Pen Argyl, Pennsylvania

Re: Linux program to convert computer algebraic in text file to PGN in 2nd text file

Post by MikeB »

hgm wrote: Mon Dec 10, 2018 8:54 am What you can do with WinBoard on Windows, you can do with XBoard on Linux or OSX...

And with Ctrl-C (or Edit->Copy Game) you can then put it back on the clipboard in PGN format. You could even specify the tags before doing that, through Edit -> Edit Tags.

E.g. a game posted elsewhere in this section in coordinate notation would look like this, after Ctrl-V, Ctrl-C:
[Event "Game from non-PGN file"]
[Site "hgm-xboard"]
[Date "2018.12.10"]
[Round "-"]
[White "?"]
[Black "?"]
[Result "0-1"]

1. Nf3 d5 2. c4 d4 3. d3 c5 4. e4 Nc6 5. Be2 e5 6. Qa4 Bd6 7. Bg5 Ne7 8.
Bxe7 Qxe7 9. Nbd2 h5 10. Rc1 h4 11. O-O g5 12. h3 g4 13. hxg4 Bxg4 14. Nxd4
exd4 15. Bxg4 Qg5 16. e5 Qxg4 17. exd6 Rg8 18. g3 hxg3 19. Ne4 Qh3 20. fxg3
Rxg3+ 21. Nxg3 Qxg3+ 22. Kh1 O-O-O 23. d7+ Kb8 24. Qxa7+ Kxa7 25. Rf5 Rh8+
26. Rh5 Rxh5#
{Black mates} 0-1
BTW, WinBoard/XBoard would accept almost anything that remotely looks like a collection of chess moves as input for a game. You can for instance directly copy-paste a HTML table that in some of its colums has the moves into it. As long as other columns do not have things that look like moves too. (I once encountered a problem that it interpreted the 00 in thinking times like 2:00 as an alternative notation for castling, but I now suppress that if there is an adjacent ':'.)
Yes, xBoard is a very forgiving interface with respect to moves - comes in handy and move numbers are not required. I copy and pasted the OP

e2e4 e7e5
g1f3 g8f6

and it is copy and pasted back from xBoard here:

[pgn][Event "Game from non-PGN file"] [Site "Mac-Pro.local"] [Date "2018.12.13"] [Round "-"] [White "?"] [Black "?"] [Result "*"] 1. e4 e5 2. Nf3 Nf6 *[/pgn]
Image
AlvaroBegue
Posts: 931
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: Linux program to convert computer algebraic in text file to PGN in 2nd text file

Post by AlvaroBegue »

jdart wrote: Mon Dec 10, 2018 3:19 am Pretty sure you could do that with python-chess, without a lot of trouble.

--Jon
This is correct. Here's a 5-minute attempt, and I am not really a Python programmer:

Code: Select all

#!/usr/bin/python3

import sys
import chess
import chess.pgn

game = chess.pgn.Game()
node = game

for line in sys.stdin:
    for move in line.split():
        node = node.add_variation(chess.Move.from_uci(move))

print(game)
Feed it moves in stdin and it will print out a PGN file in stdout.
User avatar
hgm
Posts: 27789
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Linux program to convert computer algebraic in text file to PGN in 2nd text file

Post by hgm »

But why spend 5 minutes if a solution already exists that works 'out of the box'...?
AlvaroBegue
Posts: 931
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: Linux program to convert computer algebraic in text file to PGN in 2nd text file

Post by AlvaroBegue »

hgm wrote: Fri Dec 14, 2018 2:35 pm But why spend 5 minutes if a solution already exists that works 'out of the box'...?
Well, by asking on the forum you are spending the time of a lot of us trying to help you.
smcracraft
Posts: 737
Joined: Wed Mar 08, 2006 8:08 pm
Location: Orange County California
Full name: Stuart Cracraft

Re: Linux program to convert computer algebraic in text file to PGN in 2nd text file

Post by smcracraft »

Actually this worked perfectly and is one reason why the Internet was invented, so that people could help people, openly, freely.

It wasn't just a DARPA program for decentralized command-and-control post-nukes.

Anyway, I upgraded pip to run with python3 in its script, then did this:

pip install --upgrade pip
pip install python-chess
./converter.py < my computer-algebraic output > filename.pgn

Then double-clicked the above pgn on my desktop and Stockfish brought it up for analysis.

So good to go here.

Thanks to the author(s).

Cheers!