niklasf / python-chess

A chess library for Python, with move generation and validation, PGN parsing and writing, Polyglot opening book reading, Gaviota tablebase probing, Syzygy tablebase probing, and UCI/XBoard engine communication
https://python-chess.readthedocs.io/en/latest/
GNU General Public License v3.0
2.4k stars 521 forks source link

Is there any way to write multiple comments on a move? #946

Open olleeriksson opened 1 year ago

olleeriksson commented 1 year ago

I'm trying to use python-chess to merge two pgn games, which is working fine. But it appears some PGN viewers really want the normal text comments and the %cal and %csl annotations separated into their own comment, like so:

  1. e4 { Here is a text comment } { [%cal Ge2e4] }

I seem to have no way to create this type of comment since the "comment" field in the GameNode class in pgn.py is a string, when I think I want it to be a list of strings. So I end up with this:

  1. e4 { Here is a text comment [%cal Ge2e4] } <--- without a closing and opening bracket between the text and annotation

I have also tried setting node.comment = f"{text} }} {{ {annotations}" , ie injecting a } and a { inside the comment but it doesn't work, and is hacky anyway. It appears to insert the { but the } never appears.

What's your take on this? Just something the library doesn't support?

niklasf commented 1 year ago

Hi. It's currently not really possible (except by subclassing chess.pgn.StringExporter), but I think it makes sense to add.

By the way, which PGN viewer has this requirement?

olleeriksson commented 1 year ago

This is how Lichess.org outputs comments + annotations:

2... Nf6 { Queen's Gambit Declined - Marshall defense } { [%csl Gc4][%cal Gc4d5] }

And Chesstempo.org: (though it's not obvious how to create these so not a lot of people are probably creating annotations here, but maybe some people import into Chesstempo and then export, in which case you also get this)

1.e4 e5 { This is a comment } {[%cal Gf2f5,Gg2g3][%csl Gb3,Yc4]}

And this NodeJS library for parsing PGN files seem to want it formatted this way too.

https://www.npmjs.com/package/pgn-parser

Check out test/test_grammer.js around line 170 for a test that tells alot.

I have sort of gotten around this by now by creating python script using your parser and a hack that inserts } { between the text part and the [% part after it is printed, but at least based on these three frameworks/sites I would guess this would be something good to support.