@@ -448,51 +448,52 @@ def classify_opening(game):
448448 Returns the classified game and root_node, which is the node where the
449449 classification was made
450450 """
451- ecofile = os .path .join (os .path .dirname (__file__ ), 'eco/eco.json' )
452- ecodata = json .load (open (ecofile , 'r' ))
451+ ecopath = os .path .join (os .path .dirname (__file__ ), 'eco/eco.json' )
452+ with open (ecopath , 'r' ) as ecofile :
453+ ecodata = json .load (ecofile )
453454
454- ply_count = 0
455+ ply_count = 0
455456
456- root_node = game .root ()
457- node = game .end ()
457+ root_node = game .root ()
458+ node = game .end ()
458459
459- # Opening classification for variant games is not implemented (yet?)
460- is_960 = root_node .board ().chess960
461- if is_960 :
462- variant = "chess960"
463- else :
464- variant = type (node .board ()).uci_variant
460+ # Opening classification for variant games is not implemented (yet?)
461+ is_960 = root_node .board ().chess960
462+ if is_960 :
463+ variant = "chess960"
464+ else :
465+ variant = type (node .board ()).uci_variant
465466
466- if variant != "chess" :
467- logger .info ("Skipping opening classification in variant "
468- "game: {}" .format (variant ))
469- return node .root (), root_node , game_length (game )
467+ if variant != "chess" :
468+ logger .info ("Skipping opening classification in variant "
469+ "game: {}" .format (variant ))
470+ return node .root (), root_node , game_length (game )
470471
471- logger .info ("Classifying the opening for non-variant {} "
472- "game..." .format (variant ))
472+ logger .info ("Classifying the opening for non-variant {} "
473+ "game..." .format (variant ))
473474
474- while not node == game .root ():
475- prev_node = node .parent
476-
477- fen = eco_fen (node .board ())
478- classification = classify_fen (fen , ecodata )
479-
480- if classification ["code" ] != "" :
481- # Add some comments classifying the opening
482- node .root ().headers ["ECO" ] = classification ["code" ]
483- node .root ().headers ["Opening" ] = classification ["desc" ]
484- node .comment = "{} {}" .format (classification ["code" ],
485- classification ["desc" ])
486- # Remember this position so we don't analyze the moves preceding it
487- # later
488- root_node = node
489- # Break (don't classify previous positions)
490- break
475+ while not node == game .root ():
476+ prev_node = node .parent
491477
492- ply_count += 1
493- node = prev_node
478+ fen = eco_fen (node .board ())
479+ classification = classify_fen (fen , ecodata )
480+
481+ if classification ["code" ] != "" :
482+ # Add some comments classifying the opening
483+ node .root ().headers ["ECO" ] = classification ["code" ]
484+ node .root ().headers ["Opening" ] = classification ["desc" ]
485+ node .comment = "{} {}" .format (classification ["code" ],
486+ classification ["desc" ])
487+ # Remember this position so we don't analyze the moves
488+ # preceding it later
489+ root_node = node
490+ # Break (don't classify previous positions)
491+ break
492+
493+ ply_count += 1
494+ node = prev_node
494495
495- return node .root (), root_node , ply_count
496+ return node .root (), root_node , ply_count
496497
497498
498499def add_acpl (game , root_node ):
0 commit comments