How to get an opening name from a PGN/SAN?

Viewed 74

I have a list of chess moves, for example, "e4 c5 Nf3 d6 d4 cxd4 Nxd4 Nf6 Nc3 g6" (I can convert from SAN to PGN or UCI, so don't worry about that). From these moves, I want to get the name of the opening, in this example "Sicilian Defense: Dragon Variation". This functionality is essentially identical to the one you'll find in sites like https://lichess.org/analysis#explorer, https://www.chess.com/analysis, and https://www.365chess.com/opening.php.

The current approach I have implemented involves making in my case very many requests to the Lichess Masters database via https://explorer.lichess.ovh/masters (see https://lichess.org/api#operation/openingExplorerMaster). However, the problem is that I have to make a very high number of requests, making this approach not viable.

I'm working with Python, so I'd definitely prefer that the solution would be in Python.

1 Answers

Method 1

Use pgn-extract

  • Download the eco.pgn
  • Download the pgn-extract

Sample command line:

pgn-extract.exe -e -omyoutput.pgn mygames.pgn

The eco.pgn should be located in the same folder with pgn-extract.exe and your input mygames.pgn.

Method 2

Use pgnhelper this is a python package I wrote to add eco (and others) to games in the pgn file.

  • pip install pgnhelper
  • Download the eco.pgn at from github repo. or you can use the eco.pgn from pgn-extract.

Sample command line:

pgnhelper addeco --inpgnfn manila1974.pgn --outpgnfn out_manila1974.pgn --inecopgnfn eco.pgn

pgnhelper is unique, it saves Two ECO, Two Opening name and 2 Variation names if available.

Example:

[ECO "A15"]
[ECOT "D90"]
[Opening "English"]
[OpeningT "Gruenfeld"]
[VariationT "Three knights variation"]

The T means Transposition. In the first 2 moves or 4 plies it records [Opening "English"] and in the next 12 moves it will record [OpeningT "Gruenfeld"]. Most softwares I know is only recording opening names by transposition so the [OpeningT "Gruenfeld"] in pgnhelper is only [Opening "Gruenfeld"] in other software output.

Also I created an eco github repo and make some modifications, like changing the QGD opening tag value to Queen's Gambit Declined for clarity to user, supplying missing variation names using the name of player that first make the move based from earliest recorded game, and others. The eco.pgn in github repo supplied for pgnhelper is coming from this repo.

pgnhelper source in github

Related