Tool to extract parts of games from pgn file

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

Moderators: hgm, Rebel, chrisw

JohnS
Posts: 215
Joined: Sun Feb 24, 2008 2:08 am

Tool to extract parts of games from pgn file

Post by JohnS »

I am looking for a tool to extract parts of games from a pgn file.

Each game in the file will have a diagram to show the
point where I want to extract the part (could be after a White or Black move).

Here is an example.

[Event "World-ch"]
[Site "New York"]
[Date "2016.11.11"]
[Round "1"]
[White "Carlsen, Magnus"]
[Black "Karjakin, Sergey"]
[Result "1/2-1/2"]
[ECO "D00"]
[WhiteElo "2853"]
[BlackElo "2772"]
[PlyCount "84"]
[EventDate "2016.11.11"]
[EventType "match"]
[EventRounds "12"]
[EventCountry "USA"]

1. d4 Nf6 2. Bg5 d5 3. e3 c5 4. Bxf6 gxf6 {[#]} 5. dxc5 (5. Nc3 Nc6 6. Qh5) (5.
c3 Qb6 (5... Nc6)) 5... Nc6 {comment} 6. Bb5 e6 {comment} 7. c4 dxc4 8. Nd2
Bxc5 9. Ngf3 O-O 10. O-O Na5 11. Rc1 Be7 12. Qc2 Bd7 13. Bxd7 Qxd7 14. Qc3 Qd5
15. Nxc4 Nxc4 16. Qxc4 Qxc4 17. Rxc4 Rfc8 18. Rfc1 Rxc4 19. Rxc4 Rd8 20. g3 Rd7
21. Kf1 f5 22. Ke2 Bf6 23. b3 Kf8 24. h3 h6 25. Ne1 Ke7 26. Nd3 Kd8 27. f4 h5
28. a4 Rd5 29. Nc5 b6 30. Na6 Be7 31. Nb8 a5 32. Nc6+ Ke8 33. Ne5 Bc5 34. Rc3
Ke7 35. Rd3 Rxd3 36. Kxd3 f6 37. Nc6+ Kd6 38. Nd4 Kd5 39. Nb5 Kc6 40. Nd4+ Kd6
41. Nb5+ Kd7 42. Nd4 Kd6 1/2-1/2

This could be one of the games in the file.
The tool will extract part of this game to the output file so that it looks like this.

[Event "World-ch"]
[Site "New York"]
[Date "2016.11.11"]
[Round "1"]
[White "Carlsen, Magnus"]
[Black "Karjakin, Sergey"]
[Result "1/2-1/2"]
[ECO "D00"]
[WhiteElo "2853"]
[BlackElo "2772"]
[SetUp "1"]
[FEN "rnbqkb1r/pp2pp1p/5p2/2pp4/3P4/4P3/PPP2PPP/RN1QKBNR w KQkq -"] [note: this is the position after move 4 by Black]
[PlyCount "84"] [note: this should be adjusted to the new number of plies, or can this field be deleted?]
[EventDate "2016.11.11"]
[EventType "match"]
[EventRounds "12"]
[EventCountry "USA"]

1. dxc5 (1. Nc3 Nc6 2. Qh5) (1. c3 Qb6 (1... Nc6)) 1... Nc6 {comment} 2. Bb5 e6 {comment} 3. c4 (*** rest of game is output by tool ***)

All the numbers have been readjusted to start from 1.
This includes any variations and subvariations in the game.
Any comments in this part of the game are also kept.

The tool will do this for all the games in the file and put the results in the output file.

Is there a tool to do this?

Thank you.
Ferdy
Posts: 4833
Joined: Sun Aug 10, 2008 3:15 pm
Location: Philippines

Re: Tool to extract parts of games from pgn file

Post by Ferdy »

JohnS wrote:I am looking for a tool to extract parts of games from a pgn file.

Each game in the file will have a diagram to show the
point where I want to extract the part (could be after a White or Black move).

Here is an example.

[Event "World-ch"]
[Site "New York"]
[Date "2016.11.11"]
[Round "1"]
[White "Carlsen, Magnus"]
[Black "Karjakin, Sergey"]
[Result "1/2-1/2"]
[ECO "D00"]
[WhiteElo "2853"]
[BlackElo "2772"]
[PlyCount "84"]
[EventDate "2016.11.11"]
[EventType "match"]
[EventRounds "12"]
[EventCountry "USA"]

1. d4 Nf6 2. Bg5 d5 3. e3 c5 4. Bxf6 gxf6 {[#]} 5. dxc5 (5. Nc3 Nc6 6. Qh5) (5.
c3 Qb6 (5... Nc6)) 5... Nc6 {comment} 6. Bb5 e6 {comment} 7. c4 dxc4 8. Nd2
Bxc5 9. Ngf3 O-O 10. O-O Na5 11. Rc1 Be7 12. Qc2 Bd7 13. Bxd7 Qxd7 14. Qc3 Qd5
15. Nxc4 Nxc4 16. Qxc4 Qxc4 17. Rxc4 Rfc8 18. Rfc1 Rxc4 19. Rxc4 Rd8 20. g3 Rd7
21. Kf1 f5 22. Ke2 Bf6 23. b3 Kf8 24. h3 h6 25. Ne1 Ke7 26. Nd3 Kd8 27. f4 h5
28. a4 Rd5 29. Nc5 b6 30. Na6 Be7 31. Nb8 a5 32. Nc6+ Ke8 33. Ne5 Bc5 34. Rc3
Ke7 35. Rd3 Rxd3 36. Kxd3 f6 37. Nc6+ Kd6 38. Nd4 Kd5 39. Nb5 Kc6 40. Nd4+ Kd6
41. Nb5+ Kd7 42. Nd4 Kd6 1/2-1/2

This could be one of the games in the file.
The tool will extract part of this game to the output file so that it looks like this.

[Event "World-ch"]
[Site "New York"]
[Date "2016.11.11"]
[Round "1"]
[White "Carlsen, Magnus"]
[Black "Karjakin, Sergey"]
[Result "1/2-1/2"]
[ECO "D00"]
[WhiteElo "2853"]
[BlackElo "2772"]
[SetUp "1"]
[FEN "rnbqkb1r/pp2pp1p/5p2/2pp4/3P4/4P3/PPP2PPP/RN1QKBNR w KQkq -"] [note: this is the position after move 4 by Black]
[PlyCount "84"] [note: this should be adjusted to the new number of plies, or can this field be deleted?]
[EventDate "2016.11.11"]
[EventType "match"]
[EventRounds "12"]
[EventCountry "USA"]

1. dxc5 (1. Nc3 Nc6 2. Qh5) (1. c3 Qb6 (1... Nc6)) 1... Nc6 {comment} 2. Bb5 e6 {comment} 3. c4 (*** rest of game is output by tool ***)

All the numbers have been readjusted to start from 1.
This includes any variations and subvariations in the game.
Any comments in this part of the game are also kept.

The tool will do this for all the games in the file and put the results in the output file.

Is there a tool to do this?

Thank you.
I have not noticed such tool, although there are programs, forgot it now, that can extract variation into a full game.

May I know what is the purpose of doing this, perhaps there are other ways to get your goals. Manipulating variations is not that easy. But this is possible with the help of python-chess and pgn-extract.
JohnS
Posts: 215
Joined: Sun Feb 24, 2008 2:08 am

Re: Tool to extract parts of games from pgn file

Post by JohnS »

Ferdy

The idea is to mark interesting positions in a database of games. The diagram marker is just to show where I want to extract from. It could be a comment like "Extract here" if this is easier to deal with than the diagram marker.

After extracting the game scores after the diagram marker/comment with the tool, I can analyse the games just from this position. I want the numbers reset to 1 to make if easy to see the move numbers from the diagram position.

If it is too hard to extract the game comments, then the tool can just work with games without comments or ignore the comments.

I hope this is clear.

Thanks.
John
Ferdy
Posts: 4833
Joined: Sun Aug 10, 2008 3:15 pm
Location: Philippines

Re: Tool to extract parts of games from pgn file

Post by Ferdy »

Given sample 3 games with full game records.

Code: Select all

[Event "NH Chess 5th"]
[Site "Amsterdam NED"]
[Date "2010.08.15"]
[Round "4"]
[White "Svidler, P."]
[Black "Nakamura, Hi"]
[Result "1-0"]
[Annotator "Saunders, John"]
[BlackElo "2729"]
[ECO "B12"]
[EventCountry "NED"]
[EventDate "2010.08.12"]
[EventRounds "10"]
[Source "www.chess.co.uk"]
[WhiteElo "2734"]

1.e4 c6 2.d4 d5 3.e5 Bf5 {[#]} 4.h4 { [One of the more direct ways of meeting the Caro Kann. It has a good database percentage and it is perhaps surprising it doesn't feature more than 3 Nc3 or 3 Nf3.] } 4...h5 5.c4 e6 6.Nc3 Ne7 7.Nge2 ( 7.Bg5 { has had significantly more outings. } ) 7...dxc4 8.Ng3 Bg6 9.Bg5 Qb6 10.Qd2 {[#]} Qb4 ( { This queen move has been played previously by a chess legend but may not be best. Black may need to look at something like } 10...Nd7 $5 { in order to revive this line. } ) 11.a3 Qb3 12.Nge4 ( 12.Rc1 Nd5 13.Nce4 b5 14.Be2 Nd7 15.O-O { was Adams-Karpov, Tilburg 1996, and led to a draw. Svidler's innovation looks sharper. } ) 12...Nd5 ( 12...Bxe4 $5 13.Nxe4 Nd5 14.Rh3 Qa4 15.Rc1 b5 { holds the c-pawn, though White has compensation in terms of a kingside attack, e.g. } 16.Rf3 $5 ) 13.Rh3 Qb6 14.Bxc4 Qa5 15.Nd6+ $5 { [With his lead in development, White doesn't have to commit himself in this way, but he prefers to tighten his grip on the dark squares.] } 15...Bxd6 16.exd6 Nd7 17.Rc1 Nxc3 18.Rcxc3 Nf6 ( 18...O-O $6 { is not really an option, e.g. } 19.Be7 Rfe8 20.Rhg3 { and now White has strong threats such as Bxe6 and Rxg6. } ) 19.b4 Qd8 $2 ( 19...Qf5 $5 { looks more combative, intending to meet } 20.Rhf3 { with } 20...Ne4 $5 { . After the text, Black is thrown onto the defensive. } ) 20.Qf4 Kd7 $2 ( 20...Bf5 { may have been better, but after } 21.Rhe3 O-O 22.Rg3 $1 { , White has a strong kingside attack. } ) 21.Rhe3 Re8 22.b5 Qa5 23.Bxf6 gxf6 24.Kf1 Bf5 25.Be2 Rac8 26.Rc5 { [diagram] [From here it is all one-way traffic.] } 26...Qa4 27.Rec3 a6 28.b6 { [The black queen is now a prisoner in the enemy camp.] } 28...Bg6 29.Qxf6 e5 30.Rxe5 Rxe5 31.dxe5 Re8 32.Re3 Re6 33.Qg5 Qd4 34.Kg1 Be4 ( { White threatens Bxh5 and if } 34...Qxb6 35.Bc4 { will be decisive. } ) 35.Qxh5 Rxe5 36.Qxf7+ Kxd6 37.Qxb7 1-0

[Event "World-ch"]
[Site "New York"]
[Date "2016.11.11"]
[Round "1"]
[White "Carlsen, Magnus"]
[Black "Karjakin, Sergey"]
[Result "1/2-1/2"]
[BlackElo "2772"]
[ECO "A45"]
[EventCountry "USA"]
[EventDate "2016.11.11"]
[EventRounds "12"]
[EventType "match"]
[WhiteElo "2853"]

1.d4 Nf6 2.Bg5 d5 3.e3 c5 4.Bxf6 gxf6 {[#]} 5.dxc5 ( 5.Nc3 Nc6 6.Qh5 ) ( 
5.c3 Qb6 ( 5...Nc6 ) ) 5...Nc6 { comment } 6.Bb5 e6 { comment } 7.c4 dxc4
8.Nd2 Bxc5 9.Ngf3 O-O 10.O-O Na5 11.Rc1 Be7 12.Qc2 {[#]} Bd7 13.Bxd7 Qxd7 14.Qc3
Qd5 15.Nxc4 Nxc4 16.Qxc4 Qxc4 17.Rxc4 Rfc8 18.Rfc1 Rxc4 19.Rxc4 Rd8 20.g3 Rd7
21.Kf1 f5 22.Ke2 Bf6 23.b3 Kf8 24.h3 h6 25.Ne1 Ke7 26.Nd3 Kd8 27.f4 h5 28.a4
Rd5 29.Nc5 b6 30.Na6 Be7 31.Nb8 a5 32.Nc6+ Ke8 33.Ne5 Bc5 34.Rc3 Ke7 35.Rd3
Rxd3 36.Kxd3 f6 37.Nc6+ Kd6 38.Nd4 Kd5 39.Nb5 Kc6 40.Nd4+ Kd6 41.Nb5+ Kd7
42.Nd4 Kd6 1/2-1/2

[Event "London Chess Classic"]
[Site "London"]
[Date "2011.12.05"]
[Round "3"]
[White "Carlsen, Magnus"]
[Black "Nakamura, Hikaru"]
[Result "1-0"]
[Annotator "Saunders, John"]
[BlackElo "2758"]
[BlackTeam "Bulgaria"]
[BlackTeamCountry "BUL"]
[ECO "C54"]
[EventCategory "20"]
[EventCountry "ENG"]
[EventDate "2011.12.03"]
[EventRounds "1"]
[Source "www.chess.co.uk"]
[WhiteElo "2383"]

1.e4 e5 2.Nf3 Nc6 3.Bc4 Nf6 4.d3 Bc5 5.c3 d6 6.Bb3 a6 7.Nbd2 Ba7 8.Nf1 h6 9.Ng3 O-O 10.O-O Be6 11.h3 {[#]} Qd7 ( { Of course, Carlsen plays his usual ultra-quiet game (which happens to be an exact translation of the Italian name of the opening: Giuoco Pianissimo). Perhaps Hikaru would have been better off trying to provoke something a bit sharper with } 11...d5 { here. As played, Magnus gradually works his way towards the sort of manoeuvring position he favours. } ) 12.Be3 { "I was pretty happy to get in Be3" (Carlsen) } 12...Ne7 13.Nh4 {[#]} Ng6 ( { When you've played h3, you always have to make sure you are in control if your opponent plays } 13...Bxh3 { . In this case, White's plan is to continue } 14.Bxa7 Rxa7 15.gxh3 Qxh3 16.Ng2 $1 ( 16.Nf3 $2 { loses to } 16...Ng6 $1 ) 16...Ng4 17.Re1 Qh2+ 18.Kf1 Qh3 { and now } 19.f3 $1 { secures White a winning advantage. Both players had foreseen this. However, the move played is also a little suspect as it cedes White a tiny plus. And a tiny plus is sometimes all Magnus Carlsen needs to work on. } ) 14.Nhf5 Ne7 15.Nxe7+ Qxe7 16.Bxa7 Rxa7 17.f4 c5 { "I think it's easier to play for White" (Carlsen). Nakamura later admitted he spent too much time thinking around moves 14 and 15 - a sign that he wasn't in his comfort zone. } 18.Bc2 $5 ( { Magnus said he was thinking about } 18.a4 { instead, while Vlad Kramnik expressed surprise that he should have retreated his bishop from b3. My analysis engine is a little surprised too. Perhaps it is these small, filigree differences between Magnus and the other top players which define his unique style. } ) 18...b5 19.Qd2 Rb7 20.a3 a5 21.Rf2 { [diagram] "I think this position should be fine. I'm a little confused as to what happened" (Nakamura). Incidentally, it was very good of Hikaru to come to the commentary room after his defeat, and honest of him to make such a candid confession. } 21...b4 22.axb4 axb4 23.Raf1 bxc3 24.bxc3 exf4 25.Rxf4 Nh7 26.d4 cxd4 27.cxd4 Qg5 28.Kh2 Nf6 29.Bd1 Rfb8 ( { "I guess it's losing. It's a little bit strange," said a bemused Nakamura in the commentary, still unable to understand quite why he lost. He had seen the exchange sacrifice coming but wasn't concerned about it during the game. It would be no consolation but chess engines don't seem to understand this position either. Nakamura wondered about } 29...Ra8 { so that if } 30.h4 { Black can play } 30...Qa5 { and offer a queen exchange. } ) ( 29...Ng4+ $5 { was another Nakamura suggestion in the commentary room. } ) 30.h4 Qg6 { [diagram] } 31.Rxf6 $1 { Not conclusive by any means but it complicates the game and gives Magnus the sort of game he likes to play. For the exchange, White has weakened the black pawns and he will have some dangerous piece play against Nakamura's king and queen. Meanwhile the two black rooks are some way from reaching posts where they can threaten retribution. } 31...gxf6 32.Qf4 ( 32.d5 Bd7 33.Qc3 { is what the computer advocates here but 'Deep Carlsen' has other ideas. } ) 32...Rb2 ( { Nakamura's instincts are to seek activity but with hindsight one wonders whether he might have been better off digging in with a move like } 32...Rd8 { . } ) 33.Bh5 Qg7 34.Bf3 Ra8 $2 ( { Hikaru starts to wilt under pressure of clock and position. } 34...Rd8 35.Nh5 Qg6 36.d5 Bc8 37.Nxf6+ { is good for White but Black is still in the game. } ) 35.d5 Bc8 36.Nh5 Qf8 ( 36...Qg6 37.Qxd6 { is also very difficult for Black. } ) 37.Nxf6+ Kh8 { [diagram] If you count the material, Black is still OK but the black king is very weak while its opposite number is rock solid. } 38.Rc1 $1 { It seems a little ironic, because White only has one rook to his opponent's two, but the c1 rook packs the more powerful punch. It's coming to c7, to threaten Rxf7 followed by Qxh6+ and mate, or Rc6 and simply Rxd6. } 38...Kg7 ( { If, for example, the a8 rook moves forward with } 38...Ra6 { , then White plays } 39.Rxc8 $1 { followed by 40 Qxh6 mate. } ) 39.e5 $1 { Opening the long diagonal but also giving his bishop the use of the e4 square. The black pieces find themselves out of play and the end is in sight. } 39...dxe5 40.Nh5+ ( 40.Qxe5 { wins in much the same way. } ) 40...Kh7 41.Be4+ ( 41.Be4+ Kg8 42.Qg3+ Kh8 43.Qxe5+ f6 44.Qxb2 { is crushing. } ) 1-0
Create game fragments when a comment [#] is encountered in the main line of the given 3 games.

Results:

Code: Select all

[Event "NH Chess 5th"]
[Site "Amsterdam NED"]
[Date "2010.08.15"]
[Round "4"]
[White "Svidler, P."]
[Black "Nakamura, Hi"]
[Result "*"]
[SetUp "1"]
[FEN "rn1qkbnr/pp2pppp/2p5/3pPb2/3P4/8/PPP2PPP/RNBQKBNR w KQkq - 0 1"]

1. h4 { [One of the more direct ways of meeting the Caro Kann. It has a
good database percentage and it is perhaps surprising it doesn't feature
more than 3 Nc3 or 3 Nf3.] } 1... h5 2. c4 e6 3. Nc3 Ne7 4. Nge2 (4. Bg5 {
has had significantly more outings. }) 4... dxc4 5. Ng3 Bg6 6. Bg5 Qb6 7.
Qd2 Qb4 ({ This queen move has been played previously by a chess legend but
may not be best. Black may need to look at something like } 7... Nd7 { in
order to revive this line. }) 8. a3 Qb3 9. Nge4 (9. Rc1 Nd5 10. Nce4 b5 11.
Be2 Nd7 12. O-O { was Adams-Karpov, Tilburg 1996, and led to a draw.
Svidler's innovation looks sharper. }) 9... Nd5 (9... Bxe4 10. Nxe4 Nd5 11.
Rh3 Qa4 12. Rc1 b5 { holds the c-pawn, though White has compensation in
terms of a kingside attack, e.g. } 13. Rf3) 10. Rh3 Qb6 11. Bxc4 Qa5 12.
Nd6+ { [With his lead in development, White doesn't have to commit himself
in this way, but he prefers to tighten his grip on the dark squares.] }
12... Bxd6 13. exd6 Nd7 14. Rc1 Nxc3 15. Rcxc3 Nf6 (15... O-O 16. Be7 Rfe8
17. Rhg3 { and now White has strong threats such as Bxe6 and Rxg6. }) 16.
b4 Qd8 (16... Qf5 17. Rhf3 { with } 17... Ne4 { . After the text, Black is
thrown onto the defensive. }) 17. Qf4 Kd7 (17... Bf5 18. Rhe3 O-O 19. Rg3 {
, White has a strong kingside attack. }) 18. Rhe3 Re8 19. b5 Qa5 20. Bxf6
gxf6 21. Kf1 Bf5 22. Be2 Rac8 23. Rc5 { [diagram] [From here it is all
one-way traffic.] } 23... Qa4 24. Rec3 a6 25. b6 { [The black queen is now
a prisoner in the enemy camp.] } 25... Bg6 26. Qxf6 e5 27. Rxe5 Rxe5 28.
dxe5 Re8 29. Re3 Re6 30. Qg5 Qd4 31. Kg1 Be4 ({ White threatens Bxh5 and if
} 31... Qxb6 32. Bc4 { will be decisive. }) 32. Qxh5 Rxe5 33. Qxf7+ Kxd6
34. Qxb7 *

[Event "NH Chess 5th"]
[Site "Amsterdam NED"]
[Date "2010.08.15"]
[Round "4"]
[White "Svidler, P."]
[Black "Nakamura, Hi"]
[Result "*"]
[SetUp "1"]
[FEN "rn2kb1r/pp2npp1/1qp1p1b1/4P1Bp/2pP3P/2N3N1/PP1Q1PP1/R3KB1R b KQkq - 0 1"]

1... Qb4 ({ This queen move has been played previously by a chess legend
but may not be best. Black may need to look at something like } 1... Nd7 {
in order to revive this line. }) 2. a3 Qb3 3. Nge4 (3. Rc1 Nd5 4. Nce4 b5
5. Be2 Nd7 6. O-O { was Adams-Karpov, Tilburg 1996, and led to a draw.
Svidler's innovation looks sharper. }) 3... Nd5 (3... Bxe4 4. Nxe4 Nd5 5.
Rh3 Qa4 6. Rc1 b5 { holds the c-pawn, though White has compensation in
terms of a kingside attack, e.g. } 7. Rf3) 4. Rh3 Qb6 5. Bxc4 Qa5 6. Nd6+ {
[With his lead in development, White doesn't have to commit himself in this
way, but he prefers to tighten his grip on the dark squares.] } 6... Bxd6
7. exd6 Nd7 8. Rc1 Nxc3 9. Rcxc3 Nf6 (9... O-O 10. Be7 Rfe8 11. Rhg3 { and
now White has strong threats such as Bxe6 and Rxg6. }) 10. b4 Qd8 (10...
Qf5 11. Rhf3 { with } 11... Ne4 { . After the text, Black is thrown onto
the defensive. }) 11. Qf4 Kd7 (11... Bf5 12. Rhe3 O-O 13. Rg3 { , White has
a strong kingside attack. }) 12. Rhe3 Re8 13. b5 Qa5 14. Bxf6 gxf6 15. Kf1
Bf5 16. Be2 Rac8 17. Rc5 { [diagram] [From here it is all one-way traffic.]
} 17... Qa4 18. Rec3 a6 19. b6 { [The black queen is now a prisoner in the
enemy camp.] } 19... Bg6 20. Qxf6 e5 21. Rxe5 Rxe5 22. dxe5 Re8 23. Re3 Re6
24. Qg5 Qd4 25. Kg1 Be4 ({ White threatens Bxh5 and if } 25... Qxb6 26. Bc4
{ will be decisive. }) 26. Qxh5 Rxe5 27. Qxf7+ Kxd6 28. Qxb7 *

[Event "World-ch"]
[Site "New York"]
[Date "2016.11.11"]
[Round "1"]
[White "Carlsen, Magnus"]
[Black "Karjakin, Sergey"]
[Result "*"]
[SetUp "1"]
[FEN "rnbqkb1r/pp2pp1p/5p2/2pp4/3P4/4P3/PPP2PPP/RN1QKBNR w KQkq - 0 1"]

1. dxc5 (1. Nc3 Nc6 2. Qh5) (1. c3 Qb6 (1... Nc6)) 1... Nc6 { comment } 2.
Bb5 e6 { comment } 3. c4 dxc4 4. Nd2 Bxc5 5. Ngf3 O-O 6. O-O Na5 7. Rc1 Be7
8. Qc2 Bd7 9. Bxd7 Qxd7 10. Qc3 Qd5 11. Nxc4 Nxc4 12. Qxc4 Qxc4 13. Rxc4
Rfc8 14. Rfc1 Rxc4 15. Rxc4 Rd8 16. g3 Rd7 17. Kf1 f5 18. Ke2 Bf6 19. b3
Kf8 20. h3 h6 21. Ne1 Ke7 22. Nd3 Kd8 23. f4 h5 24. a4 Rd5 25. Nc5 b6 26.
Na6 Be7 27. Nb8 a5 28. Nc6+ Ke8 29. Ne5 Bc5 30. Rc3 Ke7 31. Rd3 Rxd3 32.
Kxd3 f6 33. Nc6+ Kd6 34. Nd4 Kd5 35. Nb5 Kc6 36. Nd4+ Kd6 37. Nb5+ Kd7 38.
Nd4 Kd6 *

[Event "World-ch"]
[Site "New York"]
[Date "2016.11.11"]
[Round "1"]
[White "Carlsen, Magnus"]
[Black "Karjakin, Sergey"]
[Result "*"]
[SetUp "1"]
[FEN "r1bq1rk1/pp2bp1p/4pp2/nB6/2p5/4PN2/PPQN1PPP/2R2RK1 b - - 0 1"]

1... Bd7 2. Bxd7 Qxd7 3. Qc3 Qd5 4. Nxc4 Nxc4 5. Qxc4 Qxc4 6. Rxc4 Rfc8 7.
Rfc1 Rxc4 8. Rxc4 Rd8 9. g3 Rd7 10. Kf1 f5 11. Ke2 Bf6 12. b3 Kf8 13. h3 h6
14. Ne1 Ke7 15. Nd3 Kd8 16. f4 h5 17. a4 Rd5 18. Nc5 b6 19. Na6 Be7 20. Nb8
a5 21. Nc6+ Ke8 22. Ne5 Bc5 23. Rc3 Ke7 24. Rd3 Rxd3 25. Kxd3 f6 26. Nc6+
Kd6 27. Nd4 Kd5 28. Nb5 Kc6 29. Nd4+ Kd6 30. Nb5+ Kd7 31. Nd4 Kd6 *

[Event "London Chess Classic"]
[Site "London"]
[Date "2011.12.05"]
[Round "3"]
[White "Carlsen, Magnus"]
[Black "Nakamura, Hikaru"]
[Result "*"]
[SetUp "1"]
[FEN "r2q1rk1/bpp2pp1/p1npbn1p/4p3/4P3/1BPP1NNP/PP3PP1/R1BQ1RK1 b - - 0 1"]

1... Qd7 ({ Of course, Carlsen plays his usual ultra-quiet game (which
happens to be an exact translation of the Italian name of the opening:
Giuoco Pianissimo). Perhaps Hikaru would have been better off trying to
provoke something a bit sharper with } 1... d5 { here. As played, Magnus
gradually works his way towards the sort of manoeuvring position he
favours. }) 2. Be3 { "I was pretty happy to get in Be3" (Carlsen) } 2...
Ne7 3. Nh4 Ng6 ({ When you've played h3, you always have to make sure you
are in control if your opponent plays } 3... Bxh3 4. Bxa7 Rxa7 5. gxh3 Qxh3
6. Ng2 (6. Nf3 Ng6) 6... Ng4 7. Re1 Qh2+ 8. Kf1 Qh3 { and now } 9. f3 {
secures White a winning advantage. Both players had foreseen this. However,
the move played is also a little suspect as it cedes White a tiny plus. And
a tiny plus is sometimes all Magnus Carlsen needs to work on. }) 4. Nhf5
Ne7 5. Nxe7+ Qxe7 6. Bxa7 Rxa7 7. f4 c5 { "I think it's easier to play for
White" (Carlsen). Nakamura later admitted he spent too much time thinking
around moves 14 and 15 - a sign that he wasn't in his comfort zone. } 8.
Bc2 ({ Magnus said he was thinking about } 8. a4 { instead, while Vlad
Kramnik expressed surprise that he should have retreated his bishop from
b3. My analysis engine is a little surprised too. Perhaps it is these
small, filigree differences between Magnus and the other top players which
define his unique style. }) 8... b5 9. Qd2 Rb7 10. a3 a5 11. Rf2 {
[diagram] "I think this position should be fine. I'm a little confused as
to what happened" (Nakamura). Incidentally, it was very good of Hikaru to
come to the commentary room after his defeat, and honest of him to make
such a candid confession. } 11... b4 12. axb4 axb4 13. Raf1 bxc3 14. bxc3
exf4 15. Rxf4 Nh7 16. d4 cxd4 17. cxd4 Qg5 18. Kh2 Nf6 19. Bd1 Rfb8 ({ "I
guess it's losing. It's a little bit strange," said a bemused Nakamura in
the commentary, still unable to understand quite why he lost. He had seen
the exchange sacrifice coming but wasn't concerned about it during the
game. It would be no consolation but chess engines don't seem to understand
this position either. Nakamura wondered about } 19... Ra8 20. h4 { Black
can play } 20... Qa5 { and offer a queen exchange. }) (19... Ng4+ { was
another Nakamura suggestion in the commentary room. }) 20. h4 Qg6 {
[diagram] } 21. Rxf6 { Not conclusive by any means but it complicates the
game and gives Magnus the sort of game he likes to play. For the exchange,
White has weakened the black pawns and he will have some dangerous piece
play against Nakamura's king and queen. Meanwhile the two black rooks are
some way from reaching posts where they can threaten retribution. } 21...
gxf6 22. Qf4 (22. d5 Bd7 23. Qc3 { is what the computer advocates here but
'Deep Carlsen' has other ideas. }) 22... Rb2 ({ Nakamura's instincts are to
seek activity but with hindsight one wonders whether he might have been
better off digging in with a move like } 22... Rd8 { . }) 23. Bh5 Qg7 24.
Bf3 Ra8 ({ Hikaru starts to wilt under pressure of clock and position. }
24... Rd8 25. Nh5 Qg6 26. d5 Bc8 27. Nxf6+ { is good for White but Black is
still in the game. }) 25. d5 Bc8 26. Nh5 Qf8 (26... Qg6 27. Qxd6 { is also
very difficult for Black. }) 27. Nxf6+ Kh8 { [diagram] If you count the
material, Black is still OK but the black king is very weak while its
opposite number is rock solid. } 28. Rc1 { It seems a little ironic,
because White only has one rook to his opponent's two, but the c1 rook
packs the more powerful punch. It's coming to c7, to threaten Rxf7 followed
by Qxh6+ and mate, or Rc6 and simply Rxd6. } 28... Kg7 ({ If, for example,
the a8 rook moves forward with } 28... Ra6 29. Rxc8 { followed by 40 Qxh6
mate. }) 29. e5 { Opening the long diagonal but also giving his bishop the
use of the e4 square. The black pieces find themselves out of play and the
end is in sight. } 29... dxe5 30. Nh5+ (30. Qxe5 { wins in much the same
way. }) 30... Kh7 31. Be4+ (31. Be4+ Kg8 32. Qg3+ Kh8 33. Qxe5+ f6 34. Qxb2
{ is crushing. }) *

[Event "London Chess Classic"]
[Site "London"]
[Date "2011.12.05"]
[Round "3"]
[White "Carlsen, Magnus"]
[Black "Nakamura, Hikaru"]
[Result "*"]
[SetUp "1"]
[FEN "r4rk1/bppqnpp1/p2pbn1p/4p3/4P2N/1BPPB1NP/PP3PP1/R2Q1RK1 b - - 0 1"]

1... Ng6 ({ When you've played h3, you always have to make sure you are in
control if your opponent plays } 1... Bxh3 2. Bxa7 Rxa7 3. gxh3 Qxh3 4. Ng2
(4. Nf3 Ng6) 4... Ng4 5. Re1 Qh2+ 6. Kf1 Qh3 { and now } 7. f3 { secures
White a winning advantage. Both players had foreseen this. However, the
move played is also a little suspect as it cedes White a tiny plus. And a
tiny plus is sometimes all Magnus Carlsen needs to work on. }) 2. Nhf5 Ne7
3. Nxe7+ Qxe7 4. Bxa7 Rxa7 5. f4 c5 { "I think it's easier to play for
White" (Carlsen). Nakamura later admitted he spent too much time thinking
around moves 14 and 15 - a sign that he wasn't in his comfort zone. } 6.
Bc2 ({ Magnus said he was thinking about } 6. a4 { instead, while Vlad
Kramnik expressed surprise that he should have retreated his bishop from
b3. My analysis engine is a little surprised too. Perhaps it is these
small, filigree differences between Magnus and the other top players which
define his unique style. }) 6... b5 7. Qd2 Rb7 8. a3 a5 9. Rf2 { [diagram]
"I think this position should be fine. I'm a little confused as to what
happened" (Nakamura). Incidentally, it was very good of Hikaru to come to
the commentary room after his defeat, and honest of him to make such a
candid confession. } 9... b4 10. axb4 axb4 11. Raf1 bxc3 12. bxc3 exf4 13.
Rxf4 Nh7 14. d4 cxd4 15. cxd4 Qg5 16. Kh2 Nf6 17. Bd1 Rfb8 ({ "I guess it's
losing. It's a little bit strange," said a bemused Nakamura in the
commentary, still unable to understand quite why he lost. He had seen the
exchange sacrifice coming but wasn't concerned about it during the game. It
would be no consolation but chess engines don't seem to understand this
position either. Nakamura wondered about } 17... Ra8 18. h4 { Black can
play } 18... Qa5 { and offer a queen exchange. }) (17... Ng4+ { was another
Nakamura suggestion in the commentary room. }) 18. h4 Qg6 { [diagram] } 19.
Rxf6 { Not conclusive by any means but it complicates the game and gives
Magnus the sort of game he likes to play. For the exchange, White has
weakened the black pawns and he will have some dangerous piece play against
Nakamura's king and queen. Meanwhile the two black rooks are some way from
reaching posts where they can threaten retribution. } 19... gxf6 20. Qf4 (
20. d5 Bd7 21. Qc3 { is what the computer advocates here but 'Deep Carlsen'
has other ideas. }) 20... Rb2 ({ Nakamura's instincts are to seek activity
but with hindsight one wonders whether he might have been better off
digging in with a move like } 20... Rd8 { . }) 21. Bh5 Qg7 22. Bf3 Ra8 ({
Hikaru starts to wilt under pressure of clock and position. } 22... Rd8 23.
Nh5 Qg6 24. d5 Bc8 25. Nxf6+ { is good for White but Black is still in the
game. }) 23. d5 Bc8 24. Nh5 Qf8 (24... Qg6 25. Qxd6 { is also very
difficult for Black. }) 25. Nxf6+ Kh8 { [diagram] If you count the
material, Black is still OK but the black king is very weak while its
opposite number is rock solid. } 26. Rc1 { It seems a little ironic,
because White only has one rook to his opponent's two, but the c1 rook
packs the more powerful punch. It's coming to c7, to threaten Rxf7 followed
by Qxh6+ and mate, or Rc6 and simply Rxd6. } 26... Kg7 ({ If, for example,
the a8 rook moves forward with } 26... Ra6 27. Rxc8 { followed by 40 Qxh6
mate. }) 27. e5 { Opening the long diagonal but also giving his bishop the
use of the e4 square. The black pieces find themselves out of play and the
end is in sight. } 27... dxe5 28. Nh5+ (28. Qxe5 { wins in much the same
way. }) 28... Kh7 29. Be4+ (29. Be4+ Kg8 30. Qg3+ Kh8 31. Qxe5+ f6 32. Qxb2
{ is crushing. }) *
JohnS
Posts: 215
Joined: Sun Feb 24, 2008 2:08 am

Re: Tool to extract parts of games from pgn file

Post by JohnS »

Ferdy

Wow - this is exactly what I want. Which tool did you use?

Thanks.
John
Ferdy
Posts: 4833
Joined: Sun Aug 10, 2008 3:15 pm
Location: Philippines

Re: Tool to extract parts of games from pgn file

Post by Ferdy »

JohnS wrote:Ferdy

Wow - this is exactly what I want. Which tool did you use?

Thanks.
John
It is a python script using python-chess modules. The script output is then run under pgn-extract to comply with pgn standard.

Limitations:
You can only create a game fragment on the main line (not on the variation) by adding a comment {[#]} after a main line move see uploaded src.pgn below.

Try this exe below, I will later upload the python source in github after some code cleanup, documentations and modifications depending on your comments.

Note that exe is only capable of reading up to a max of 3 variations deep but 99 variations per depth level that is,

Code: Select all

main_line .. move1 move2 {[#]} move3 move4
    (1-first_variation (1-second_variation (1-third_variation)))
    (1-first_variation (1-second_variation (2-third_variation)))
    (1-first_variation (1-second_variation (99-third_variation)))
    ...
    (1-first_variation (2-second_variation (1-third_variation)))
    (1-first_variation (99-second_variation (99-third_variation)))
    ...
    (2-first_variation (1-second_variation (1-third_variation)))
    (99-first_variation (99-second_variation (99-third_variation)))
continue_main_line move5 move6 {[#]} move7
    (variations again)
...
How to use?

* prepare src.pgn - your pgn file with games
* prepare pgn-extract.exe
* gfe.exe

Run gfe.exe and redirect its output to a file say t1.pgn see example below.
Run pgn-extract on t1.pgn for reformatting and output it to say final.pgn

Image

Download gfe.exe and sample src.pgn
https://drive.google.com/file/d/0BwAOsu ... sp=sharing
JohnS
Posts: 215
Joined: Sun Feb 24, 2008 2:08 am

Re: Tool to extract parts of games from pgn file

Post by JohnS »

Ferdy

Many thanks for this tool. I will test it and report back if there are any issues.

Here is a simple batch file to run the tool.

rem run gfe on input file %1.pgn
set tempfile=temp.pgn
copy %1.pgn src.pgn /y
gfe > %tempfile%
pgn-extract --output %1-ext.pgn %tempfile%
del temp.pgn src.pgn

Save this as g.bat (or any name you like). To run the batch file on the input file in.pgn, open a command prompt, go to the appropriate directory and use the command

g in <enter>

This gives the extracted file in-ext.pgn as output.

Note that this overwrites any existing files src.pgn and temp.pgn! All files should exist in the directory where the batch file is.

Please feel free to include this batch file (or a modified version of it) in any future release of the tool.

Thanks.
John
JohnS
Posts: 215
Joined: Sun Feb 24, 2008 2:08 am

Re: Tool to extract parts of games from pgn file

Post by JohnS »

Ferdy

Is it possible to leave the game result as it is in the input file rather than change it to "*"? Otherwise the tool gives a warning when a game ends in checkmate.

Here's an example that shows the issue.

[Event "?"]
[Site "?"]
[Date "2017.03.19"]
[Round "?"]
[White "W"]
[Black "B"]
[Result "1-0"]
[ECO "A02"]
[PlyCount "4"]
[EventDate "2017.??.??"]

1. f4 e5 {[#]} 2. g4 Qh4# 1-0

It may be safer to let the user specify the input file rather than using the hard-coded name src.pgn. The call statement could be

gfe infile outfile

or

gfe infile > outfile

Thanks.
John
Ferdy
Posts: 4833
Joined: Sun Aug 10, 2008 3:15 pm
Location: Philippines

Re: Tool to extract parts of games from pgn file

Post by Ferdy »

JohnS wrote:Ferdy

Is it possible to leave the game result as it is in the input file rather than change it to "*"? Otherwise the tool gives a warning when a game ends in checkmate.

Here's an example that shows the issue.

[Event "?"]
[Site "?"]
[Date "2017.03.19"]
[Round "?"]
[White "W"]
[Black "B"]
[Result "1-0"]
[ECO "A02"]
[PlyCount "4"]
[EventDate "2017.??.??"]

1. f4 e5 {[#]} 2. g4 Qh4# 1-0

It may be safer to let the user specify the input file rather than using the hard-coded name src.pgn. The call statement could be

gfe infile outfile

or

gfe infile > outfile

Thanks.
John
From readme.

Code: Select all

1. Name
Game Fragment Extractor

2. Description
A script and exe file that will extract a game fragment based on
a full chess game records or pgn file and a user specified marker
comment in the main line of the game. The script is created from
an idea of John Smith who searches a tool to output such records.

Example,
1.d4 Nf6 2.Bg5 d5 3.e3 c5 4.Bxf6 gxf6 &#123;&#91;#&#93;&#125; 5.dxc5 ( 5.Nc3 Nc6 6.Qh5 )...
The comment &#91;#&#93; is the marker that would tell the extractor where to
start extracting the game. See included sample.pgn file.

The python script is written under version 2.7.11 and was tested
on python-chess version 0.17.0. For more information see the
beginning part of source file gfe.py.

3. External files required
pgn-extract.exe
This file can be found at&#58;
https&#58;//www.cs.kent.ac.uk/people/staff/djb/pgn-extract/

4. Usage
gfe.exe -i <your input pgn file> -o <your output pgn file>

or


gfe.exe --input <your input pgn file> --output <your output pgn file>
Released in github.

https://github.com/fsmosca/GameFragment ... r/releases
JohnS
Posts: 215
Joined: Sun Feb 24, 2008 2:08 am

Re: Tool to extract parts of games from pgn file

Post by JohnS »

Ferdy

Many thanks for the new version.

John