Skip to main content

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
  1. Create a file in detectors/tactics/
  2. Add translations to i18n/ru.py and i18n/en.py
  3. Register in detector.py

Tests

python -m pytest tests/ -v

89 tests — all passing.

License

MIT

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

chess_detect-0.1.1.tar.gz (33.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chess_detect-0.1.1-py3-none-any.whl (29.3 kB view details)

Uploaded Python 3

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

Hashes for chess_detect-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5bc53927cd380fa0153dfd6624549d7817c04fd6c32829c7675cbbaa2f17294b
MD5 fae88f87f2248b0a186895bf7cf9710f
BLAKE2b-256 3c9f5aee9a6676a7f99a0d05aa109ea82d53f566ed90066600a4fea638c53c06

See more details on using hashes here.

Provenance

The following attestation bundles were made for chess_detect-0.1.1.tar.gz:

Publisher: publish.yml on aslyamov/chess_detect

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for chess_detect-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3d3810e0724d5dc193eba01b6b9f527b55b5f8e19f7c814aff1a23f55640fde1
MD5 4f416334f24cc30e549226e194903ff0
BLAKE2b-256 c93fbb82fff693d6ebb949880288b45f26a71f2b23f77e79f63ae7e3760f6f2e

See more details on using hashes here.

Provenance

The following attestation bundles were made for chess_detect-0.1.1-py3-none-any.whl:

Publisher: publish.yml on aslyamov/chess_detect

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page