On that do you also want to remove the comment before the A0?.
Yes I would like to remove also the comment before the eco code. I run the tactical analysis with the setting to 0, to make it fast. So the analysis is not trustworthy. Can you please make it also possible to not remove the remaining moves? If I want to delete the remaining moves I can do it myself by placing a * after the comment with the opening name.
Jonathan003 wrote: ↑Sun Mar 17, 2019 6:44 pm
Yes I would like to remove also the comment before the eco code.
Will try that.
I run the tactical analysis with the setting to 0, to make it fast. So the analysis is not trustworthy. Can you please make it also possible to not remove the remaining moves? If I want to delete the remaining moves I can do it myself by placing a * after the comment with the opening name.
Can you give an example? Want to see this remaining moves in pgn.
You already have chessbase. May I know what do you want to achieve with all of these?
"""
pgn_modifier.py
Read pgn and remove some comments and cut the move length at specific move.
Requirements:
python 3
python-chess v0.26.0 and up
Dev log:
v0.3
* Remove all text in the move comment before the ECO code. Example,
{ White is slightly better. A67: Modern Benoni: Taimanov Variation }
Remove "White is slightly better."
v0.2
* Read ECO letter + single digit, that is A1, B2 etc.
* Prompt user of pgn file and the output is 'output_[user pgn filename].
"""
import os
import chess.pgn
def main():
pgnfn = input('enter pgn filename? ')
output_pgnfn = 'output_' + pgnfn
# Delete existing output_mygames.pgn
if os.path.isfile(output_pgnfn):
os.remove(output_pgnfn)
# Build ECO, used to cut the game length
ECO = []
for i in range(100):
# A00 to E99, A0, A1 ... A9 ... E9
if i < 10:
aecos = 'A{}'.format(i)
becos = 'B{}'.format(i)
cecos = 'C{}'.format(i)
decos = 'D{}'.format(i)
eecos = 'E{}'.format(i)
aeco = 'A{:02d}'.format(i)
beco = 'B{:02d}'.format(i)
ceco = 'C{:02d}'.format(i)
deco = 'D{:02d}'.format(i)
eeco = 'E{:02d}'.format(i)
ECO.append(aecos)
ECO.append(becos)
ECO.append(cecos)
ECO.append(decos)
ECO.append(eecos)
ECO.append(aeco)
ECO.append(beco)
ECO.append(ceco)
ECO.append(deco)
ECO.append(eeco)
gcnt = 0
# Parse games in pgn file
with open(pgnfn, 'r') as pgn:
game = chess.pgn.read_game(pgn)
while game:
gcnt += 1
# Console progress
print('game: {}'.format(gcnt))
mygame = chess.pgn.Game()
mynode = mygame
# Copy orig game header to our saved game header
for k, v in game.headers.items():
mygame.headers[k] = v
for node in game.mainline():
game_move = node.move
move_comment = node.comment
# Modify move comment, remove comments enclosed in [ ]
# [%emt 0:00:05] A87: Dutch Defence: Leningrad System: Nf3
if '[' in move_comment and ']' in move_comment:
move_comment = move_comment.split(']')[1].strip()
mynode = mynode.add_main_variation(game_move)
# Check the move comment where we can cut the game length
# Also remove comments before ECO if there is ECO
is_foundvalidcomment = False
newcom = move_comment
for c in ECO:
if c in move_comment:
is_foundvalidcomment = True
# Remove comments to the left of ECO
# White is slightly better. A67: Modern Benoni: Taimanov Variation
eco_com = move_comment.split(':')[0].split()[-1].strip()
newcom = eco_com + ':' + move_comment.split(eco_com + ':')[1]
break
if is_foundvalidcomment:
mynode.comment = newcom
# Don't copy other moves we stop here
if is_foundvalidcomment:
break
# Print to output file
with open(output_pgnfn, 'a') as w:
w.write('{}\n\n'.format(mygame))
# Print to console
print(mygame)
print()
# Read another game
game = chess.pgn.read_game(pgn)
if __name__ == '__main__':
main()
Can you give an example? Want to see this remaining moves in pgn.
You already have chessbase. May I know what do you want to achieve with all of these?
I'm very busy with finding best ways to make and train repertoires. I divide the repertoires in small pgn files ordered by opening. Than I make repertoires in chesstempo opening trainer beta. In chesstempo opening trainer beta it is possible to set bookmarks. That's why I want to use these annotations with the opening names. I first want to cut the repertoires eco related length. I want it eco related lengt, and not just cut the games after 12 ply's or so. Later I want to train the repertoires more ply's deep. That's why I also want an option to keep the remaining moves after the annotations with the opening name.
I will try the latest codes you send with bigger pgn files to see the results. Thanks already for all of your effort!
There where 4 games out of the 345 games that did not get an annotation with the opening name for the white repertoire.
And there where 10 games out of the 839 games that did not get an annotation with the opening name for the black repertoire.
To invest it I did the following. I converted the repertoires to cbh in chessbase 15. I created a search booster. And I searched for all games with a tex annotation, and deleted these. So the remaining games where the games with no annotation with the eco code and opening names. I called these invest_white.pgn and invest_black.pgn
I repeated the process on these files. I first created tactical analysis. And then I run the pgn modifier again. Strange enough there are now less games with no opening annotation. But there is still 1 for output_invest_white.pgn and 4 for output_invest_black.pgn
I post the results here.
invest_white.pgn
[Event "Chess Position Trainer"]
[Site "Chess Position Trainer"]
[Date "????.??.??"]
[Round "?"]
[White "My White Opening"]
[Black "Black Opening"]
[Result "*"]
[ECO "D10"]
[Annotator "Tactical Analysis 2.10 (6s)"]
[PlyCount "27"]
I'm happy with the results. If I also want the moves after the opening annotations I can do it by importing and exporting in chess position trainer. And for the few games without a opening annotation I can do it manually.
So thank you very much Ferdy for helping me out.
Jonathan003 wrote: ↑Sun Mar 17, 2019 10:45 pm
I'm happy with the results. If I also want the moves after the opening annotations I can do it by importing and exporting in chess position trainer. And for the few games without a opening annotation I can do it manually.
So thank you very much Ferdy for helping me out.
Parsing of whatever comments in the game is getting complicated now. I thought about a new method.
So is this your process?
1. You have a game, not necessarily a complete game. No comments, variations and NAGS etc.
2. Identify its ECO code and name/variation
3. Add the ECO code and name/variation as comment at appropriate move numbers.
If that is so I thought about doing like this.
1. Get the game
2. Get the eco.pgn (pgn-extract site has this, maybe same as what Dann has shared except Dann has the required pgn 7 tags which is better).
3. Read the game moves in reverse
4. If position in the game has a match to the position found in eco.pgn then insert that ECO and name/variation as comment to the last move in the game where the match is found.