{"id":24777081,"url":"https://github.com/dvdagames/pgn-tokenizer","last_synced_at":"2026-02-26T18:07:36.505Z","repository":{"id":274077675,"uuid":"921728506","full_name":"DVDAGames/pgn-tokenizer","owner":"DVDAGames","description":"A byte pair encoding (BPE) tokenizer for chess portable game notation (PGN)","archived":false,"fork":false,"pushed_at":"2025-03-28T17:03:15.000Z","size":1572,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-10-29T01:49:51.700Z","etag":null,"topics":["bpe","byte-pair-encoding","chess","llm","pgn","tokenizer"],"latest_commit_sha":null,"homepage":"https://huggingface.co/InterwebAlchemy/PGNTokenizer","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DVDAGames.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"docs/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":["ericrallen"],"patreon":null,"open_collective":null,"ko_fi":"ericrallen","tidelift":null,"custom":"https://supporters.eff.org/donate/"}},"created_at":"2025-01-24T14:00:29.000Z","updated_at":"2025-03-29T01:55:59.000Z","dependencies_parsed_at":"2025-05-30T13:05:49.739Z","dependency_job_id":"af6b6e6b-bdc4-471b-8968-3946135686cb","html_url":"https://github.com/DVDAGames/pgn-tokenizer","commit_stats":null,"previous_names":["dvdagames/pgn-tokenizer"],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/DVDAGames/pgn-tokenizer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DVDAGames%2Fpgn-tokenizer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DVDAGames%2Fpgn-tokenizer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DVDAGames%2Fpgn-tokenizer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DVDAGames%2Fpgn-tokenizer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DVDAGames","download_url":"https://codeload.github.com/DVDAGames/pgn-tokenizer/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DVDAGames%2Fpgn-tokenizer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29867166,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-26T16:38:37.846Z","status":"ssl_error","status_checked_at":"2026-02-26T16:37:58.932Z","response_time":89,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bpe","byte-pair-encoding","chess","llm","pgn","tokenizer"],"created_at":"2025-01-29T07:49:16.623Z","updated_at":"2026-02-26T18:07:36.457Z","avatar_url":"https://github.com/DVDAGames.png","language":"Python","funding_links":["https://github.com/sponsors/ericrallen","https://ko-fi.com/ericrallen","https://supporters.eff.org/donate/"],"categories":[],"sub_categories":[],"readme":"# PGN Tokenizer\n\n![PGN Tokenizer Visualization](https://github.com/DVDAGames/pgn-tokenizer/raw/main/docs/assets/pgn-tokenizer.png)\n\nThis is a Byte Pair Encoding (BPE) tokenizer for chess Portable Game Notation (PGN).\n\n## Installation\n\nYou can install it with your package manager of choice:\n\n### uv\n\n```bash\nuv add pgn-tokenizer\n```\n\n### pip\n\n```bash\npip install pgn-tokenizer\n```\n\n## Usage\n\nIt exposes a simple interface with `.encode()` and `.decode()` methods, and a `.vocab_size` property, but you can also access the underlying `PreTrainedTokenizerFast` class from the `transformers` library via the `.tokenizer` property.\n\n```python\nfrom pgn_tokenizer import PGNTokenizer\n\n# Initialize the tokenizer\ntokenizer = PGNTokenizer()\n\n# Tokenize a PGN string\ntokens = tokenizer.encode(\"1.e4 Nf6 2.e5 Nd5 3.c4 Nb6\")\n\n# Decode the tokens back to a PGN string\ndecoded = tokenizer.decode(tokens)\n\n# get vocab from underlying tokenizer class\nvocab = tokenizer.tokenizer.get_vocab()\n```\n\n## Implementation\n\nIt is uses the [`tokenizers`](https://huggingface.co/docs/tokenizers/) library from Hugging Face for training the tokenizer and the [`transformers`](https://huggingface.co/docs/transformers/) library from Hugging Face for initializing the tokenizer from the pretrained tokenizer model for faster tokenization.\n\n**Note**: This is part of a work-in-progress project to investigate how language models might understand chess without an engine or any chess-specific knowledge.\n\n## Tokenizer Comparison\n\nMore traditional, language-focused BPE tokenizer implementations are not suited for PGN strings because they are more likely to break the actual moves apart.\n\nFor example `1.e4 Nf6` would likely be tokenized as `1`, `.`, `e`, `4`, ` N`, `f`, `6` or `1`, `.e`, `4`, ` `, ` N`, `f`, `6` depending on the tokenizer's vocabulary, but with the specialized PGN tokenizer it would be tokenized as `1.`, `e4`, ` Nf6`.\n\n### Visualization\n\nHere is a visualization of the vocabulary of this specialized PGN tokenizer compared to the BPE tokenizer vocabularies of the `cl100k_base` (the vocabulary for the `gpt-3.5-turbo` and `gpt-4` models' tokenizer) and the `o200k_base` (the vocabulary for the `gpt-4o` model's tokenizer):\n\n#### PGN Tokenizer\n\n![PGN Tokenizer Visualization](https://github.com/DVDAGames/pgn-tokenizer/raw/main/docs/assets/pgn-tokenizer.png)\n\n**Note**: The tokenizer was trained with ~2.8 Million chess games in PGN notation with a target vocabulary size of `4096`.\n\n#### GPT-3.5-turbo and GPT-4 Tokenizers\n\n![GPT-4 Tokenizer Visualization](https://github.com/DVDAGames/pgn-tokenizer/raw/main/docs/assets/gpt-4-tokenizer.png)\n\n#### GPT-4o Tokenizer\n\n![GPT-4o Tokenizer Visualization](https://github.com/DVDAGames/pgn-tokenizer/raw/main/docs/assets/gpt-4o-tokenizer.png)\n\n**Note**: These visualizations were generated with a function adapted from an [educational Jupyter Notebook in the `tiktoken` repository](https://github.com/openai/tiktoken/blob/main/tiktoken/_educational.py#L186).\n\n## Acknowledgements\n\n- [@karpathy](https://github.com/karpathy) for the [Let's build the GPT Tokenizer tutorial](https://youtu.be/zduSFxRajkE)\n- [Hugging Face](https://huggingface.co/) for the [`tokenizers`](https://huggingface.co/docs/tokenizers/) and [`transformers`](https://huggingface.co/docs/transformers/) libraries.\n- Kaggle user [MilesH14](https://www.kaggle.com/milesh14), whoever you are for the now-missing dataset of 3.5 million chess games referenced in many places, including this [research documentation](https://chess-research-project.readthedocs.io/en/latest/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdvdagames%2Fpgn-tokenizer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdvdagames%2Fpgn-tokenizer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdvdagames%2Fpgn-tokenizer/lists"}