Automatic detection of chess tactical themes in PGN games
Project description
chess_detect
Automatic detection of chess tactical themes in PGN games using python-chess.
Analyzes PGN files and annotates moves with tactical motifs: forks, pins, skewers, discovered checks, and more. Supports Russian and English output with visual PGN arrow annotations.
Installation
pip install chess-detect
For development:
git clone https://github.com/aslyamov/chess_detect.git
cd chess_detect
pip install -e ".[dev]"
Quick Start
from chess_detect import ChessDetector
detector = ChessDetector(lang="en") # or lang="ru"
pgn = """
1. e4 e5 2. Nf3 Nc6 3. Bb5 a6
"""
result = detector.analyze(pgn)
print(result)
# PGN with tactical annotations in comments
Supported Tactics
| Theme | Russian | English |
|---|---|---|
| Double Check | Двойной шах | Double Check |
| Fork | Вилка | Fork |
| Discovered Check | Вскрытый шах | Discovered Check |
| Pin | Связка | Pin |
| Skewer | Копьё | Skewer |
| Trapped Piece | Пойманная фигура | Trapped Piece |
| Hanging Capture | Взятие висящей фигуры | Hanging Capture |
| Removing Defender (material) | Уничтожение защитника (выигрыш фигуры) | Removing Defender (winning material) |
| Removing Defender (mate) | Уничтожение защитника (угроза мата) | Removing Defender (mate threat) |
| Exploiting Pin | Использование связки | Exploiting Pin |
Planned
- Deflection
- Decoy
- Zwischenzug
- Discovered Attack
- Back Rank Mate
- Smothered Mate
PGN Arrow Annotations
Detected tactics are visualized with green arrows ([%cal]) and square highlights ([%csl]) in the PGN output. These annotations are displayed by chess viewers like Lichess and chess.com.
Example output:
1. Nc7+ { [%csl Ga8,Ge8][%cal Gc7a8,Gc7e8] Fork (Nc7 → Qa8, Ke8) }
Architecture
chess_detect/
├── __init__.py # Exports ChessDetector
├── detector.py # Main class
├── context.py # MoveContext — lazy evaluation
├── utils.py # Utilities (piece values, vulnerability checks)
├── i18n/ # Translations
│ ├── ru.py
│ └── en.py
└── detectors/
├── base.py # BaseDetector
└── tactics/
├── double_check.py
├── fork.py
├── discovered_check.py
├── pin.py
├── skewer.py
├── trapped.py
├── hanging_capture.py
├── removing_defender.py
└── exploiting_pin.py
Adding a New Detector
from ..base import BaseDetector
from ...context import MoveContext
class MyTacticDetector(BaseDetector):
key = "my_tactic" # i18n key
def detect(self, ctx: MoveContext) -> bool:
# ctx.board_before — position before the move
# ctx.board_after — position after the move
# ctx.move — the move itself
# ctx.is_check — is it check?
# ctx.is_capture — is it a capture?
# ctx.captured_piece — captured piece
return False
- Create a file in
detectors/tactics/ - Add translations to
i18n/ru.pyandi18n/en.py - Register in
detector.py
Tests
python -m pytest tests/ -v
89 tests — all passing.
License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file chess_detect-0.1.1.tar.gz.
File metadata
- Download URL: chess_detect-0.1.1.tar.gz
- Upload date:
- Size: 33.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5bc53927cd380fa0153dfd6624549d7817c04fd6c32829c7675cbbaa2f17294b
|
|
| MD5 |
fae88f87f2248b0a186895bf7cf9710f
|
|
| BLAKE2b-256 |
3c9f5aee9a6676a7f99a0d05aa109ea82d53f566ed90066600a4fea638c53c06
|
Provenance
The following attestation bundles were made for chess_detect-0.1.1.tar.gz:
Publisher:
publish.yml on aslyamov/chess_detect
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
chess_detect-0.1.1.tar.gz -
Subject digest:
5bc53927cd380fa0153dfd6624549d7817c04fd6c32829c7675cbbaa2f17294b - Sigstore transparency entry: 926955001
- Sigstore integration time:
-
Permalink:
aslyamov/chess_detect@c0112f3d3761cce8010305fbb55fac01bff8a463 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/aslyamov
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c0112f3d3761cce8010305fbb55fac01bff8a463 -
Trigger Event:
push
-
Statement type:
File details
Details for the file chess_detect-0.1.1-py3-none-any.whl.
File metadata
- Download URL: chess_detect-0.1.1-py3-none-any.whl
- Upload date:
- Size: 29.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3d3810e0724d5dc193eba01b6b9f527b55b5f8e19f7c814aff1a23f55640fde1
|
|
| MD5 |
4f416334f24cc30e549226e194903ff0
|
|
| BLAKE2b-256 |
c93fbb82fff693d6ebb949880288b45f26a71f2b23f77e79f63ae7e3760f6f2e
|
Provenance
The following attestation bundles were made for chess_detect-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on aslyamov/chess_detect
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
chess_detect-0.1.1-py3-none-any.whl -
Subject digest:
3d3810e0724d5dc193eba01b6b9f527b55b5f8e19f7c814aff1a23f55640fde1 - Sigstore transparency entry: 926955006
- Sigstore integration time:
-
Permalink:
aslyamov/chess_detect@c0112f3d3761cce8010305fbb55fac01bff8a463 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/aslyamov
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c0112f3d3761cce8010305fbb55fac01bff8a463 -
Trigger Event:
push
-
Statement type: