Skip to content

Conversation

@cristivlas
Copy link

@cristivlas cristivlas commented Dec 12, 2023

I think the 1st move in the English is always C4, but not sure about the rest of the moves, I generated the transposed PGN with this quick and dirty script:

import chess
import chess.pgn
import copy
import io

def find_transposition_sequence(pgn_moves, transposition):
    pgn_board = chess.Board()
    game = chess.pgn.read_game(io.StringIO(pgn_moves))

    for move in game.mainline_moves():
        pgn_board.push(move)
    
    move_stack_copy = copy.deepcopy(pgn_board.move_stack)
    transposition_board = chess.Board()

    for san in transposition:
        try:
            move = transposition_board.push_san(san)
        except:
            continue
        if move in move_stack_copy:
            move_stack_copy.remove(move)
        else:
            transposition_board.pop()
        
    def search(board, remaining_moves):
        if board.board_fen() == pgn_board.board_fen():
            game = chess.pgn.Game.from_board(board)
            exporter = chess.pgn.StringExporter(headers=False, variations=False, comments=False, columns=None)
            return game.accept(exporter).rstrip('* ')

        for move in remaining_moves:
            if move in board.legal_moves:
                board.push(move)
                next_moves = remaining_moves[:]
                next_moves.remove(move)
                result = search(board, next_moves)
                if result:
                    return result
                board.pop()

        return None

    # Search for a sequence that leads to the same FEN
    transposition_game = search(transposition_board, move_stack_copy)

    return str(transposition_game) if transposition_game else ""


with open('a.tsv') as f:
    for line in f:
        line = line.strip()
        opening = line.split('\t')
        if opening[1].startswith('English Opening'):
            pgn = opening[-1]
            trans = find_transposition_sequence(pgn, ['c4', 'e6'])
            if trans:
                opening[-1] = trans
            line = '\t'.join(opening)

        print(line)

@cmgchess
Copy link
Contributor

…ish Opening: Anglo-Indian Defense, Nimzo-English)
@niklasf niklasf merged commit 40e56cb into lichess-org:master Dec 15, 2023
@niklasf
Copy link
Member

niklasf commented Dec 15, 2023

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants