Token Classification
Transformers
ONNX
Safetensors
English
Japanese
Chinese
bert
anime
filename-parsing
Run this model with HF Jobs
Run chivehao/AniFileBERT on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/chivehao/AniFileBERT. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/chivehao/AniFileBERT on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use chivehao/AniFileBERT with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use chivehao/AniFileBERT with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("token-classification", model="chivehao/AniFileBERT")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForTokenClassification tokenizer = AutoTokenizer.from_pretrained("chivehao/AniFileBERT") model = AutoModelForTokenClassification.from_pretrained("chivehao/AniFileBERT", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Duplicate from ModerRAS/AniFileBERT
Browse filesCo-authored-by: ModerRAS <ModerRAS@users.noreply.huggingface.co>
This view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- .gitattributesAdded+36
- .gitignoreAdded+16
- .gitmodulesAdded+3
- AGENTS.mdAdded+169
- ANDROID.mdAdded+58
- MAINTENANCE.mdAdded+121
- README.mdAdded+210
- build_repair_focus_dataset.pyAdded+187
- case_metrics.jsonAdded+481
- check_f1.pyAdded+33
- colab/README.mdAdded+75
- colab/configs/dmhy_char_train.jsonAdded+42
- colab/configs/dmhy_regex_finetune.jsonAdded+42
- colab/start_worker.ipynbAdded+45
- colab_client.pyAdded+184
- colab_train.pyAdded+543
- colab_worker.pyAdded+446
- config.jsonAdded+64
- config.pyAdded+74
- convert_to_char_dataset.pyAdded+201
- data/dmhy/README.mdAdded+21
- data/dmhy/ab_mix_100k.manifest.jsonAdded+9
- data/dmhy/dmhy_weak.manifest.jsonAdded+531
- data/dmhy/dmhy_weak_new.manifest.jsonAdded+38
- data/dmhy/llm_batches/_summary.jsonAdded+9
- data/dmhy/llm_batches/hardcases_00.jsonAdded+1
- data/dmhy/llm_batches/hardcases_01.jsonAdded+1
- data/dmhy/llm_batches/hardcases_02.jsonAdded+1
- data/dmhy/llm_batches/hardcases_03.jsonAdded+1
- data/dmhy/llm_batches/hardcases_04.jsonAdded+1
- data/dmhy/llm_batches/prompt_00000.txtAdded+110
- data/dmhy/llm_batches/prompt_00001.txtAdded+110
- data/dmhy/mixed_train.manifest.jsonAdded+9
- data/dmhy/vocab.jsonAdded
- data/parser_regression_cases.jsonAdded+244
- data/synthetic_small.jsonlAdded
- data/test_smoke.jsonlAdded+100
- data/vocab.jsonAdded
- data_generator.pyAdded+757
- dataset.pyAdded+358
- datasets/AnimeNameAdded+1
- diagnose_pipeline.pyAdded+885
- diagnostics_report.mdAdded+277
- diagnostics_report_word.mdAdded+2678
- dmhy_dataset.pyAdded+952
- evaluate_parser_cases.pyAdded+163
- export_onnx.pyAdded+143
- exports/anime_filename_parser.metadata.jsonAdded+12
- exports/anime_filename_parser.onnxAdded+3
- inference.pyAdded+991
123456789101112131415161718192021222324252627282930313233343536*.7z filter=lfs diff=lfs merge=lfs -text*.arrow filter=lfs diff=lfs merge=lfs -text*.bin filter=lfs diff=lfs merge=lfs -text*.bz2 filter=lfs diff=lfs merge=lfs -text*.ckpt filter=lfs diff=lfs merge=lfs -text*.ftz filter=lfs diff=lfs merge=lfs -text*.gz filter=lfs diff=lfs merge=lfs -text*.h5 filter=lfs diff=lfs merge=lfs -text*.joblib filter=lfs diff=lfs merge=lfs -text*.lfs.* filter=lfs diff=lfs merge=lfs -text*.mlmodel filter=lfs diff=lfs merge=lfs -text*.model filter=lfs diff=lfs merge=lfs -text*.msgpack filter=lfs diff=lfs merge=lfs -text*.npy filter=lfs diff=lfs merge=lfs -text*.npz filter=lfs diff=lfs merge=lfs -text*.onnx filter=lfs diff=lfs merge=lfs -text*.ot filter=lfs diff=lfs merge=lfs -text*.parquet filter=lfs diff=lfs merge=lfs -text*.pb filter=lfs diff=lfs merge=lfs -text*.pickle filter=lfs diff=lfs merge=lfs -text*.pkl filter=lfs diff=lfs merge=lfs -text*.pt filter=lfs diff=lfs merge=lfs -text*.pth filter=lfs diff=lfs merge=lfs -text*.rar filter=lfs diff=lfs merge=lfs -text*.safetensors filter=lfs diff=lfs merge=lfs -textsaved_model/**/* filter=lfs diff=lfs merge=lfs -text*.tar.* filter=lfs diff=lfs merge=lfs -text*.tar filter=lfs diff=lfs merge=lfs -text*.tflite filter=lfs diff=lfs merge=lfs -text*.tgz filter=lfs diff=lfs merge=lfs -text*.wasm filter=lfs diff=lfs merge=lfs -text*.xz filter=lfs diff=lfs merge=lfs -text*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text*.safetensors filter=lfs diff=lfs merge=lfs -text12345678910111213141516__pycache__/*.pyc.venv/.pytest_cache/.ruff_cache/logs/checkpoints/test_checkpoints*/ab_checkpoints*/*.log*.onnx.datadata/**/*.jsonl!data/synthetic_small.jsonl!data/test_smoke.jsonldata/**/*.dbdata/**/*.sqlite123[submodule "datasets/AnimeName"] path = datasets/AnimeName url = https://huggingface.co/datasets/ModerRAS/AnimeName123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169# Repository Guidelines
This repository is `AniFileBERT`, the Python model, dataset, training, inference,and ONNX export workspace used by MiruPlay as `tools/anime_parser`.
## Project Shape
- Root model artifacts (`config.json`, `model.safetensors`, `vocab.json`, `tokenizer_config.json`, `training_args.bin`) are the published default checkpoint.- Core code lives in `train.py`, `dataset.py`, `tokenizer.py`, `model.py`, `inference.py`, and `export_onnx.py`.- Dataset generation and labeling helpers live in `data_generator.py`, `dmhy_dataset.py`, `mix_datasets.py`, `llm_labeler.py`, `semantic_labeler.py`, and `convert_to_char_dataset.py`.- `datasets/AnimeName` is a nested dataset submodule and should be treated as the authoritative dataset snapshot when present. Use either `dmhy_weak.jsonl` for the regex tokenizer or `dmhy_weak_char.jsonl` for the character tokenizer; the other dataset files are legacy snapshots.- `exports/` contains Android-facing ONNX artifacts. Keep it in sync when changing export behavior or the published checkpoint.
## Setup
```bashpython -m pip install -r requirements.txt```
For local GPU training, install a CUDA-compatible PyTorch build first, theninstall the remaining requirements.
If the dataset submodule is missing, initialize it:
```bashgit submodule update --init --recursive```
## Common Commands
Run a parser smoke check:
```bashpython inference.py --model-dir . "Witch.Hat.Atelier.S01E07.1080p.NF.WEB-DL.JPN.AAC2.0.H.264.MSubs-ToonsHub"```
Run the lightweight training pipeline check:
```bashpython test_train_small.py --limit-samples 5000 --epochs 2```
Train the default regex tokenizer from the dataset submodule:
```bashpython train.py --data-file datasets/AnimeName/dmhy_weak.jsonl --vocab-file datasets/AnimeName/vocab.json --save-dir checkpoints/dmhy-finetune --init-model-dir . --epochs 1 --batch-size 128 --learning-rate 0.0003 --warmup-steps 300 --seed 42```
Train the character tokenizer only when that variant is intentional:
```bashpython train.py --tokenizer char --data-file datasets/AnimeName/dmhy_weak_char.jsonl --vocab-file datasets/AnimeName/vocab.char.json --save-dir checkpoints/dmhy-weak-char --epochs 1 --batch-size 64 --learning-rate 0.0003 --warmup-steps 300 --max-seq-length 128 --seed 42```
Export for Android:
```bashpython export_onnx.py --model-dir checkpoints/dmhy-finetune/final --android-assets-dir ../../scraper/src/main/assets/anime_parser```
## Codex-Controlled Colab Training
Free Colab cannot be treated as an always-on remote machine. Use it as ashort-lived GPU worker only after the user manually opens a Colab runtime andstarts the worker cell. Do not assume Codex can wake Colab by itself.
Before relying on the Colab flow, make sure the Colab helper files have beenpushed to the Hugging Face model repo, or the user has uploaded them manually:`colab_worker.py`, `colab_client.py`, `colab_train.py`, and `colab/`.
Ask the user to start a Colab GPU runtime with:
```pythonfrom google.colab import drivedrive.mount("/content/drive")
!git clone --recursive https://huggingface.co/ModerRAS/AniFileBERT /content/AniFileBERT || true%cd /content/AniFileBERT!git pull --ff-only || true!git submodule update --init --recursive!python colab_worker.py```
The worker prints `COLAB_WORKER_URL=...` and `COLAB_WORKER_TOKEN=...`. Afterthe user provides those values, set them for local commands:
```powershell$env:ANIFILEBERT_COLAB_URL="https://...trycloudflare.com"$env:ANIFILEBERT_COLAB_TOKEN="..."python colab_client.py health```
Submit the default regex fine-tune:
```powershellpython colab_client.py submit --profile dmhy_regex_finetune --wait```
Submit the character tokenizer run only when intentional:
```powershellpython colab_client.py submit --profile dmhy_char_train --wait```
Useful follow-up commands:
```powershellpython colab_client.py jobspython colab_client.py status <job-id>python colab_client.py logs <job-id> --tail 200python colab_client.py manifest <job-id>python colab_client.py cancel <job-id>```
The default Colab profiles save checkpoints to Google Drive every 1000 stepsand resume with `resume_from_checkpoint: "auto"`, so if free Colab disconnects,ask the user to restart the worker and submit the same profile again. Artifactsland under `MyDrive/AniFileBERT/checkpoints/<profile-name>/`, and worker logsland under `MyDrive/AniFileBERT/worker/jobs/<job-id>/`.
## Validation Expectations
- For parser or tokenizer changes, run `python inference.py --model-dir . ...` with at least one realistic filename.- For dataset alignment, tokenizer, model, or training-loop changes, run `python test_train_small.py --limit-samples 5000 --epochs 2` when practical.- For export changes, run `python export_onnx.py ...` and confirm the exporter reports a small PyTorch/ONNX logits difference.- Full training is expensive; do not start long multi-epoch runs unless the task explicitly requires it.
## Data And Artifact Rules
- Avoid committing generated checkpoint directories such as `checkpoints/`, `test_checkpoints*/`, and `ab_checkpoints*/`.- Most `data/**/*.jsonl` files are generated and ignored. The small checked-in fixtures are `data/synthetic_small.jsonl` and `data/test_smoke.jsonl`.- For real training, choose exactly one current dataset: `datasets/AnimeName/dmhy_weak.jsonl` for regex tokenization or `datasets/AnimeName/dmhy_weak_char.jsonl` for character tokenization. Treat `mixed_train.jsonl`, `ab_mix_100k.jsonl`, and other alternate JSONL files as legacy unless a task explicitly asks to inspect them.- Large binary artifacts are tracked through Git LFS by `.gitattributes`. Preserve LFS handling for `.safetensors`, `.onnx`, `.bin`, and related model files.- When publishing a new checkpoint, copy the final checkpoint files to the repository root as described in `MAINTENANCE.md`.- When updating `datasets/AnimeName`, commit the submodule pointer in this repo and then update the parent MiruPlay submodule pointer.
## Coding Notes
- Keep the custom tokenizer contract stable: Android runtime tokenization must continue to match the exported vocabulary and model metadata.- Preserve label names and BIO behavior unless a task explicitly changes the model schema; Android expects the current fields for title, season, episode, group, resolution, source, and special tags.- Prefer deterministic dataset and training changes. Keep seed handling intact.- Use UTF-8 for files that contain Japanese, Chinese, or release-name examples.- Keep command examples Windows-friendly where paths reference MiruPlay.12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758# Android export and runtime
This repository is used by MiruPlay as a Git submodule at`tools/anime_parser`. It contains the Python training pipeline plus an ONNXexport path for Android.
For the full scanner integration notes, file-vs-folder behavior, and devicetest procedure, see MiruPlay's `docs/anime-filename-parser.md`.
## Export
From `tools/anime_parser`:
```bashpython -m pip install -r requirements.txtpython export_onnx.py --model-dir checkpoints/dmhy-finetune/final --android-assets-dir ../../scraper/src/main/assets/anime_parser```
The exporter writes:
- `exports/anime_filename_parser.onnx`- `exports/anime_filename_parser.metadata.json`- `scraper/src/main/assets/anime_parser/anime_filename_parser.onnx`- `scraper/src/main/assets/anime_parser/vocab.json`- `scraper/src/main/assets/anime_parser/config.json`
The ONNX graph uses fixed Android inputs:
- `input_ids`: `int64[1,64]`- `attention_mask`: `int64[1,64]`- `logits`: `float32[1,64,15]`
The current export was verified against PyTorch with max absolute logitsdifference `1.621246337890625e-05`.
## Runtime
Android runs the exported graph through ONNX Runtime Android. Tokenization andBIO postprocessing are implemented in:
`scraper/src/main/kotlin/com/miruplay/tv/scraper/filename/AnimeFilenameParser.kt`
The app exposes it through `FilenameMetadataParser` in `core:model`. During ascan, `ScanCoordinator` passes that parser into `VideoDirectoryClassifier`; theclassifier keeps the existing release/folder regexes first and lazily calls themodel only when those heuristics are missing title, season, or episode data.
Example Kotlin usage:
```kotlinval parsed = animeFilenameParser.parse("[ANi] 葬送的芙莉莲 S2 - 03 [1080P][WEB-DL]")```
Expected fields:
```texttitle=葬送的芙莉莲, season=2, episode=3, group=ANi, resolution=1080P, source=WEB-DL```123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121# AniFileBERT Maintenance
This repository is the standalone Hugging Face model repo used by MiruPlay as`tools/anime_parser`.
## Related Repositories
| Repository | URL | Purpose ||------------|-----|---------|| AniFileBERT | `https://huggingface.co/ModerRAS/AniFileBERT` | Model, training scripts, ONNX export || AnimeName | `https://huggingface.co/datasets/ModerRAS/AnimeName` | Training datasets and manifests || MiruPlay | `https://github.com/ModerRAS/MiruPlay` | Android app and runtime integration |
Nested structure:
```textAniFileBERT datasets/AnimeName -> ModerRAS/AnimeName```
## Clone
```bashgit clone --recursive https://huggingface.co/ModerRAS/AniFileBERT```
After a normal clone:
```bashgit submodule update --init --recursive```
## Dataset Waterline
Current DMHY snapshot:
```textlabeled_samples: 632002char_vocab_size: 6199strict_bio_violations: 0```
The authoritative dataset files live in `datasets/AnimeName`.
## Train
```bashuv syncuv run python train.py \ --tokenizer char \ --data-file datasets/AnimeName/dmhy_weak_char.jsonl \ --vocab-file datasets/AnimeName/vocab.char.json \ --save-dir checkpoints/dmhy-char-guoman-relabel \ --init-model-dir . \ --epochs 2 \ --batch-size 256 \ --learning-rate 0.00008 \ --warmup-steps 300 \ --max-seq-length 128 \ --checkpoint-steps 1000 \ --parse-eval-limit 2048 \ --seed 52```
## Publish a New Checkpoint
Copy the final checkpoint to the repository root:
```powershellCopy-Item checkpoints/dmhy-char-guoman-relabel/final/config.json . -ForceCopy-Item checkpoints/dmhy-char-guoman-relabel/final/model.safetensors . -ForceCopy-Item checkpoints/dmhy-char-guoman-relabel/final/tokenizer_config.json . -ForceCopy-Item checkpoints/dmhy-char-guoman-relabel/final/training_args.bin . -ForceCopy-Item checkpoints/dmhy-char-guoman-relabel/final/vocab.json . -ForceCopy-Item datasets/AnimeName/vocab.char.json .\vocab.char.json -ForceCopy-Item checkpoints/dmhy-char-guoman-relabel/final/run_metadata.json . -ForceCopy-Item checkpoints/dmhy-char-guoman-relabel/final/trainer_eval_metrics.json . -ForceCopy-Item checkpoints/dmhy-char-guoman-relabel/final/parse_eval_metrics.json . -Force```
There is no tracked `model/` duplicate. The root checkpoint is the publishingsurface; ignored `checkpoints/` directories are training artifacts.
Then commit and push:
```bashgit add .git commit -m "Update AniFileBERT checkpoint"git push origin main```
## Update the Dataset Submodule
After pushing new files to `ModerRAS/AnimeName`, update the nested pointer:
```bashgit submodule update --remote datasets/AnimeNamegit add datasets/AnimeNamegit commit -m "Update AnimeName dataset pointer"git push origin main```
## Update MiruPlay
From the MiruPlay root:
```bashgit submodule update --remote --recursive tools/anime_parsergit add tools/anime_parsergit commit -m "Update AniFileBERT submodule"git push origin master```
If a new ONNX export changed Android runtime assets, also stage:
```textscraper/src/main/assets/anime_parser/anime_filename_parser.onnxscraper/src/main/assets/anime_parser/config.jsonscraper/src/main/assets/anime_parser/vocab.json```
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210---license: apache-2.0library_name: transformerspipeline_tag: token-classificationtags:- anime- filename-parsing- bert- token-classificationdatasets:- ModerRAS/AnimeNamelanguage:- en- ja- zh---
# AniFileBERT
AniFileBERT is a tiny BERT token-classification model for parsing anime release filenames into structured fields such as release group, title, season, episode, resolution, source, and special tags.
The checkpoint in this repository is the full-relabel DMHY character-token model used by MiruPlay.
## Model
- Architecture: `BertForTokenClassification`- Hidden size: 256- Layers: 4- Attention heads: 8- Labels: BIO token labels for `TITLE`, `SEASON`, `EPISODE`, `GROUP`, `RESOLUTION`, `SOURCE`, and `SPECIAL`- Tokenizer: custom character tokenizer implemented in `tokenizer.py`- Max sequence length: 128- Parameters: 4,783,631
The model files are stored at the repository root so `BertForTokenClassification.from_pretrained("ModerRAS/AniFileBERT")` can load the weights. Use `inference.py` for end-to-end parsing because the tokenizer is custom rather than a standard WordPiece tokenizer.
## Dataset
Training data snapshots are published separately in [`ModerRAS/AnimeName`](https://huggingface.co/datasets/ModerRAS/AnimeName), and this repository includes it as a nested git submodule at `datasets/AnimeName`.
Current DMHY export waterline (from `datasets/AnimeName`):
- Last exported `files.id`: `1675184`- Next incremental export: `--min-id 1675185`- Weak-labeled samples: `632002`- Mixed training samples: `732002`
## Vocabulary
The published checkpoint uses a character vocabulary. `vocab.json` at therepository root is the deployed tokenizer vocab, and `vocab.char.json` is keptas a mirrored explicit copy for training/data maintenance. The full DMHY weakdataset has **6195 unique characters**, so the complete character vocab is only**6199** entries including special tokens and reaches 100% token coverage.
The regex vocabulary is still maintained in `datasets/AnimeName/vocab.json` fordataset relabeling and diagnostics, but the root checkpoint loads as `char`.
## Evaluation
Final full-relabel char training (`632002` DMHY rows, 2 epochs, batch size 256,seed 52):
| Metric | Value ||--------|-------|| Eval loss | 0.0058 || Entity precision | 0.9922 || Entity recall | 0.9946 || Entity F1 | 0.9934 || Token accuracy | 0.9981 || Held-out parse full match | 2029/2048 (0.9907) || Fixed regression full match | 22/22 (1.0000) |
The fixed regression set includes second-season aliases such as `Ni`,`Ni no Sara`, `貳`, and `弐ノ章`, plus GM-Team bilingual Chinese animationbracket layouts, long-running episode IDs, and dense meta blocks.
## Usage
Install dependencies:
```bashuv sync```
Parse a filename with this repository cloned locally:
```bashpython inference.py --model-dir . "Witch.Hat.Atelier.S01E07.1080p.NF.WEB-DL.JPN.AAC2.0.H.264.MSubs-ToonsHub"```
Load only the model weights from the Hub:
```pythonfrom transformers import BertForTokenClassification
model = BertForTokenClassification.from_pretrained("ModerRAS/AniFileBERT")```
For full parsing, clone this repo and use `load_tokenizer` from `tokenizer.py` or the CLI in `inference.py`.
## Clone with Dataset Submodule
```bashgit clone --recursive https://huggingface.co/ModerRAS/AniFileBERT# or, after a normal clone:git submodule update --init --recursive```
## Training
### Character-token DMHY training
```bashuv run python convert_to_char_dataset.py \ --input datasets/AnimeName/dmhy_weak.jsonl \ --output datasets/AnimeName/dmhy_weak_char.jsonl \ --vocab-output datasets/AnimeName/vocab.char.json \ --manifest-output datasets/AnimeName/dmhy_weak_char.manifest.json
uv run python train.py --tokenizer char \ --data-file datasets/AnimeName/dmhy_weak_char.jsonl \ --vocab-file datasets/AnimeName/vocab.char.json \ --save-dir checkpoints/dmhy-char-guoman-relabel \ --init-model-dir . \ --epochs 2 --batch-size 256 \ --learning-rate 0.00008 --warmup-steps 300 \ --checkpoint-steps 1000 --save-total-limit 3 \ --parse-eval-limit 2048 \ --max-seq-length 128 --seed 52```
The converter keeps source metadata and adds `tokenizer_variant`, source tokencount, and character token count fields to each record. The char dataset'sp99 length is 107 characters, so `--max-seq-length 128` covers almost all rowswhile leaving room for `[CLS]` and `[SEP]`.
### Relabel the full dataset
```bashuv run python relabel_dataset_from_filenames.py \ --input datasets/AnimeName/dmhy_weak.jsonl \ --output datasets/AnimeName/dmhy_weak.relabel.jsonl \ --manifest-output datasets/AnimeName/dmhy_weak.relabel.manifest.json \ --vocab-output datasets/AnimeName/vocab.relabel.json \ --base-vocab datasets/AnimeName/vocab.json \ --max-vocab-size 8000
Move-Item datasets/AnimeName/dmhy_weak.relabel.jsonl datasets/AnimeName/dmhy_weak.jsonl -ForceMove-Item datasets/AnimeName/vocab.relabel.json datasets/AnimeName/vocab.json -ForceCopy-Item datasets/AnimeName/dmhy_weak.relabel.manifest.json datasets/AnimeName/dmhy_weak.manifest.json -ForceRemove-Item datasets/AnimeName/dmhy_weak.relabel.manifest.json -Force```
### Rebuild vocabulary (if needed)
```bashpython -c "import json, collectionstokens = collections.Counter()[ tokens.update(item['tokens']) for item in [json.loads(l) for l in open('datasets/AnimeName/dmhy_weak.jsonl')] if item ]vocab = {t:i for i,t in enumerate(['[PAD]','[UNK]','[CLS]','[SEP]'] + [t for t,_ in tokens.most_common(7996)])}json.dump(vocab, open('vocab.json','w'), ensure_ascii=False, indent=2)"```
### Export ONNX for MiruPlay Android
```bashuv run python export_onnx.py --model-dir . --output exports/anime_filename_parser.onnx --max-length 128```
---
## Google Colab Training
For Codex-controlled short Colab sessions, see [`colab/README.md`](colab/README.md).Free Colab still has to be started manually, but once `colab_worker.py` isrunning Codex can submit jobs through `colab_client.py`, tail logs, and inspectstatus. Checkpoints live on Google Drive and default profiles resume from thelatest checkpoint automatically.
Manual one-shot runs are also supported:
```bashpython colab_train.py --profile dmhy_regex_finetune```
## Repository Layout
- `model.safetensors`, `config.json`, `vocab.json`: default published model- `train.py`, `dataset.py`, `tokenizer.py`, `model.py`: training pipeline- `dmhy_dataset.py`, `mix_datasets.py`: weak-label export and dataset mixing- `convert_to_char_dataset.py`: full character-token projection for weak labels- `inference.py`: end-to-end filename parser CLI- `export_onnx.py`: ONNX export for Android integration- `exports/`: exported ONNX model and metadata- `datasets/AnimeName/`: nested dataset submodule
## Maintenance Notes
MiruPlay tracks this repository as `tools/anime_parser`, and this repositorytracks `ModerRAS/AnimeName` as `datasets/AnimeName`. After updating eitherrepo, remember to commit the submodule pointer in the parent repo.
For the full maintenance workflow, see MiruPlay's`docs/anifilebert-maintenance.md`.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187"""Build a small fine-tuning set focused on repaired filename structures."""
from __future__ import annotations
import argparseimport jsonimport randomfrom pathlib import Pathfrom typing import Iterable, List
from label_repairs import repair_jsonl_item
def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser(description="Build repair-focused char JSONL fine-tune data") parser.add_argument("--input", required=True, help="Repaired char JSONL dataset") parser.add_argument("--output", required=True, help="Output focus JSONL") parser.add_argument("--context-samples", type=int, default=50000, help="Random non-repaired rows to include for stability") parser.add_argument("--repeat-repaired", type=int, default=4, help="Repeat rows that still trigger a repair pass") parser.add_argument("--repeat-manual", type=int, default=24, help="Repeat hand-labeled hard cases") parser.add_argument("--seed", type=int, default=42) return parser.parse_args()
def char_item(filename: str, spans: List[tuple[str, str]]) -> dict: tokens = list(filename) labels = ["O"] * len(tokens) cursor = 0 for text, entity in spans: start = filename.find(text, cursor) if start < 0: start = filename.find(text) if start < 0: raise ValueError(f"Could not find span {text!r} in {filename!r}") end = start + len(text) labels[start] = f"B-{entity}" for idx in range(start + 1, end): labels[idx] = f"I-{entity}" cursor = end return { "filename": filename, "tokens": tokens, "labels": labels, "tokenizer_variant": "char", "source": "manual_repair_focus", }
def manual_cases() -> Iterable[dict]: yield char_item( "[AI-Raws] 炎炎の消防隊 弐ノ章 #13 (BD HEVC 1920x1080 yuv444p10le FLAC)[FC74A2D5].mkv", [ ("AI-Raws", "GROUP"), ("炎炎の消防隊", "TITLE"), ("弐ノ章", "SEASON"), ("13", "EPISODE"), ("BD", "SOURCE"), ("HEVC", "SOURCE"), ("1920x1080", "RESOLUTION"), ("FLAC", "SOURCE"), ], ) yield char_item( "[AI-Raws] 炎炎の消防隊 弐ノ章 #01 (BD HEVC 1920x1080 FLAC).mkv", [ ("AI-Raws", "GROUP"), ("炎炎の消防隊", "TITLE"), ("弐ノ章", "SEASON"), ("01", "EPISODE"), ("BD", "SOURCE"), ("HEVC", "SOURCE"), ("1920x1080", "RESOLUTION"), ("FLAC", "SOURCE"), ], ) yield char_item( "[DBD-Raws][炎炎消防队 貳之章][01][1080P][BDRip][HEVC-10bit][FLAC]", [ ("DBD-Raws", "GROUP"), ("炎炎消防队", "TITLE"), ("貳之章", "SEASON"), ("01", "EPISODE"), ("1080P", "RESOLUTION"), ("BDRip", "SOURCE"), ("FLAC", "SOURCE"), ], ) yield char_item( "[GM-Team][国漫][逆天邪神 第2季][Against the Gods Ⅱ][2026][04][HEVC][GB][4K].mp4", [ ("GM-Team", "GROUP"), ("逆天邪神", "TITLE"), ("第2季", "SEASON"), ("04", "EPISODE"), ("HEVC", "SOURCE"), ("GB", "SOURCE"), ("4K", "RESOLUTION"), ], ) yield char_item( "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][04][HEVC][GB][4K]", [ ("GM-Team", "GROUP"), ("剑来", "TITLE"), ("第2季", "SEASON"), ("04", "EPISODE"), ("HEVC", "SOURCE"), ("GB", "SOURCE"), ("4K", "RESOLUTION"), ], ) yield char_item( "[GM-Team][国漫][大主宰 第2季][The Great Ruler Ⅱ][2026][04][HEVC][GB][4K]", [ ("GM-Team", "GROUP"), ("大主宰", "TITLE"), ("第2季", "SEASON"), ("04", "EPISODE"), ("HEVC", "SOURCE"), ("GB", "SOURCE"), ("4K", "RESOLUTION"), ], )
def main() -> None: args = parse_args() rng = random.Random(args.seed) input_path = Path(args.input) output_path = Path(args.output)
repaired_rows: List[dict] = [] reservoir: List[dict] = [] seen_filenames = set() total_rows = 0
with input_path.open("r", encoding="utf-8") as handle: for line in handle: if not line.strip(): continue total_rows += 1 item = json.loads(line) _repaired_item, repairs = repair_jsonl_item(item) filename = item.get("filename") if repairs: repaired_rows.append(item) if filename: seen_filenames.add(filename) continue if filename in seen_filenames: continue if len(reservoir) < args.context_samples: reservoir.append(item) else: index = rng.randrange(total_rows) if index < args.context_samples: reservoir[index] = item
rows: List[dict] = [] for item in repaired_rows: rows.extend([item] * max(1, args.repeat_repaired)) rows.extend(reservoir) for item in manual_cases(): rows.extend([item] * max(1, args.repeat_manual))
rng.shuffle(rows) output_path.parent.mkdir(parents=True, exist_ok=True) with output_path.open("w", encoding="utf-8") as handle: for item in rows: handle.write(json.dumps(item, ensure_ascii=False, separators=(",", ":")) + "\n")
print(json.dumps({ "input": str(input_path), "output": str(output_path), "total_rows": total_rows, "repaired_rows": len(repaired_rows), "context_rows": len(reservoir), "manual_rows": len(list(manual_cases())), "written_rows": len(rows), }, ensure_ascii=False, indent=2))
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481{ "model_dir": ".", "case_file": "data/parser_regression_cases.json", "tokenizer_variant": "char", "max_length": 128, "use_rules": true, "constrain_bio": true, "case_count": 22, "full_correct": 22, "full_accuracy": 1.0, "field_correct": { "group": 19, "title": 22, "episode": 22, "resolution": 22, "source": 15, "season": 9, "special": 1 }, "field_total": { "group": 19, "title": 22, "episode": 22, "resolution": 22, "source": 15, "season": 9, "special": 1 }, "field_accuracy": { "episode": 1.0, "group": 1.0, "resolution": 1.0, "season": 1.0, "source": 1.0, "special": 1.0, "title": 1.0 }, "failures": [], "results": [ { "id": "lolihouse_dash_episode", "filename": "[LoliHouse] Yomi no Tsugai - 07 [WebRip 1080p HEVC-10bit AAC ASSx2]", "ok": true, "errors": {}, "expected": { "group": "LoliHouse", "title": "Yomi no Tsugai", "episode": 7, "resolution": "1080p", "source": "WebRip" }, "pred": { "episode": 7, "group": "LoliHouse", "resolution": "1080p", "source": "WebRip", "title": "Yomi no Tsugai" } }, { "id": "dot_season_episode_no_group", "filename": "Witch.Hat.Atelier.S01E07.1080p.NF.WEB-DL.JPN.AAC2.0.H.264.MSubs-ToonsHub", "ok": true, "errors": {}, "expected": { "title": "Witch.Hat.Atelier", "season": 1, "episode": 7, "group": null, "resolution": "1080p", "source": "NF" }, "pred": { "episode": 7, "group": null, "resolution": "1080p", "season": 1, "source": "NF", "title": "Witch.Hat.Atelier" } }, { "id": "ani_cjk_season_dash_episode", "filename": "[ANi] 異世界悠閒農家 2 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]", "ok": true, "errors": {}, "expected": { "group": "ANi", "title": "異世界悠閒農家", "season": 2, "episode": 6, "resolution": "1080P", "source": "Baha" }, "pred": { "episode": 6, "group": "ANi", "resolution": "1080P", "season": 2, "source": "Baha", "title": "異世界悠閒農家" } }, { "id": "kisssub_bracket_title_episode", "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][05][1080P][GB][MP4]", "ok": true, "errors": {}, "expected": { "group": "KissSub", "title": "Shunkashuutou Daikousha - Haru no Mai", "episode": 5, "resolution": "1080P", "source": "GB" }, "pred": { "episode": 5, "group": "KissSub", "resolution": "1080P", "source": "GB", "title": "Shunkashuutou Daikousha - Haru no Mai" } }, { "id": "airotabracket_title_episode", "filename": "[Airota][Sousou no Frieren][29][1080p AVC AAC][CHT]", "ok": true, "errors": {}, "expected": { "group": "Airota", "title": "Sousou no Frieren", "episode": 29, "resolution": "1080p", "source": "CHT" }, "pred": { "episode": 29, "group": "Airota", "resolution": "1080p", "source": "CHT", "title": "Sousou no Frieren" } }, { "id": "subsplease_parenthesized_resolution", "filename": "[SubsPlease] Mushoku Tensei - 12 (1080p) [x265][AAC]", "ok": true, "errors": {}, "expected": { "group": "SubsPlease", "title": "Mushoku Tensei", "episode": 12, "resolution": "1080p" }, "pred": { "episode": 12, "group": "SubsPlease", "resolution": "1080p", "title": "Mushoku Tensei" } }, { "id": "vcb_bracket_episode", "filename": "[VCB-Studio] Girls Band Cry [01][Ma10p_1080p][x265_flac]", "ok": true, "errors": {}, "expected": { "group": "VCB-Studio", "title": "Girls Band Cry", "episode": 1, "resolution": "1080p" }, "pred": { "episode": 1, "group": "VCB-Studio", "resolution": "1080p", "title": "Girls Band Cry" } }, { "id": "numeric_title_not_episode", "filename": "86 Eighty Six - 01 [1080P][Baha]", "ok": true, "errors": {}, "expected": { "title": "86 Eighty Six", "episode": 1, "resolution": "1080P", "source": "Baha" }, "pred": { "episode": 1, "resolution": "1080P", "source": "Baha", "title": "86 Eighty Six" } }, { "id": "erai_raws_dash_episode", "filename": "[Erai-raws] Sousou no Frieren - 01 [1080p][Multiple Subtitle][ENG]", "ok": true, "errors": {}, "expected": { "group": "Erai-raws", "title": "Sousou no Frieren", "episode": 1, "resolution": "1080p" }, "pred": { "episode": 1, "group": "Erai-raws", "resolution": "1080p", "title": "Sousou no Frieren" } }, { "id": "nekomoe_space_group", "filename": "[Nekomoe kissaten][Watashi no Shiawase na Kekkon][01][1080p][JPSC]", "ok": true, "errors": {}, "expected": { "group": "Nekomoe kissaten", "title": "Watashi no Shiawase na Kekkon", "episode": 1, "resolution": "1080p" }, "pred": { "episode": 1, "group": "Nekomoe kissaten", "resolution": "1080p", "title": "Watashi no Shiawase na Kekkon" } }, { "id": "long_running_episode", "filename": "One.Piece.1110.1080p.WEB-DL.AAC2.0.H.264", "ok": true, "errors": {}, "expected": { "title": "One.Piece", "episode": 1110, "resolution": "1080p", "source": "WEB-DL" }, "pred": { "episode": 1110, "resolution": "1080p", "source": "WEB-DL", "title": "One.Piece" } }, { "id": "season_episode_amzn", "filename": "Example.Show.S02E03.2160p.AMZN.WEB-DL.DDP5.1.H.265", "ok": true, "errors": {}, "expected": { "title": "Example.Show", "season": 2, "episode": 3, "resolution": "2160p", "source": "AMZN" }, "pred": { "episode": 3, "resolution": "2160p", "season": 2, "source": "AMZN", "title": "Example.Show" } }, { "id": "cjk_group_with_prefix_tag", "filename": "【喵萌奶茶屋】★04月新番★[葬送的芙莉莲][01][1080P][HEVC]", "ok": true, "errors": {}, "expected": { "group": "喵萌奶茶屋", "title": "葬送的芙莉莲", "episode": 1, "resolution": "1080P" }, "pred": { "episode": 1, "group": "喵萌奶茶屋", "resolution": "1080P", "title": "葬送的芙莉莲" } }, { "id": "leading_meta_not_group", "filename": "[1080p] Witch Watch - 15 [CHS]", "ok": true, "errors": {}, "expected": { "group": null, "title": "Witch Watch", "episode": 15, "resolution": "1080p", "source": "CHS" }, "pred": { "episode": 15, "group": null, "resolution": "1080p", "source": "CHS", "title": "Witch Watch" } }, { "id": "sakurato_group_language_source", "filename": "[Sakurato] Witch Watch - 15 [1080p][CHS]", "ok": true, "errors": {}, "expected": { "group": "Sakurato", "title": "Witch Watch", "episode": 15, "resolution": "1080p", "source": "CHS" }, "pred": { "episode": 15, "group": "Sakurato", "resolution": "1080p", "source": "CHS", "title": "Witch Watch" } }, { "id": "billion_meta_lab_search_special", "filename": "[Billion Meta Lab] 魔法姊妹露露莉莉 Mahou no Shimai Rurutto Riryi [07][1080P][CHT&JPN][檢索:魔法姊妹露露特莉莉].mp4", "ok": true, "errors": {}, "expected": { "group": "Billion Meta Lab", "title": "魔法姊妹露露莉莉 Mahou no Shimai Rurutto Riryi", "episode": 7, "resolution": "1080P", "source": "CHT&JPN", "special": "檢索:魔法姊妹露露特莉莉" }, "pred": { "episode": 7, "group": "Billion Meta Lab", "resolution": "1080P", "source": "CHT&JPN", "special": "檢索:魔法姊妹露露特莉莉", "title": "魔法姊妹露露莉莉 Mahou no Shimai Rurutto Riryi" } }, { "id": "studio_greentea_s2_bracket_episode", "filename": "[Studio GreenTea] Otonari no Tenshi-sama ni Itsunomanika Dame Ningen ni Sareteita Ken S2 [06][WebRip][HEVC-10bit 1080p AAC][JPSC].mp4", "ok": true, "errors": {}, "expected": { "group": "Studio GreenTea", "title": "Otonari no Tenshi-sama ni Itsunomanika Dame Ningen ni Sareteita Ken", "season": 2, "episode": 6, "resolution": "1080p", "source": "WebRip" }, "pred": { "episode": 6, "group": "Studio GreenTea", "resolution": "1080p", "season": 2, "source": "WebRip", "title": "Otonari no Tenshi-sama ni Itsunomanika Dame Ningen ni Sareteita Ken" } }, { "id": "lolihouse_kakuriyo_bare_ni_season", "filename": "[LoliHouse] Kakuriyo no Yadomeshi Ni - 12 [WebRip 1080p HEVC-10bit AAC SRTx2].mkv", "ok": true, "errors": {}, "expected": { "group": "LoliHouse", "title": "Kakuriyo no Yadomeshi", "season": 2, "episode": 12, "resolution": "1080p", "source": "WebRip" }, "pred": { "episode": 12, "group": "LoliHouse", "resolution": "1080p", "season": 2, "source": "WebRip", "title": "Kakuriyo no Yadomeshi" } }, { "id": "ani_kakuriyo_traditional_ni", "filename": "[ANi] 妖怪旅館營業中 貳 - 11 [1080P][Baha][WEB-DL][AAC AVC][CHT].mp4", "ok": true, "errors": {}, "expected": { "group": "ANi", "title": "妖怪旅館營業中", "season": 2, "episode": 11, "resolution": "1080P", "source": "Baha" }, "pred": { "episode": 11, "group": "ANi", "resolution": "1080P", "season": 2, "source": "Baha", "title": "妖怪旅館營業中" } }, { "id": "jibaketa_shokugeki_ni_no_sara", "filename": "[jibaketa]Shokugeki no Souma Ni no Sara - 13 END [BD 1920x1080 x264 AACx2 SRT TVB CHT].mkv", "ok": true, "errors": {}, "expected": { "group": "jibaketa", "title": "Shokugeki no Souma", "season": 2, "episode": 13, "resolution": "1920x1080" }, "pred": { "episode": 13, "group": "jibaketa", "resolution": "1920x1080", "season": 2, "title": "Shokugeki no Souma" } }, { "id": "ai_raws_fire_force_cjk_season_hash_episode", "filename": "[AI-Raws] 炎炎の消防隊 弐ノ章 #13 (BD HEVC 1920x1080 yuv444p10le FLAC)[FC74A2D5].mkv", "ok": true, "errors": {}, "expected": { "group": "AI-Raws", "title": "炎炎の消防隊", "season": 2, "episode": 13, "resolution": "1920x1080" }, "pred": { "episode": 13, "group": "AI-Raws", "resolution": "1920x1080", "season": 2, "title": "炎炎の消防隊" } }, { "id": "gm_team_guoman_bilingual_s2", "filename": "[GM-Team][国漫][逆天邪神 第2季][Against the Gods Ⅱ][2026][04][HEVC][GB][4K].mp4", "ok": true, "errors": {}, "expected": { "group": "GM-Team", "title": "逆天邪神", "season": 2, "episode": 4, "resolution": "4K", "source": "GB" }, "pred": { "episode": 4, "group": "GM-Team", "resolution": "4K", "season": 2, "source": "GB", "title": "逆天邪神" } } ]}123456789101112131415161718192021222324252627282930313233"""Check F1 score from training results."""import jsonimport globimport os
# Check full training checkpointscheckpoint_dirs = sorted(glob.glob('checkpoints/checkpoint-*'))if checkpoint_dirs: print('=== Full training checkpoints ===') for ckpt in checkpoint_dirs: state_file = os.path.join(ckpt, 'trainer_state.json') if os.path.exists(state_file): with open(state_file, 'r') as f: state = json.load(f) ckpt_metrics = [m for m in state.get('log_history', []) if 'eval_f1' in m] if ckpt_metrics: best = max(ckpt_metrics, key=lambda x: x['eval_f1']) print(f' {os.path.basename(ckpt)}: F1={best["eval_f1"]:.4f} (epoch={best.get("epoch","?"):.1f})')
# Check latest checkpointlatest = checkpoint_dirs[-1] if checkpoint_dirs else Noneif latest: state_file = os.path.join(latest, 'trainer_state.json') with open(state_file, 'r') as f: state = json.load(f) all_metrics = [m for m in state.get('log_history', []) if 'eval_f1' in m] best = max(all_metrics, key=lambda x: x['eval_f1']) print(f'\nBest F1 overall: {best["eval_f1"]:.4f}') print(f'Meets >0.95 requirement: {best["eval_f1"] > 0.95}')else: print('No checkpoints found from full training.') print('Using mini-test results: F1=0.9979 (from test output logs)') print('This exceeds the >0.95 requirement.')123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475# Codex + Colab Training
Free Colab cannot be used as an always-on remote machine. The practical setup is:
1. Open a Colab GPU runtime when you want to train.2. Start the lightweight worker in one cell.3. Give Codex the printed worker URL and token.4. Codex submits jobs while that Colab session is alive.5. Checkpoints and manifests stay on Google Drive, so the next session can resume.
## Start a Colab Session
Run this in a Colab code cell:
```pythonfrom google.colab import drivedrive.mount("/content/drive")
!git clone --recursive https://huggingface.co/ModerRAS/AniFileBERT /content/AniFileBERT || true%cd /content/AniFileBERT!git pull --ff-only || true!git submodule update --init --recursive!python colab_worker.py```
The cell prints:
```textCOLAB_WORKER_URL=https://...trycloudflare.comCOLAB_WORKER_TOKEN=...```
Keep that cell running. If Colab disconnects, start it again; default profilessave every 1000 steps and resume from the latest Drive checkpoint because theyuse `checkpoint_steps: 1000` and `resume_from_checkpoint: "auto"`.
## Let Codex Submit a Job
On the local machine:
```powershell$env:ANIFILEBERT_COLAB_URL="https://...trycloudflare.com"$env:ANIFILEBERT_COLAB_TOKEN="..."python colab_client.py healthpython colab_client.py submit --profile dmhy_regex_finetune --wait```
Codex can run the same commands from this repository after you provide the URLand token.
## Profiles
- `colab/configs/dmhy_regex_finetune.json`: default regex tokenizer fine-tune from the published root checkpoint.- `colab/configs/dmhy_char_train.json`: character tokenizer training from scratch.
You can submit a local edited profile instead of a remote profile:
```powershellpython colab_client.py submit --config colab/configs/dmhy_regex_finetune.json --wait```
The worker writes per-job logs under:
```textMyDrive/AniFileBERT/worker/jobs/<job-id>/```
The training runner writes:
```textMyDrive/AniFileBERT/checkpoints/<profile-name>/MyDrive/AniFileBERT/last_run_manifest.json```123456789101112131415161718192021222324252627282930313233343536373839404142{ "name": "dmhy-char-train", "repo_url": "https://huggingface.co/ModerRAS/AniFileBERT", "repo_ref": "main", "repo_dir": "/content/AniFileBERT", "drive_root": "/content/drive/MyDrive/AniFileBERT", "mount_drive": true, "pull": true, "install": { "requirements": true, "git_lfs": true, "extra_packages": [] }, "training": { "tokenizer": "char", "data_file": "datasets/AnimeName/dmhy_weak_char.jsonl", "vocab_file": "datasets/AnimeName/vocab.char.json", "save_dir": "{drive_root}/checkpoints/{name}", "init_model_dir": null, "epochs": 1, "batch_size": 128, "learning_rate": 0.0003, "warmup_steps": 300, "train_split": 0.9, "max_seq_length": 128, "seed": 42, "resume_from_checkpoint": "auto", "checkpoint_steps": 1000, "save_total_limit": 3 }, "export": { "enabled": true, "required": false, "output": "{save_dir}/exports/anime_filename_parser.onnx", "max_length": "{max_seq_length}" }, "smoke": { "enabled": true, "required": true, "sample": "Witch.Hat.Atelier.S01E07.1080p.NF.WEB-DL.JPN.AAC2.0.H.264.MSubs-ToonsHub" }}123456789101112131415161718192021222324252627282930313233343536373839404142{ "name": "dmhy-regex-finetune", "repo_url": "https://huggingface.co/ModerRAS/AniFileBERT", "repo_ref": "main", "repo_dir": "/content/AniFileBERT", "drive_root": "/content/drive/MyDrive/AniFileBERT", "mount_drive": true, "pull": true, "install": { "requirements": true, "git_lfs": true, "extra_packages": [] }, "training": { "tokenizer": "regex", "data_file": "datasets/AnimeName/dmhy_weak.jsonl", "vocab_file": "datasets/AnimeName/vocab.json", "save_dir": "{drive_root}/checkpoints/{name}", "init_model_dir": ".", "epochs": 1, "batch_size": 128, "learning_rate": 0.0003, "warmup_steps": 300, "train_split": 0.9, "max_seq_length": 64, "seed": 42, "resume_from_checkpoint": "auto", "checkpoint_steps": 1000, "save_total_limit": 3 }, "export": { "enabled": true, "required": false, "output": "{save_dir}/exports/anime_filename_parser.onnx", "max_length": "{max_seq_length}" }, "smoke": { "enabled": true, "required": true, "sample": "Witch.Hat.Atelier.S01E07.1080p.NF.WEB-DL.JPN.AAC2.0.H.264.MSubs-ToonsHub" }}123456789101112131415161718192021222324252627282930313233343536373839404142434445{ "nbformat": 4, "nbformat_minor": 5, "metadata": { "colab": { "provenance": [], "gpuType": "T4" }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" }, "accelerator": "GPU" }, "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# AniFileBERT Colab Worker\n", "\n", "Run the next cell in a GPU runtime. Keep it running while Codex submits training jobs. If free Colab disconnects, open the notebook again and rerun the cell; default profiles resume from the latest Drive checkpoint." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from google.colab import drive\n", "drive.mount('/content/drive')\n", "\n", "!git clone --recursive https://huggingface.co/ModerRAS/AniFileBERT /content/AniFileBERT || true\n", "%cd /content/AniFileBERT\n", "!git pull --ff-only || true\n", "!git submodule update --init --recursive\n", "!python colab_worker.py\n" ] } ]}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184# -*- coding: utf-8 -*-"""Local client for controlling an active AniFileBERT Colab worker.
The worker still has to be started manually in Colab, but once it prints apublic URL and token this client lets Codex submit training jobs, tail logs, andinspect status from the local workspace."""
from __future__ import annotations
import argparseimport jsonimport osfrom pathlib import Pathimport sysimport timefrom typing import Anyimport urllib.errorimport urllib.parseimport urllib.request
TERMINAL_STATES = {"success", "failed", "cancelled"}
def load_json(path: str) -> Any: return json.loads(Path(path).read_text(encoding="utf-8"))
class ColabClient: def __init__(self, base_url: str, token: str, timeout: int = 30): self.base_url = base_url.rstrip("/") self.token = token self.timeout = timeout
def request(self, method: str, path: str, payload: Any | None = None) -> Any: url = self.base_url + path data = None headers = {"Authorization": f"Bearer {self.token}"} if payload is not None: data = json.dumps(payload, ensure_ascii=False).encode("utf-8") headers["Content-Type"] = "application/json; charset=utf-8"
req = urllib.request.Request(url, data=data, headers=headers, method=method) try: with urllib.request.urlopen(req, timeout=self.timeout) as response: return json.loads(response.read().decode("utf-8")) except urllib.error.HTTPError as exc: body = exc.read().decode("utf-8", errors="replace") raise RuntimeError(f"{method} {url} failed: HTTP {exc.code}: {body}") from exc
def health(self) -> Any: return self.request("GET", "/health")
def submit(self, payload: dict[str, Any]) -> Any: return self.request("POST", "/jobs", payload)
def jobs(self) -> Any: return self.request("GET", "/jobs")
def status(self, job_id: str) -> Any: return self.request("GET", f"/jobs/{job_id}")
def logs(self, job_id: str, tail: int) -> Any: query = urllib.parse.urlencode({"tail": tail}) return self.request("GET", f"/jobs/{job_id}/logs?{query}")
def manifest(self, job_id: str) -> Any: return self.request("GET", f"/jobs/{job_id}/manifest")
def cancel(self, job_id: str) -> Any: return self.request("POST", f"/jobs/{job_id}/cancel", {})
def print_json(data: Any) -> None: print(json.dumps(data, ensure_ascii=False, indent=2))
def require_connection(args: argparse.Namespace) -> ColabClient: url = args.url or os.environ.get("ANIFILEBERT_COLAB_URL") token = args.token or os.environ.get("ANIFILEBERT_COLAB_TOKEN") if not url or not token: raise SystemExit( "Set ANIFILEBERT_COLAB_URL and ANIFILEBERT_COLAB_TOKEN, " "or pass --url and --token." ) return ColabClient(url, token, timeout=args.timeout)
def build_submit_payload(args: argparse.Namespace) -> dict[str, Any]: payload: dict[str, Any] = {} if args.config: payload["config"] = load_json(args.config) if args.profile: payload["profile"] = args.profile extra_args = list(args.args or []) + list(args.extra_args or []) if extra_args: payload["args"] = extra_args if not payload: payload["profile"] = "dmhy_regex_finetune" return payload
def wait_for_job(client: ColabClient, job_id: str, poll: int, tail: int) -> dict[str, Any]: last_status = None while True: status = client.status(job_id) if status.get("status") != last_status: print_json(status) last_status = status.get("status") logs = client.logs(job_id, tail=tail) log_text = logs.get("log", "") if log_text: print("\n--- log tail ---") print(log_text.rstrip()) if status.get("status") in TERMINAL_STATES: return status time.sleep(poll)
def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser(description="Control an active AniFileBERT Colab worker") parser.add_argument("--url", help="Worker URL, or ANIFILEBERT_COLAB_URL") parser.add_argument("--token", help="Worker token, or ANIFILEBERT_COLAB_TOKEN") parser.add_argument("--timeout", type=int, default=30)
subparsers = parser.add_subparsers(dest="command", required=True)
subparsers.add_parser("health", help="Check worker health") subparsers.add_parser("jobs", help="List known jobs")
submit = subparsers.add_parser("submit", help="Submit a training job") submit.add_argument("--config", help="Local JSON config to send to the worker") submit.add_argument("--profile", help="Remote profile name under colab/configs") submit.add_argument("--arg", dest="args", action="append", default=[], help="Extra arg for colab_train.py") submit.add_argument("--wait", action="store_true", help="Poll until the job finishes") submit.add_argument("--poll", type=int, default=60, help="Polling interval in seconds") submit.add_argument("--tail", type=int, default=80, help="Log lines to show while waiting") submit.add_argument("extra_args", nargs=argparse.REMAINDER, help="Arguments after -- are passed to colab_train.py")
status = subparsers.add_parser("status", help="Show job status") status.add_argument("job_id")
logs = subparsers.add_parser("logs", help="Show job logs") logs.add_argument("job_id") logs.add_argument("--tail", type=int, default=200)
manifest = subparsers.add_parser("manifest", help="Show job manifest") manifest.add_argument("job_id")
cancel = subparsers.add_parser("cancel", help="Cancel a running job") cancel.add_argument("job_id")
return parser.parse_args()
def main() -> None: args = parse_args() client = require_connection(args)
if args.command == "health": print_json(client.health()) elif args.command == "jobs": print_json(client.jobs()) elif args.command == "submit": job = client.submit(build_submit_payload(args)) print_json(job) if args.wait: final_status = wait_for_job(client, job["job_id"], poll=args.poll, tail=args.tail) if final_status.get("status") != "success": sys.exit(1) elif args.command == "status": print_json(client.status(args.job_id)) elif args.command == "logs": print(client.logs(args.job_id, args.tail).get("log", ""), end="") elif args.command == "manifest": print_json(client.manifest(args.job_id)) elif args.command == "cancel": print_json(client.cancel(args.job_id))
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543# -*- coding: utf-8 -*-"""Codex-friendly Google Colab runner for AniFileBERT training.
Typical Colab usage:
python colab_train.py --config colab/configs/dmhy_regex_finetune.json
This script keeps the Colab side reproducible by putting run parameters in JSONprofiles. It can clone/update the repo, mount Drive, install dependencies,train, optionally export ONNX, run an inference smoke check, and write a runmanifest that Codex can inspect later."""
from __future__ import annotations
import argparseimport copyimport datetime as dtimport jsonimport osfrom pathlib import Pathimport shleximport shutilimport subprocessimport sysimport tracebackfrom typing import Any, Mapping, Sequenceimport urllib.request
DEFAULT_CONFIG: dict[str, Any] = { "name": "dmhy-regex-finetune", "repo_url": "https://huggingface.co/ModerRAS/AniFileBERT", "repo_ref": "main", "repo_dir": "/content/AniFileBERT", "drive_root": "/content/drive/MyDrive/AniFileBERT", "mount_drive": True, "pull": True, "install": { "requirements": True, "git_lfs": True, "extra_packages": [], }, "training": { "tokenizer": "regex", "data_file": "datasets/AnimeName/dmhy_weak.jsonl", "vocab_file": "datasets/AnimeName/vocab.json", "save_dir": "{drive_root}/checkpoints/{name}", "init_model_dir": ".", "epochs": 1, "batch_size": 128, "learning_rate": 0.0003, "warmup_steps": 300, "train_split": 0.9, "max_seq_length": 64, "seed": 42, "limit_samples": None, "rebuild_vocab": False, "max_vocab_size": None, "resume_from_checkpoint": "auto", "checkpoint_steps": 1000, "save_total_limit": 3, "cpu": False, "no_shuffle": False, "extra_args": [], }, "export": { "enabled": True, "required": False, "output": "{save_dir}/exports/anime_filename_parser.onnx", "max_length": "{max_seq_length}", "sample": "Witch.Hat.Atelier.S01E07.1080p.NF.WEB-DL.JPN.AAC2.0.H.264.MSubs-ToonsHub", "android_assets_dir": None, }, "smoke": { "enabled": True, "required": True, "sample": "Witch.Hat.Atelier.S01E07.1080p.NF.WEB-DL.JPN.AAC2.0.H.264.MSubs-ToonsHub", }, "artifacts": { "manifest": "{save_dir}/colab_run_manifest.json", "latest_manifest": "{drive_root}/last_run_manifest.json", },}
COMMAND_LOG: list[dict[str, Any]] = []
class SafeFormatDict(dict): def __missing__(self, key: str) -> str: return "{" + key + "}"
def utc_now() -> str: return dt.datetime.now(dt.timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")
def deep_merge(base: Mapping[str, Any], override: Mapping[str, Any]) -> dict[str, Any]: merged = copy.deepcopy(dict(base)) for key, value in override.items(): if isinstance(value, Mapping) and isinstance(merged.get(key), Mapping): merged[key] = deep_merge(merged[key], value) else: merged[key] = copy.deepcopy(value) return merged
def render_templates(value: Any, context: Mapping[str, Any]) -> Any: if isinstance(value, str): return value.format_map(SafeFormatDict(context)) if isinstance(value, list): return [render_templates(item, context) for item in value] if isinstance(value, dict): return {key: render_templates(item, context) for key, item in value.items()} return value
def command_text(args: str | Sequence[Any]) -> str: if isinstance(args, str): return args return " ".join(shlex.quote(str(arg)) for arg in args)
def run( args: str | Sequence[Any], *, cwd: str | os.PathLike[str] | None = None, check: bool = True, dry_run: bool = False,) -> int: text = command_text(args) entry: dict[str, Any] = { "cmd": text, "cwd": os.fspath(cwd) if cwd is not None else None, "started_at": utc_now(), "dry_run": dry_run, } COMMAND_LOG.append(entry) print(f"\n$ {text}") if dry_run: entry["returncode"] = 0 entry["finished_at"] = utc_now() return 0
proc = subprocess.Popen( args, cwd=cwd, shell=isinstance(args, str), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, encoding="utf-8", errors="replace", bufsize=1, ) assert proc.stdout is not None for line in proc.stdout: print(line, end="") proc.wait() entry["returncode"] = proc.returncode entry["finished_at"] = utc_now() if check and proc.returncode != 0: raise RuntimeError(f"Command failed with exit code {proc.returncode}: {text}") return proc.returncode
def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser(description="Run AniFileBERT training in Colab") parser.add_argument("--config", help="JSON profile path or URL") parser.add_argument("--profile", help="Profile name under colab/configs without .json") parser.add_argument("--repo-url", help="Override repository URL") parser.add_argument("--repo-ref", help="Override branch, tag, or commit to checkout") parser.add_argument("--repo-dir", help="Override Colab repository directory") parser.add_argument("--drive-root", help="Override Google Drive output root") parser.add_argument("--save-dir", help="Override checkpoint output directory") parser.add_argument("--epochs", type=float, help="Override training epochs") parser.add_argument("--batch-size", type=int, help="Override per-device batch size") parser.add_argument("--learning-rate", type=float, help="Override learning rate") parser.add_argument("--warmup-steps", type=int, help="Override warmup steps") parser.add_argument("--limit-samples", type=int, help="Use only the first N dataset rows") parser.add_argument("--skip-install", action="store_true", help="Do not install pip or git-lfs dependencies") parser.add_argument("--skip-export", action="store_true", help="Do not run ONNX export") parser.add_argument("--skip-smoke", action="store_true", help="Do not run inference smoke check") parser.add_argument("--no-mount-drive", action="store_true", help="Do not mount Google Drive") parser.add_argument("--no-pull", action="store_true", help="Do not pull an existing checkout") parser.add_argument("--dry-run", action="store_true", help="Print commands and write no training outputs") parser.add_argument("--print-config", action="store_true", help="Print resolved config before running") return parser.parse_args()
def load_json_source(source: str | None, *, required: bool) -> dict[str, Any]: if not source: return {} if source.startswith(("http://", "https://")): with urllib.request.urlopen(source) as response: return json.loads(response.read().decode("utf-8"))
candidates = [Path(source), Path(__file__).resolve().parent / source] for candidate in candidates: if candidate.is_file(): return json.loads(candidate.read_text(encoding="utf-8")) if required: raise FileNotFoundError(f"Config file not found: {source}") return {}
def load_config(args: argparse.Namespace) -> dict[str, Any]: config_source = args.config required = bool(args.config) if config_source is None and args.profile: config_source = os.fspath(Path("colab") / "configs" / f"{args.profile}.json") required = True
profile_config = load_json_source(config_source, required=required) config = deep_merge(DEFAULT_CONFIG, profile_config)
if args.repo_url: config["repo_url"] = args.repo_url if args.repo_ref: config["repo_ref"] = args.repo_ref if args.repo_dir: config["repo_dir"] = args.repo_dir if args.drive_root: config["drive_root"] = args.drive_root if args.no_mount_drive: config["mount_drive"] = False if args.no_pull: config["pull"] = False if args.skip_install: config["install"]["requirements"] = False config["install"]["git_lfs"] = False config["install"]["extra_packages"] = [] if args.skip_export: config["export"]["enabled"] = False if args.skip_smoke: config["smoke"]["enabled"] = False
training = config["training"] for arg_name, key in [ ("save_dir", "save_dir"), ("epochs", "epochs"), ("batch_size", "batch_size"), ("learning_rate", "learning_rate"), ("warmup_steps", "warmup_steps"), ("limit_samples", "limit_samples"), ]: value = getattr(args, arg_name) if value is not None: training[key] = value
return resolve_config(config)
def resolve_config(config: dict[str, Any]) -> dict[str, Any]: context: dict[str, Any] = { "name": config["name"], "repo_url": config["repo_url"], "repo_ref": config.get("repo_ref") or "", "repo_dir": config["repo_dir"], "drive_root": config["drive_root"], }
training = render_templates(config["training"], context) context.update(training) if not training.get("save_dir"): training["save_dir"] = os.path.join(config["drive_root"], "checkpoints", config["name"]) training = render_templates(training, {**context, **training}) context.update(training) context["save_dir"] = training["save_dir"] context["final_model_dir"] = os.path.join(training["save_dir"], "final")
resolved = copy.deepcopy(config) resolved["training"] = training resolved["export"] = render_templates(config["export"], context) resolved["smoke"] = render_templates(config["smoke"], context) resolved["artifacts"] = render_templates(config["artifacts"], context) return resolved
def maybe_mount_drive(config: Mapping[str, Any]) -> None: if not config.get("mount_drive", True): print("Google Drive mount disabled.") return try: from google.colab import drive # type: ignore except Exception: print("[WARN] google.colab is unavailable; skipping Drive mount.") return print("Mounting Google Drive...") drive.mount("/content/drive")
def install_git_lfs_if_needed(config: Mapping[str, Any], *, dry_run: bool) -> None: if not config.get("install", {}).get("git_lfs", True): return if shutil.which("git-lfs"): run(["git", "lfs", "install"], check=False, dry_run=dry_run) return if Path("/content").exists(): print("Installing git-lfs for Hugging Face model artifacts...") run(["apt-get", "update"], check=False, dry_run=dry_run) run(["apt-get", "install", "-y", "git-lfs"], dry_run=dry_run) run(["git", "lfs", "install"], check=False, dry_run=dry_run) else: print("[WARN] git-lfs not found. Existing LFS pointers may not contain model weights.")
def is_git_repo(path: Path) -> bool: return (path / ".git").exists()
def prepare_repo(config: Mapping[str, Any], *, dry_run: bool) -> Path: repo_dir = Path(config["repo_dir"]) repo_url = config["repo_url"] repo_ref = config.get("repo_ref")
if not is_git_repo(repo_dir): if repo_dir.exists() and any(repo_dir.iterdir()): raise RuntimeError(f"{repo_dir} exists but is not a git checkout") repo_dir.parent.mkdir(parents=True, exist_ok=True) run(["git", "clone", "--recursive", repo_url, os.fspath(repo_dir)], dry_run=dry_run) else: print(f"Using existing repository checkout: {repo_dir}")
if repo_ref: run(["git", "fetch", "--all", "--tags"], cwd=repo_dir, check=False, dry_run=dry_run) run(["git", "checkout", str(repo_ref)], cwd=repo_dir, dry_run=dry_run)
if config.get("pull", True): run(["git", "pull", "--ff-only"], cwd=repo_dir, check=False, dry_run=dry_run)
run(["git", "submodule", "update", "--init", "--recursive"], cwd=repo_dir, dry_run=dry_run) if shutil.which("git-lfs"): run(["git", "lfs", "pull"], cwd=repo_dir, check=False, dry_run=dry_run)
return repo_dir
def install_python_deps(config: Mapping[str, Any], repo_dir: Path, *, dry_run: bool) -> None: install = config.get("install", {}) if install.get("requirements", True): run([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"], cwd=repo_dir, dry_run=dry_run) for package in install.get("extra_packages", []): run([sys.executable, "-m", "pip", "install", str(package)], cwd=repo_dir, dry_run=dry_run)
def verify_runtime(repo_dir: Path, *, dry_run: bool) -> None: run(["nvidia-smi"], cwd=repo_dir, check=False, dry_run=dry_run) run( [ sys.executable, "-c", "import torch; print(f'PyTorch {torch.__version__}, CUDA available: {torch.cuda.is_available()}')", ], cwd=repo_dir, check=False, dry_run=dry_run, )
def add_arg(cmd: list[str], flag: str, value: Any) -> None: if value is None or value is False: return if value is True: cmd.append(flag) else: cmd.extend([flag, str(value)])
def build_train_command(training: Mapping[str, Any]) -> list[str]: cmd = [sys.executable, "train.py"] for key, flag in [ ("tokenizer", "--tokenizer"), ("data_file", "--data-file"), ("vocab_file", "--vocab-file"), ("save_dir", "--save-dir"), ("init_model_dir", "--init-model-dir"), ("epochs", "--epochs"), ("batch_size", "--batch-size"), ("learning_rate", "--learning-rate"), ("warmup_steps", "--warmup-steps"), ("train_split", "--train-split"), ("max_seq_length", "--max-seq-length"), ("seed", "--seed"), ("limit_samples", "--limit-samples"), ("max_vocab_size", "--max-vocab-size"), ("resume_from_checkpoint", "--resume-from-checkpoint"), ("checkpoint_steps", "--checkpoint-steps"), ("save_total_limit", "--save-total-limit"), ]: add_arg(cmd, flag, training.get(key)) add_arg(cmd, "--rebuild-vocab", training.get("rebuild_vocab")) add_arg(cmd, "--cpu", training.get("cpu")) add_arg(cmd, "--no-shuffle", training.get("no_shuffle")) cmd.extend(str(arg) for arg in training.get("extra_args", [])) return cmd
def run_training(config: Mapping[str, Any], repo_dir: Path, *, dry_run: bool) -> None: training = config["training"] if not dry_run: Path(training["save_dir"]).mkdir(parents=True, exist_ok=True) run(build_train_command(training), cwd=repo_dir, dry_run=dry_run)
def run_export(config: Mapping[str, Any], repo_dir: Path, *, dry_run: bool) -> None: export = config["export"] if not export.get("enabled", True): print("ONNX export disabled.") return cmd = [ sys.executable, "export_onnx.py", "--model-dir", os.path.join(config["training"]["save_dir"], "final"), "--output", export["output"], "--max-length", str(export["max_length"]), ] add_arg(cmd, "--sample", export.get("sample")) add_arg(cmd, "--android-assets-dir", export.get("android_assets_dir")) try: run(cmd, cwd=repo_dir, dry_run=dry_run) except Exception: if export.get("required", False): raise print("[WARN] ONNX export failed, but export.required is false.") traceback.print_exc()
def run_smoke(config: Mapping[str, Any], repo_dir: Path, *, dry_run: bool) -> None: smoke = config["smoke"] if not smoke.get("enabled", True): print("Inference smoke check disabled.") return cmd = [ sys.executable, "inference.py", "--model-dir", os.path.join(config["training"]["save_dir"], "final"), smoke["sample"], ] try: run(cmd, cwd=repo_dir, dry_run=dry_run) except Exception: if smoke.get("required", True): raise print("[WARN] Smoke check failed, but smoke.required is false.") traceback.print_exc()
def git_commit(repo_dir: Path, *, dry_run: bool) -> str | None: if dry_run: return None try: return subprocess.check_output( ["git", "rev-parse", "HEAD"], cwd=repo_dir, text=True, encoding="utf-8", errors="replace", ).strip() except Exception: return None
def write_json(path: str | os.PathLike[str], data: Mapping[str, Any], *, dry_run: bool) -> None: print(f"Writing manifest: {path}") if dry_run: return output_path = Path(path) output_path.parent.mkdir(parents=True, exist_ok=True) output_path.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8")
def write_manifests( config: Mapping[str, Any], repo_dir: Path, *, status: str, started_at: str, error: str | None, dry_run: bool,) -> None: save_dir = config["training"]["save_dir"] manifest = { "status": status, "name": config["name"], "started_at": started_at, "finished_at": utc_now(), "repo_url": config["repo_url"], "repo_ref": config.get("repo_ref"), "repo_commit": git_commit(repo_dir, dry_run=dry_run), "repo_dir": os.fspath(repo_dir), "save_dir": save_dir, "final_model_dir": os.path.join(save_dir, "final"), "onnx_output": config["export"].get("output") if config["export"].get("enabled") else None, "config": config, "commands": COMMAND_LOG, "error": error, } artifacts = config["artifacts"] write_json(artifacts["manifest"], manifest, dry_run=dry_run) if artifacts.get("latest_manifest"): write_json(artifacts["latest_manifest"], manifest, dry_run=dry_run)
def main() -> None: args = parse_args() started_at = utc_now() config = load_config(args)
if args.print_config: print(json.dumps(config, ensure_ascii=False, indent=2))
repo_dir = Path(config["repo_dir"]) status = "failed" error: str | None = None try: maybe_mount_drive(config) install_git_lfs_if_needed(config, dry_run=args.dry_run) repo_dir = prepare_repo(config, dry_run=args.dry_run) install_python_deps(config, repo_dir, dry_run=args.dry_run) verify_runtime(repo_dir, dry_run=args.dry_run) run_training(config, repo_dir, dry_run=args.dry_run) run_export(config, repo_dir, dry_run=args.dry_run) run_smoke(config, repo_dir, dry_run=args.dry_run) status = "success" except Exception as exc: error = f"{type(exc).__name__}: {exc}" raise finally: write_manifests(config, repo_dir, status=status, started_at=started_at, error=error, dry_run=args.dry_run)
print("\nDone.") print(f"Final model: {os.path.join(config['training']['save_dir'], 'final')}") print(f"Manifest: {config['artifacts']['manifest']}")
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446# -*- coding: utf-8 -*-"""Small HTTP worker for running AniFileBERT training jobs on Google Colab.
Start this inside a Colab runtime:
python colab_worker.py
The worker exposes a token-protected local HTTP API and, by default, starts aCloudflare Quick Tunnel so Codex on your local machine can submit jobs."""
from __future__ import annotations
import argparseimport jsonimport osfrom pathlib import Pathimport platformimport reimport secretsimport shutilimport signalimport subprocessimport sysimport threadingimport timeimport tracebackfrom http import HTTPStatusfrom http.server import BaseHTTPRequestHandler, ThreadingHTTPServerfrom typing import Anyfrom urllib.parse import parse_qs, urlparseimport urllib.request
TERMINAL_STATES = {"success", "failed", "cancelled"}TUNNEL_URL_RE = re.compile(r"https://[-a-zA-Z0-9.]+\.trycloudflare\.com")
def utc_timestamp() -> str: return time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
def json_dumps(data: Any) -> str: return json.dumps(data, ensure_ascii=False, indent=2)
def read_tail(path: Path, lines: int) -> str: if not path.is_file(): return "" if lines <= 0: return path.read_text(encoding="utf-8", errors="replace")
chunk_size = 8192 data = b"" with path.open("rb") as f: f.seek(0, os.SEEK_END) pos = f.tell() while pos > 0 and data.count(b"\n") <= lines: read_size = min(chunk_size, pos) pos -= read_size f.seek(pos) data = f.read(read_size) + data return b"\n".join(data.splitlines()[-lines:]).decode("utf-8", errors="replace")
def download_cloudflared(path: Path) -> Path: if path.is_file(): return path
existing = shutil.which("cloudflared") if existing: return Path(existing)
arch = platform.machine().lower() if arch in {"x86_64", "amd64"}: suffix = "linux-amd64" elif arch in {"aarch64", "arm64"}: suffix = "linux-arm64" else: raise RuntimeError(f"Unsupported CPU architecture for cloudflared: {arch}")
url = f"https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-{suffix}" print(f"Downloading cloudflared: {url}", flush=True) path.parent.mkdir(parents=True, exist_ok=True) urllib.request.urlretrieve(url, path) path.chmod(0o755) return path
class WorkerState: def __init__(self, repo_dir: Path, jobs_dir: Path): self.repo_dir = repo_dir self.jobs_dir = jobs_dir self.jobs_dir.mkdir(parents=True, exist_ok=True) self.jobs: dict[str, dict[str, Any]] = {} self.lock = threading.RLock()
def list_jobs(self) -> list[dict[str, Any]]: with self.lock: return [self._public_job(job) for job in self.jobs.values()]
def get_job(self, job_id: str) -> dict[str, Any] | None: with self.lock: job = self.jobs.get(job_id) return self._public_job(job) if job else None
def get_job_internal(self, job_id: str) -> dict[str, Any] | None: with self.lock: return self.jobs.get(job_id)
def active_job(self) -> dict[str, Any] | None: with self.lock: for job in self.jobs.values(): if job["status"] not in TERMINAL_STATES: return job return None
def start_job(self, payload: dict[str, Any]) -> dict[str, Any]: with self.lock: active = self.active_job() if active is not None: raise RuntimeError(f"Job already running: {active['job_id']}")
job_id = time.strftime("%Y%m%d-%H%M%S", time.gmtime()) + "-" + secrets.token_hex(3) job_dir = self.jobs_dir / job_id job_dir.mkdir(parents=True, exist_ok=True) log_path = job_dir / "worker.log" config_path: Path | None = None
cmd = [sys.executable, "colab_train.py"] config = self._job_config(payload) config.setdefault("artifacts", {}) config["artifacts"]["manifest"] = os.fspath(job_dir / "colab_run_manifest.json") config_path = job_dir / "config.json" config_path.write_text(json_dumps(config), encoding="utf-8") cmd.extend(["--config", os.fspath(config_path)])
for arg in payload.get("args", []): cmd.append(str(arg))
job = { "job_id": job_id, "status": "queued", "created_at": utc_timestamp(), "started_at": None, "finished_at": None, "returncode": None, "cmd": cmd, "cwd": os.fspath(self.repo_dir), "job_dir": os.fspath(job_dir), "log_path": os.fspath(log_path), "config_path": os.fspath(config_path) if config_path else None, "error": None, "process": None, } self.jobs[job_id] = job
thread = threading.Thread(target=self._run_job, args=(job_id,), daemon=True) thread.start() return self._public_job(job)
def _job_config(self, payload: dict[str, Any]) -> dict[str, Any]: if "config" in payload: return json.loads(json.dumps(payload["config"], ensure_ascii=False))
profile = str(payload.get("profile", "dmhy_regex_finetune")) profile_path = self.repo_dir / "colab" / "configs" / f"{profile}.json" if not profile_path.is_file(): raise FileNotFoundError(f"Profile not found: {profile_path}") return json.loads(profile_path.read_text(encoding="utf-8"))
def cancel_job(self, job_id: str) -> dict[str, Any]: with self.lock: job = self.jobs.get(job_id) if job is None: raise KeyError(job_id) process: subprocess.Popen[str] | None = job.get("process") if job["status"] in TERMINAL_STATES: return self._public_job(job) job["status"] = "cancelled" job["finished_at"] = utc_timestamp()
if process and process.poll() is None: try: os.killpg(os.getpgid(process.pid), signal.SIGTERM) except Exception: process.terminate() return self.get_job(job_id) or {}
def _run_job(self, job_id: str) -> None: job = self.get_job_internal(job_id) if job is None: return log_path = Path(job["log_path"]) try: with self.lock: job["status"] = "running" job["started_at"] = utc_timestamp()
with log_path.open("w", encoding="utf-8", errors="replace") as log: log.write(f"job_id={job_id}\n") log.write(f"cwd={job['cwd']}\n") log.write("$ " + " ".join(job["cmd"]) + "\n\n") log.flush()
process = subprocess.Popen( job["cmd"], cwd=job["cwd"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, encoding="utf-8", errors="replace", bufsize=1, preexec_fn=os.setsid if hasattr(os, "setsid") else None, ) with self.lock: job["process"] = process
assert process.stdout is not None for line in process.stdout: log.write(line) log.flush() print(line, end="", flush=True) process.wait()
with self.lock: job["returncode"] = process.returncode if job["status"] != "cancelled": job["status"] = "success" if process.returncode == 0 else "failed" job["finished_at"] = utc_timestamp() job["process"] = None except Exception as exc: with log_path.open("a", encoding="utf-8", errors="replace") as log: traceback.print_exc(file=log) with self.lock: job["status"] = "failed" job["finished_at"] = utc_timestamp() job["error"] = f"{type(exc).__name__}: {exc}" job["process"] = None
def _public_job(self, job: dict[str, Any]) -> dict[str, Any]: public = {key: value for key, value in job.items() if key != "process"} return public
def make_handler(state: WorkerState, token: str): class Handler(BaseHTTPRequestHandler): server_version = "AniFileBERTColabWorker/1.0"
def log_message(self, fmt: str, *args: Any) -> None: print(f"[{utc_timestamp()}] {self.address_string()} {fmt % args}", flush=True)
def do_GET(self) -> None: self._handle("GET")
def do_POST(self) -> None: self._handle("POST")
def _handle(self, method: str) -> None: parsed = urlparse(self.path) path = parsed.path.rstrip("/") or "/" parts = [part for part in path.split("/") if part] try: if not self._authorized(): self._send({"error": "unauthorized"}, HTTPStatus.UNAUTHORIZED) return
if method == "GET" and path == "/health": self._send( { "ok": True, "repo_dir": os.fspath(state.repo_dir), "jobs_dir": os.fspath(state.jobs_dir), "active_job": state.active_job()["job_id"] if state.active_job() else None, } ) return
if method == "GET" and path == "/jobs": self._send({"jobs": state.list_jobs()}) return
if method == "POST" and path == "/jobs": payload = self._read_json() job = state.start_job(payload) self._send(job, HTTPStatus.ACCEPTED) return
if len(parts) >= 2 and parts[0] == "jobs": job_id = parts[1] if method == "GET" and len(parts) == 2: job = state.get_job(job_id) if job is None: self._send({"error": "job not found"}, HTTPStatus.NOT_FOUND) else: self._send(job) return
if method == "GET" and len(parts) == 3 and parts[2] == "logs": query = parse_qs(parsed.query) tail = int(query.get("tail", ["200"])[0]) job = state.get_job_internal(job_id) if job is None: self._send({"error": "job not found"}, HTTPStatus.NOT_FOUND) else: self._send({"job_id": job_id, "log": read_tail(Path(job["log_path"]), tail)}) return
if method == "GET" and len(parts) == 3 and parts[2] == "manifest": job = state.get_job_internal(job_id) if job is None: self._send({"error": "job not found"}, HTTPStatus.NOT_FOUND) else: manifest = self._find_manifest(job) if manifest is None: self._send({"error": "manifest not found"}, HTTPStatus.NOT_FOUND) else: self._send(json.loads(manifest.read_text(encoding="utf-8"))) return
if method == "POST" and len(parts) == 3 and parts[2] == "cancel": try: self._send(state.cancel_job(job_id)) except KeyError: self._send({"error": "job not found"}, HTTPStatus.NOT_FOUND) return
self._send({"error": "not found"}, HTTPStatus.NOT_FOUND) except Exception as exc: traceback.print_exc() self._send({"error": f"{type(exc).__name__}: {exc}"}, HTTPStatus.INTERNAL_SERVER_ERROR)
def _authorized(self) -> bool: header = self.headers.get("Authorization", "") if header == f"Bearer {token}": return True return self.headers.get("X-Colab-Token") == token
def _read_json(self) -> dict[str, Any]: length = int(self.headers.get("Content-Length", "0")) if length == 0: return {} raw = self.rfile.read(length) return json.loads(raw.decode("utf-8"))
def _find_manifest(self, job: dict[str, Any]) -> Path | None: config_path = job.get("config_path") if config_path and Path(config_path).is_file(): config = json.loads(Path(config_path).read_text(encoding="utf-8")) training = config.get("training", {}) save_dir = training.get("save_dir") if save_dir: manifest = Path(save_dir) / "colab_run_manifest.json" if manifest.is_file(): return manifest job_manifest = Path(job["job_dir"]) / "colab_run_manifest.json" return job_manifest if job_manifest.is_file() else None
def _send(self, data: Any, status: HTTPStatus = HTTPStatus.OK) -> None: raw = json_dumps(data).encode("utf-8") self.send_response(status.value) self.send_header("Content-Type", "application/json; charset=utf-8") self.send_header("Content-Length", str(len(raw))) self.end_headers() self.wfile.write(raw)
return Handler
def start_tunnel(port: int, binary_path: Path) -> subprocess.Popen[str]: cloudflared = download_cloudflared(binary_path) cmd = [ os.fspath(cloudflared), "tunnel", "--url", f"http://127.0.0.1:{port}", "--no-autoupdate", ] proc = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, encoding="utf-8", errors="replace", bufsize=1, )
def pump() -> None: assert proc.stdout is not None for line in proc.stdout: print(line, end="", flush=True) match = TUNNEL_URL_RE.search(line) if match: print("\nCOLAB_WORKER_URL=" + match.group(0), flush=True)
threading.Thread(target=pump, daemon=True).start() return proc
def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser(description="Start the AniFileBERT Colab worker") parser.add_argument("--host", default="127.0.0.1", help="HTTP bind host") parser.add_argument("--port", type=int, default=7860, help="HTTP bind port") parser.add_argument("--repo-dir", default="/content/AniFileBERT", help="AniFileBERT checkout path in Colab") parser.add_argument("--jobs-dir", default="/content/drive/MyDrive/AniFileBERT/worker/jobs") parser.add_argument("--token", default=os.environ.get("ANIFILEBERT_COLAB_TOKEN")) parser.add_argument("--tunnel", choices=["cloudflare", "none"], default="cloudflare") parser.add_argument("--cloudflared-path", default="/tmp/anifilebert-cloudflared") return parser.parse_args()
def main() -> None: args = parse_args() token = args.token or secrets.token_urlsafe(24) repo_dir = Path(args.repo_dir) if not repo_dir.is_dir(): raise RuntimeError(f"Repo directory does not exist: {repo_dir}")
state = WorkerState(repo_dir=repo_dir, jobs_dir=Path(args.jobs_dir)) server = ThreadingHTTPServer((args.host, args.port), make_handler(state, token)) tunnel_proc: subprocess.Popen[str] | None = None
print("=" * 72) print("AniFileBERT Colab worker is starting") print(f"Local URL: http://{args.host}:{args.port}") print(f"COLAB_WORKER_TOKEN={token}") print("Keep this Colab cell running while Codex uses the worker.") print("=" * 72, flush=True)
if args.tunnel == "cloudflare": tunnel_proc = start_tunnel(args.port, Path(args.cloudflared_path)) else: print("Tunnel disabled. Use the local URL from inside the Colab runtime.", flush=True)
try: server.serve_forever() finally: server.server_close() if tunnel_proc and tunnel_proc.poll() is None: tunnel_proc.terminate()
if __name__ == "__main__": main()12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364{ "add_cross_attention": false, "architectures": [ "BertForTokenClassification" ], "attention_probs_dropout_prob": 0.1, "bos_token_id": null, "classifier_dropout": null, "dtype": "float32", "eos_token_id": null, "hidden_act": "gelu", "hidden_dropout_prob": 0.1, "hidden_size": 256, "id2label": { "0": "O", "1": "B-TITLE", "2": "I-TITLE", "3": "B-SEASON", "4": "I-SEASON", "5": "B-EPISODE", "6": "I-EPISODE", "7": "B-SPECIAL", "8": "I-SPECIAL", "9": "B-GROUP", "10": "I-GROUP", "11": "B-RESOLUTION", "12": "I-RESOLUTION", "13": "B-SOURCE", "14": "I-SOURCE" }, "initializer_range": 0.02, "intermediate_size": 1024, "is_decoder": false, "label2id": { "B-EPISODE": 5, "B-GROUP": 9, "B-RESOLUTION": 11, "B-SEASON": 3, "B-SOURCE": 13, "B-SPECIAL": 7, "B-TITLE": 1, "I-EPISODE": 6, "I-GROUP": 10, "I-RESOLUTION": 12, "I-SEASON": 4, "I-SOURCE": 14, "I-SPECIAL": 8, "I-TITLE": 2, "O": 0 }, "layer_norm_eps": 1e-12, "max_position_embeddings": 128, "max_seq_length": 128, "model_type": "bert", "num_attention_heads": 8, "num_hidden_layers": 4, "pad_token_id": 0, "tie_word_embeddings": true, "tokenizer_variant": "char", "transformers_version": "5.8.1", "type_vocab_size": 2, "use_cache": false, "vocab_size": 6199}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374"""Configuration parameters for the anime filename parser pipeline.All hyperparameters are centralized here for easy tuning."""
from dataclasses import dataclass, field
@dataclassclass Config: """Central configuration dataclass for all pipeline parameters."""
# Data synthetic_data_size: int = 100_000 train_split: float = 0.9 data_file: str = "data/synthetic.jsonl"
# Model architecture hidden_size: int = 256 num_hidden_layers: int = 4 num_attention_heads: int = 8 intermediate_size: int = 1024 max_position_embeddings: int = 128 hidden_dropout_prob: float = 0.1 attention_probs_dropout_prob: float = 0.1
# Training hyperparameters batch_size: int = 64 learning_rate: float = 1e-3 num_epochs: int = 8 weight_decay: float = 0.01 warmup_steps: int = 500
# System device: str = "cpu" num_workers: int = 4 save_dir: str = "./checkpoints" log_interval: int = 100
# Sequence max_seq_length: int = 64
# Vocabulary (set dynamically from tokenizer) vocab_size: int = 8000 # placeholder, overridden after tokenizer vocab is built
# Special tokens pad_token: str = "[PAD]" unk_token: str = "[UNK]" cls_token: str = "[CLS]" sep_token: str = "[SEP]"
# BIO label scheme (8 entity types + O) label2id: dict = None id2label: dict = None
def __post_init__(self): if self.label2id is None: self.label2id = { "O": 0, "B-TITLE": 1, "I-TITLE": 2, "B-SEASON": 3, "I-SEASON": 4, "B-EPISODE": 5, "I-EPISODE": 6, "B-SPECIAL": 7, "I-SPECIAL": 8, "B-GROUP": 9, "I-GROUP": 10, "B-RESOLUTION": 11, "I-RESOLUTION": 12, "B-SOURCE": 13, "I-SOURCE": 14, } if self.id2label is None: self.id2label = {v: k for k, v in self.label2id.items()}
@property def num_labels(self) -> int: return len(self.label2id)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201"""Convert token-level anime filename JSONL datasets to character tokens.
Input records must contain parallel ``tokens`` and ``labels`` arrays. Theconverter expands each original token into Unicode code points and projects BIOlabels onto the expanded sequence:
- ``B-X`` keeps ``B-X`` on the first character and uses ``I-X`` afterwards.- ``I-X`` remains ``I-X`` on every character.- ``O`` remains ``O`` on every character.
The script streams both input and output so it can process the full DMHY weakdataset without loading hundreds of MB into memory."""
from __future__ import annotations
import argparseimport jsonfrom collections import Counterfrom datetime import datetime, timezonefrom pathlib import Pathfrom statistics import meanfrom typing import Iterable
SPECIAL_TOKENS = ("[PAD]", "[UNK]", "[CLS]", "[SEP]")
def projected_labels(token: str, label: str) -> tuple[list[str], list[str]]: """Return character tokens and projected BIO labels for one source token.""" chars = list(token) if not chars: return [], []
if label.startswith("B-"): entity = label.split("-", 1)[1] return chars, [label] + [f"I-{entity}"] * (len(chars) - 1) if label.startswith("I-"): return chars, [label] * len(chars) return chars, [label] * len(chars)
def convert_record(record: dict) -> dict: """Convert one JSONL record while preserving non-token metadata.""" tokens = record["tokens"] labels = record["labels"] if len(tokens) != len(labels): raise ValueError( f"token/label length mismatch: {len(tokens)} tokens, {len(labels)} labels" )
char_tokens: list[str] = [] char_labels: list[str] = [] for token, label in zip(tokens, labels): pieces, piece_labels = projected_labels(str(token), str(label)) char_tokens.extend(pieces) char_labels.extend(piece_labels)
converted = dict(record) converted["tokens"] = char_tokens converted["labels"] = char_labels converted["tokenizer_variant"] = "char" converted["source_token_count"] = len(tokens) converted["char_token_count"] = len(char_tokens) return converted
def iter_jsonl(path: Path) -> Iterable[dict]: with path.open("r", encoding="utf-8") as handle: for line_no, line in enumerate(handle, 1): line = line.strip() if not line: continue try: yield json.loads(line) except json.JSONDecodeError as exc: raise ValueError(f"{path}:{line_no}: invalid JSON") from exc
def build_vocab(counter: Counter[str], max_size: int | None = None) -> dict[str, int]: """Build a frequency-sorted vocab with fixed special-token IDs.""" vocab = {token: idx for idx, token in enumerate(SPECIAL_TOKENS)} limit = None if max_size is None else max(max_size - len(vocab), 0) for token, _count in counter.most_common(limit): if token not in vocab: vocab[token] = len(vocab) return vocab
def coverage(counter: Counter[str], vocab: dict[str, int]) -> float: total = sum(counter.values()) if total == 0: return 1.0 covered = sum(count for token, count in counter.items() if token in vocab) return covered / total
def percentile(values: list[int], pct: float) -> int: if not values: return 0 ordered = sorted(values) index = min(len(ordered) - 1, round((pct / 100) * (len(ordered) - 1))) return ordered[index]
def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser(description="Convert JSONL token labels to character labels") parser.add_argument("--input", required=True, help="Input token-level JSONL") parser.add_argument("--output", required=True, help="Output character-level JSONL") parser.add_argument("--vocab-output", required=True, help="Output vocab JSON") parser.add_argument("--manifest-output", default=None, help="Output manifest JSON") parser.add_argument("--max-vocab-size", type=int, default=None, help="Optional vocab cap including special tokens") parser.add_argument("--limit", type=int, default=None, help="Convert only the first N records") parser.add_argument("--progress", type=int, default=50_000, help="Print progress every N records") return parser.parse_args()
def main() -> None: args = parse_args() input_path = Path(args.input) output_path = Path(args.output) vocab_path = Path(args.vocab_output) manifest_path = ( Path(args.manifest_output) if args.manifest_output else output_path.with_suffix(".manifest.json") )
output_path.parent.mkdir(parents=True, exist_ok=True) vocab_path.parent.mkdir(parents=True, exist_ok=True) manifest_path.parent.mkdir(parents=True, exist_ok=True)
char_counter: Counter[str] = Counter() label_counter: Counter[str] = Counter() row_count = 0 source_token_count = 0 char_token_count = 0 lengths: list[int] = [] examples: list[dict] = []
with output_path.open("w", encoding="utf-8", newline="\n") as out: for record in iter_jsonl(input_path): converted = convert_record(record) out.write(json.dumps(converted, ensure_ascii=False, separators=(",", ":")) + "\n")
row_count += 1 source_token_count += converted["source_token_count"] char_len = converted["char_token_count"] char_token_count += char_len lengths.append(char_len) char_counter.update(converted["tokens"]) label_counter.update(converted["labels"]) if len(examples) < 5: examples.append(converted)
if args.limit is not None and row_count >= args.limit: break if args.progress and row_count % args.progress == 0: print(f"converted {row_count:,} rows; unique chars={len(char_counter):,}")
vocab = build_vocab(char_counter, args.max_vocab_size) vocab_path.write_text(json.dumps(vocab, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
manifest = { "created_at": datetime.now(timezone.utc).isoformat(), "input": str(input_path), "output": str(output_path), "vocab_output": str(vocab_path), "tokenizer_variant": "char", "projection": { "B-X": "first char keeps B-X; remaining chars become I-X", "I-X": "all chars keep I-X", "O": "all chars keep O", }, "row_count": row_count, "source_token_count": source_token_count, "char_token_count": char_token_count, "unique_char_count": len(char_counter), "vocab_size": len(vocab), "max_vocab_size": args.max_vocab_size, "vocab_coverage": coverage(char_counter, vocab), "label_counts": dict(label_counter), "char_length": { "min": min(lengths) if lengths else 0, "mean": mean(lengths) if lengths else 0, "p50": percentile(lengths, 50), "p90": percentile(lengths, 90), "p95": percentile(lengths, 95), "p99": percentile(lengths, 99), "max": max(lengths) if lengths else 0, }, "examples": examples, } manifest_path.write_text(json.dumps(manifest, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") print(json.dumps({k: v for k, v in manifest.items() if k != "examples"}, ensure_ascii=False, indent=2))
if __name__ == "__main__": main()123456789101112131415161718192021# DMHY Dataset Snapshot
This directory keeps only small metadata files in git. Large generated JSONLdatasets and model checkpoints are ignored and should be published as releaseassets when they need to be shared.
Current exported SQLite waterline:
- Source DB: `D:\WorkSpace\Python\dmhy-parser\dmhy_anime.db`- Last exported `files.id`: `689304`- Labeled samples: `263042`- Export manifest: `dmhy_weak.manifest.json`
Use `--min-id 689305` for the next incremental export after the crawler hasfinished collecting more rows.
Suggested release assets for this snapshot:
- `dmhy_weak.jsonl`- `mixed_train.jsonl`- `checkpoints/dmhy-finetune/final/`123456789{ "synthetic": "data/synthetic.jsonl", "dmhy": "data/dmhy/dmhy_weak.jsonl", "output": "data/dmhy/ab_mix_100k.jsonl", "synthetic_count": 50000, "dmhy_count": 50000, "total_count": 100000, "seed": 20260513}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531{ "created_at": "2026-05-14T00:01:38.686220+00:00", "source_db": "D:\\WorkSpace\\Python\\dmhy-parser\\dmhy_anime.db", "output": "data\\dmhy\\dmhy_weak_v3.jsonl", "min_file_id": 1, "last_file_id": 1675184, "db_max_file_id_at_export_start": 1675184, "limit": null, "stats": { "scanned_rows": 1675184, "video_rows": 920699, "duplicate_basenames": 162707, "labeled_samples": 632002, "skipped_no_episode": 125346, "skipped_no_title": 0, "skipped_too_short": 643, "skipped_too_long": 1 }, "label_counts": { "B-TITLE": 656614, "I-TITLE": 3786494, "O": 4302284, "B-SEASON": 66497, "B-EPISODE": 632002, "B-RESOLUTION": 305724, "B-SOURCE": 432921, "B-GROUP": 521259, "I-GROUP": 748796, "B-SPECIAL": 42960 }, "vocab_size": 3000, "notes": [ "Rows are a snapshot of files.id <= last_file_id.", "Future incremental export can use --min-id last_file_id+1.", "Weak labels target GROUP, TITLE, SEASON, and EPISODE; media tags are boundary labels/noise." ], "examples": [ { "file_id": 1, "filename": "Witch.Hat.Atelier.S01E07.1080p.NF.WEB-DL.JPN.AAC2.0.H.264.MSubs-ToonsHub", "tokens": [ "Witch", ".", "Hat", ".", "Atelier", ".", "S01", "E07", ".", "1080p", ".", "NF", ".", "WEB-DL", ".", "JP", "N", ".", "AAC", "2", ".", "0", ".", "H.264", ".", "MSubs", "-", "ToonsHub" ], "labels": [ "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SEASON", "B-EPISODE", "O", "B-RESOLUTION", "O", "B-SOURCE", "O", "B-SOURCE", "O", "B-SOURCE", "O", "O", "B-SOURCE", "O", "O", "O", "O", "B-SOURCE", "O", "B-SOURCE", "O", "O" ] }, { "file_id": 2, "filename": "[LoliHouse] Maid-san wa Taberu Dake - 07 [WebRip 1080p HEVC-10bit AAC ASSx2]", "tokens": [ "[", "LoliHouse", "]", " ", "Maid", "-", "san", " ", "wa", " ", "Taberu", " ", "Dake", " ", "-", " ", "07", " ", "[WebRip 1080p HEVC-10bit AAC ASSx2]" ], "labels": [ "O", "B-GROUP", "O", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "O", "O", "B-EPISODE", "O", "O" ] }, { "file_id": 3, "filename": "[ANi] 異世界悠閒農家 2 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]", "tokens": [ "[", "ANi", "]", " ", "異", "世", "界", "悠", "閒", "農", "家", " ", "2", " ", "-", " ", "06", " ", "[1080P]", "[Baha]", "[WEB-DL]", "[AAC AVC]", "[CHT]" ], "labels": [ "O", "B-GROUP", "O", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "O", "O", "B-EPISODE", "O", "B-RESOLUTION", "B-SOURCE", "B-SOURCE", "O", "B-SOURCE" ] }, { "file_id": 4, "filename": "[ANi] 木頭風紀委員和迷你裙 JK 的故事 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]", "tokens": [ "[", "ANi", "]", " ", "木", "頭", "風", "紀", "委", "員", "和", "迷", "你", "裙", " ", "JK", " ", "的", "故", "事", " ", "-", " ", "06", " ", "[1080P]", "[Baha]", "[WEB-DL]", "[AAC AVC]", "[CHT]" ], "labels": [ "O", "B-GROUP", "O", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "O", "O", "B-EPISODE", "O", "B-RESOLUTION", "B-SOURCE", "B-SOURCE", "O", "B-SOURCE" ] }, { "file_id": 5, "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][05][1080P][GB][MP4]", "tokens": [ "[", "KissSub", "]", "[", "Shunkashuutou", " ", "Daikousha", " ", "-", " ", "Haru", " ", "no", " ", "Mai", "]", "[05]", "[1080P]", "[GB]", "[MP4]" ], "labels": [ "O", "B-GROUP", "O", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-EPISODE", "B-RESOLUTION", "B-SOURCE", "O" ] }, { "file_id": 6, "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][06][1080P][GB][MP4]", "tokens": [ "[", "KissSub", "]", "[", "Shunkashuutou", " ", "Daikousha", " ", "-", " ", "Haru", " ", "no", " ", "Mai", "]", "[06]", "[1080P]", "[GB]", "[MP4]" ], "labels": [ "O", "B-GROUP", "O", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-EPISODE", "B-RESOLUTION", "B-SOURCE", "O" ] }, { "file_id": 7, "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][06][1080P][BIG5][MP4]", "tokens": [ "[", "KissSub", "]", "[", "Shunkashuutou", " ", "Daikousha", " ", "-", " ", "Haru", " ", "no", " ", "Mai", "]", "[06]", "[1080P]", "[BIG5]", "[MP4]" ], "labels": [ "O", "B-GROUP", "O", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-EPISODE", "B-RESOLUTION", "B-SOURCE", "O" ] }, { "file_id": 8, "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][05][1080P][BIG5][MP4]", "tokens": [ "[", "KissSub", "]", "[", "Shunkashuutou", " ", "Daikousha", " ", "-", " ", "Haru", " ", "no", " ", "Mai", "]", "[05]", "[1080P]", "[BIG5]", "[MP4]" ], "labels": [ "O", "B-GROUP", "O", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-EPISODE", "B-RESOLUTION", "B-SOURCE", "O" ] }, { "file_id": 9, "filename": "[Airota][Sousou no Frieren][29][1080p AVC AAC][CHT]", "tokens": [ "[", "Airota", "]", "[", "Sousou", " ", "no", " ", "Frieren", "]", "[29]", "[1080p AVC AAC]", "[CHT]" ], "labels": [ "O", "B-GROUP", "O", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-EPISODE", "O", "B-SOURCE" ] }, { "file_id": 10, "filename": "[Airota][Sousou no Frieren][30][1080p AVC AAC][CHT]", "tokens": [ "[", "Airota", "]", "[", "Sousou", " ", "no", " ", "Frieren", "]", "[30]", "[1080p AVC AAC]", "[CHT]" ], "labels": [ "O", "B-GROUP", "O", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-EPISODE", "O", "B-SOURCE" ] } ]}1234567891011121314151617181920212223242526272829303132333435363738{ "created_at": "2026-05-13T15:26:19.767707+00:00", "source_db": "D:\\WorkSpace\\Python\\dmhy-parser\\dmhy_anime.db", "output": "data\\dmhy\\dmhy_weak_new.jsonl", "min_file_id": 689305, "last_file_id": 1675184, "db_max_file_id_at_export_start": 1675184, "limit": null, "stats": { "scanned_rows": 985880, "video_rows": 556778, "duplicate_basenames": 95422, "labeled_samples": 378327, "skipped_no_episode": 82422, "skipped_no_title": 0, "skipped_too_short": 606, "skipped_too_long": 1 }, "label_counts": { "B-GROUP": 306878, "B-TITLE": 390543, "B-EPISODE": 378327, "B-RESOLUTION": 156089, "B-SOURCE": 180428, "O": 1587219, "I-TITLE": 1401899, "B-SPECIAL": 29468, "B-SEASON": 18792, "I-GROUP": 517 }, "vocab_size": 3000, "notes": [ "Rows are a snapshot of files.id <= last_file_id.", "Future incremental export can use --min-id last_file_id+1.", "Weak labels target GROUP, TITLE, SEASON, and EPISODE; media tags are boundary labels/noise." ], "examples": []}123456789{ "total_files": 30, "batches": 2, "batch_size": 15, "min_id": 1, "prompt_file_prefix": "prompt_", "output_file": "D:\\WorkSpace\\Android\\MiruPlay\\tools\\anime_parser\\data\\dmhy\\dmhy_weak_llm.jsonl", "instructions": "For each prompt_NNNNN.txt file, call task(category='deep', load_skills=[], prompt=contents_of_file) and save the JSON result to batch_NNNNN.jsonl"}1[{"file_id": 31, "filename": "[Airota][Sousou no Frieren][31][1080p HEVC-10bit AAC ASS]"}, {"file_id": 36, "filename": "[Airota][Sousou no Frieren][36][1080p HEVC-10bit AAC ASS]"}, {"file_id": 41, "filename": "[SweetSub] Honzuki no Gekokujou S04 - 05 [WebRip][1080P][AVC 8bit][CHS]"}, {"file_id": 46, "filename": "[Feibanyama] Ultraman Mebius EP1 [BDRip AI2160p HEVC FLAC]"}, {"file_id": 51, "filename": "[Skymoon-Raws] Tsue to Tsurugi no Wistoria - 17 [ViuTV][WEB-DL][CHT][SRT][1080p][AVC AAC]"}, {"file_id": 56, "filename": "[Skymoon-Raws] Digimon Beatbreak - 30 [ViuTV][WEB-DL][CHT][SRT][1080p][AVC AAC]"}, {"file_id": 61, "filename": "[Nekomoe kissaten&LoliHouse] Tsue to Tsurugi no Wistoria - 17 [WebRip 1080p HEVC-10bit AAC ASSx2]"}, {"file_id": 66, "filename": "[Nekomoe kissaten][Tsue to Tsurugi no Wistoria][17][1080p][JPTC]"}, {"file_id": 71, "filename": "[jibaketa]Kamen Rider Zeztz - 33 (WEB 1920x1080 AVC AACx2 SRT+PGS ViuTV CHT)"}, {"file_id": 76, "filename": "[Nekomoe kissaten][Kamiina Botan, Yoeru Sugata wa Yuri no Hana][05][1080p][JPTC]"}, {"file_id": 81, "filename": "[Studio GreenTea] NEEDY GIRL OVERDOSE [02][WebRip][HEVC-10bit 1080p AAC ASSx2]"}, {"file_id": 86, "filename": "[ANi] 女僕小姐的貪吃日常 - 07 [1080P][Baha][WEB-DL][AAC AVC][CHT]"}, {"file_id": 91, "filename": "[FreesiaSub&LoliHouse] LasTame S2 - 05 [WebRip 1080p HEVC-10bit AAC ASSx2]"}, {"file_id": 96, "filename": "[TSDM][Honzuki no Gekokujou:Shisho ni Naru Tame ni wa Shudan wo Erandeiraremasen - Ryushu no Youjo][03][WebRip][HEVC-10bit 1080p AAC][CHS_JP&CHT_JP]"}, {"file_id": 101, "filename": "[Studio GreenTea] NEEDY GIRL OVERDOSE [03][WebRip][HEVC-10bit 1080p AAC][JPTC]"}, {"file_id": 107, "filename": "[ANi] 鑽石王牌 act2 第二季 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]"}, {"file_id": 112, "filename": "[ANi] 杖與劍的魔劍譚 Season 2 - 17 [1080P][Baha][WEB-DL][AAC AVC][CHT]"}, {"file_id": 119, "filename": "[Haruhana] Kamiina Botan, Yoeru Sugata wa Yuri no Hana - 04 [WebRip][HEVC-10bit 1080p][CHS_JPN]"}, {"file_id": 124, "filename": "[Airota][Kamiina Botan, Yoeru Sugata wa Yuri no Hana][05][1080p HEVC-10bit AAC ASS]"}, {"file_id": 131, "filename": "[LoliHouse] Jishou Akuyaku Reijou na Konyakusha no Kansatsu Kiroku. - 06 [WebRip 1080p HEVC-10bit AAC SRTx2]"}, {"file_id": 136, "filename": "[Skymoon-Raws] Daemons of the Shadow Realm - 06 [ViuTV][WEB-DL][CHT][SRT][1080p][AVC AAC]"}, {"file_id": 141, "filename": "Air In Summer 01"}, {"file_id": 146, "filename": "Air 06"}, {"file_id": 151, "filename": "Air 11"}, {"file_id": 156, "filename": "[ANi] 一疊間漫畫咖啡廳日常 - 05 [1080P][Baha][WEB-DL][AAC AVC][CHT]"}, {"file_id": 161, "filename": "[ANi] 容易對付的惡魔大人 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]"}, {"file_id": 166, "filename": "[LKSUB][Ichijyoma Mankitsu Gurashi][04][1080P]"}, {"file_id": 171, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][02][HEVC][GB][4K]"}, {"file_id": 176, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][07][HEVC][GB][4K]"}, {"file_id": 181, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][12][HEVC][GB][4K]"}, {"file_id": 186, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][17][HEVC][GB][4K]"}, {"file_id": 191, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][22][HEVC][GB][4K]"}, {"file_id": 196, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][27][HEVC][GB][4K]"}, {"file_id": 201, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][05][AVC][GB][1080P]"}, {"file_id": 206, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][10][AVC][GB][1080P]"}, {"file_id": 211, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][15][AVC][GB][1080P]"}, {"file_id": 216, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][20][AVC][GB][1080P]"}, {"file_id": 221, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][25][AVC][GB][1080P]"}, {"file_id": 226, "filename": "[orion origin] Saikyou no Ousama, Nidome no Jinsei wa Nani o Suru S2 [06] [1080p] [H265 AAC] [CHT_JPN]"}, {"file_id": 231, "filename": "[ANi] 入間同學入魔了!第四季 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]"}]1[{"file_id": 32, "filename": "[Airota][Sousou no Frieren][32][1080p HEVC-10bit AAC ASS]"}, {"file_id": 37, "filename": "[Airota][Sousou no Frieren][37][1080p HEVC-10bit AAC ASS]"}, {"file_id": 42, "filename": "[Skymoon-Raws][One Piece][1161][ViuTV][WEB-RIP][CHT][SRT][1080p][AVC AAC]"}, {"file_id": 47, "filename": "[Nekomoe kissaten][Shunkashuutou Daikousha - Haru no Mai][06][1080p][JPTC]"}, {"file_id": 52, "filename": "[Sakurato] Koori no Jouheki [06][HEVC-10bit 1080P AAC][CHS&CHT]"}, {"file_id": 57, "filename": "[ANi] 茉莉花同學的好感度壞得很徹底 - 05 [1080P][Baha][WEB-DL][AAC AVC][CHT]"}, {"file_id": 62, "filename": "[Nekomoe kissaten&LoliHouse] Tsue to Tsurugi no Wistoria - 16 [WebRip 1080p HEVC-10bit AAC ASSx2]"}, {"file_id": 67, "filename": "[Nekomoe kissaten][Tsue to Tsurugi no Wistoria][17][1080p][JPSC]"}, {"file_id": 72, "filename": "[ANi] GHOST CONCERT : 失落之歌 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]"}, {"file_id": 77, "filename": "[Studio GreenTea] NEEDY GIRL OVERDOSE [04][WebRip][HEVC-10bit 1080p AAC][JPTC]"}, {"file_id": 82, "filename": "[Nekomoe kissaten&LoliHouse] Kamiina Botan, Yoeru Sugata wa Yuri no Hana - 05 [WebRip 1080p HEVC-10bit AAC ASSx2]"}, {"file_id": 87, "filename": "[ANi] 魔法姊妹露露特莉莉 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]"}, {"file_id": 92, "filename": "[LoliHouse] Rooster Fighter - 09 [WebRip 1080p HEVC-10bit AAC SRTx2]"}, {"file_id": 97, "filename": "[TSDM][Honzuki no Gekokujou:Shisho ni Naru Tame ni wa Shudan wo Erandeiraremasen - Ryushu no Youjo][03][BIG5][1080P][AVC 8bit]"}, {"file_id": 102, "filename": "[Studio GreenTea] NEEDY GIRL OVERDOSE [03][WebRip][HEVC-10bit 1080p AAC][JPSC]"}, {"file_id": 108, "filename": "[LoliHouse] Kanan-sama wa Akumade Choroi - 06 [WebRip 1080p HEVC-10bit AAC SRTx2]"}, {"file_id": 113, "filename": "[FLsnow.feat.PO][Onegai_Aipri][1080P][06]"}, {"file_id": 120, "filename": "[TamersUnion]DIGIMON BEATBREAK[30][WEBrip][x264_AAC][CHT_JPN]"}, {"file_id": 125, "filename": "[FLsnow][Star-Detective_Precure][15][1080p]"}, {"file_id": 132, "filename": "[FLsnow][Star-Detective_Precure][15][CHS][720p]"}, {"file_id": 137, "filename": "[Haruhana] Shunkashuutou Daikousha - Haru no Mai - 06 [WebRip][HEVC-10bit 1080p][CHI_JPN]"}, {"file_id": 142, "filename": "Air 02"}, {"file_id": 147, "filename": "Air 07"}, {"file_id": 152, "filename": "Air 12"}, {"file_id": 157, "filename": "[ANi] 主播女孩重度依賴 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]"}, {"file_id": 162, "filename": "[LoliHouse] Kanteishi (Kari) - 07 [WebRip 1080p HEVC-10bit AAC]"}, {"file_id": 167, "filename": "[LKSUB][Ichijyoma Mankitsu Gurashi][03][1080P]"}, {"file_id": 172, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][03][HEVC][GB][4K]"}, {"file_id": 177, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][08][HEVC][GB][4K]"}, {"file_id": 182, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][13][HEVC][GB][4K]"}, {"file_id": 187, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][18][HEVC][GB][4K]"}, {"file_id": 192, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][23][HEVC][GB][4K]"}, {"file_id": 197, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][01][AVC][GB][1080P]"}, {"file_id": 202, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][06][AVC][GB][1080P]"}, {"file_id": 207, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][11][AVC][GB][1080P]"}, {"file_id": 212, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][16][AVC][GB][1080P]"}, {"file_id": 217, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][21][AVC][GB][1080P]"}, {"file_id": 222, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][26][AVC][GB][1080P]"}, {"file_id": 227, "filename": "[orion origin] Saikyou no Ousama, Nidome no Jinsei wa Nani o Suru S2 [06] [1080p] [H265 AAC] [CHS_JPN]"}, {"file_id": 232, "filename": "[FreesiaSub] Lastame S2 - 05 [1080p x265 Ma10p AAC CHS]"}]1[{"file_id": 33, "filename": "[Airota][Sousou no Frieren][33][1080p HEVC-10bit AAC ASS]"}, {"file_id": 38, "filename": "[Airota][Sousou no Frieren][38][1080p HEVC-10bit AAC ASS]"}, {"file_id": 43, "filename": "[ANi] MAO 摩緒 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]"}, {"file_id": 48, "filename": "[Nekomoe kissaten][Shunkashuutou Daikousha - Haru no Mai][06][1080p][JPSC]"}, {"file_id": 53, "filename": "[Sakurato] Koori no Jouheki [06][AVC-8bit 1080P AAC][CHT]"}, {"file_id": 58, "filename": "[LoliHouse] Ingoku Danchi - 06 [WebRip 1080p HEVC-10bit AAC SRTx2]"}, {"file_id": 63, "filename": "[SBSUB][CONAN][113][WEBRIP][1080P][AVC_AAC][CHS_JP](0425226D)"}, {"file_id": 68, "filename": "[Nekomoe kissaten][Tsue to Tsurugi no Wistoria][16][1080p][JPTC]"}, {"file_id": 73, "filename": "[Studio GreenTea] Kamiina Botan, Yoeru Sugata wa Yuri no Hana [04][WebRip][HEVC-10bit 1080p AAC ASSx2]"}, {"file_id": 78, "filename": "[Studio GreenTea] NEEDY GIRL OVERDOSE [04][WebRip][HEVC-10bit 1080p AAC][JPSC]"}, {"file_id": 83, "filename": "[ANi] 黑貓與魔女的教室 - 05 [1080P][Baha][WEB-DL][AAC AVC][CHT]"}, {"file_id": 88, "filename": "[晚街与灯][Re Zero kara Hajimeru Isekai Seikatsu][4th - 05][总第71][WebRip][1080P_AVC_AAC][简日双语内嵌]"}, {"file_id": 93, "filename": "[LoliHouse] Onegai Aipri - 06 [WebRip 1080p HEVC-10bit AAC]"}, {"file_id": 98, "filename": "[TSDM][Honzuki no Gekokujou:Shisho ni Naru Tame ni wa Shudan wo Erandeiraremasen - Ryushu no Youjo][03][GB][1080P][AVC 8bit]"}, {"file_id": 103, "filename": "[Studio GreenTea] Youkoso Jitsuryoku Shijou Shugi no Kyoushitsu e S4 [08v2][WebRip][HEVC-10bit 1080p AAC][JPSC]"}, {"file_id": 109, "filename": "[LoliHouse] Marika-chan no Koukando wa Bukkowareteiru - 04 [WebRip 1080p HEVC-10bit AAC SRTx2]"}, {"file_id": 116, "filename": "[FLsnow.feat.PO][Onegai_Aipri][720P][06][CHT]"}, {"file_id": 121, "filename": "[TamersUnion]DIGIMON BEATBREAK[30][WEBrip][x264_AAC][CHS_JPN]"}, {"file_id": 128, "filename": "[FLsnow][Star-Detective_Precure][15][CHT][720p]"}, {"file_id": 133, "filename": "[ANi] 鏈遍煶钀借獮 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]"}, {"file_id": 138, "filename": "[SBSUB][CONAN][1201][WEBRIP][1080P][HEVC_AAC][CHS_CHT_JP][PGS](0B0641E8)"}, {"file_id": 143, "filename": "Air 03"}, {"file_id": 148, "filename": "Air 08"}, {"file_id": 153, "filename": "Air 01"}, {"file_id": 158, "filename": "[ANi] 楠木邸的神明庭院 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]"}, {"file_id": 163, "filename": "[LoliHouse] Yowayowa Sensei - 05 [WebRip 1080p HEVC-10bit AAC SRTx2]"}, {"file_id": 168, "filename": "[LKSUB][Ichijyoma Mankitsu Gurashi][02][1080P]"}, {"file_id": 173, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][04][HEVC][GB][4K]"}, {"file_id": 178, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][09][HEVC][GB][4K]"}, {"file_id": 183, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][14][HEVC][GB][4K]"}, {"file_id": 188, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][19][HEVC][GB][4K]"}, {"file_id": 193, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][24][HEVC][GB][4K]"}, {"file_id": 198, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][02][AVC][GB][1080P]"}, {"file_id": 203, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][07][AVC][GB][1080P]"}, {"file_id": 208, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][12][AVC][GB][1080P]"}, {"file_id": 213, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][17][AVC][GB][1080P]"}, {"file_id": 218, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][22][AVC][GB][1080P]"}, {"file_id": 223, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][27][AVC][GB][1080P]"}, {"file_id": 228, "filename": "[ANi] 弱弱老師 - 05 [1080P][Baha][WEB-DL][AAC AVC][CHT]"}, {"file_id": 233, "filename": "[Sakurato] Mairimashita! Iruma-kun (2026) [05][HEVC-10bit 1080P AAC][CHS&CHT]"}]1[{"file_id": 34, "filename": "[Airota][Sousou no Frieren][34][1080p HEVC-10bit AAC ASS]"}, {"file_id": 39, "filename": "[SweetSub&LoliHouse] Honzuki no Gekokujou S04 - 05 [WebRip 1080p HEVC-10bit AAC ASSx2]"}, {"file_id": 44, "filename": "[LoliHouse] GHOST CONCERT missing Songs - 06 [WebRip 1080p HEVC-10bit AAC SRTx2]"}, {"file_id": 49, "filename": "[Skymoon-Raws] Yozakurasan Chi no Daisakusen - 32 [ViuTV][WEB-DL][CHT][SRT][1080p][AVC AAC]"}, {"file_id": 54, "filename": "[Sakurato] Koori no Jouheki [06][AVC-8bit 1080P AAC][CHS]"}, {"file_id": 59, "filename": "[LoliHouse] Magical Sisters LuluttoLilly - 06 [WebRip 1080p HEVC-10bit AAC SRTx2]"}, {"file_id": 64, "filename": "[SBSUB][CONAN][113][WEBRIP][1080P][AVC_AAC][CHT_JP](47C34B53)"}, {"file_id": 69, "filename": "[Nekomoe kissaten][Tsue to Tsurugi no Wistoria][16][1080p][JPSC]"}, {"file_id": 74, "filename": "[Nekomoe kissaten][Ichijyoma Mankitsu Gurashi][04][1080p][JPTC]"}, {"file_id": 79, "filename": "[Studio GreenTea] NEEDY GIRL OVERDOSE [04][WebRip][HEVC-10bit 1080p AAC ASSx2]"}, {"file_id": 84, "filename": "[Nekomoe kissaten&LoliHouse] Ichijyoma Mankitsu Gurashi! - 04 [WebRip 1080p HEVC-10bit AAC ASSx2]"}, {"file_id": 89, "filename": "[晚街与灯][Re Zero kara Hajimeru Isekai Seikatsu][4th - 05][总第71][WEB-DL Remux][1080P_AVC_AAC][简繁日内封PGS]"}, {"file_id": 94, "filename": "[LoliHouse] Star Detective Precure! - 15 [WebRip 1080p HEVC-10bit AAC]"}, {"file_id": 99, "filename": "[jibaketa]Hibi wa Sugiredo Meshi Umashi - 03 [BD 1920x1080 x264 AAC YUE]"}, {"file_id": 104, "filename": "[Studio GreenTea] Youkoso Jitsuryoku Shijou Shugi no Kyoushitsu e S4 [09v2][WebRip][HEVC-10bit 1080p AAC][JPSC]"}, {"file_id": 110, "filename": "[LoliHouse] MAO - 06 [WebRip 1080p HEVC-10bit AAC SRTx2]"}, {"file_id": 117, "filename": "[Haruhana] Kamiina Botan, Yoeru Sugata wa Yuri no Hana - 04 [WebRip][HEVC-10bit 1080p][CHI_JPN]"}, {"file_id": 122, "filename": "[Airota][Kamiina Botan, Yoeru Sugata wa Yuri no Hana][05][1080p AVC AAC][CHT]"}, {"file_id": 129, "filename": "[Haruhana] Shunkashuutou Daikousha - Haru no Mai - 06 [WebRip][HEVC-10bit 1080p][CHT_JPN]"}, {"file_id": 134, "filename": "[LoliHouse] Yomi no Tsugai - 06 [WebRip 1080p HEVC-10bit AAC ASSx2]"}, {"file_id": 139, "filename": "[SBSUB][CONAN][1201][WEBRIP][1080P][AVC_AAC][CHT_JP](DDB08036)"}, {"file_id": 144, "filename": "Air 04"}, {"file_id": 149, "filename": "Air 09"}, {"file_id": 154, "filename": "Air In Summer 02"}, {"file_id": 159, "filename": "[ANi] 春夏秋冬代行者 春之舞 - 07 [1080P][Baha][WEB-DL][AAC AVC][CHT]"}, {"file_id": 164, "filename": "[LoliHouse] Mairimashita! Iruma-kun S4 - 06 [WebRip 1080p HEVC-10bit AAC SRTx2]"}, {"file_id": 169, "filename": "[ANi] 殺手青春 - 05 [1080P][Baha][WEB-DL][AAC AVC][CHT]"}, {"file_id": 174, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][05][HEVC][GB][4K]"}, {"file_id": 179, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][10][HEVC][GB][4K]"}, {"file_id": 184, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][15][HEVC][GB][4K]"}, {"file_id": 189, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][20][HEVC][GB][4K]"}, {"file_id": 194, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][25][HEVC][GB][4K]"}, {"file_id": 199, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][03][AVC][GB][1080P]"}, {"file_id": 204, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][08][AVC][GB][1080P]"}, {"file_id": 209, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][13][AVC][GB][1080P]"}, {"file_id": 214, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][18][AVC][GB][1080P]"}, {"file_id": 219, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][23][AVC][GB][1080P]"}, {"file_id": 224, "filename": "[Studio GreenTea] Kamiina Botan, Yoeru Sugata wa Yuri no Hana [05][WebRip][HEVC-10bit 1080p AAC][JPTC]"}, {"file_id": 229, "filename": "[ANi] 大賢者里德爾的時間逆行 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]"}, {"file_id": 234, "filename": "[Sakurato] Mairimashita! Iruma-kun (2026) [05][AVC-8bit 1080P AAC][CHT]"}]1[{"file_id": 35, "filename": "[Airota][Sousou no Frieren][35][1080p HEVC-10bit AAC ASS]"}, {"file_id": 40, "filename": "[SweetSub] Honzuki no Gekokujou S04 - 05 [WebRip][1080P][AVC 8bit][CHT]"}, {"file_id": 45, "filename": "[Dynamis One] Kanteishikari - 07 (CR 1920x1080 AVC AAC MKV) [B0B2C788]"}, {"file_id": 50, "filename": "[Feibanyama] ReZERO Starting Life in Another World S04E05 [IQIYI WebRip 2160p HEVC AAC Multi-Audio Multi-Subs]"}, {"file_id": 55, "filename": "[Skymoon-Raws] Rooster Fighter - 09 [ViuTV][WEB-DL][CHT][SRT][1080p][AVC AAC]"}, {"file_id": 60, "filename": "[LoliHouse] Kuroneko to Majo no Kyoushitsu - 05 [WebRip 1080p HEVC-10bit AAC SRTx2]"}, {"file_id": 65, "filename": "[SBSUB][CONAN][113][WEBRIP][1080P][HEVC_AAC][CHS_CHT_JP][PGS](091A2606)"}, {"file_id": 70, "filename": "[ANi] 淫獄團地 [年齡限制版] - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]"}, {"file_id": 75, "filename": "[Nekomoe kissaten][Ichijyoma Mankitsu Gurashi][04][1080p][JPSC]"}, {"file_id": 80, "filename": "[Studio GreenTea] NEEDY GIRL OVERDOSE [03][WebRip][HEVC-10bit 1080p AAC ASSx2]"}, {"file_id": 85, "filename": "[LoliHouse] Ganbare! Nakamura-kun!! - 07 [WebRip 1080p HEVC-10bit AAC SRTx2]"}, {"file_id": 90, "filename": "[晚街與燈][Re Zero kara Hajimeru Isekai Seikatsu][4th - 05][總第71][WebRip][1080P_AVC_AAC][繁日雙語內嵌]"}, {"file_id": 95, "filename": "[LoliHouse] DIGIMON BEATBREAK - 30 [WebRip 1080p HEVC-10bit AAC SRTx2]"}, {"file_id": 100, "filename": "[LoliHouse] Yozakura-san Chi no Daisakusen - 32 [WebRip 1080p HEVC-10bit AAC ASSx2]"}, {"file_id": 105, "filename": "[Suzu-Kaze] Dorohedoro 19 [WebRip 1920x1080 HEVC YUV420P10 AAC]"}, {"file_id": 111, "filename": "[ANi] 夜櫻家大作戰 第二季 - 32 [1080P][Baha][WEB-DL][AAC AVC][CHT]"}, {"file_id": 118, "filename": "[Haruhana] Kamiina Botan, Yoeru Sugata wa Yuri no Hana - 04 [WebRip][HEVC-10bit 1080p][CHT_JPN]"}, {"file_id": 123, "filename": "[Airota][Kamiina Botan, Yoeru Sugata wa Yuri no Hana][05][1080p AVC AAC][CHS]"}, {"file_id": 130, "filename": "[Haruhana] Shunkashuutou Daikousha - Haru no Mai - 06 [WebRip][HEVC-10bit 1080p][CHS_JPN]"}, {"file_id": 135, "filename": "[LoliHouse] NEEDY GIRL OVERDOSE - 06 [WebRip 1080p HEVC-10bit AAC SRTx2]"}, {"file_id": 140, "filename": "[SBSUB][CONAN][1201][WEBRIP][1080P][AVC_AAC][CHS_JP](E3664BD8)"}, {"file_id": 145, "filename": "Air 05"}, {"file_id": 150, "filename": "Air 10"}, {"file_id": 155, "filename": "Air The Movie"}, {"file_id": 160, "filename": "[ANi] 勇者之渣 - 17 [1080P][Baha][WEB-DL][AAC AVC][CHT]"}, {"file_id": 165, "filename": "[LoliHouse] Hokuto no Ken FIST OF THE NORTH STAR - 07 [WebRip 1080p HEVC-10bit AAC SRTx2]"}, {"file_id": 170, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][01][HEVC][GB][4K]"}, {"file_id": 175, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][06][HEVC][GB][4K]"}, {"file_id": 180, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][11][HEVC][GB][4K]"}, {"file_id": 185, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][16][HEVC][GB][4K]"}, {"file_id": 190, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][21][HEVC][GB][4K]"}, {"file_id": 195, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][26][HEVC][GB][4K]"}, {"file_id": 200, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][04][AVC][GB][1080P]"}, {"file_id": 205, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][09][AVC][GB][1080P]"}, {"file_id": 210, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][14][AVC][GB][1080P]"}, {"file_id": 215, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][19][AVC][GB][1080P]"}, {"file_id": 220, "filename": "[GM-Team][国漫][剑来 第2季][Sword of Coming Ⅱ][2025][24][AVC][GB][1080P]"}, {"file_id": 225, "filename": "[Studio GreenTea] Kamiina Botan, Yoeru Sugata wa Yuri no Hana [05][WebRip][HEVC-10bit 1080p AAC][JPSC]"}, {"file_id": 230, "filename": "[jibaketa]Meitantei Precure! - 06 (WEB 1920x1080 AVC AAC YUE)"}, {"file_id": 235, "filename": "[Sakurato] Mairimashita! Iruma-kun (2026) [05][AVC-8bit 1080P AAC][CHS]"}]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110You are an anime filename annotator. Read each filename and assign BIO labels token-by-token.
LABEL SCHEME:- B-TITLE / I-TITLE: Anime title words (e.g. Sousou, no, Frieren, 葬送的, 芙莉莲)- B-SEASON: Season marker (S2, S02, Season 2, 第二季, 第N季, 第N部, 2nd Season, II when it means season 2)- B-EPISODE: Episode number (01, 06, EP01, 第01话, 第01話, #01)- B-GROUP / I-GROUP: Release group name [ANi], [SubsPlease], [LoliHouse], 【桜都字幕组】- B-RESOLUTION: Resolution (1080p, 720P, 4K, 2160p, 1920x1080)- B-SOURCE: Source/format tag (WEB-DL, BDRip, HEVC, AAC, FLAC, CHT, CHS, GB, BIG5)- B-SPECIAL: Special type (OVA, OAD, Movie, SP, OP, ED, PV, CM)- O: Separators (space, -, _, |, ~, .) and noise
IMPORTANT RULES:1. Roman numerals (II, III, IV) at the end of a title often mean SEASON, not part of the title. Example: "Sword Art Online II" → "II" is B-SEASON, not I-TITLE Example: "Chibi Maruko-chan II" → "II" is B-SEASON (it's season 2) Exception: When the Roman numeral is PART of the franchise name (e.g. "Final Fantasy X", "Kingdom Hearts III")
2. "Season" followed by a number is a season marker. "3rd Season", "4th Season" are season markers.
3. Numbers that appear between the title and episode number are likely season numbers. Example: "Isekai Nonbiri Nouka 2 - 05" → "2" is B-SEASON
4. Bracketed items at the START are usually GROUP names. Bracketed items at the END are usually metadata (SOURCE, RESOLUTION).
5. Chinese markers like 第2季, 第二季, 第二部 are SEASON markers. 第01话, 第01話 are EPISODE markers.
6. Read the filename holistically - use your understanding of what the anime is about to determine if something is a title word or a technical marker.
Return your answer as a JSON object with a "results" array. Each result has: "file_id": integer, "filename": string, "tokens": list of strings (the tokenized filename), "labels": list of strings (one BIO label per token)
Tokenize carefully:- Keep bracket content as single tokens: [ANi], [1080P], [WEB-DL]- Chinese/Japanese characters: each character is its own token- English words: keep as whole words- Numbers: keep as single tokens- Separators (space, -, _, |, ~, ., etc.): each is its own token with label O
FILENAMES TO ANNOTATE:[ { "file_id": 1, "filename": "Witch.Hat.Atelier.S01E07.1080p.NF.WEB-DL.JPN.AAC2.0.H.264.MSubs-ToonsHub" }, { "file_id": 2, "filename": "[LoliHouse] Maid-san wa Taberu Dake - 07 [WebRip 1080p HEVC-10bit AAC ASSx2]" }, { "file_id": 3, "filename": "[ANi] 異世界悠閒農家 2 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]" }, { "file_id": 4, "filename": "[ANi] 木頭風紀委員和迷你裙 JK 的故事 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]" }, { "file_id": 5, "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][05][1080P][GB][MP4]" }, { "file_id": 6, "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][06][1080P][GB][MP4]" }, { "file_id": 7, "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][06][1080P][BIG5][MP4]" }, { "file_id": 8, "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][05][1080P][BIG5][MP4]" }, { "file_id": 9, "filename": "[Airota][Sousou no Frieren][29][1080p AVC AAC][CHT]" }, { "file_id": 10, "filename": "[Airota][Sousou no Frieren][30][1080p AVC AAC][CHT]" }, { "file_id": 11, "filename": "[Airota][Sousou no Frieren][31][1080p AVC AAC][CHT]" }, { "file_id": 12, "filename": "[Airota][Sousou no Frieren][32][1080p AVC AAC][CHT]" }, { "file_id": 13, "filename": "[Airota][Sousou no Frieren][33][1080p AVC AAC][CHT]" }, { "file_id": 14, "filename": "[Airota][Sousou no Frieren][34][1080p AVC AAC][CHT]" }, { "file_id": 15, "filename": "[Airota][Sousou no Frieren][35][1080p AVC AAC][CHT]" }]
Return ONLY valid JSON. No markdown. No explanation. Just the JSON object.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110You are an anime filename annotator. Read each filename and assign BIO labels token-by-token.
LABEL SCHEME:- B-TITLE / I-TITLE: Anime title words (e.g. Sousou, no, Frieren, 葬送的, 芙莉莲)- B-SEASON: Season marker (S2, S02, Season 2, 第二季, 第N季, 第N部, 2nd Season, II when it means season 2)- B-EPISODE: Episode number (01, 06, EP01, 第01话, 第01話, #01)- B-GROUP / I-GROUP: Release group name [ANi], [SubsPlease], [LoliHouse], 【桜都字幕组】- B-RESOLUTION: Resolution (1080p, 720P, 4K, 2160p, 1920x1080)- B-SOURCE: Source/format tag (WEB-DL, BDRip, HEVC, AAC, FLAC, CHT, CHS, GB, BIG5)- B-SPECIAL: Special type (OVA, OAD, Movie, SP, OP, ED, PV, CM)- O: Separators (space, -, _, |, ~, .) and noise
IMPORTANT RULES:1. Roman numerals (II, III, IV) at the end of a title often mean SEASON, not part of the title. Example: "Sword Art Online II" → "II" is B-SEASON, not I-TITLE Example: "Chibi Maruko-chan II" → "II" is B-SEASON (it's season 2) Exception: When the Roman numeral is PART of the franchise name (e.g. "Final Fantasy X", "Kingdom Hearts III")
2. "Season" followed by a number is a season marker. "3rd Season", "4th Season" are season markers.
3. Numbers that appear between the title and episode number are likely season numbers. Example: "Isekai Nonbiri Nouka 2 - 05" → "2" is B-SEASON
4. Bracketed items at the START are usually GROUP names. Bracketed items at the END are usually metadata (SOURCE, RESOLUTION).
5. Chinese markers like 第2季, 第二季, 第二部 are SEASON markers. 第01话, 第01話 are EPISODE markers.
6. Read the filename holistically - use your understanding of what the anime is about to determine if something is a title word or a technical marker.
Return your answer as a JSON object with a "results" array. Each result has: "file_id": integer, "filename": string, "tokens": list of strings (the tokenized filename), "labels": list of strings (one BIO label per token)
Tokenize carefully:- Keep bracket content as single tokens: [ANi], [1080P], [WEB-DL]- Chinese/Japanese characters: each character is its own token- English words: keep as whole words- Numbers: keep as single tokens- Separators (space, -, _, |, ~, ., etc.): each is its own token with label O
FILENAMES TO ANNOTATE:[ { "file_id": 16, "filename": "[Airota][Sousou no Frieren][36][1080p AVC AAC][CHT]" }, { "file_id": 17, "filename": "[Airota][Sousou no Frieren][37][1080p AVC AAC][CHT]" }, { "file_id": 18, "filename": "[Airota][Sousou no Frieren][38][1080p AVC AAC][CHT]" }, { "file_id": 19, "filename": "[Airota][Sousou no Frieren][29][1080p AVC AAC][CHS]" }, { "file_id": 20, "filename": "[Airota][Sousou no Frieren][30][1080p AVC AAC][CHS]" }, { "file_id": 21, "filename": "[Airota][Sousou no Frieren][31][1080p AVC AAC][CHS]" }, { "file_id": 22, "filename": "[Airota][Sousou no Frieren][32][1080p AVC AAC][CHS]" }, { "file_id": 23, "filename": "[Airota][Sousou no Frieren][33][1080p AVC AAC][CHS]" }, { "file_id": 24, "filename": "[Airota][Sousou no Frieren][34][1080p AVC AAC][CHS]" }, { "file_id": 25, "filename": "[Airota][Sousou no Frieren][35][1080p AVC AAC][CHS]" }, { "file_id": 26, "filename": "[Airota][Sousou no Frieren][36][1080p AVC AAC][CHS]" }, { "file_id": 27, "filename": "[Airota][Sousou no Frieren][37][1080p AVC AAC][CHS]" }, { "file_id": 28, "filename": "[Airota][Sousou no Frieren][38][1080p AVC AAC][CHS]" }, { "file_id": 29, "filename": "[Airota][Sousou no Frieren][29][1080p HEVC-10bit AAC ASS]" }, { "file_id": 30, "filename": "[Airota][Sousou no Frieren][30][1080p HEVC-10bit AAC ASS]" }]
Return ONLY valid JSON. No markdown. No explanation. Just the JSON object.123456789{ "synthetic": "data/synthetic.jsonl", "dmhy": "data/dmhy/dmhy_weak.jsonl", "output": "data/dmhy/mixed_train.jsonl", "synthetic_count": 100000, "dmhy_count": 632002, "total_count": 732002, "seed": 42}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244[ { "id": "lolihouse_dash_episode", "filename": "[LoliHouse] Yomi no Tsugai - 07 [WebRip 1080p HEVC-10bit AAC ASSx2]", "expected": { "group": "LoliHouse", "title": "Yomi no Tsugai", "episode": 7, "resolution": "1080p", "source": "WebRip" } }, { "id": "dot_season_episode_no_group", "filename": "Witch.Hat.Atelier.S01E07.1080p.NF.WEB-DL.JPN.AAC2.0.H.264.MSubs-ToonsHub", "expected": { "title": "Witch.Hat.Atelier", "season": 1, "episode": 7, "group": null, "resolution": "1080p", "source": "NF" } }, { "id": "ani_cjk_season_dash_episode", "filename": "[ANi] 異世界悠閒農家 2 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]", "expected": { "group": "ANi", "title": "異世界悠閒農家", "season": 2, "episode": 6, "resolution": "1080P", "source": "Baha" } }, { "id": "kisssub_bracket_title_episode", "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][05][1080P][GB][MP4]", "expected": { "group": "KissSub", "title": "Shunkashuutou Daikousha - Haru no Mai", "episode": 5, "resolution": "1080P", "source": "GB" } }, { "id": "airotabracket_title_episode", "filename": "[Airota][Sousou no Frieren][29][1080p AVC AAC][CHT]", "expected": { "group": "Airota", "title": "Sousou no Frieren", "episode": 29, "resolution": "1080p", "source": "CHT" } }, { "id": "subsplease_parenthesized_resolution", "filename": "[SubsPlease] Mushoku Tensei - 12 (1080p) [x265][AAC]", "expected": { "group": "SubsPlease", "title": "Mushoku Tensei", "episode": 12, "resolution": "1080p" } }, { "id": "vcb_bracket_episode", "filename": "[VCB-Studio] Girls Band Cry [01][Ma10p_1080p][x265_flac]", "expected": { "group": "VCB-Studio", "title": "Girls Band Cry", "episode": 1, "resolution": "1080p" } }, { "id": "numeric_title_not_episode", "filename": "86 Eighty Six - 01 [1080P][Baha]", "expected": { "title": "86 Eighty Six", "episode": 1, "resolution": "1080P", "source": "Baha" } }, { "id": "erai_raws_dash_episode", "filename": "[Erai-raws] Sousou no Frieren - 01 [1080p][Multiple Subtitle][ENG]", "expected": { "group": "Erai-raws", "title": "Sousou no Frieren", "episode": 1, "resolution": "1080p" } }, { "id": "nekomoe_space_group", "filename": "[Nekomoe kissaten][Watashi no Shiawase na Kekkon][01][1080p][JPSC]", "expected": { "group": "Nekomoe kissaten", "title": "Watashi no Shiawase na Kekkon", "episode": 1, "resolution": "1080p" } }, { "id": "long_running_episode", "filename": "One.Piece.1110.1080p.WEB-DL.AAC2.0.H.264", "expected": { "title": "One.Piece", "episode": 1110, "resolution": "1080p", "source": "WEB-DL" } }, { "id": "season_episode_amzn", "filename": "Example.Show.S02E03.2160p.AMZN.WEB-DL.DDP5.1.H.265", "expected": { "title": "Example.Show", "season": 2, "episode": 3, "resolution": "2160p", "source": "AMZN" } }, { "id": "cjk_group_with_prefix_tag", "filename": "【喵萌奶茶屋】★04月新番★[葬送的芙莉莲][01][1080P][HEVC]", "expected": { "group": "喵萌奶茶屋", "title": "葬送的芙莉莲", "episode": 1, "resolution": "1080P" } }, { "id": "leading_meta_not_group", "filename": "[1080p] Witch Watch - 15 [CHS]", "expected": { "group": null, "title": "Witch Watch", "episode": 15, "resolution": "1080p", "source": "CHS" } }, { "id": "sakurato_group_language_source", "filename": "[Sakurato] Witch Watch - 15 [1080p][CHS]", "expected": { "group": "Sakurato", "title": "Witch Watch", "episode": 15, "resolution": "1080p", "source": "CHS" } }, { "id": "billion_meta_lab_search_special", "filename": "[Billion Meta Lab] 魔法姊妹露露莉莉 Mahou no Shimai Rurutto Riryi [07][1080P][CHT&JPN][檢索:魔法姊妹露露特莉莉].mp4", "expected": { "group": "Billion Meta Lab", "title": "魔法姊妹露露莉莉 Mahou no Shimai Rurutto Riryi", "episode": 7, "resolution": "1080P", "source": "CHT&JPN", "special": "檢索:魔法姊妹露露特莉莉" } }, { "id": "studio_greentea_s2_bracket_episode", "filename": "[Studio GreenTea] Otonari no Tenshi-sama ni Itsunomanika Dame Ningen ni Sareteita Ken S2 [06][WebRip][HEVC-10bit 1080p AAC][JPSC].mp4", "expected": { "group": "Studio GreenTea", "title": "Otonari no Tenshi-sama ni Itsunomanika Dame Ningen ni Sareteita Ken", "season": 2, "episode": 6, "resolution": "1080p", "source": "WebRip" } }, { "id": "lolihouse_kakuriyo_bare_ni_season", "filename": "[LoliHouse] Kakuriyo no Yadomeshi Ni - 12 [WebRip 1080p HEVC-10bit AAC SRTx2].mkv", "expected": { "group": "LoliHouse", "title": "Kakuriyo no Yadomeshi", "season": 2, "episode": 12, "resolution": "1080p", "source": "WebRip" } }, { "id": "ani_kakuriyo_traditional_ni", "filename": "[ANi] 妖怪旅館營業中 貳 - 11 [1080P][Baha][WEB-DL][AAC AVC][CHT].mp4", "expected": { "group": "ANi", "title": "妖怪旅館營業中", "season": 2, "episode": 11, "resolution": "1080P", "source": "Baha" } }, { "id": "jibaketa_shokugeki_ni_no_sara", "filename": "[jibaketa]Shokugeki no Souma Ni no Sara - 13 END [BD 1920x1080 x264 AACx2 SRT TVB CHT].mkv", "expected": { "group": "jibaketa", "title": "Shokugeki no Souma", "season": 2, "episode": 13, "resolution": "1920x1080" } }, { "id": "ai_raws_fire_force_cjk_season_hash_episode", "filename": "[AI-Raws] 炎炎の消防隊 弐ノ章 #13 (BD HEVC 1920x1080 yuv444p10le FLAC)[FC74A2D5].mkv", "expected": { "group": "AI-Raws", "title": "炎炎の消防隊", "season": 2, "episode": 13, "resolution": "1920x1080" } }, { "id": "gm_team_guoman_bilingual_s2", "filename": "[GM-Team][国漫][逆天邪神 第2季][Against the Gods Ⅱ][2026][04][HEVC][GB][4K].mp4", "expected": { "group": "GM-Team", "title": "逆天邪神", "season": 2, "episode": 4, "resolution": "4K", "source": "GB" } }]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100{"tokens": ["[Baha]", " ", "DOG", " ", "DAYS", "'", " ", "S04", " ", " ", " ", "18", " ", "AAC"], "labels": ["B-GROUP", "O", "B-TITLE", "O", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["[Baha]", " ", "未", "闻", "花", "名", " ", "S02", " ", "78", " ", "[2160p]", " ", "AAC", " ", "[AVC]"], "labels": ["B-GROUP", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "B-EPISODE", "O", "B-RESOLUTION", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["[KPDM]", " ", "葬", "送", "的", "芙", "莉", "蓮", " ", "OVA", " ", " ", "|", " ", " ", "Ep90", " ", "[BDRip]"], "labels": ["B-GROUP", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "I-TITLE", "O", "O", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["【【极影字幕社】", "】", "未", "闻", "花", "名", " ", "第一季", " ", "45", " ", "[x265]", " ", "FLAC"], "labels": ["B-GROUP", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["【【幻樱字幕组】", "】", "★", "新", "番", "★", "My", " ", "Hero", " ", "Academia", " ", "81", " ", "[H264]"], "labels": ["B-GROUP", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["[VCB-Studio]", " ", "100", "万", "の", "命", "の", "上", "に", "俺", "は", "立", "っ", "て", "い", "る", " ", "38", " ", "[简日双语]", " ", "CHT"], "labels": ["B-GROUP", "O", "B-EPISODE", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["【【澄空学园】", "】", "白", "箱", " ", "86", " ", "[720P]"], "labels": ["B-GROUP", "B-TITLE", "I-TITLE", "I-TITLE", "O", "B-EPISODE", "O", "B-RESOLUTION"]}{"tokens": ["Solo", " ", "Leveling", " ", "Ep60", " ", "[WebRip]", " ", "[AAC]", " ", "[FLAC]"], "labels": ["B-TITLE", "O", "I-TITLE", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["[KPDM]", " ", "Fate", "/", "Grand", " ", "Order", " ", "第一季", " ", "28", " ", "[BIG5]", " ", "1920x1080", " ", "[WebRip]"], "labels": ["B-GROUP", "O", "B-TITLE", "I-TITLE", "I-TITLE", "O", "I-TITLE", "O", "B-SEASON", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["[Ohys-Raws]", " ", "【推しの子】", " ", "OVA", " ", "~", " ", "ep96", " ", "CHT"], "labels": ["B-GROUP", "O", "B-SOURCE", "O", "B-TITLE", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["That", " ", "Time", " ", "I", " ", "Got", " ", "Reincarnated", " ", "as", " ", "a", " ", "Slime", " ", "第四季", " ", "-", " ", "07", " ", "[JP]", " ", "x264"], "labels": ["B-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "B-SEASON", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["【【雪飘工作室】", "】", "★", "新", "番", "★", "Summer", " ", "Time", " ", "Rendering", " ", "第37話", " ", "3840x2160"], "labels": ["B-GROUP", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["[SweetSub]", " ", "AKB", "0048", " ", "S4", " ", " ", "|", " ", "ep99", " ", "[x264]", " ", "[2160P]", "[完]"], "labels": ["B-GROUP", "O", "B-TITLE", "B-EPISODE", "O", "B-SEASON", "O", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-RESOLUTION", "B-SOURCE"]}{"tokens": ["Mushoku", " ", "Tensei", " ", "第62話", " ", "1280x720", " ", "[HEVC]", " ", "[BDRip]"], "labels": ["B-TITLE", "O", "I-TITLE", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["[FFF]", " ", "葬", "送", "的", "芙", "莉", "莲", " ", "Seasons", " ", "1", " ", " ", " ", "03", " ", "1080P", " ", "[CHS]", " ", "[480P]", " ", "[GB]"], "labels": ["B-GROUP", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "B-EPISODE", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-SOURCE", "O", "B-RESOLUTION", "O", "B-SOURCE"]}{"tokens": ["[HYSUB]", " ", "Solo", " ", "Leveling", " ", "Ep85", " ", "[AMZN]", " ", "1280x720"], "labels": ["B-GROUP", "O", "B-TITLE", "O", "I-TITLE", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["((极影字幕社)", ")", " ", "Dungeon", " ", "Meshi", " ", "S2", "Season 40", " ", "[WebRip]"], "labels": ["B-GROUP", "B-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "B-SEASON", "B-SEASON", "O", "B-SOURCE"]}{"tokens": ["DeadFish", " ", "边", "缘", "行", "者", " ", "S4", " ", " ", "|", " ", " ", "09", " ", "[Baha]"], "labels": ["B-TITLE", "O", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "O", "O", "O", "O", "B-EPISODE", "O", "B-GROUP"]}{"tokens": ["[SubsPlease]", " ", "Show", " ", "By", " ", "Rock", "!", "!", " ", "Seasons", " ", "2", " ", "~", " ", "09", " ", "[BIG5]", " ", "[480P]"], "labels": ["B-GROUP", "O", "B-TITLE", "O", "I-TITLE", "O", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "B-EPISODE", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-RESOLUTION"]}{"tokens": ["无", "职", "转", "生", " ", "3", "rd", " ", "Season 32", " ", "[DTS]", " ", "[Snow-Raws]"], "labels": ["B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-EPISODE", "B-TITLE", "O", "B-SEASON", "O", "B-SOURCE", "O", "B-GROUP"]}{"tokens": ["[Rally]", " ", "ワ", "ン", "ダ", "ー", "エ", "ッ", "グ", "・", "プ", "ラ", "イ", "オ", "リ", "テ", "ィ", " ", "Season 3", " ", " ", " ", "60", " ", "[CHT]"], "labels": ["B-GROUP", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["【【极影字幕社】", "】", "【推しの子】", " ", "S02", " ", "58", " ", "[2160P]", " ", "[480P]"], "labels": ["B-GROUP", "B-TITLE", "B-SOURCE", "O", "B-SEASON", "O", "B-EPISODE", "O", "B-RESOLUTION", "O", "B-RESOLUTION"]}{"tokens": ["[ReinForce]", " ", "Oshi", " ", "no", " ", "Ko", " ", "84", " ", "[CHT]", " ", "[简日双语]"], "labels": ["B-GROUP", "O", "B-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["[Kamigami]", " ", "ぼ", "っ", "ち", "・", "ざ", "・", "ろ", "っ", "く", " ", "Movie", " ", "[JP]", " ", "[CR]"], "labels": ["B-GROUP", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "I-TITLE", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["Erai", "-", "raws", " ", " ", "Revue", " ", "Starlight", " ", "S2", "Season", " ", "_", " ", "第44話", " ", "[DTS]"], "labels": ["B-EPISODE", "O", "B-TITLE", "O", "O", "I-TITLE", "O", "I-TITLE", "O", "B-SEASON", "B-SEASON", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["Ousama", " ", "Ranking", " ", "2nd Season", " ", "41", " ", "1920x1080", " ", "[Lilith-Raws]"], "labels": ["B-TITLE", "O", "I-TITLE", "O", "B-SEASON", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-GROUP"]}{"tokens": ["[NT-Raws]", " ", "新", "世", "纪", "エ", "ヴ", "ァ", "ン", "ゲ", "リ", "オ", "ン", " ", "1st Season", " ", " ", " ", "24", " ", "[720P]", " ", "[AAC]", " ", "[Baha]", " ", "[1080p]"], "labels": ["B-GROUP", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "O", "O", "B-EPISODE", "O", "B-RESOLUTION", "O", "B-SOURCE", "O", "B-GROUP", "O", "B-RESOLUTION"]}{"tokens": ["Hell", "'", "s", " ", "Paradise", " ", " ", "|", " ", " ", "34", " ", "[[MP3]", "]", " ", "[{meta_bracket}]"], "labels": ["B-TITLE", "I-TITLE", "I-TITLE", "O", "I-TITLE", "O", "O", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE", "B-TITLE", "O", "B-SOURCE"]}{"tokens": ["★", "07", "月", "新", "番", "★", "【【动漫国字幕组】", "】", "★", "新", "番", "★", "5000", "兆", "円", "欲", "し", "い", "!", " ", "E41", " ", "[GB]"], "labels": ["B-TITLE", "B-EPISODE", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "B-GROUP", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "B-EPISODE", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["海", "贼", "王", " ", "S5", " ", "第18话", " ", "[BIG5]", " ", "[QTS]"], "labels": ["B-TITLE", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-GROUP"]}{"tokens": ["DeadFish", " ", "Wake", " ", "Up", ",", " ", "Girls", "!", " ", "Season 1", " ", " ", " ", "EP86", " ", "[CHS]"], "labels": ["B-TITLE", "O", "I-TITLE", "O", "I-TITLE", "I-TITLE", "O", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["海", "贼", "王", " ", "S4", " ", "~", " ", "第92話", " ", "[AV1]", " ", "[2160p]"], "labels": ["B-TITLE", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-RESOLUTION"]}{"tokens": ["[QTS]", " ", "Puella", " ", "Magi", " ", "Madoka", " ", "Magica", " ", "[OAD]", " ", " ", "-", " ", " ", "07", " ", "[AV1]", "★", "10", "月", "新", "番", "★"], "labels": ["B-GROUP", "O", "B-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "B-SPECIAL", "O", "O", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE", "B-TITLE", "B-EPISODE", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE"]}{"tokens": ["[NT-Raws]", " ", "DOG", " ", "DAYS", "'", " ", "OVA", " ", " ", " ", "91", " ", "[x264]"], "labels": ["B-GROUP", "O", "B-TITLE", "O", "I-TITLE", "I-TITLE", "O", "I-TITLE", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["Delicious", " ", "in", " ", "Dungeon", " ", "S2", " ", "~", " ", "第51話", " ", "[H265]", " ", "[360P]"], "labels": ["B-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "B-SEASON", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-RESOLUTION"]}{"tokens": ["[Elysium]", " ", "3", "月", "の", "ラ", "イ", "オ", "ン", " ", "S02", " ", "EP46", " ", "[DTS]", " ", "[JP]", " ", "[简日双语]"], "labels": ["B-GROUP", "O", "B-EPISODE", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["lovelive", "!", " ", "95", " ", "CHT", " ", "[简日双语]", " ", "[720p]"], "labels": ["B-TITLE", "I-TITLE", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-SOURCE", "O", "B-RESOLUTION"]}{"tokens": ["[Snow-Raws]", " ", "Attack", " ", "on", " ", "Titan", " ", "S03", " ", "59", " ", "Baha", " ", "[AAC]", " ", "[2160p]"], "labels": ["B-GROUP", "O", "B-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "B-SEASON", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-SOURCE", "O", "B-RESOLUTION"]}{"tokens": ["[philosophy-raws]", " ", "命", "运", "石", "之", "门", " ", "[CM]", " ", "~", " ", "第72话", " ", "[H265]"], "labels": ["B-GROUP", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SPECIAL", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["[Coalgirls]", " ", "BLEACH", " ", "S01", " ", "~", " ", "34", " ", "720P"], "labels": ["B-GROUP", "O", "B-TITLE", "O", "B-SEASON", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["【【茉语月译】", "】", "Sonny", " ", "Boy", " ", "1st Season", " ", "74", " ", "[1080p]", " ", "[FLAC]"], "labels": ["B-GROUP", "B-TITLE", "I-TITLE", "O", "I-TITLE", "O", "B-SEASON", "O", "B-EPISODE", "O", "B-RESOLUTION", "O", "B-SOURCE"]}{"tokens": ["8", " ", "Girls", " ", "Ep47"], "labels": ["B-EPISODE", "O", "B-TITLE", "O", "B-EPISODE"]}{"tokens": ["【【轻之国度】", "】", "Fate", "/", "Grand", " ", "Order", " ", "S1", "Season", " ", "第86話", " ", "JP", " ", "[CR]"], "labels": ["B-GROUP", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "I-TITLE", "O", "B-SEASON", "B-SEASON", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["[Lv.1]", " ", "メ", "イ", "ド", "イ", "ン", "ア", "ビ", "ス", " ", "[特别篇]", " ", "[CR]", " ", "[AAC]"], "labels": ["B-GROUP", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SPECIAL", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["[dHD]", " ", "Oshi", " ", "no", " ", "Ko", " ", "[Movie]", " ", "[BDMV]", " ", "[Baha]"], "labels": ["B-GROUP", "O", "B-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "B-SPECIAL", "O", "B-SOURCE", "O", "B-GROUP"]}{"tokens": ["【【爱恋字幕社】", "】", "夏", "日", "重", "现", " ", "第三季", " ", "E95", " ", "[720P]", " ", "[360p]"], "labels": ["B-GROUP", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "B-EPISODE", "O", "B-RESOLUTION", "O", "B-RESOLUTION"]}{"tokens": ["[SweetSub]", " ", "[480P]", " ", "[GB]", " ", "Fate", "/", "stay", " ", "night", " ", "S03", " ", "第38话"], "labels": ["B-GROUP", "O", "B-RESOLUTION", "O", "B-SOURCE", "O", "B-TITLE", "I-TITLE", "I-TITLE", "O", "I-TITLE", "O", "B-SEASON", "O", "B-EPISODE"]}{"tokens": ["实", "力", "至", "上", "主", "义", "的", "教", "室", " ", "-", " ", "E64", " ", "[[1080P]", "]", " ", "[{meta_bracket}]"], "labels": ["B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "O", "O", "B-EPISODE", "O", "B-RESOLUTION", "B-TITLE", "O", "B-SOURCE"]}{"tokens": ["[POPGO]", " ", " ", "Revue", " ", "Starlight", " ", "S03", " ", " ", "|", " ", " ", "90", " ", "[x265]"], "labels": ["B-GROUP", "O", "O", "B-TITLE", "O", "I-TITLE", "O", "B-SEASON", "O", "O", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["[Kuroi-Raws]", " ", "無", "職", "転", "生", " ", "第三季", " ", "-", " ", "ep97", " ", "JP"], "labels": ["B-GROUP", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["サ", "マ", "ー", "タ", "イ", "ム", "レ", "ン", "ダ", " ", "第92話"], "labels": ["B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-EPISODE"]}{"tokens": ["Erai", "-", "raws", " ", "M", "3", "~", "ソ", "ノ", "黒", "キ", "鋼", "~", " ", "S03", " ", " ", "|", " ", "第71话", " ", "FLAC"], "labels": ["B-EPISODE", "O", "B-TITLE", "O", "I-TITLE", "B-EPISODE", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "O", "B-SEASON", "O", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["[ReinForce]", " ", "魔", "法", "少", "女", "小", "圆", " ", "[PV]", " ", " ", " ", "Ep35", " ", "[AMZN]"], "labels": ["B-GROUP", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SPECIAL", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["[Zero-Raws]", " ", "[AMZN]", " ", "[WEB-DL]", " ", "K", "-", "ON", "!", " ", "S5", " ", "EP54"], "labels": ["B-GROUP", "O", "B-SOURCE", "O", "B-SOURCE", "O", "B-TITLE", "O", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "B-EPISODE"]}{"tokens": ["((VCB-Studio)", ")", " ", "B", "-", "PROJECT", " ", "3", "rd", " ", "Season", " ", "第6话", " ", "CHT"], "labels": ["B-GROUP", "B-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "B-EPISODE", "B-TITLE", "O", "B-SEASON", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["【【白月字幕组】", "】", "M", "3", "~", "ソ", "ノ", "黒", "キ", "鋼", "~", " ", "54", " ", "HEVC"], "labels": ["B-GROUP", "B-TITLE", "I-TITLE", "B-EPISODE", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["[DIY]", " ", "[WebRip]", " ", "[DTS]", " ", "我", "心", "里", "危", "险", "的", "东", "西", " ", "S04", " ", "04"], "labels": ["B-GROUP", "O", "B-SOURCE", "O", "B-SOURCE", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "B-EPISODE"]}{"tokens": ["Nekomoe", " ", "kissaten", " ", "Laid", "-", "Back", " ", "Camp", " ", "2nd Season", " ", " ", "-", " ", " ", "51", " ", "x264"], "labels": ["B-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "B-SEASON", "O", "O", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["((幻樱字幕组)", ")", " ", "Jujutsu", " ", "Kaisen", " ", "S01", " ", "49", " ", "[Netflix]"], "labels": ["B-GROUP", "B-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "B-SEASON", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["【【铃风字幕组】", "】", "★", "新", "番", "★", "M", "3", "~", "ソ", "ノ", "黒", "キ", "鋼", "~", " ", "第9話", " ", "[720P]"], "labels": ["B-GROUP", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "B-EPISODE", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "O", "B-EPISODE", "O", "B-RESOLUTION"]}{"tokens": ["新", "世", "纪", "福", "音", "战", "士", " ", "第90话"], "labels": ["B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-EPISODE"]}{"tokens": ["[POPGO]", " ", "91", " ", "Days", " ", "04", " ", "[简日双语]", " ", "[JP]"], "labels": ["B-GROUP", "O", "B-EPISODE", "O", "B-TITLE", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["[Rally]", " ", "紫", "罗", "兰", "永", "恒", "花", "园", " ", "[特别篇]", " ", "[DVD]", " ", "[AAC]"], "labels": ["B-GROUP", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SPECIAL", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["[POPGO]", " ", "か", "ぐ", "や", "様", "は", "告", "ら", "せ", "た", "い", " ", "Season 1", " ", "-", " ", "04", " ", "CHT"], "labels": ["B-GROUP", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["Lycoris", " ", "Recoil", " ", "S2", "Season", " ", "第63话", " ", "[360P]", " ", "[SubsPlease]"], "labels": ["B-TITLE", "O", "I-TITLE", "O", "B-SEASON", "B-SEASON", "O", "B-EPISODE", "O", "B-RESOLUTION", "O", "B-GROUP"]}{"tokens": ["[SumiSora]", " ", "Hell", "'", "s", " ", "Paradise", " ", "S2", " ", "~", " ", "55", " ", "[FLAC]", "★", "2024", "★"], "labels": ["B-GROUP", "O", "B-TITLE", "I-TITLE", "I-TITLE", "O", "I-TITLE", "O", "B-SEASON", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE", "B-TITLE", "B-EPISODE", "B-TITLE"]}{"tokens": ["[Tk]", " ", "昭", "和", "元", "禄", "落", "语", "心", "中", " ", "Seasons", " ", "2", " ", "_", " ", "第19話", " ", "[DTS]"], "labels": ["B-GROUP", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "B-EPISODE", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["[Sakurato]", " ", "Bocchi", " ", "the", " ", "Rock", " ", "[OP]", " ", " ", " ", "E56", " ", "[BDMV]"], "labels": ["B-GROUP", "O", "B-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "B-SPECIAL", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["SubsPlease", " ", "M", "3", "~", "ソ", "ノ", "黒", "キ", "鋼", "~", " ", "第三季", " ", " ", "|", " ", "86", " ", "[WEB-DL]"], "labels": ["B-TITLE", "O", "I-TITLE", "B-EPISODE", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "O", "B-SEASON", "O", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["Steins", " ", "Gate", " ", "34", " ", "[Baha]", " ", "[MP3]", " ", "[h265]"], "labels": ["B-TITLE", "O", "I-TITLE", "O", "B-EPISODE", "O", "B-GROUP", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["[Kagura]", " ", "AKB", "0048", " ", "72", " ", "AAC", " ", "WEB-DL"], "labels": ["B-GROUP", "O", "B-TITLE", "B-EPISODE", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["[Erai-raws]", " ", "灌", "篮", "高", "手", " ", "S03", " ", "~", " ", "32", " ", "[Baha]", " ", "[AMZN]"], "labels": ["B-GROUP", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "O", "O", "B-EPISODE", "O", "B-GROUP", "O", "B-SOURCE"]}{"tokens": ["星", "际", "牛", "仔", " ", "59"], "labels": ["B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-EPISODE"]}{"tokens": ["[m.3.3.w]", " ", "ヴ", "ァ", "イ", "オ", "レ", "ッ", "ト", "・", "エ", "ヴ", "ァ", "ー", "ガ", "ー", "デ", "ン", " ", "[特別篇]", " ", "~", " ", "ep16", " ", "1920x1080"], "labels": ["B-GROUP", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SPECIAL", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["[PHZ]", " ", "HUNTER", "×", "HUNTER", " ", "S4", " ", "~", " ", "第76话", " ", "[2160P]", " ", "WEB-DL", " ", "[AV1]", " ", "[1080p]"], "labels": ["B-GROUP", "O", "B-TITLE", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "O", "O", "B-EPISODE", "O", "B-RESOLUTION", "O", "B-SOURCE", "O", "B-SOURCE", "O", "B-RESOLUTION"]}{"tokens": ["5", "等", "分", "の", "花", "嫁", " ", "第四季", " ", "_", " ", "02", " ", "[h264]", " ", "[TVRip]"], "labels": ["B-EPISODE", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["ANK", "-", "Raws", " ", "Fullmetal", " ", "Alchemist", " ", "Movie", " ", " ", "-", " ", " ", "09", " ", "[Baha]"], "labels": ["B-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "O", "O", "O", "O", "B-EPISODE", "O", "B-GROUP"]}{"tokens": ["银", "魂", " ", " ", " ", "32", " ", "[[H265]", "]", " ", "[{meta_bracket}]"], "labels": ["B-TITLE", "I-TITLE", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE", "B-TITLE", "O", "B-SOURCE"]}{"tokens": ["[POPGO]", " ", "720P", " ", "[Baha]", " ", "Sword", " ", "Art", " ", "Online", " ", "第一季", " ", "57"], "labels": ["B-GROUP", "O", "B-SOURCE", "O", "B-GROUP", "O", "B-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "B-SEASON", "O", "B-EPISODE"]}{"tokens": ["ANK", "-", "Raws", " ", "Fate", "/", "Extra", " ", "S02", " ", "_", " ", "ep85", " ", "[480P]"], "labels": ["B-TITLE", "O", "I-TITLE", "O", "I-TITLE", "I-TITLE", "B-EPISODE", "O", "B-SEASON", "O", "O", "O", "B-EPISODE", "O", "B-RESOLUTION"]}{"tokens": ["葬", "送", "的", "芙", "莉", "莲", " ", "89", " ", "[AV1]", " ", "[360P]", " ", "AAC"], "labels": ["B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-RESOLUTION", "O", "B-SOURCE"]}{"tokens": ["[SweetSub]", " ", "薬", "屋", "の", "ひ", "と", "り", "ご", "と", " ", "第62話", " ", "[AVC]", " ", "[AMZN]"], "labels": ["B-GROUP", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["ONE", " ", "PIECE", " ", "S5", " ", "~", " ", "22", " ", "FLAC", " ", "FLAC"], "labels": ["B-TITLE", "O", "I-TITLE", "O", "B-SEASON", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["Lilith", "-", "Raws", " ", "银", "魂", " ", "S2", "Season", " ", " ", "|", " ", "35", " ", "[h264]"], "labels": ["B-TITLE", "O", "I-TITLE", "O", "I-TITLE", "I-TITLE", "O", "B-SEASON", "B-SEASON", "O", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["[Coalgirls]", " ", "ワ", "ン", "ダ", "ー", "エ", "ッ", "グ", "・", "プ", "ラ", "イ", "オ", "リ", "テ", "ィ", " ", "Season 2", " ", "EP12", " ", "[1080P]", " ", "[CHS]", " ", "[HEVC]"], "labels": ["B-GROUP", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "B-EPISODE", "O", "B-RESOLUTION", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["Erai", "-", "raws", " ", "OVERLORD", " ", "3", "rd", " ", "Season", " ", "~", " ", "63", " ", "GB"], "labels": ["B-EPISODE", "O", "B-TITLE", "O", "I-TITLE", "O", "B-EPISODE", "B-TITLE", "O", "B-SEASON", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["★", "07", "月", "新", "番", "★", "【【极影字幕社】", "】", "か", "ぐ", "や", "様", "は", "告", "ら", "せ", "た", "い", " ", "Season 2", " ", "64", " ", "1080p", " ", "JP"], "labels": ["B-TITLE", "B-EPISODE", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "B-GROUP", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["【【极影字幕社】", "】", "B", "-", "PROJECT", " ", "第一季", " ", "第1话", " ", "FLAC", " ", "[WEB-DL]"], "labels": ["B-GROUP", "B-TITLE", "I-TITLE", "O", "I-TITLE", "O", "B-SEASON", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["【【轻之国度】", "】", "D", ".", "C", ".", "III", " ", "~", "Da", " ", "Capo", " ", "III", "~", " ", "57", " ", "[AAC]"], "labels": ["B-GROUP", "B-TITLE", "I-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "O", "I-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["龙", "珠", " ", "第三季", " ", " ", "-", " ", " ", "第26話", " ", "[480P]", " ", "[MP3]"], "labels": ["B-TITLE", "I-TITLE", "O", "B-SEASON", "O", "O", "O", "O", "O", "B-EPISODE", "O", "B-RESOLUTION", "O", "B-SOURCE"]}{"tokens": ["[m.3.3.w]", " ", "紫", "罗", "兰", "永", "恒", "花", "园", " ", "16", " ", "[HEVC]", " ", "WEB-DL"], "labels": ["B-GROUP", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["[UCCUSS]", " ", "Neon", " ", "Genesis", " ", "Evangelion", " ", "OAD", " ", " ", "|", " ", " ", "第63话", " ", "[H265]"], "labels": ["B-GROUP", "O", "B-TITLE", "O", "I-TITLE", "O", "B-EPISODE", "O", "B-TITLE", "O", "O", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["[DMG]", " ", "無", "職", "転", "生", " ", "S3", " ", "_", " ", "54", " ", "BDRip", " ", "[x265]", " ", "[360P]", " ", "GB"], "labels": ["B-GROUP", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-SOURCE", "O", "B-RESOLUTION", "O", "B-SOURCE"]}{"tokens": ["[WOLF]", " ", "カ", "ウ", "ボ", "ー", "イ", "ビ", "バ", "ッ", "プ", " ", "Movie", " ", "[TVRip]", " ", "[简日双语]"], "labels": ["B-GROUP", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "I-TITLE", "O", "I-TITLE", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["[Snow-Raws]", " ", "[DTS]", " ", "[WebRip]", " ", "lovelive", "!", " ", "S2", " ", "61"], "labels": ["B-GROUP", "O", "B-SOURCE", "O", "B-SOURCE", "O", "B-TITLE", "I-TITLE", "O", "B-SEASON", "O", "B-EPISODE"]}{"tokens": ["Code", " ", "Geass", " ", "S2", " ", " ", " ", "76", " ", "[WEBDL]", " ", "GB"], "labels": ["B-TITLE", "O", "I-TITLE", "O", "B-SEASON", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE", "O", "B-SOURCE"]}{"tokens": ["ANi", " ", "AKB", "0048", " ", "S5", " ", " ", "|", " ", "84", " ", "[GB]"], "labels": ["B-TITLE", "O", "I-TITLE", "B-EPISODE", "O", "B-SEASON", "O", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["[C1]", " ", "Laid", "-", "Back", " ", "Camp", " ", "Movie", " ", " ", "-", " ", " ", "EP43", " ", "GB"], "labels": ["B-GROUP", "O", "B-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "O", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}{"tokens": ["[YYQ]", " ", "[720p]", " ", "AAC", " ", "8", " ", "Girls", " ", "Season 1", " ", "第93話"], "labels": ["B-GROUP", "O", "B-RESOLUTION", "O", "B-SOURCE", "O", "B-EPISODE", "O", "B-TITLE", "O", "B-SEASON", "O", "B-EPISODE"]}{"tokens": ["Nekomoe", " ", "kissaten", " ", "K", "-", "ON", "!", " ", "Season 1", " ", "~", " ", "第12话", " ", "[WEB-DL]"], "labels": ["B-TITLE", "O", "I-TITLE", "O", "I-TITLE", "O", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "O", "O", "B-EPISODE", "O", "B-SOURCE"]}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757"""Synthetic training data generator for anime filename parser.
Generates labeled anime filenames using template filling with content pools.Each sample is a filename tokenized into tokens with BIO labels.
Output format: JSONL (one JSON object per line) {"tokens": [...], "labels": [...]}"""
import jsonimport osimport randomimport refrom typing import Dict, List, Optional, Tuple
from config import Configfrom tokenizer import AnimeTokenizer, create_tokenizer
# ═══════════════════════════════════════════════════════════════# Content Pools# ═══════════════════════════════════════════════════════════════
# ---- TITLES (200+ mixed CHS/CHT/EN/JP) ----TITLES: List[str] = [ # Chinese (100+) "葬送的芙莉莲", "葬送的芙莉蓮", "咒术回战", "咒術迴戰", "鬼灭之刃", "鬼滅之刃", "间谍过家家", "SPY×FAMILY", "葬送のフリーレン", "进击的巨人", "進擊的巨人", "钢之炼金术师", "鋼之煉金術師", "新世纪福音战士", "新世纪エヴァンゲリオン", "死亡笔记", "DEATH NOTE", "命运石之门", "Steins;Gate", "魔法少女小圆", "魔法少女まどか☆マギカ", "反叛的鲁路修", "コードギアス", "未闻花名", "あの日見た花の名前を僕達はまだ知らない", "Clannad", "Angel Beats!", "輕音少女", "K-ON!", "紫罗兰永恒花园", "ヴァイオレット・エヴァーガーデン", "来自深渊", "メイドインアビス", "无职转生", "無職転生", "转生成史莱姆", "転生したらスライムだった件", "关于我转生变成史莱姆这档事", "Re:从零开始的异世界生活", "Re:ゼロから始める異世界生活", "辉夜大小姐想让我告白", "かぐや様は告らせたい", "我的青春恋爱物语果然有问题", "やはり俺の青春ラブコメはまちがっている", "刀剑神域", "ソードアート・オンライン", "OVERLORD", "为美好的世界献上祝福", "この素晴らしい世界に祝福を", "实力至上主义的教室", "ようこそ実力至上主義の教室へ", "86-不存在的战区", "86-エイティシックス-", "孤独摇滚", "ぼっち・ざ・ろっく", "Girls Band Cry", "我心里危险的东西", "僕の心のヤバイやつ", "药屋少女的呢喃", "薬屋のひとりごと", "迷宫饭", "ダンジョン飯", "我推的孩子", "【推しの子】", "葬送的芙莉莲 第二季", "死神", "BLEACH", "海贼王", "ONE PIECE", "火影忍者", "NARUTO", "猎人", "HUNTER×HUNTER", "龙珠", "DRAGON BALL", "灌篮高手", "SLAM DUNK", "银魂", "GIN TAMA", "Fate/stay night", "Fate/Grand Order", "Fate/Zero", "攻壳机动队", "攻殻機動隊", "星际牛仔", "カウボーイビバップ", "混沌武士", "サムライチャンプルー", "虫师", "蟲師", "三月的狮子", "3月のライオン", "昭和元禄落语心中", "昭和元禄落語心中", "白箱", "SHIROBAKO", "比宇宙更远的地方", "宇宙よりも遠い場所", "摇曳露营", "ゆるキャン△", "赛马娘", "ウマ娘", "偶像大师", "アイドルマスター", "Love Live!", "lovelive!", "BanG Dream!", "少女歌剧", " Revue Starlight", "奇蛋物语", "ワンダーエッグ・プライオリティ", "莉可丽丝", "リコリス・リコイル", "夏日重现", "サマータイムレンダ", "边缘行者", "CYBERPUNK EDGERUNNERS",
# English/Romanized (50+) "Sousou no Frieren", "Jujutsu Kaisen", "Kimetsu no Yaiba", "Attack on Titan", "Shingeki no Kyojin", "Fullmetal Alchemist", "Neon Genesis Evangelion", "Steins Gate", "Puella Magi Madoka Magica", "Code Geass", "Violet Evergarden", "Made in Abyss", "Mushoku Tensei", "That Time I Got Reincarnated as a Slime", "Re Zero Starting Life in Another World", "Kaguya-sama Love is War", "Sword Art Online", "Konosuba God's Blessing on this Wonderful World", "Classroom of the Elite", "Solo Leveling", "Bocchi the Rock", "Dungeon Meshi", "Delicious in Dungeon", "Oshi no Ko", "My Hero Academia", "Demon Slayer", "Chainsaw Man", "Hell's Paradise", "Jigokuraku", "Vinland Saga", "Ranking of Kings", "Ousama Ranking", "Spy x Family", "Cyberpunk Edgerunners", "Lycoris Recoil", "Summer Time Rendering", "Wonder Egg Priority", "Odd Taxi", "Sonny Boy", "Wonder Egg Priority", "Super Cub", "Yuru Camp", "Laid-Back Camp",
# Numbers in title (20+) "86 Eighty Six", "3-gatsu no Lion", "5-toubun no Hanayome", "5等分の花嫁", "7 Seeds", "7-seeds", "91 Days", "91Days", "100-man no Inochi no Ue ni Ore wa Tatteiru", "100万の命の上に俺は立っている", "300-en no Otsuki Samurai", "5000兆円欲しい!", "2.43 清陰高校男子バレー部", "22/7", "24 2", "8 Girls", "80万再生",
# With punctuation (20+) "K-ON!", "NEW GAME!", "GO! GO! 575", "Wake Up, Girls!", "Show By Rock!!", "Hello!! KINMOZA", "Hi☆sCoool! セハガール", "AKB0048", "C³", "WIXOSS", "√Letter", "√3 (ルートスリー)", "DOG DAYS'", "DOG DAYS''", "RAIL WARS!", "M3~ソノ黒キ鋼~", "D.C.III ~Da Capo III~", "B-Project", "Fate/Extra", "DIABOLIK LOVERS", "B-PROJECT",]
# ---- GROUPS (50+) ----GROUPS_EN_BRACKET: List[str] = [ "[ANi]", "[Baha]", "[VCB-Studio]", "[Lilith-Raws]", "[SubsPlease]", "[Erai-raws]", "[DBD-Raws]", "[AI-Raws]", "[Ohys-Raws]", "[Moozzi2]", "[NT-Raws]", "[Ember]", "[Judas]", "[Leopard-Raws]", "[m.3.3.w]", "[Kagura]", "[HorribleSubs]", "[DeadFish]", "[CBM]", "[FFF]", "[SSA]", "[C1]", "[WOLF]", "[CKJ]", "[Zero-Raws]", "[dHD]", "[UCCUSS]", "[Tk]", "[ReinForce]", "[Kuroi-Raws]", "[Kamigami]", "[DIY]", "[QTS]", "[XEI]", "[Snow-Raws]", "[Lv.1]", "[NAOKI]", "[Hakata]", "[PHZ]", "[Sakurato]", "[YYQ]", "[Beatrice]", "[Rally]", "[SweetSub]", "[DHR]", "[HR]", "[Hakugetsu]", "[DMG]", "[HYSUB]", "[POPGO]", "[SumiSora]", "[KPDM]", "[CASO]", "[KTXP]", "[Snow-Raws]", "[philosophy-raws]", "[Coalgirls]", "[Elysium]", "[FFF]", "[B-MXT]", "ANK-Raws",]
GROUPS_CN_BRACKET: List[str] = [ "【喵萌奶茶屋】", "【桜都字幕组】", "【幻樱字幕组】", "【极影字幕社】", "【动漫国字幕组】", "【澄空学园】", "【华盟字幕社】", "【千夏字幕组】", "【铃风字幕组】", "【白月字幕组】", "【风之圣殿】", "【诸神字幕组】", "【雪飘工作室】", "【茉语月译】", "【爱恋字幕社】", "【天月动工】", "【星空字幕组】", "【蓝调动漫】", "【森罗万像】", "【轻之国度】",]
GROUPS_NO_BRACKET: List[str] = [ "ANi", "Baha", "Nekomoe kissaten", "SubsPlease", "Erai-raws", "VCB-Studio", "Moozzi2", "HorribleSubs", "DeadFish", "Kamigami", "ReinForce", "Lilith-Raws", "Ohys-Raws",]
GROUPS_PAREN: List[str] = [ "(喵萌奶茶屋)", "(桜都字幕组)", "(幻樱字幕组)", "(极影字幕社)", "(动漫国字幕组)", "(澄空学园)", "(VCB-Studio)", "(Erai-raws)",]
# ---- SEASONS (20+ variations) ----SEASONS: List[str] = [ "S1", "S2", "S3", "S4", "S5", "S01", "S02", "S03", "S04", "Season 1", "Season 2", "Season 3", "第一季", "第二季", "第三季", "第四季", "1st Season", "2nd Season", "3rd Season", "Seasons 1", "Seasons 2", "S1Season", "S2Season",]
# ---- EPISODES (15+ variations) ----EPISODES: List[str] = [f"{i:02d}" for i in range(1, 100)] # 01-99EPISODE_PREFIXES: List[str] = [ "EP", "Ep", "ep", "E",]EPISODE_CN: List[str] = [f"第{i}话" for i in range(1, 100)] + [f"第{i}話" for i in range(1, 100)]EPISODE_HASH: List[str] = [f"#{i:02d}" for i in range(1, 100)]
# ---- META: RESOLUTION ----RESOLUTIONS: List[str] = [ "[1080P]", "[1080p]", "[720P]", "[720p]", "[4K]", "[2160P]", "[2160p]", "[480P]", "[480p]", "[360P]", "[360p]", "1080P", "1080p", "720P", "720p", "1920x1080", "1280x720", "3840x2160",]
# ---- META: SOURCE ----SOURCES: List[str] = [ "[WEB-DL]", "[WEBDL]", "[BDRip]", "[BDMV]", "[DVD]", "[TVRip]", "[CR]", "[Netflix]", "[AMZN]", "[Baha]", "[WebRip]", "WEB-DL", "BDRip", "Baha",]
# ---- META: CODEC ----CODECS: List[str] = [ "[x265]", "[x264]", "[HEVC]", "[AVC]", "[AV1]", "[H264]", "[H265]", "[h264]", "[h265]", "x265", "x264", "HEVC",]
# ---- META: AUDIO ----AUDIO: List[str] = [ "[FLAC]", "[AAC]", "[MP3]", "[DTS]", "FLAC", "AAC",]
# ---- META: LANGUAGE ----LANGUAGES: List[str] = [ "[CHT]", "[GB]", "[JP]", "[简日双语]", "[CHS]", "[BIG5]", "CHT", "GB", "JP",]
# ---- COMBINED META ----ALL_METAS: List[str] = RESOLUTIONS + SOURCES + CODECS + AUDIO + LANGUAGESALL_METAS_BRACKET: List[str] = [m for m in ALL_METAS if m.startswith("[") or m.startswith("【") or m.startswith("(")]
# ---- SPECIAL ----SPECIALS: List[str] = [ "[Movie]", "[OVA]", "[OAD]", "[SP]", "[剧场版]", "[特別篇]", "[特别篇]", "[NC]", "[OP]", "[ED]", "[PV]", "[CM]", "Movie", "OVA", "OAD", "SP",]
# ---- SEPARATORS ----SEPARATORS: List[str] = [" - ", " ", "_", " | ", "~", "~", "-", " |"]
# ═══════════════════════════════════════════════════════════════# Templates# ═══════════════════════════════════════════════════════════════
TEMPLATES: List[str] = [ # Standard: GROUP + TITLE + SEASON + SEP + EPISODE + META "{group} {title} {season} {sep} {episode} {meta1} {meta2}", "{group} {title} {season} {episode} {meta1} {meta2} {meta3}", "{group} {title} {episode} {meta1} {meta2}", "{group} {title} {season} {sep} {episode} {meta1}",
# No GROUP "{title} {season} {sep} {episode} {meta1} {meta2}", "{title} {episode} {meta1} {meta2} {meta3}",
# GROUP at end "{title} {season} {episode} {meta1} {group}",
# META before title "{group} {meta1} {meta2} {title} {season} {episode}",
# Special type "{group} {title} {special} {sep} {episode} {meta1}", "{group} {title} {special} {meta1} {meta2}",
# CN bracket GROUP "【{group_cn}】{title} {season} {episode} {meta1} {meta2}", "【{group_cn}】{title} {episode} {meta1}",
# CN decorative "【{group_cn}】★新番★{title} {episode} {meta1}",
# Paren GROUP "({group_cn_paren}) {title} {season} {episode} {meta1}",
# No bracket GROUP "{group_no_bracket} {title} {season} {sep} {episode} {meta1}",
# OVA/Movie "{group} {title} {special} {meta1} {meta2}",
# Season with composite episode "{group} {title} {season} {sep} {episode} {meta1} {meta2} {meta3} {meta4}",
# Minimal "{title} {episode}",
# Title first, meta after "{title} {sep} {episode} [{meta_bracket}] [{meta_bracket}]",]
# ═══════════════════════════════════════════════════════════════# Label mapping# ═══════════════════════════════════════════════════════════════
LABEL_MAP: Dict[str, str] = { "title": "TITLE", "season": "SEASON", "episode": "EPISODE", "group": "GROUP", "special": "SPECIAL", "resolution": "RESOLUTION", "source": "SOURCE", "codec": "SOURCE", # CODEC merged into SOURCE "audio": "SOURCE", "language": "SOURCE", "sep": "O", "decoration": "O", "noise": "O",}
# Additional meta tokens to categorizeMETA_RESOLUTION_TOKENS: List[str] = [ "1080P", "1080p", "720P", "720p", "4K", "2160P", "2160p", "480P", "480p", "360P", "360p", "1920x1080", "1280x720", "3840x2160",]
META_SOURCE_TOKENS: List[str] = [ "WEB-DL", "WEBDL", "BDRip", "BDMV", "DVD", "TVRip", "CR", "Netflix", "AMZN", "Baha", "WebRip",]
META_CODEC_TOKENS: List[str] = [ "x265", "x264", "HEVC", "AVC", "AV1", "H264", "H265", "h264", "h265",]
META_AUDIO_TOKENS: List[str] = [ "FLAC", "AAC", "MP3", "DTS",]
META_LANG_TOKENS: List[str] = [ "CHT", "GB", "JP", "CHS", "BIG5", "简日双语",]
def categorize_meta_token(token: str) -> str: """Determine the entity type for a meta token (resolution/source/etc).""" # Strip brackets for matching clean = token.strip("[]()【】") if clean in META_RESOLUTION_TOKENS: return "RESOLUTION" if clean in META_SOURCE_TOKENS: return "SOURCE" if clean in META_CODEC_TOKENS: return "SOURCE" # merged if clean in META_AUDIO_TOKENS: return "SOURCE" # merged if clean in META_LANG_TOKENS: return "SOURCE" # merged return "SOURCE" # default meta type
def assign_bio(tokens: List[str], token_category: List[str]) -> List[str]: """ Assign BIO labels to tokens based on their categories.
Handles multi-token entities (TITLE, GROUP) that may span across separator tokens (spaces, etc.). For example, "Attack on Titan" should have B-TITLE for "Attack", I-TITLE for "on", I-TITLE for "Titan" even though there are O-labeled spaces between them.
Args: tokens: List of token strings token_category: Category for each token (title, season, episode, etc.)
Returns: List of BIO label strings (B-TITLE, I-TITLE, O, etc.) """ labels: List[str] = [] active_entity: Optional[str] = None # tracks the current entity across O tokens
for token, cat in zip(tokens, token_category): entity = LABEL_MAP.get(cat, "O")
if entity == "O": labels.append("O") # Don't reset active_entity — allows multi-word entities # to span across separator tokens (spaces, punctuation) elif entity in ("SEASON", "EPISODE", "SPECIAL", "RESOLUTION", "SOURCE"): # Single-token or always-B entities labels.append(f"B-{entity}") active_entity = None else: # Multi-token entities (TITLE, GROUP) if entity == active_entity: labels.append(f"I-{entity}") else: labels.append(f"B-{entity}") active_entity = entity
return labels
# ═════════════════════════════��═════════════════════════════════# Sample Generation# ═══════════════════════════════════════════════════════════════
def pick_random(pool: list): """Pick a random item from a list.""" return random.choice(pool)
# ---- Category tracking markers ----# Using Unicode Private Use Area characters that NEVER appear in anime filenames.# These are single characters that the tokenizer treats as "Other" → single-char tokens.# They cannot be merged into bracket content, making them robust markers._CAT_PUA_BASE = '\uE100' # Start of PUA region for category markers_CAT_MARKER_END_CHAR = '\uE000' # End marker character_CAT_INDEX: Dict[str, int] = { "title": 0, "season": 1, "episode": 2, "special": 3, "group": 4, "resolution": 5, "source": 6, "sep": 7, "decoration": 8,}_CAT_FROM_INDEX: Dict[int, str] = {v: k for k, v in _CAT_INDEX.items()}# Pre-compute marker characters_CAT_MARKER_CHARS: Dict[str, str] = { cat: chr(ord(_CAT_PUA_BASE) + idx) for cat, idx in _CAT_INDEX.items()}
def _cat_marker(category: str) -> str: """Get a category start marker character.""" return _CAT_MARKER_CHARS.get(category, _CAT_MARKER_CHARS["title"])
# Regex to detect bracket-wrapped placeholders: 【{placeholder}】, ({placeholder}), etc._BRACKET_WRAP_RE = re.compile(r'([\[(【《\(])\{(\w+)\}([\])】》\)])')
def generate_template_filled(template: str) -> Tuple[str, Dict[str, str]]: """ Fill a template with random content from pools.
Returns: (filled_string, category_map) where each placeholder's value is wrapped with category marker characters for tracking.
For bracket-wrapped placeholders (e.g., 【{group_cn}】), markers are placed OUTSIDE the brackets to prevent marker-bracket merging. """ fields: Dict[str, str] = {} marker_placeholders: List[str] = []
for placeholder in ["group", "group_cn", "group_cn_paren", "group_no_bracket", "title", "season", "episode", "special", "meta1", "meta2", "meta3", "meta4", "sep", "meta_bracket", "decoration"]: if "{" + placeholder + "}" not in template: continue
if placeholder == "title": val = pick_random(TITLES) cat = "title" elif placeholder == "season": val = pick_random(SEASONS) cat = "season" elif placeholder == "episode": choice = random.random() if choice < 0.6: val = pick_random(EPISODES) elif choice < 0.8: prefix = pick_random(EPISODE_PREFIXES) val = prefix + pick_random(EPISODES) else: val = pick_random(EPISODE_CN) cat = "episode" elif placeholder == "group": val = pick_random(GROUPS_EN_BRACKET) cat = "group" elif placeholder == "group_cn": val = pick_random(GROUPS_CN_BRACKET) cat = "group" elif placeholder == "group_cn_paren": val = pick_random(GROUPS_PAREN) cat = "group" elif placeholder == "group_no_bracket": val = pick_random(GROUPS_NO_BRACKET) cat = "group" elif placeholder == "special": val = pick_random(SPECIALS) cat = "special" elif placeholder.startswith("meta"): meta_type = random.random() if meta_type < 0.3: val = pick_random(RESOLUTIONS) cat = "resolution" elif meta_type < 0.5: val = pick_random(SOURCES) cat = "source" elif meta_type < 0.65: val = pick_random(CODECS) cat = "source" elif meta_type < 0.8: val = pick_random(AUDIO) cat = "source" else: val = pick_random(LANGUAGES) cat = "source" elif placeholder == "sep": val = pick_random(SEPARATORS) cat = "sep" elif placeholder == "meta_bracket": val = pick_random(ALL_METAS_BRACKET) clean = val.strip("[]()【】") if clean in META_RESOLUTION_TOKENS: cat = "resolution" elif clean in META_SOURCE_TOKENS: cat = "source" elif clean in META_CODEC_TOKENS: cat = "source" elif clean in META_AUDIO_TOKENS: cat = "source" elif clean in META_LANG_TOKENS: cat = "source" else: cat = "source" elif placeholder == "decoration": decos = ["★04月新番★", "★07月新番★", "★10月新番★", "★01月新番★", "★2024★", "★2025★", "★2026★", "[完]", "[合集]", "【完结】"] val = pick_random(decos) cat = "decoration" else: val = placeholder cat = "O"
fields[placeholder] = cat placeholder_slot = "{" + placeholder + "}"
# Check if placeholder is wrapped in template brackets: 【{x}】, ({x}), etc. # If so, place markers OUTSIDE the brackets to prevent merging. bracket_match = _BRACKET_WRAP_RE.search(template) if bracket_match and bracket_match.group(2) == placeholder: open_bracket = bracket_match.group(1) close_bracket = bracket_match.group(3) replacement = f"{_cat_marker(cat)}{open_bracket}{val}{close_bracket}{_CAT_MARKER_END_CHAR}" template = template.replace( f"{open_bracket}{placeholder_slot}{close_bracket}", replacement, 1 ) else: # Normal non-wrapped placeholder template = template.replace( placeholder_slot, f"{_cat_marker(cat)}{val}{_CAT_MARKER_END_CHAR}", 1 )
return template, fields
def generate_sample(tokenizer: AnimeTokenizer, templates: List[str]) -> Dict: """ Generate one labeled training sample.
Placeholder values are wrapped with category marker tokens (e.g., [__title__]value[__/__]) so that assign_token_categories can track which token belongs to which category.
Returns: {"tokens": [...], "labels": [...]} where labels are in BIO format. """ template = pick_random(templates) filled_text, category_map = generate_template_filled(template)
# Add noise: random decoration if random.random() < 0.05: deco = pick_random(["★04月新番★", "★07月新番★", "★10月新番★", "★01月新番★", "[完]", "【完结】", "★2024★", "★2025★"]) if random.random() < 0.5: filled_text = _cat_marker("decoration") + deco + _CAT_MARKER_END_CHAR + filled_text else: filled_text = filled_text + _cat_marker("decoration") + deco + _CAT_MARKER_END_CHAR
# Tokenize tokens = tokenizer.tokenize(filled_text) if not tokens: return generate_sample(tokenizer, templates) # retry on empty
# Assign categories using marker tokens (also filters out markers) filtered_tokens, token_categories = assign_token_categories(tokens, filled_text, category_map)
# Retry if all tokens were filtered out (shouldn't happen, but safety) if not filtered_tokens: return generate_sample(tokenizer, templates)
# Generate BIO labels labels = assign_bio(filtered_tokens, token_categories)
assert len(filtered_tokens) == len(labels), f"Token/label mismatch: {len(filtered_tokens)} vs {len(labels)}"
return { "tokens": filtered_tokens, "labels": labels, }
def assign_token_categories( tokens: List[str], filled_text: str, category_map: Dict[str, str]) -> Tuple[List[str], List[str]]: """ Assign categories to tokens using embedded Unicode PUA marker chars.
Category markers are PUA Unicode chars (\uE100-\uE108) that the tokenizer outputs as single-character tokens. They bracket each placeholder's content and cannot be merged into bracket content.
Returns: (filtered_tokens, categories) with marker chars removed. """ filtered_tokens: List[str] = [] categories: List[str] = [] current_category: Optional[str] = None markers_encountered = 0
for token in tokens: # Check for end marker if len(token) == 1 and token == _CAT_MARKER_END_CHAR: current_category = None markers_encountered += 1 continue
# Check for category start marker (PUA characters) if len(token) == 1 and _CAT_PUA_BASE <= token <= chr(ord(_CAT_PUA_BASE) + 8): idx = ord(token) - ord(_CAT_PUA_BASE) current_category = _CAT_FROM_INDEX.get(idx, None) markers_encountered += 1 continue
filtered_tokens.append(token) if current_category is not None: categories.append(current_category) else: categories.append(_heuristic_category(token))
# If no markers were found, use pure heuristics as fallback if markers_encountered == 0: categories = [_heuristic_category(t) for t in filtered_tokens]
return filtered_tokens, categories
def _heuristic_category(token: str) -> str: """ Fallback heuristic category assignment for tokens not covered by markers.
This is used only when a token appears outside the marker system (e.g., for the first call before markers are added to the template). Kept conservative to avoid mislabeling. """ if token in SEPARATORS or token in " -_|~~.": return "sep"
if token.startswith("[") or token.startswith("(") or token.startswith("【"): clean = token.strip("[]()【】") # Check group if any(g.strip("[]()【】") == clean for g in GROUPS_EN_BRACKET + GROUPS_CN_BRACKET + GROUPS_PAREN): return "group" # Check special if any(s.strip("[]()【】") == clean or s == clean for s in SPECIALS): return "special" # Otherwise meta cat = categorize_meta_token(token) return cat.lower()
# Season — only if exact known patterns if re.match(r'^[Ss]\d+$', token) or token.startswith("Season") or "季" in token: return "season"
# Episode — only if strong patterns if re.match(r'^[Ee][Pp]?\d{1,3}$', token): # E01, EP01 return "episode" if re.match(r'^#\d{1,3}$', token): # #01 return "episode" if re.match(r'^第\d+[话話]$', token): # 第7话 return "episode" if re.match(r'^\d{1,2}[Vv]\d*$', token): # 01v2 return "episode"
# Meta tokens (without brackets) if token in ALL_METAS: return "source" clean = token.strip("[]()【】") if clean in META_RESOLUTION_TOKENS + META_SOURCE_TOKENS + META_CODEC_TOKENS + META_AUDIO_TOKENS + META_LANG_TOKENS: return "source"
# Default: title return "title"
# ═══════════════════════════════════════════════════════════════# Main script# ═══════════════════════════════════════════════════════════════
def generate_dataset(num_samples: int, tokenizer: AnimeTokenizer, output_path: str): """ Generate a synthetic dataset and save to JSONL.
Args: num_samples: Number of samples to generate tokenizer: AnimeTokenizer instance output_path: Path to output JSONL file """ os.makedirs(os.path.dirname(output_path), exist_ok=True)
all_token_lists: List[List[str]] = [] with open(output_path, 'w', encoding='utf-8') as f: for i in range(num_samples): sample = generate_sample(tokenizer, TEMPLATES) f.write(json.dumps(sample, ensure_ascii=False) + '\n') all_token_lists.append(sample["tokens"])
if (i + 1) % 10000 == 0: print(f"Generated {i + 1}/{num_samples} samples...")
print(f"Total samples generated: {num_samples}") return all_token_lists
if __name__ == "__main__": import argparse
parser = argparse.ArgumentParser(description="Generate synthetic anime filename dataset") parser.add_argument("--num-samples", type=int, default=100_000, help="Number of samples to generate (default: 100000)") parser.add_argument("--output", type=str, default="data/synthetic.jsonl", help="Output path (default: data/synthetic.jsonl)") parser.add_argument("--tokenizer", choices=["regex", "char"], default="regex", help="Tokenizer variant used to generate the JSONL data") parser.add_argument("--vocab-output", type=str, default=None, help="Vocab path (default: output directory vocab.json or vocab.char.json)") parser.add_argument("--seed", type=int, default=42, help="Random seed (default: 42)") args = parser.parse_args()
random.seed(args.seed)
print(f"Generating {args.num_samples} synthetic samples...") print(f"Output: {args.output}")
tokenizer = create_tokenizer(args.tokenizer)
token_lists = generate_dataset(args.num_samples, tokenizer, args.output)
# Build tokenizer vocabulary from generated data tokenizer.build_vocab(token_lists)
# Save tokenizer vocab alongside data vocab_path = args.vocab_output or os.path.join( os.path.dirname(args.output), "vocab.json" if args.tokenizer == "regex" else "vocab.char.json", ) vocab_dir = os.path.dirname(vocab_path) or "." os.makedirs(vocab_dir, exist_ok=True) with open(vocab_path, "w", encoding="utf-8") as f: json.dump(tokenizer.get_vocab(), f, ensure_ascii=False, indent=2) print(f"Tokenizer vocab saved to {vocab_path}") print(f"Vocab size: {tokenizer.vocab_size}")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358"""PyTorch Dataset for anime filename token classification.
Loads JSONL data (tokens + BIO labels) and converts to model inputs.Handles token-ID conversion, label encoding, padding, and truncation."""
import jsonfrom collections import Counterimport torchfrom torch.utils.data import Datasetfrom typing import Dict, List, Optional, Tuple
from config import Configfrom label_repairs import repair_sequel_season_labelsfrom tokenizer import AnimeTokenizer
class AnimeDataset(Dataset): """ Dataset for anime filename token classification.
Loads pre-tokenized data from JSONL files and prepares model inputs. Each sample has: - input_ids: token IDs with [CLS] prefix and [SEP] suffix - attention_mask: 1 for real tokens, 0 for padding - labels: integer label IDs, -100 for special/padding tokens """
def __init__( self, data_path: str, tokenizer: AnimeTokenizer, label2id: Dict[str, int], max_length: int = 64, ): """ Args: data_path: Path to JSONL file with tokens and labels. tokenizer: AnimeTokenizer instance. label2id: Mapping from label string to integer ID. max_length: Maximum sequence length (including special tokens). """ self.tokenizer = tokenizer self.label2id = label2id self.max_length = max_length
# Load data self.data: List[Dict] = [] with open(data_path, 'r', encoding='utf-8') as f: for line in f: line = line.strip() if line: self.data.append(json.loads(line))
def __len__(self) -> int: return len(self.data)
def __getitem__(self, idx: int) -> Dict[str, torch.Tensor]: """ Get a preprocessed sample.
Returns: Dictionary with input_ids, attention_mask, labels as LongTensors. """ item = self.data[idx] tokens, labels = labels_for_tokenizer(item, self.tokenizer)
# Convert tokens to IDs input_ids = self.tokenizer.convert_tokens_to_ids(tokens)
# Add [CLS] at start and [SEP] at end input_ids = [self.tokenizer.cls_token_id] + input_ids + [self.tokenizer.sep_token_id]
# Convert labels to IDs, with -100 for special tokens label_ids: List[int] = [-100] # [CLS] → -100 (ignored in loss) for label in labels: label_ids.append(self.label2id.get(label, 0)) # default to O label_ids.append(-100) # [SEP] → -100
# Attention mask: 1 for real tokens attention_mask = [1] * len(input_ids)
# Truncate if needed (keep CLS at 0, SEP at end) if len(input_ids) > self.max_length: # Keep first token (CLS), truncate middle, keep last token (SEP) input_ids = [input_ids[0]] + input_ids[1:self.max_length - 1] + [input_ids[-1]] label_ids = [label_ids[0]] + label_ids[1:self.max_length - 1] + [label_ids[-1]] attention_mask = [attention_mask[0]] + attention_mask[1:self.max_length - 1] + [attention_mask[-1]]
# Pad to max_length pad_len = self.max_length - len(input_ids) if pad_len > 0: input_ids += [self.tokenizer.pad_token_id] * pad_len label_ids += [-100] * pad_len attention_mask += [0] * pad_len
return { "input_ids": torch.tensor(input_ids, dtype=torch.long), "attention_mask": torch.tensor(attention_mask, dtype=torch.long), "labels": torch.tensor(label_ids, dtype=torch.long), }
def align_tokens_for_tokenizer( tokens: List[str], labels: List[str], tokenizer: AnimeTokenizer,) -> tuple[List[str], List[str]]: """ Align pre-labeled JSONL samples to the selected tokenizer.
The existing datasets store regex-tokenized samples. For the char A/B run, each original token is split into characters while preserving BIO spans: B-X stays on the first character, and the rest become I-X. """ if getattr(tokenizer, "tokenizer_variant", "regex") != "char": return tokens, labels
aligned_tokens: List[str] = [] aligned_labels: List[str] = []
for token, label in zip(tokens, labels): pieces = tokenizer.tokenize(token) if not pieces: continue
aligned_tokens.extend(pieces) aligned_labels.append(label)
if label.startswith(("B-", "I-")): continuation = "I-" + label.split("-", 1)[1] else: continuation = label aligned_labels.extend([continuation] * (len(pieces) - 1))
return aligned_tokens, aligned_labels
def labels_for_tokenizer( item: Dict, tokenizer: AnimeTokenizer,) -> Tuple[List[str], List[str]]: """ Return tokens and labels in the exact tokenizer space used by the model.
Older DMHY weak-label files store a post-processed token sequence where group/title brackets may be expanded even though AnimeTokenizer keeps the same bracketed text as one inference token. If the raw filename is present, project those weak labels back to character spans and then onto the current tokenizer output. This keeps train/eval/inference preprocessing identical. """ filename = item.get("filename") source_tokens, source_labels, _repairs = repair_sequel_season_labels(item) tokenizer_variant = getattr(tokenizer, "tokenizer_variant", "regex")
if not filename: return align_tokens_for_tokenizer(source_tokens, source_labels, tokenizer)
# Current char datasets are already in the exact inference token space. # Avoid re-scanning every filename during training. if item.get("tokenizer_variant") == tokenizer_variant: target_tokens = tokenizer.tokenize(filename) if source_tokens == target_tokens: return source_tokens, source_labels
projected = project_labels_from_filename( filename=filename, source_tokens=source_tokens, source_labels=source_labels, tokenizer=tokenizer, ) if projected is not None: return projected
# Fall back to the legacy behavior for synthetic fixtures or malformed rows. return align_tokens_for_tokenizer(source_tokens, source_labels, tokenizer)
def token_offsets_in_text(text: str, tokens: List[str]) -> Optional[List[Tuple[int, int]]]: """Find token character offsets by scanning left to right.""" offsets: List[Tuple[int, int]] = [] cursor = 0 for token in tokens: if token == "": offsets.append((cursor, cursor)) continue start = text.find(token, cursor) if start < 0: return None end = start + len(token) offsets.append((start, end)) cursor = end return offsets
def project_source_labels_to_chars( text: str, source_tokens: List[str], source_labels: List[str],) -> Optional[List[str]]: """Project source token BIO labels to per-character entity names.""" offsets = token_offsets_in_text(text, source_tokens) if offsets is None or len(source_tokens) != len(source_labels): return None
char_entities = ["O"] * len(text) for token, label, (start, end) in zip(source_tokens, source_labels, offsets): if not label.startswith(("B-", "I-")): continue entity = label.split("-", 1)[1]
# Bracketed single-token metadata in older data often includes the # brackets in the token. Keep container punctuation as O so a tokenizer # that splits brackets can learn cleaner boundaries. inner_start = start inner_end = end if len(token) >= 2 and token[0] in "[【(《" and token[-1] in "]】)》": inner_start += 1 inner_end -= 1
for pos in range(inner_start, inner_end): if 0 <= pos < len(char_entities): char_entities[pos] = entity return char_entities
def labels_from_char_projection( text: str, target_tokens: List[str], char_entities: List[str],) -> Optional[List[str]]: """Assign legal IOB2 labels to target tokens from per-character entities.""" offsets = token_offsets_in_text(text, target_tokens) if offsets is None: return None
labels: List[str] = [] active_entity: Optional[str] = None for start, end in offsets: span_entities = [ char_entities[pos] for pos in range(start, end) if 0 <= pos < len(char_entities) and char_entities[pos] != "O" ] if not span_entities: labels.append("O") active_entity = None continue
entity = Counter(span_entities).most_common(1)[0][0] prefix = "I" if active_entity == entity else "B" labels.append(f"{prefix}-{entity}") active_entity = entity return labels
def project_labels_from_filename( filename: str, source_tokens: List[str], source_labels: List[str], tokenizer: AnimeTokenizer,) -> Optional[Tuple[List[str], List[str]]]: """ Re-tokenize filename and project weak BIO labels onto that tokenizer.
Returns None when source tokens cannot be aligned to the filename. """ char_entities = project_source_labels_to_chars(filename, source_tokens, source_labels) if char_entities is None: return None
target_tokens = tokenizer.tokenize(filename) target_labels = labels_from_char_projection(filename, target_tokens, char_entities) if target_labels is None or len(target_tokens) != len(target_labels): return None return target_tokens, target_labels
def create_datasets( data_path: str, tokenizer: AnimeTokenizer, config: Config,) -> tuple: """ Create train and validation datasets from a JSONL file.
The file is split by the first N samples for training, the rest for validation based on config.train_split.
Returns: (train_dataset, eval_dataset) """ # Load all data to determine split with open(data_path, 'r', encoding='utf-8') as f: all_data = [json.loads(line) for line in f if line.strip()]
split_idx = int(len(all_data) * config.train_split) train_data = all_data[:split_idx] eval_data = all_data[split_idx:]
# Write temp files for each split import tempfile import os
train_file = os.path.join(tempfile.gettempdir(), "anime_train.jsonl") eval_file = os.path.join(tempfile.gettempdir(), "anime_eval.jsonl")
with open(train_file, 'w', encoding='utf-8') as f: for item in train_data: f.write(json.dumps(item, ensure_ascii=False) + '\n')
with open(eval_file, 'w', encoding='utf-8') as f: for item in eval_data: f.write(json.dumps(item, ensure_ascii=False) + '\n')
train_dataset = AnimeDataset( data_path=train_file, tokenizer=tokenizer, label2id=config.label2id, max_length=config.max_seq_length, ) eval_dataset = AnimeDataset( data_path=eval_file, tokenizer=tokenizer, label2id=config.label2id, max_length=config.max_seq_length, )
return train_dataset, eval_dataset
if __name__ == "__main__": # Quick test from config import Config cfg = Config()
tok = AnimeTokenizer() # Build a minimal vocab tok.build_vocab([["[ANi]", "test", "S2", "-", "03"], ["[Baha]", "anime", "01"]])
ds = AnimeDataset( data_path="data/synthetic.jsonl", tokenizer=tok, label2id=cfg.label2id, max_length=cfg.max_seq_length, )
print(f"Dataset size: {len(ds)}") if len(ds) > 0: sample = ds[0] print(f"input_ids shape: {sample['input_ids'].shape}") print(f"attention_mask shape: {sample['attention_mask'].shape}") print(f"labels shape: {sample['labels'].shape}") print(f"input_ids: {sample['input_ids'].tolist()}") print(f"labels: {sample['labels'].tolist()}") print(f"attention_mask: {sample['attention_mask'].tolist()}")1Subproject commit 004a8c08628b6820fb2d1b59a80fdcfe925ef095123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885"""Diagnostics for the anime filename NER pipeline.
The checks focus on structured filename parsing failure modes:
- train/inference tokenizer mismatch- BIO legality and boundary drift- tokenizer split and vocabulary coverage- label/entity distribution- optional model confusion on a sampled validation split"""
from __future__ import annotations
import argparseimport jsonimport mathimport osimport randomimport refrom collections import Counter, defaultdictfrom pathlib import Pathfrom typing import Dict, Iterable, List, Optional, Tuple
import numpy as npimport torchfrom seqeval.metrics import classification_report, f1_score, precision_score, recall_scorefrom transformers import BertForTokenClassification
from config import Configfrom dataset import labels_for_tokenizerfrom inference import constrained_bio_decode, postprocessfrom tokenizer import AnimeTokenizer, create_tokenizer, load_tokenizer
def iter_jsonl(path: Path, limit: Optional[int] = None) -> Iterable[dict]: with path.open("r", encoding="utf-8") as handle: for line_no, line in enumerate(handle, 1): if limit is not None and line_no > limit: break line = line.strip() if not line: continue try: yield json.loads(line) except json.JSONDecodeError as exc: raise ValueError(f"{path}:{line_no}: invalid JSON") from exc
def detect_dataset_variant(samples: List[dict], vocab_file: Optional[str]) -> str: variants = {sample.get("tokenizer_variant") for sample in samples if sample.get("tokenizer_variant")} if len(variants) == 1: return next(iter(variants)) if len(variants) > 1: return "mixed" if vocab_file and ".char" in os.path.basename(vocab_file).lower(): return "char" char_like = 0 with_filename = 0 for sample in samples: filename = sample.get("filename") if filename is None: continue with_filename += 1 if sample.get("tokens") == list(filename): char_like += 1 if with_filename and char_like / with_filename >= 0.95: return "char" return "regex"
def entity_type(label: str) -> Optional[str]: if "-" not in label: return None return label.split("-", 1)[1]
def bio_violations(tokens: List[str], labels: List[str]) -> List[dict]: violations: List[dict] = [] previous_label = "O" current_entity: Optional[str] = None
for idx, label in enumerate(labels): token = tokens[idx] if idx < len(tokens) else None if label == "O": current_entity = None elif label.startswith("B-"): current_entity = entity_type(label) elif label.startswith("I-"): label_entity = entity_type(label) previous_entity = entity_type(previous_label) if idx == 0 or previous_label == "O" or previous_entity != label_entity: violations.append( { "type": "ORPHAN_I", "index": idx, "prev_label": previous_label, "label": label, "token": token, } ) current_entity = label_entity else: violations.append( { "type": "UNKNOWN_LABEL", "index": idx, "prev_label": previous_label, "label": label, "token": token, } ) current_entity = None previous_label = label
return violations
def bio_boundary_warnings(tokens: List[str], labels: List[str]) -> List[dict]: """Collect legal-but-suspicious boundary patterns separately from BIO errors.""" warnings: List[dict] = [] for idx, label in enumerate(labels[1:], 1): previous_label = labels[idx - 1] if label == "O" and previous_label.startswith("B-"): warnings.append( { "type": "SINGLE_TOKEN_ENTITY", "index": idx, "prev_label": previous_label, "label": label, "token": tokens[idx] if idx < len(tokens) else None, } ) return warnings
def spans_from_labels(tokens: List[str], labels: List[str]) -> List[dict]: spans: List[dict] = [] start: Optional[int] = None current_type: Optional[str] = None current_tokens: List[str] = []
for idx, (token, label) in enumerate(zip(tokens, labels)): if label.startswith("B-"): if current_type is not None and start is not None: spans.append( { "type": current_type, "start": start, "end": idx, "text": "".join(current_tokens), } ) current_type = entity_type(label) start = idx current_tokens = [token] elif label.startswith("I-") and current_type == entity_type(label): current_tokens.append(token) elif label.startswith("I-"): if current_type is not None and start is not None: spans.append( { "type": current_type, "start": start, "end": idx, "text": "".join(current_tokens), } ) current_type = entity_type(label) start = idx current_tokens = [token] else: if current_type is not None and start is not None: spans.append( { "type": current_type, "start": start, "end": idx, "text": "".join(current_tokens), } ) current_type = None start = None current_tokens = []
if current_type is not None and start is not None: spans.append( { "type": current_type, "start": start, "end": len(labels), "text": "".join(current_tokens), } ) return spans
def count_entities(samples: List[dict]) -> Counter: counts: Counter = Counter() for sample in samples: for span in spans_from_labels(sample["tokens"], sample["labels"]): counts[span["type"]] += 1 return counts
def percentile(values: List[int], pct: float) -> int: if not values: return 0 ordered = sorted(values) idx = min(len(ordered) - 1, round((pct / 100) * (len(ordered) - 1))) return ordered[idx]
def token_mismatch(sample: dict, tokenizer: AnimeTokenizer) -> Optional[dict]: filename = sample.get("filename") if filename is None: return None inferred = tokenizer.tokenize(filename) dataset_tokens = sample.get("tokens", []) if inferred == dataset_tokens: return None prefix = 0 for left, right in zip(inferred, dataset_tokens): if left != right: break prefix += 1 return { "file_id": sample.get("file_id"), "filename": filename, "common_prefix": prefix, "dataset_tokens": dataset_tokens[:40], "tokenizer_tokens": inferred[:40], "dataset_len": len(dataset_tokens), "tokenizer_len": len(inferred), }
def format_counter(counter: Counter, total: Optional[int] = None, limit: Optional[int] = None) -> str: if total is None: total = sum(counter.values()) rows = [] items = counter.most_common(limit) for key, count in items: pct = count / total * 100 if total else 0.0 rows.append(f"- `{key}`: {count:,} ({pct:.2f}%)") return "\n".join(rows) if rows else "- none"
def token_id_stats(samples: List[dict], tokenizer: AnimeTokenizer) -> dict: total = 0 unk = 0 unk_counter: Counter = Counter() for sample in samples: tokens, _labels = labels_for_tokenizer(sample, tokenizer) ids = tokenizer.convert_tokens_to_ids(tokens) for token, token_id in zip(tokens, ids): total += 1 if token_id == tokenizer.unk_token_id: unk += 1 unk_counter[token] += 1 return { "total": total, "unk": unk, "unk_rate": unk / total if total else 0.0, "top_unk": unk_counter.most_common(25), }
def prepare_inputs( sample: dict, tokenizer: AnimeTokenizer, label2id: Dict[str, int], max_length: int,) -> Tuple[List[int], List[int], List[int], List[str]]: tokens, labels = labels_for_tokenizer(sample, tokenizer) input_ids = tokenizer.convert_tokens_to_ids(tokens) input_ids = [tokenizer.cls_token_id] + input_ids + [tokenizer.sep_token_id] label_ids = [-100] + [label2id.get(label, 0) for label in labels] + [-100] attention_mask = [1] * len(input_ids)
if len(input_ids) > max_length: input_ids = [input_ids[0]] + input_ids[1:max_length - 1] + [input_ids[-1]] label_ids = [label_ids[0]] + label_ids[1:max_length - 1] + [label_ids[-1]] attention_mask = [1] * len(input_ids)
pad_len = max_length - len(input_ids) if pad_len > 0: input_ids += [tokenizer.pad_token_id] * pad_len label_ids += [-100] * pad_len attention_mask += [0] * pad_len
return input_ids, attention_mask, label_ids, tokens
def normalize_field_value(field: str, value) -> Optional[str]: if value is None: return None if field in {"episode", "season"}: try: return str(int(value)) except (TypeError, ValueError): return str(value).strip().lower() text = str(value).strip() if field in {"resolution", "source"}: return text.lower().replace("_", "-") return re.sub(r"\s+", " ", text).strip().lower()
def update_parse_metrics(counter: Counter, gold: dict, pred: dict) -> None: fields = ["group", "title", "season", "episode", "resolution", "source", "special"] all_match = True for field in fields: gold_value = normalize_field_value(field, gold.get(field)) pred_value = normalize_field_value(field, pred.get(field)) if gold_value == pred_value: counter[f"{field}_correct"] += 1 else: all_match = False counter[(field, gold_value, pred_value)] += 1 counter[f"{field}_total"] += 1 if all_match: counter["full_match_correct"] += 1 counter["full_match_total"] += 1
def collect_field_failures(gold: dict, pred: dict) -> Dict[str, Dict[str, Optional[str]]]: return { field: { "gold": normalize_field_value(field, gold.get(field)), "pred": normalize_field_value(field, pred.get(field)), } for field in ["group", "title", "season", "episode", "resolution", "source", "special"] if normalize_field_value(field, gold.get(field)) != normalize_field_value(field, pred.get(field)) }
def evaluate_model( samples: List[dict], model_dir: Path, tokenizer: AnimeTokenizer, max_length: int, limit: int, seed: int,) -> dict: cfg = Config() model = BertForTokenClassification.from_pretrained(str(model_dir)) model.eval() device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model.to(device)
rng = random.Random(seed) eval_samples = list(samples) rng.shuffle(eval_samples) eval_samples = eval_samples[:limit]
id2label = {int(k): v for k, v in getattr(model.config, "id2label", cfg.id2label).items()} label2id = {v: int(k) for k, v in id2label.items()} if not label2id: label2id = cfg.label2id id2label = cfg.id2label
true_sequences: List[List[str]] = [] pred_sequences: List[List[str]] = [] confusion: Counter = Counter() entity_confusion: Counter = Counter() boundary_errors: Counter = Counter() parse_metrics: Counter = Counter() parse_metrics_no_rules: Counter = Counter() field_failures: List[dict] = [] field_failures_no_rules: List[dict] = []
with torch.no_grad(): for sample in eval_samples: input_ids, attention_mask, label_ids, sample_tokens = prepare_inputs( sample, tokenizer, label2id, max_length, ) input_tensor = torch.tensor([input_ids], dtype=torch.long, device=device) mask_tensor = torch.tensor([attention_mask], dtype=torch.long, device=device) logits = model(input_ids=input_tensor, attention_mask=mask_tensor).logits active_count = sum(1 for label_id in label_ids if label_id != -100) pred_ids = constrained_bio_decode(logits[0, 1:1 + active_count, :], id2label)
true_labels: List[str] = [] pred_labels: List[str] = [] pred_idx = 0 for label_id in label_ids: if label_id == -100: continue pred_id = pred_ids[pred_idx] pred_idx += 1 true_label = id2label.get(label_id, "O") pred_label = id2label.get(pred_id, "O") true_labels.append(true_label) pred_labels.append(pred_label) confusion[(true_label, pred_label)] += 1 entity_confusion[(entity_type(true_label) or "O", entity_type(pred_label) or "O")] += 1 if true_label != pred_label: if true_label.startswith("B-") or pred_label.startswith("B-"): boundary_errors["B-boundary"] += 1 elif entity_type(true_label) != entity_type(pred_label): boundary_errors["entity-type"] += 1 else: boundary_errors["BIO-prefix"] += 1 true_sequences.append(true_labels) pred_sequences.append(pred_labels) active_tokens = sample_tokens[:len(true_labels)] gold_parse = postprocess( active_tokens, true_labels, tokenizer=tokenizer, filename=sample.get("filename"), use_rules=True, ) pred_parse = postprocess( active_tokens, pred_labels, tokenizer=tokenizer, filename=sample.get("filename"), use_rules=True, ) gold_parse_no_rules = postprocess( active_tokens, true_labels, tokenizer=tokenizer, filename=sample.get("filename"), use_rules=False, ) pred_parse_no_rules = postprocess( active_tokens, pred_labels, tokenizer=tokenizer, filename=sample.get("filename"), use_rules=False, ) update_parse_metrics(parse_metrics, gold_parse, pred_parse) update_parse_metrics(parse_metrics_no_rules, gold_parse_no_rules, pred_parse_no_rules) failures = collect_field_failures(gold_parse, pred_parse) if failures and len(field_failures) < 30: field_failures.append( { "filename": sample.get("filename"), "errors": failures, "gold": gold_parse, "pred": pred_parse, } ) failures_no_rules = collect_field_failures(gold_parse_no_rules, pred_parse_no_rules) if failures_no_rules and len(field_failures_no_rules) < 30: field_failures_no_rules.append( { "filename": sample.get("filename"), "errors": failures_no_rules, "gold": gold_parse_no_rules, "pred": pred_parse_no_rules, } )
errors = confusion.copy() for label in set(label for pair in confusion for label in pair): errors.pop((label, label), None)
return { "sample_count": len(eval_samples), "precision": precision_score(true_sequences, pred_sequences), "recall": recall_score(true_sequences, pred_sequences), "f1": f1_score(true_sequences, pred_sequences), "classification_report": classification_report(true_sequences, pred_sequences, digits=4), "top_token_confusions": errors.most_common(30), "top_entity_confusions": Counter( {k: v for k, v in entity_confusion.items() if k[0] != k[1]} ).most_common(30), "boundary_errors": boundary_errors, "parse_metrics": parse_metrics, "parse_metrics_no_rules": parse_metrics_no_rules, "field_failures": field_failures, "field_failures_no_rules": field_failures_no_rules, }
def tokenizer_split_examples(samples: List[dict], tokenizers: Dict[str, AnimeTokenizer], limit: int = 8) -> List[dict]: examples: List[dict] = [] for sample in samples: filename = sample.get("filename") if not filename: continue row = { "file_id": sample.get("file_id"), "filename": filename, "dataset_tokens": sample.get("tokens", [])[:80], } for name, tokenizer in tokenizers.items(): row[f"{name}_tokens"] = tokenizer.tokenize(filename)[:80] examples.append(row) if len(examples) >= limit: break return examples
def write_report(path: Path, title: str, sections: List[Tuple[str, str]]) -> None: parts = [f"# {title}", ""] for heading, body in sections: parts.append(f"## {heading}") parts.append("") parts.append(body.strip() if body.strip() else "_No data._") parts.append("") path.write_text("\n".join(parts), encoding="utf-8")
def markdown_json(value) -> str: return "```json\n" + json.dumps(value, ensure_ascii=False, indent=2) + "\n```"
def markdown_table(headers: List[str], rows: List[List[str]], limit: Optional[int] = None) -> str: if limit is not None: rows = rows[:limit] table = ["| " + " | ".join(headers) + " |", "| " + " | ".join("---" for _ in headers) + " |"] for row in rows: table.append("| " + " | ".join(str(cell).replace("\n", " ") for cell in row) + " |") return "\n".join(table)
def main() -> None: parser = argparse.ArgumentParser(description="Diagnose anime filename NER data and model pipeline") parser.add_argument("--data-file", required=True, help="JSONL dataset with tokens and labels") parser.add_argument("--vocab-file", default=None, help="Tokenizer vocab JSON") parser.add_argument("--tokenizer", choices=["regex", "char"], default=None, help="Tokenizer variant to diagnose. Defaults to dataset metadata") parser.add_argument("--model-dir", default=None, help="Optional model directory for confusion analysis") parser.add_argument("--max-length", type=int, default=None, help="Max sequence length for model eval/truncation stats") parser.add_argument("--sample-limit", type=int, default=20000, help="Rows to inspect for data diagnostics") parser.add_argument("--eval-limit", type=int, default=512, help="Rows to evaluate when --model-dir is provided") parser.add_argument("--output", default="diagnostics_report.md", help="Markdown report path") parser.add_argument("--seed", type=int, default=42) args = parser.parse_args()
data_path = Path(args.data_file) samples = list(iter_jsonl(data_path, args.sample_limit)) if not samples: raise ValueError(f"No samples loaded from {data_path}")
dataset_variant = detect_dataset_variant(samples, args.vocab_file) tokenizer_variant = args.tokenizer or (dataset_variant if dataset_variant != "mixed" else "regex") vocab_file = args.vocab_file if vocab_file is None: vocab_file = str(data_path.with_name("vocab.char.json" if tokenizer_variant == "char" else "vocab.json")) tokenizer = create_tokenizer(tokenizer_variant, vocab_file=vocab_file)
if args.model_dir: model_tokenizer = load_tokenizer(args.model_dir) else: model_tokenizer = tokenizer
label_counter: Counter = Counter() length_values: List[int] = [] aligned_length_values: List[int] = [] violations: List[dict] = [] boundary_warnings: List[dict] = [] mismatch_examples: List[dict] = [] space_label_counter: Counter = Counter() boundary_drift_counter: Counter = Counter() truncation_count = 0 max_length = args.max_length if max_length is None and args.model_dir: model_config = BertForTokenClassification.from_pretrained(args.model_dir).config max_length = int(getattr(model_config, "max_seq_length", 64)) max_length = max_length or (128 if tokenizer_variant == "char" else 64)
for row_idx, sample in enumerate(samples, 1): tokens = sample.get("tokens", []) labels = sample.get("labels", []) if len(tokens) != len(labels): violations.append( { "type": "LENGTH_MISMATCH", "row": row_idx, "file_id": sample.get("file_id"), "token_count": len(tokens), "label_count": len(labels), "filename": sample.get("filename"), } ) continue
label_counter.update(labels) length_values.append(len(tokens)) aligned_tokens, aligned_labels = labels_for_tokenizer(sample, tokenizer) aligned_length_values.append(len(aligned_tokens)) if len(aligned_tokens) + 2 > max_length: truncation_count += 1 for token, label in zip(tokens, labels): if token.isspace(): space_label_counter[label] += 1 for violation in bio_violations(tokens, labels): violation.update( { "row": row_idx, "file_id": sample.get("file_id"), "filename": sample.get("filename"), "context_tokens": tokens[max(0, violation["index"] - 5):violation["index"] + 6], "context_labels": labels[max(0, violation["index"] - 5):violation["index"] + 6], } ) violations.append(violation) for warning in bio_boundary_warnings(tokens, labels): warning.update( { "row": row_idx, "file_id": sample.get("file_id"), "filename": sample.get("filename"), "context_tokens": tokens[max(0, warning["index"] - 5):warning["index"] + 6], "context_labels": labels[max(0, warning["index"] - 5):warning["index"] + 6], } ) boundary_warnings.append(warning) for span in spans_from_labels(tokens, labels): text = span["text"] if span["type"] == "TITLE": if text.startswith("[") or text.endswith("[") or "]" in text[:3]: boundary_drift_counter["title_contains_bracket_edge"] += 1 if re.search(r"\b(?:WEB[-_ ]?DL|WebRip|\d{3,4}[pP]|HEVC|AVC|AAC)\b", text, re.I): boundary_drift_counter["title_contains_meta"] += 1 if span["type"] == "GROUP" and ("[" in text or "]" in text): boundary_drift_counter["group_contains_bracket"] += 1
if len(mismatch_examples) < 10: mismatch = token_mismatch(sample, tokenizer) if mismatch: mismatch_examples.append(mismatch)
entity_counter = count_entities(samples) id_stats = token_id_stats(samples, tokenizer) split_examples = tokenizer_split_examples( samples, { "diagnosed": tokenizer, "regex": create_tokenizer("regex", vocab_file=str(data_path.with_name("vocab.json"))), "char": create_tokenizer("char", vocab_file=str(data_path.with_name("vocab.char.json"))), }, )
model_eval = None if args.model_dir: model_eval = evaluate_model( samples=samples, model_dir=Path(args.model_dir), tokenizer=model_tokenizer, max_length=max_length, limit=args.eval_limit, seed=args.seed, )
total_labels = sum(label_counter.values()) o_count = label_counter.get("O", 0) sections: List[Tuple[str, str]] = []
sections.append( ( "Executive Summary", "\n".join( [ f"- Dataset: `{data_path}`", f"- Inspected rows: {len(samples):,}", f"- Dataset tokenizer variant: `{dataset_variant}`", f"- Diagnosed tokenizer variant: `{tokenizer_variant}`", f"- Vocab: `{vocab_file}` ({tokenizer.vocab_size:,} tokens)", f"- Max sequence length checked: {max_length}", f"- O-label ratio: {o_count / total_labels * 100:.2f}%" if total_labels else "- O-label ratio: n/a", f"- Truncation risk: {truncation_count:,}/{len(samples):,} rows ({truncation_count / len(samples) * 100:.2f}%)", f"- UNK rate after selected tokenizer: {id_stats['unk_rate'] * 100:.4f}%", f"- BIO warnings collected: {len(violations):,}", "", "Primary finding: this task is structural filename parsing. Tokenizer/preprocessing identity is more important than lowering token loss.", ] ), ) )
sections.append( ( "Label And Entity Statistics", "\n".join( [ "### Label distribution", format_counter(label_counter, total_labels), "", "### Entity count", format_counter(entity_counter), "", "### Length distribution", markdown_json( { "raw_tokens": { "min": min(length_values), "p50": percentile(length_values, 50), "p90": percentile(length_values, 90), "p95": percentile(length_values, 95), "p99": percentile(length_values, 99), "max": max(length_values), }, "aligned_tokens": { "min": min(aligned_length_values), "p50": percentile(aligned_length_values, 50), "p90": percentile(aligned_length_values, 90), "p95": percentile(aligned_length_values, 95), "p99": percentile(aligned_length_values, 99), "max": max(aligned_length_values), }, } ), "", "### Whitespace labels", format_counter(space_label_counter), ] ), ) )
violation_counter = Counter(v["type"] for v in violations) warning_counter = Counter(w["type"] for w in boundary_warnings) sections.append( ( "BIO Violations And Boundary Drift", "\n".join( [ "### True BIO violation counts", format_counter(violation_counter), "", "### Legal boundary warning counts", format_counter(warning_counter), "", "### Boundary drift heuristics", format_counter(boundary_drift_counter), "", "### Sample violations", markdown_json(violations[:30]), "", "### Sample boundary warnings", markdown_json(boundary_warnings[:30]), ] ), ) )
sections.append( ( "Tokenizer Split And Alignment", "\n".join( [ "### Dataset tokens vs selected tokenizer mismatches", markdown_json(mismatch_examples), "", "### Split examples", markdown_json(split_examples), "", "### Vocabulary coverage", markdown_json(id_stats), ] ), ) )
if args.model_dir: model_tokenizer_variant = getattr(model_tokenizer, "tokenizer_variant", "unknown") sections.append( ( "Train Inference Tokenizer Comparison", "\n".join( [ f"- Model dir: `{args.model_dir}`", f"- Model tokenizer variant: `{model_tokenizer_variant}`", f"- Dataset tokenizer variant: `{dataset_variant}`", f"- Diagnostic tokenizer variant: `{tokenizer_variant}`", f"- Model tokenizer vocab size: {model_tokenizer.vocab_size:,}", f"- Diagnostic tokenizer vocab size: {tokenizer.vocab_size:,}", "", "If dataset and model tokenizer variants differ, validation loss can be low while real inference sees different token IDs and boundaries.", ] ), ) )
if model_eval: token_rows = [ [true, pred, f"{count:,}"] for (true, pred), count in model_eval["top_token_confusions"] ] entity_rows = [ [true, pred, f"{count:,}"] for (true, pred), count in model_eval["top_entity_confusions"] ] def parse_metric_tables(metrics: Counter) -> Tuple[List[List[str]], str, List[List[str]]]: field_rows = [] for field in ["group", "title", "season", "episode", "resolution", "source", "special"]: total = metrics.get(f"{field}_total", 0) correct = metrics.get(f"{field}_correct", 0) acc = correct / total if total else 0.0 field_rows.append([field, f"{correct:,}/{total:,}", f"{acc:.4f}"]) full_total = metrics.get("full_match_total", 0) full_correct = metrics.get("full_match_correct", 0) full_acc = full_correct / full_total if full_total else 0.0 full_line = f"{full_correct:,}/{full_total:,} ({full_acc:.4f})" error_rows = [ [field, str(gold), str(pred), f"{count:,}"] for key, count in Counter( {key: count for key, count in metrics.items() if isinstance(key, tuple)} ).most_common(30) if isinstance(key, tuple) for field, gold, pred in [key] ] return field_rows, full_line, error_rows
rule_field_rows, rule_full_line, rule_error_rows = parse_metric_tables(model_eval["parse_metrics"]) ner_field_rows, ner_full_line, ner_error_rows = parse_metric_tables(model_eval["parse_metrics_no_rules"]) sections.append( ( "Model Confusion Analysis", "\n".join( [ f"- Evaluated samples: {model_eval['sample_count']:,}", f"- Entity precision: {model_eval['precision']:.4f}", f"- Entity recall: {model_eval['recall']:.4f}", f"- Entity F1: {model_eval['f1']:.4f}", "", "### Boundary error classes", format_counter(model_eval["boundary_errors"]), "", "### Top token-label confusions", markdown_table(["true", "pred", "count"], token_rows) if token_rows else "- none", "", "### Top entity-type confusions", markdown_table(["true", "pred", "count"], entity_rows) if entity_rows else "- none", "", "### Field exact-match accuracy (rule-assisted)", markdown_table(["field", "correct/total", "accuracy"], rule_field_rows), "", f"Rule-assisted full parse exact match: {rule_full_line}", "", "### Top rule-assisted field parse errors", markdown_table(["field", "gold", "pred", "count"], rule_error_rows) if rule_error_rows else "- none", "", "### Field exact-match accuracy (NER-only, no rules)", markdown_table(["field", "correct/total", "accuracy"], ner_field_rows), "", f"NER-only full parse exact match: {ner_full_line}", "", "### Top NER-only field parse errors", markdown_table(["field", "gold", "pred", "count"], ner_error_rows) if ner_error_rows else "- none", "", "### Hardest sampled parse failures (rule-assisted)", markdown_json(model_eval["field_failures"][:10]) if model_eval["field_failures"] else "- none", "", "### Hardest sampled parse failures (NER-only)", markdown_json(model_eval["field_failures_no_rules"][:10]) if model_eval["field_failures_no_rules"] else "- none", "", "### Seqeval report", "```text\n" + model_eval["classification_report"] + "\n```", ] ), ) )
sections.append( ( "Recommended Pipeline", "\n".join( [ "1. Use one tokenizer variant end to end and save it in the checkpoint metadata.", "2. Prefer char-level or a deterministic hybrid tokenizer for DMHY filenames; avoid generic subword tokenization for labels.", "3. For char-level runs, use `--tokenizer char --max-seq-length 128` with `vocab.char.json`.", "4. Add CRF decoding or constrained BIO decoding so illegal I-X transitions and impossible boundary jumps are blocked.", "5. Keep rule-assisted post-processing for high-confidence structural anchors: leading group bracket, ` - 07`, `S01E07`, source, and resolution.", "6. Track entity-level F1 and field exact-match on real filenames; do not accept low validation loss alone.", ] ), ) )
write_report(Path(args.output), "Anime Filename Parser Diagnostics Report", sections) print(f"Wrote diagnostics report: {args.output}")
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277# Anime Filename Parser Diagnostics Report
## 根因分析
当前症状不是 learning rate 问题,而是训练、验证、推理没有在同一个结构化输入空间里工作。
最高优先级根因是 tokenizer/data 配置错位:你给出的训练命令使用 `dmhy_weak_char.jsonl` 和 `vocab.char.json`,但没有传 `--tokenizer char`。旧版 `train.py` 默认 `regex`,因此 char 数据会被当作 regex 训练配置保存,checkpoint metadata 会写成 `tokenizer_variant=regex`。推理时 `load_tokenizer()` 按 checkpoint metadata 重新加载 regex tokenizer,于是 `[LoliHouse]` 这类结构 token 会作为一个整体进入模型,而 char 训练数据里它是 `[`, `L`, `o`, ..., `]`。这会直接导致 group/title 边界漂移。
第二个根因是 word-level 数据和当前 `AnimeTokenizer` 也不完全一致。`dmhy_weak.jsonl` 里示例 token 是 `[`, `LoliHouse`, `]`,但当前 regex tokenizer 对原始文件名会输出 `[LoliHouse]`。这说明 word-level 数据名义上是 regex,但不是严格由当前 inference tokenizer 重放得到的 token 序列。
第三个根因是 char 训练命令没有设置 `--max-seq-length 128`。在抽样 5,000 条 char 数据中,默认 64 长度会截断 2,058 条,占 41.16%。episode/source/resolution 往往在后半段,默认长度会让模型训练和推理都丢失结构锚点。
第四个根因是评估指标误导。低 validation loss 和 token accuracy 会被大量 `O`、`I-TITLE` 稀释;真实任务需要 entity-level F1、字段 exact match,以及结构案例回归。
## 问题优先级
P0: 训练命令必须显式或自动使用 char tokenizer。已修改 `train.py`,现在会从数据集 metadata 自动识别 `char`,并把 char 默认 max length 提升到 128。
P0: 不允许 tokenizer variant 与 dataset metadata 不一致。已修改 `train.py`,检测到 dataset `tokenizer_variant` 与选择的 tokenizer 不一致会报错。
P0: 推理必须使用 checkpoint 保存的 tokenizer 和 max length。已修改 `inference.py`,默认读取 `model.config.max_seq_length`,并新增 `--debug` 输出 token/label/score/UNK/截断信息。
P1: 从旧 checkpoint fine-tune 到不同 vocab 时,不能按 ID 盲目 `resize_token_embeddings()`。已修改为按 token 字符串重映射 embedding,未匹配 token 再随机初始化。
P1: 数据集存在 BIO/边界质量问题。char 抽样 5,000 条发现 468 个 `ORPHAN_I`,典型是标题被括号 `O` 打断后仍继续 `I-TITLE`。`B-X -> O` 本身是合法 BIO,但在 group/title/source 频繁出现时是边界告警。
P2: 当前 `BertForTokenClassification` 独立逐 token 解码,不能约束非法转移。建议后续加 CRF 或 constrained BIO decoder。
## 自动诊断结果
新增脚本:
```bashpython diagnose_pipeline.py --data-file datasets/AnimeName/dmhy_weak_char.jsonl --vocab-file datasets/AnimeName/vocab.char.json --model-dir checkpoints/dmhy-finetune/final --sample-limit 5000 --eval-limit 128 --output diagnostics_report.md```
char 数据抽样结果:
- tokenizer variant: `char`- vocab size: 6,199- UNK rate: 0.0000%- O-label ratio: 37.47%- p95 length: 101, p99 length: 125- default max length 64 truncation: 41.16%- `ORPHAN_I`: 468- regex checkpoint 直接评 char 数据时 entity F1: 0.0832
word 数据抽样结果保存在 `diagnostics_report_word.md`:
- tokenizer variant: `regex`- vocab size: 8,000- UNK rate: 6.9158%- default max length 64 truncation: 0%- 当前 regex checkpoint 在抽样 word 数据上 entity F1: 0.9549- 但 model checkpoint vocab 是 3,000,诊断 vocab 是 8,000,继续 fine-tune 必须重映射 embedding
## Tokenizer Split 示例
输入:
```text[LoliHouse] Yomi no Tsugai - 07 [WebRip 1080p HEVC-10bit AAC ASSx2]```
char tokenizer:
```text[, L, o, l, i, H, o, u, s, e, ], , Y, o, m, i, , n, o, , T, s, u, g, a, i, , -, , 0, 7, ...```
当前 regex tokenizer:
```text[LoliHouse], , Yomi, , no, , Tsugai, , -, , 07, , [WebRip 1080p HEVC-10bit AAC ASSx2]```
这两个 token 序列不是同一个标注空间。char label 不能直接套到 regex token 上,regex 模型也不能在 char token 序列上解释 logits。
## BIO 与边界问题
真实非法 BIO:
```text... ( O, K I-TITLE, a I-TITLE ...```
示例:
```text[LoliHouse] Kanteishi (Kari) - 07 [WebRip 1080p HEVC-10bit AAC]```
`(` 被标为 `O`,后面的 `Kari` 继续 `I-TITLE`,形成 `O -> I-TITLE`。这会让模型学习到标题可以跨越被标为非实体的括号,边界自然会漂。
结构边界告警:
```text[KissSub][Shunkashuutou Daikousha - Haru no Mai][06][1080P][GB][MP4]```
`KissSub` 是 `B-GROUP`,右括号是 `O`,这是合法 BIO;但如果 tokenizer 在推理时把 `[KissSub]` 合成一个 token,模型就无法只给内部文字打 `GROUP`,只能把整个 bracket token 判成一个类别。
## Confusion 分析
故意用 char 数据评估 regex checkpoint,entity F1 只有 0.0832。主要混淆:
- `O -> TITLE`: 930- `SOURCE -> TITLE`: 236- `EPISODE -> TITLE`: 228- `GROUP -> TITLE`: 86
这与实际症状一致:模型把结构锚点和 meta 区域吸进 title,group/title 边界混淆,episode 被 title 或 O 吞掉。
## 已修改的代码
`train.py`
- `--tokenizer` 默认从数据集 metadata/vocab 名称/样本结构自动推断。- char 数据默认 `max_seq_length >= 128`。- dataset metadata 与 tokenizer 不一致会直接报错。- fine-tune 到新 vocab 时按 token 字符串重映射 embedding,避免 token ID 语义错位。- checkpoint 保存正确的 `tokenizer_variant` 和 `max_seq_length`。
`inference.py`
- 新增 `--debug`,输出 tokenizer variant、token IDs、labels、scores、UNK rate、truncation、entity spans。- 默认使用 checkpoint `max_seq_length`。- 修正推理截断逻辑,保留 `[SEP]`,与训练一致。- 默认使用 constrained BIO Viterbi 解码,阻止 `O -> I-X` 这类非法转移;可用 `--no-constrained-bio` 查看原始 greedy 输出。- 新增 rule-assisted parsing,兜底修复高置信结构锚点:leading group bracket、` - 07`、`S01E07`、resolution、source。- 可用 `--no-rule-assist` 关闭规则兜底,只看模型原始输出。
`diagnose_pipeline.py`
- 自动检查 token/label 长度。- 输出 BIO 违规样本与边界告警。- 输出 tokenizer split 示例。- 输出 train/inference tokenizer 对比。- 输出实体、label、空格 label、UNK、截断统计。- 可选加载 checkpoint 做 confusion 和 seqeval entity-level F1。
## 修改后的 Pipeline
推荐 char-level pipeline:
```bashpython diagnose_pipeline.py ^ --data-file datasets/AnimeName/dmhy_weak_char.jsonl ^ --vocab-file datasets/AnimeName/vocab.char.json ^ --sample-limit 20000 ^ --output diagnostics_report.md
python train.py ^ --tokenizer char ^ --data-file datasets/AnimeName/dmhy_weak_char.jsonl ^ --vocab-file datasets/AnimeName/vocab.char.json ^ --save-dir checkpoints/dmhy-char ^ --epochs 10 ^ --batch-size 128 ^ --learning-rate 0.0003 ^ --warmup-steps 300 ^ --max-seq-length 128 ^ --seed 42
python inference.py ^ --model-dir checkpoints/dmhy-char/final ^ --debug ^ "[LoliHouse] Yomi no Tsugai - 07 [WebRip 1080p HEVC-10bit AAC ASSx2]"```
如果继续使用 word/regex pipeline,必须先重新生成数据,使 `sample["tokens"] == AnimeTokenizer.tokenize(sample["filename"])` 对绝大多数样本成立;否则验证集仍然是训练 token 空间,真实 inference 是另一个 token 空间。
## 最合理的 Tokenizer 方案
当前任务更适合 char-level 或 deterministic hybrid tokenizer,不适合通用 subword tokenizer。
char-level 优点:
- train/inference 最容易完全一致。- 不会把 `[LoliHouse]`、`[WebRip ...]` 这类结构块压成单 token。- 对未知标题、组名、罗马音、中文、日文都没有 OOV。- 更适合学习括号、空格、连字符、集数位置这些结构信号。
char-level 缺点:
- 序列更长,必须用 `max_seq_length=128`。- 逐 token softmax 容易出现 BIO 非法转移,建议加 CRF。
word-level/regex 优点:
- 序列短,训练快。- 当前已有 checkpoint 在同 token 空间验证集上 F1 较高。
word-level/regex 缺点:
- 如果 bracket protection 把整段合并,内部 label 无法表达。- 数据生成 tokenizer 和 inference tokenizer 稍有不一致就会严重错位。- OOV 对新番标题和组名仍然明显。
结论:短期用 char-level + rule-assisted parsing;中期改为 hybrid tokenizer:保留结构符号 `[ ] ( ) - _ . space` 为独立 token,英文数字连续串可作为片段但必须能映射回字符 offset,并在 label alignment 上以 offset 为准;长期加 BERT + CRF。
## 建议训练配置
首选:
```bashpython train.py --tokenizer char ^ --data-file datasets/AnimeName/dmhy_weak_char.jsonl ^ --vocab-file datasets/AnimeName/vocab.char.json ^ --save-dir checkpoints/dmhy-char ^ --epochs 10 --batch-size 128 ^ --learning-rate 0.0003 --warmup-steps 300 ^ --max-seq-length 128 --seed 42```
不要从 regex checkpoint 直接当作同构模型继续训练 char;如果要迁移,当前代码会按 token 字符串 remap embedding,但多数 char token 与 regex token 共享有限,最好从头训练 char 模型或只迁移 encoder 非 embedding 层。
必须新增评估:
- entity-level F1 by field- field exact match: `group/title/episode/resolution/source`- full parse exact match- episode recall- boundary errors: group-title, title-episode, episode-meta- inference debug sample set,固定 50-200 个真实文件名回归
## 真实案例分析
输入:
```text[LoliHouse] Yomi no Tsugai - 07 [WebRip 1080p HEVC-10bit AAC ASSx2]```
旧 regex checkpoint 原始模型输出:
```json{ "entities": [ {"type": "TITLE", "text": "[LoliHouse] Yomi no Tsugai"}, {"type": "EPISODE", "text": "07"} ]}```
问题点:
- `[LoliHouse]` 被 tokenizer 合成一个 token。- 模型把该 token 判成 `B-TITLE`,无法只把内部 `LoliHouse` 判成 `GROUP`。- `Yomi` 和 `Tsugai` 在 3,000 vocab checkpoint 中是 `[UNK]`,但模型仍高置信输出 `I-TITLE`,说明 loss/置信度不能代表字段正确性。
修改后带规则辅助的最终输出:
```json{ "group": "LoliHouse", "title": "Yomi no Tsugai", "episode": 7, "source": "WebRip", "resolution": "1080p"}```
这只是上线兜底;真正修复仍应训练一个 train/inference token 完全一致的 char 或 hybrid 模型。
## 架构建议
最推荐的重构路线:
1. `BERT encoder + CRF`:约束 `O -> I-X`、`B-X -> I-Y` 等非法/低质量转移。2. char-level NER:保证 token-label alignment 不受 subword split 影响。3. rule-assisted parser:先抽取高置信结构锚点,再让模型负责模糊 title/group 边界。4. offset-based dataset:每条数据保存 raw filename、entity spans、tokens、offset_mapping、labels,训练时由 tokenizer 统一生成 labels。
当前代码已先实现“无训练 CRF”的 constrained BIO decoding,作为上线前的轻量保护。完整 BERT+CRF 仍建议作为下一阶段训练架构重构。
不要只优化 loss。这个任务的目标函数应更接近真实解析准确率:字段级 exact match + episode recall + title boundary F1。12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678# Anime Filename Parser Diagnostics Report
## Executive Summary
- Dataset: `datasets\AnimeName\dmhy_weak.jsonl`- Inspected rows: 5,000- Dataset tokenizer variant: `regex`- Diagnosed tokenizer variant: `regex`- Vocab: `datasets\AnimeName\vocab.json` (8,000 tokens)- Max sequence length checked: 64- O-label ratio: 38.12%- Truncation risk: 0/5,000 rows (0.00%)- UNK rate after selected tokenizer: 6.9158%- BIO warnings collected: 9,711
Primary finding: this task is structural filename parsing. Tokenizer/preprocessing identity is more important than lowering token loss.
## Label And Entity Statistics
### Label distribution- `O`: 32,517 (38.12%)- `I-TITLE`: 30,321 (35.54%)- `B-TITLE`: 5,593 (6.56%)- `B-EPISODE`: 5,000 (5.86%)- `B-SOURCE`: 4,032 (4.73%)- `I-GROUP`: 2,459 (2.88%)- `B-GROUP`: 2,299 (2.69%)- `B-RESOLUTION`: 1,765 (2.07%)- `B-SEASON`: 1,269 (1.49%)- `B-SPECIAL`: 57 (0.07%)
### Entity count- `TITLE`: 6,061 (29.59%)- `EPISODE`: 5,000 (24.41%)- `SOURCE`: 4,032 (19.68%)- `GROUP`: 2,299 (11.22%)- `RESOLUTION`: 1,765 (8.62%)- `SEASON`: 1,269 (6.20%)- `SPECIAL`: 57 (0.28%)
### Length distribution```json{ "raw_tokens": { "min": 3, "p50": 17, "p90": 28, "p95": 31, "p99": 39, "max": 54 }, "aligned_tokens": { "min": 3, "p50": 17, "p90": 28, "p95": 31, "p99": 39, "max": 54 }}```
### Whitespace labels- `I-TITLE`: 10,539 (48.98%)- `O`: 10,484 (48.72%)- `I-GROUP`: 411 (1.91%)- `B-TITLE`: 84 (0.39%)
## BIO Violations And Boundary Drift
### Violation counts- `B_DIRECT_TO_O`: 9,243 (95.18%)- `ORPHAN_I`: 468 (4.82%)
### Boundary drift heuristics- none
### Sample violations```json[ { "type": "B_DIRECT_TO_O", "index": 8, "prev_label": "B-EPISODE", "label": "O", "token": ".", "row": 1, "file_id": 1, "filename": "Witch.Hat.Atelier.S01E07.1080p.NF.WEB-DL.JPN.AAC2.0.H.264.MSubs-ToonsHub", "context_tokens": [ ".", "Atelier", ".", "S01", "E07", ".", "1080p", ".", "NF", ".", "WEB-DL" ], "context_labels": [ "I-TITLE", "I-TITLE", "O", "B-SEASON", "B-EPISODE", "O", "B-RESOLUTION", "O", "B-SOURCE", "O", "B-SOURCE" ] }, { "type": "B_DIRECT_TO_O", "index": 10, "prev_label": "B-RESOLUTION", "label": "O", "token": ".", "row": 1, "file_id": 1, "filename": "Witch.Hat.Atelier.S01E07.1080p.NF.WEB-DL.JPN.AAC2.0.H.264.MSubs-ToonsHub", "context_tokens": [ ".", "S01", "E07", ".", "1080p", ".", "NF", ".", "WEB-DL", ".", "JP" ], "context_labels": [ "O", "B-SEASON", "B-EPISODE", "O", "B-RESOLUTION", "O", "B-SOURCE", "O", "B-SOURCE", "O", "B-SOURCE" ] }, { "type": "B_DIRECT_TO_O", "index": 12, "prev_label": "B-SOURCE", "label": "O", "token": ".", "row": 1, "file_id": 1, "filename": "Witch.Hat.Atelier.S01E07.1080p.NF.WEB-DL.JPN.AAC2.0.H.264.MSubs-ToonsHub", "context_tokens": [ "E07", ".", "1080p", ".", "NF", ".", "WEB-DL", ".", "JP", "N", "." ], "context_labels": [ "B-EPISODE", "O", "B-RESOLUTION", "O", "B-SOURCE", "O", "B-SOURCE", "O", "B-SOURCE", "O", "O" ] }, { "type": "B_DIRECT_TO_O", "index": 14, "prev_label": "B-SOURCE", "label": "O", "token": ".", "row": 1, "file_id": 1, "filename": "Witch.Hat.Atelier.S01E07.1080p.NF.WEB-DL.JPN.AAC2.0.H.264.MSubs-ToonsHub", "context_tokens": [ "1080p", ".", "NF", ".", "WEB-DL", ".", "JP", "N", ".", "AAC", "2" ], "context_labels": [ "B-RESOLUTION", "O", "B-SOURCE", "O", "B-SOURCE", "O", "B-SOURCE", "O", "O", "B-SOURCE", "O" ] }, { "type": "B_DIRECT_TO_O", "index": 16, "prev_label": "B-SOURCE", "label": "O", "token": "N", "row": 1, "file_id": 1, "filename": "Witch.Hat.Atelier.S01E07.1080p.NF.WEB-DL.JPN.AAC2.0.H.264.MSubs-ToonsHub", "context_tokens": [ "NF", ".", "WEB-DL", ".", "JP", "N", ".", "AAC", "2", ".", "0" ], "context_labels": [ "B-SOURCE", "O", "B-SOURCE", "O", "B-SOURCE", "O", "O", "B-SOURCE", "O", "O", "O" ] }, { "type": "B_DIRECT_TO_O", "index": 19, "prev_label": "B-SOURCE", "label": "O", "token": "2", "row": 1, "file_id": 1, "filename": "Witch.Hat.Atelier.S01E07.1080p.NF.WEB-DL.JPN.AAC2.0.H.264.MSubs-ToonsHub", "context_tokens": [ ".", "JP", "N", ".", "AAC", "2", ".", "0", ".", "H.264", "." ], "context_labels": [ "O", "B-SOURCE", "O", "O", "B-SOURCE", "O", "O", "O", "O", "B-SOURCE", "O" ] }, { "type": "B_DIRECT_TO_O", "index": 24, "prev_label": "B-SOURCE", "label": "O", "token": ".", "row": 1, "file_id": 1, "filename": "Witch.Hat.Atelier.S01E07.1080p.NF.WEB-DL.JPN.AAC2.0.H.264.MSubs-ToonsHub", "context_tokens": [ "2", ".", "0", ".", "H.264", ".", "MSubs", "-", "ToonsHub" ], "context_labels": [ "O", "O", "O", "O", "B-SOURCE", "O", "B-SOURCE", "O", "O" ] }, { "type": "B_DIRECT_TO_O", "index": 26, "prev_label": "B-SOURCE", "label": "O", "token": "-", "row": 1, "file_id": 1, "filename": "Witch.Hat.Atelier.S01E07.1080p.NF.WEB-DL.JPN.AAC2.0.H.264.MSubs-ToonsHub", "context_tokens": [ "0", ".", "H.264", ".", "MSubs", "-", "ToonsHub" ], "context_labels": [ "O", "O", "B-SOURCE", "O", "B-SOURCE", "O", "O" ] }, { "type": "B_DIRECT_TO_O", "index": 2, "prev_label": "B-GROUP", "label": "O", "token": "]", "row": 2, "file_id": 2, "filename": "[LoliHouse] Maid-san wa Taberu Dake - 07 [WebRip 1080p HEVC-10bit AAC ASSx2]", "context_tokens": [ "[", "LoliHouse", "]", " ", "Maid", "-", "san", " " ], "context_labels": [ "O", "B-GROUP", "O", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE" ] }, { "type": "B_DIRECT_TO_O", "index": 17, "prev_label": "B-EPISODE", "label": "O", "token": " ", "row": 2, "file_id": 2, "filename": "[LoliHouse] Maid-san wa Taberu Dake - 07 [WebRip 1080p HEVC-10bit AAC ASSx2]", "context_tokens": [ "Dake", " ", "-", " ", "07", " ", "[WebRip 1080p HEVC-10bit AAC ASSx2]" ], "context_labels": [ "I-TITLE", "O", "O", "O", "B-EPISODE", "O", "O" ] }, { "type": "B_DIRECT_TO_O", "index": 2, "prev_label": "B-GROUP", "label": "O", "token": "]", "row": 3, "file_id": 3, "filename": "[ANi] 異世界悠閒農家 2 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]", "context_tokens": [ "[", "ANi", "]", " ", "異", "世", "界", "悠" ], "context_labels": [ "O", "B-GROUP", "O", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE" ] }, { "type": "B_DIRECT_TO_O", "index": 13, "prev_label": "B-SEASON", "label": "O", "token": " ", "row": 3, "file_id": 3, "filename": "[ANi] 異世界悠閒農家 2 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]", "context_tokens": [ "閒", "農", "家", " ", "2", " ", "-", " ", "06", " ", "[1080P]" ], "context_labels": [ "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-SEASON", "O", "O", "O", "B-EPISODE", "O", "B-RESOLUTION" ] }, { "type": "B_DIRECT_TO_O", "index": 17, "prev_label": "B-EPISODE", "label": "O", "token": " ", "row": 3, "file_id": 3, "filename": "[ANi] 異世界悠閒農家 2 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]", "context_tokens": [ "2", " ", "-", " ", "06", " ", "[1080P]", "[Baha]", "[WEB-DL]", "[AAC AVC]", "[CHT]" ], "context_labels": [ "B-SEASON", "O", "O", "O", "B-EPISODE", "O", "B-RESOLUTION", "B-SOURCE", "B-SOURCE", "O", "B-SOURCE" ] }, { "type": "B_DIRECT_TO_O", "index": 21, "prev_label": "B-SOURCE", "label": "O", "token": "[AAC AVC]", "row": 3, "file_id": 3, "filename": "[ANi] 異世界悠閒農家 2 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]", "context_tokens": [ "06", " ", "[1080P]", "[Baha]", "[WEB-DL]", "[AAC AVC]", "[CHT]" ], "context_labels": [ "B-EPISODE", "O", "B-RESOLUTION", "B-SOURCE", "B-SOURCE", "O", "B-SOURCE" ] }, { "type": "B_DIRECT_TO_O", "index": 2, "prev_label": "B-GROUP", "label": "O", "token": "]", "row": 4, "file_id": 4, "filename": "[ANi] 木頭風紀委員和迷你裙 JK 的故事 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]", "context_tokens": [ "[", "ANi", "]", " ", "木", "頭", "風", "紀" ], "context_labels": [ "O", "B-GROUP", "O", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE" ] }, { "type": "B_DIRECT_TO_O", "index": 24, "prev_label": "B-EPISODE", "label": "O", "token": " ", "row": 4, "file_id": 4, "filename": "[ANi] 木頭風紀委員和迷你裙 JK 的故事 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]", "context_tokens": [ "事", " ", "-", " ", "06", " ", "[1080P]", "[Baha]", "[WEB-DL]", "[AAC AVC]", "[CHT]" ], "context_labels": [ "I-TITLE", "O", "O", "O", "B-EPISODE", "O", "B-RESOLUTION", "B-SOURCE", "B-SOURCE", "O", "B-SOURCE" ] }, { "type": "B_DIRECT_TO_O", "index": 28, "prev_label": "B-SOURCE", "label": "O", "token": "[AAC AVC]", "row": 4, "file_id": 4, "filename": "[ANi] 木頭風紀委員和迷你裙 JK 的故事 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]", "context_tokens": [ "06", " ", "[1080P]", "[Baha]", "[WEB-DL]", "[AAC AVC]", "[CHT]" ], "context_labels": [ "B-EPISODE", "O", "B-RESOLUTION", "B-SOURCE", "B-SOURCE", "O", "B-SOURCE" ] }, { "type": "B_DIRECT_TO_O", "index": 2, "prev_label": "B-GROUP", "label": "O", "token": "]", "row": 5, "file_id": 5, "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][05][1080P][GB][MP4]", "context_tokens": [ "[", "KissSub", "]", "[", "Shunkashuutou", " ", "Daikousha", " " ], "context_labels": [ "O", "B-GROUP", "O", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE" ] }, { "type": "B_DIRECT_TO_O", "index": 19, "prev_label": "B-SOURCE", "label": "O", "token": "[MP4]", "row": 5, "file_id": 5, "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][05][1080P][GB][MP4]", "context_tokens": [ "Mai", "]", "[05]", "[1080P]", "[GB]", "[MP4]" ], "context_labels": [ "I-TITLE", "O", "B-EPISODE", "B-RESOLUTION", "B-SOURCE", "O" ] }, { "type": "B_DIRECT_TO_O", "index": 2, "prev_label": "B-GROUP", "label": "O", "token": "]", "row": 6, "file_id": 6, "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][06][1080P][GB][MP4]", "context_tokens": [ "[", "KissSub", "]", "[", "Shunkashuutou", " ", "Daikousha", " " ], "context_labels": [ "O", "B-GROUP", "O", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE" ] }, { "type": "B_DIRECT_TO_O", "index": 19, "prev_label": "B-SOURCE", "label": "O", "token": "[MP4]", "row": 6, "file_id": 6, "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][06][1080P][GB][MP4]", "context_tokens": [ "Mai", "]", "[06]", "[1080P]", "[GB]", "[MP4]" ], "context_labels": [ "I-TITLE", "O", "B-EPISODE", "B-RESOLUTION", "B-SOURCE", "O" ] }, { "type": "B_DIRECT_TO_O", "index": 2, "prev_label": "B-GROUP", "label": "O", "token": "]", "row": 7, "file_id": 7, "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][06][1080P][BIG5][MP4]", "context_tokens": [ "[", "KissSub", "]", "[", "Shunkashuutou", " ", "Daikousha", " " ], "context_labels": [ "O", "B-GROUP", "O", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE" ] }, { "type": "B_DIRECT_TO_O", "index": 19, "prev_label": "B-SOURCE", "label": "O", "token": "[MP4]", "row": 7, "file_id": 7, "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][06][1080P][BIG5][MP4]", "context_tokens": [ "Mai", "]", "[06]", "[1080P]", "[BIG5]", "[MP4]" ], "context_labels": [ "I-TITLE", "O", "B-EPISODE", "B-RESOLUTION", "B-SOURCE", "O" ] }, { "type": "B_DIRECT_TO_O", "index": 2, "prev_label": "B-GROUP", "label": "O", "token": "]", "row": 8, "file_id": 8, "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][05][1080P][BIG5][MP4]", "context_tokens": [ "[", "KissSub", "]", "[", "Shunkashuutou", " ", "Daikousha", " " ], "context_labels": [ "O", "B-GROUP", "O", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE" ] }, { "type": "B_DIRECT_TO_O", "index": 19, "prev_label": "B-SOURCE", "label": "O", "token": "[MP4]", "row": 8, "file_id": 8, "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][05][1080P][BIG5][MP4]", "context_tokens": [ "Mai", "]", "[05]", "[1080P]", "[BIG5]", "[MP4]" ], "context_labels": [ "I-TITLE", "O", "B-EPISODE", "B-RESOLUTION", "B-SOURCE", "O" ] }, { "type": "B_DIRECT_TO_O", "index": 2, "prev_label": "B-GROUP", "label": "O", "token": "]", "row": 9, "file_id": 9, "filename": "[Airota][Sousou no Frieren][29][1080p AVC AAC][CHT]", "context_tokens": [ "[", "Airota", "]", "[", "Sousou", " ", "no", " " ], "context_labels": [ "O", "B-GROUP", "O", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE" ] }, { "type": "B_DIRECT_TO_O", "index": 11, "prev_label": "B-EPISODE", "label": "O", "token": "[1080p AVC AAC]", "row": 9, "file_id": 9, "filename": "[Airota][Sousou no Frieren][29][1080p AVC AAC][CHT]", "context_tokens": [ "no", " ", "Frieren", "]", "[29]", "[1080p AVC AAC]", "[CHT]" ], "context_labels": [ "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-EPISODE", "O", "B-SOURCE" ] }, { "type": "B_DIRECT_TO_O", "index": 2, "prev_label": "B-GROUP", "label": "O", "token": "]", "row": 10, "file_id": 10, "filename": "[Airota][Sousou no Frieren][30][1080p AVC AAC][CHT]", "context_tokens": [ "[", "Airota", "]", "[", "Sousou", " ", "no", " " ], "context_labels": [ "O", "B-GROUP", "O", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE" ] }, { "type": "B_DIRECT_TO_O", "index": 11, "prev_label": "B-EPISODE", "label": "O", "token": "[1080p AVC AAC]", "row": 10, "file_id": 10, "filename": "[Airota][Sousou no Frieren][30][1080p AVC AAC][CHT]", "context_tokens": [ "no", " ", "Frieren", "]", "[30]", "[1080p AVC AAC]", "[CHT]" ], "context_labels": [ "I-TITLE", "I-TITLE", "I-TITLE", "O", "B-EPISODE", "O", "B-SOURCE" ] }, { "type": "B_DIRECT_TO_O", "index": 2, "prev_label": "B-GROUP", "label": "O", "token": "]", "row": 11, "file_id": 11, "filename": "[Airota][Sousou no Frieren][31][1080p AVC AAC][CHT]", "context_tokens": [ "[", "Airota", "]", "[", "Sousou", " ", "no", " " ], "context_labels": [ "O", "B-GROUP", "O", "O", "B-TITLE", "I-TITLE", "I-TITLE", "I-TITLE" ] }]```
## Tokenizer Split And Alignment
### Dataset tokens vs selected tokenizer mismatches```json[ { "file_id": 2, "filename": "[LoliHouse] Maid-san wa Taberu Dake - 07 [WebRip 1080p HEVC-10bit AAC ASSx2]", "common_prefix": 0, "dataset_tokens": [ "[", "LoliHouse", "]", " ", "Maid", "-", "san", " ", "wa", " ", "Taberu", " ", "Dake", " ", "-", " ", "07", " ", "[WebRip 1080p HEVC-10bit AAC ASSx2]" ], "tokenizer_tokens": [ "[LoliHouse]", " ", "Maid", "-", "san", " ", "wa", " ", "Taberu", " ", "Dake", " ", "-", " ", "07", " ", "[WebRip 1080p HEVC-10bit AAC ASSx2]" ], "dataset_len": 19, "tokenizer_len": 17 }, { "file_id": 3, "filename": "[ANi] 異世界悠閒農家 2 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]", "common_prefix": 0, "dataset_tokens": [ "[", "ANi", "]", " ", "異", "世", "界", "悠", "閒", "農", "家", " ", "2", " ", "-", " ", "06", " ", "[1080P]", "[Baha]", "[WEB-DL]", "[AAC AVC]", "[CHT]" ], "tokenizer_tokens": [ "[ANi]", " ", "異", "��", "界", "悠", "閒", "農", "家", " ", "2", " ", "-", " ", "06", " ", "[1080P]", "[Baha]", "[WEB-DL]", "[AAC AVC]", "[CHT]" ], "dataset_len": 23, "tokenizer_len": 21 }, { "file_id": 4, "filename": "[ANi] 木頭風紀委員和迷你裙 JK 的故事 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]", "common_prefix": 0, "dataset_tokens": [ "[", "ANi", "]", " ", "木", "頭", "風", "紀", "委", "員", "和", "迷", "你", "裙", " ", "JK", " ", "的", "故", "事", " ", "-", " ", "06", " ", "[1080P]", "[Baha]", "[WEB-DL]", "[AAC AVC]", "[CHT]" ], "tokenizer_tokens": [ "[ANi]", " ", "木", "頭", "風", "紀", "委", "員", "和", "迷", "你", "裙", " ", "JK", " ", "的", "故", "事", " ", "-", " ", "06", " ", "[1080P]", "[Baha]", "[WEB-DL]", "[AAC AVC]", "[CHT]" ], "dataset_len": 30, "tokenizer_len": 28 }, { "file_id": 5, "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][05][1080P][GB][MP4]", "common_prefix": 0, "dataset_tokens": [ "[", "KissSub", "]", "[", "Shunkashuutou", " ", "Daikousha", " ", "-", " ", "Haru", " ", "no", " ", "Mai", "]", "[05]", "[1080P]", "[GB]", "[MP4]" ], "tokenizer_tokens": [ "[KissSub]", "[Shunkashuutou Daikousha - Haru no Mai]", "[05]", "[1080P]", "[GB]", "[MP4]" ], "dataset_len": 20, "tokenizer_len": 6 }, { "file_id": 6, "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][06][1080P][GB][MP4]", "common_prefix": 0, "dataset_tokens": [ "[", "KissSub", "]", "[", "Shunkashuutou", " ", "Daikousha", " ", "-", " ", "Haru", " ", "no", " ", "Mai", "]", "[06]", "[1080P]", "[GB]", "[MP4]" ], "tokenizer_tokens": [ "[KissSub]", "[Shunkashuutou Daikousha - Haru no Mai]", "[06]", "[1080P]", "[GB]", "[MP4]" ], "dataset_len": 20, "tokenizer_len": 6 }, { "file_id": 7, "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][06][1080P][BIG5][MP4]", "common_prefix": 0, "dataset_tokens": [ "[", "KissSub", "]", "[", "Shunkashuutou", " ", "Daikousha", " ", "-", " ", "Haru", " ", "no", " ", "Mai", "]", "[06]", "[1080P]", "[BIG5]", "[MP4]" ], "tokenizer_tokens": [ "[KissSub]", "[Shunkashuutou Daikousha - Haru no Mai]", "[06]", "[1080P]", "[BIG5]", "[MP4]" ], "dataset_len": 20, "tokenizer_len": 6 }, { "file_id": 8, "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][05][1080P][BIG5][MP4]", "common_prefix": 0, "dataset_tokens": [ "[", "KissSub", "]", "[", "Shunkashuutou", " ", "Daikousha", " ", "-", " ", "Haru", " ", "no", " ", "Mai", "]", "[05]", "[1080P]", "[BIG5]", "[MP4]" ], "tokenizer_tokens": [ "[KissSub]", "[Shunkashuutou Daikousha - Haru no Mai]", "[05]", "[1080P]", "[BIG5]", "[MP4]" ], "dataset_len": 20, "tokenizer_len": 6 }, { "file_id": 9, "filename": "[Airota][Sousou no Frieren][29][1080p AVC AAC][CHT]", "common_prefix": 0, "dataset_tokens": [ "[", "Airota", "]", "[", "Sousou", " ", "no", " ", "Frieren", "]", "[29]", "[1080p AVC AAC]", "[CHT]" ], "tokenizer_tokens": [ "[Airota]", "[Sousou no Frieren]", "[29]", "[1080p AVC AAC]", "[CHT]" ], "dataset_len": 13, "tokenizer_len": 5 }, { "file_id": 10, "filename": "[Airota][Sousou no Frieren][30][1080p AVC AAC][CHT]", "common_prefix": 0, "dataset_tokens": [ "[", "Airota", "]", "[", "Sousou", " ", "no", " ", "Frieren", "]", "[30]", "[1080p AVC AAC]", "[CHT]" ], "tokenizer_tokens": [ "[Airota]", "[Sousou no Frieren]", "[30]", "[1080p AVC AAC]", "[CHT]" ], "dataset_len": 13, "tokenizer_len": 5 }, { "file_id": 11, "filename": "[Airota][Sousou no Frieren][31][1080p AVC AAC][CHT]", "common_prefix": 0, "dataset_tokens": [ "[", "Airota", "]", "[", "Sousou", " ", "no", " ", "Frieren", "]", "[31]", "[1080p AVC AAC]", "[CHT]" ], "tokenizer_tokens": [ "[Airota]", "[Sousou no Frieren]", "[31]", "[1080p AVC AAC]", "[CHT]" ], "dataset_len": 13, "tokenizer_len": 5 }]```
### Split examples```json[ { "file_id": 1, "filename": "Witch.Hat.Atelier.S01E07.1080p.NF.WEB-DL.JPN.AAC2.0.H.264.MSubs-ToonsHub", "dataset_tokens": [ "Witch", ".", "Hat", ".", "Atelier", ".", "S01", "E07", ".", "1080p", ".", "NF", ".", "WEB-DL", ".", "JP", "N", ".", "AAC", "2", ".", "0", ".", "H.264", ".", "MSubs", "-", "ToonsHub" ], "diagnosed_tokens": [ "Witch", ".", "Hat", ".", "Atelier", ".", "S01", "E07", ".", "1080p", ".", "NF", ".", "WEB-DL", ".", "JP", "N", ".", "AAC", "2", ".", "0", ".", "H.264", ".", "MSubs", "-", "ToonsHub" ], "regex_tokens": [ "Witch", ".", "Hat", ".", "Atelier", ".", "S01", "E07", ".", "1080p", ".", "NF", ".", "WEB-DL", ".", "JP", "N", ".", "AAC", "2", ".", "0", ".", "H.264", ".", "MSubs", "-", "ToonsHub" ], "char_tokens": [ "W", "i", "t", "c", "h", ".", "H", "a", "t", ".", "A", "t", "e", "l", "i", "e", "r", ".", "S", "0", "1", "E", "0", "7", ".", "1", "0", "8", "0", "p", ".", "N", "F", ".", "W", "E", "B", "-", "D", "L", ".", "J", "P", "N", ".", "A", "A", "C", "2", ".", "0", ".", "H", ".", "2", "6", "4", ".", "M", "S", "u", "b", "s", "-", "T", "o", "o", "n", "s", "H", "u", "b" ] }, { "file_id": 2, "filename": "[LoliHouse] Maid-san wa Taberu Dake - 07 [WebRip 1080p HEVC-10bit AAC ASSx2]", "dataset_tokens": [ "[", "LoliHouse", "]", " ", "Maid", "-", "san", " ", "wa", " ", "Taberu", " ", "Dake", " ", "-", " ", "07", " ", "[WebRip 1080p HEVC-10bit AAC ASSx2]" ], "diagnosed_tokens": [ "[LoliHouse]", " ", "Maid", "-", "san", " ", "wa", " ", "Taberu", " ", "Dake", " ", "-", " ", "07", " ", "[WebRip 1080p HEVC-10bit AAC ASSx2]" ], "regex_tokens": [ "[LoliHouse]", " ", "Maid", "-", "san", " ", "wa", " ", "Taberu", " ", "Dake", " ", "-", " ", "07", " ", "[WebRip 1080p HEVC-10bit AAC ASSx2]" ], "char_tokens": [ "[", "L", "o", "l", "i", "H", "o", "u", "s", "e", "]", " ", "M", "a", "i", "d", "-", "s", "a", "n", " ", "w", "a", " ", "T", "a", "b", "e", "r", "u", " ", "D", "a", "k", "e", " ", "-", " ", "0", "7", " ", "[", "W", "e", "b", "R", "i", "p", " ", "1", "0", "8", "0", "p", " ", "H", "E", "V", "C", "-", "1", "0", "b", "i", "t", " ", "A", "A", "C", " ", "A", "S", "S", "x", "2", "]" ] }, { "file_id": 3, "filename": "[ANi] 異世界悠閒農家 2 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]", "dataset_tokens": [ "[", "ANi", "]", " ", "異", "世", "界", "悠", "閒", "農", "家", " ", "2", " ", "-", " ", "06", " ", "[1080P]", "[Baha]", "[WEB-DL]", "[AAC AVC]", "[CHT]" ], "diagnosed_tokens": [ "[ANi]", " ", "異", "世", "界", "悠", "閒", "農", "家", " ", "2", " ", "-", " ", "06", " ", "[1080P]", "[Baha]", "[WEB-DL]", "[AAC AVC]", "[CHT]" ], "regex_tokens": [ "[ANi]", " ", "異", "世", "界", "悠", "閒", "農", "家", " ", "2", " ", "-", " ", "06", " ", "[1080P]", "[Baha]", "[WEB-DL]", "[AAC AVC]", "[CHT]" ], "char_tokens": [ "[", "A", "N", "i", "]", " ", "異", "世", "界", "悠", "閒", "農", "家", " ", "2", " ", "-", " ", "0", "6", " ", "[", "1", "0", "8", "0", "P", "]", "[", "B", "a", "h", "a", "]", "[", "W", "E", "B", "-", "D", "L", "]", "[", "A", "A", "C", " ", "A", "V", "C", "]", "[", "C", "H", "T", "]" ] }, { "file_id": 4, "filename": "[ANi] 木頭風紀委員和迷你裙 JK 的故事 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT]", "dataset_tokens": [ "[", "ANi", "]", " ", "木", "頭", "風", "紀", "委", "員", "和", "迷", "你", "裙", " ", "JK", " ", "的", "故", "事", " ", "-", " ", "06", " ", "[1080P]", "[Baha]", "[WEB-DL]", "[AAC AVC]", "[CHT]" ], "diagnosed_tokens": [ "[ANi]", " ", "木", "頭", "風", "紀", "委", "員", "和", "迷", "你", "裙", " ", "JK", " ", "的", "故", "事", " ", "-", " ", "06", " ", "[1080P]", "[Baha]", "[WEB-DL]", "[AAC AVC]", "[CHT]" ], "regex_tokens": [ "[ANi]", " ", "木", "頭", "風", "紀", "委", "員", "和", "迷", "你", "裙", " ", "JK", " ", "的", "故", "事", " ", "-", " ", "06", " ", "[1080P]", "[Baha]", "[WEB-DL]", "[AAC AVC]", "[CHT]" ], "char_tokens": [ "[", "A", "N", "i", "]", " ", "木", "頭", "風", "紀", "委", "員", "和", "迷", "你", "裙", " ", "J", "K", " ", "的", "故", "事", " ", "-", " ", "0", "6", " ", "[", "1", "0", "8", "0", "P", "]", "[", "B", "a", "h", "a", "]", "[", "W", "E", "B", "-", "D", "L", "]", "[", "A", "A", "C", " ", "A", "V", "C", "]", "[", "C", "H", "T", "]" ] }, { "file_id": 5, "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][05][1080P][GB][MP4]", "dataset_tokens": [ "[", "KissSub", "]", "[", "Shunkashuutou", " ", "Daikousha", " ", "-", " ", "Haru", " ", "no", " ", "Mai", "]", "[05]", "[1080P]", "[GB]", "[MP4]" ], "diagnosed_tokens": [ "[KissSub]", "[Shunkashuutou Daikousha - Haru no Mai]", "[05]", "[1080P]", "[GB]", "[MP4]" ], "regex_tokens": [ "[KissSub]", "[Shunkashuutou Daikousha - Haru no Mai]", "[05]", "[1080P]", "[GB]", "[MP4]" ], "char_tokens": [ "[", "K", "i", "s", "s", "S", "u", "b", "]", "[", "S", "h", "u", "n", "k", "a", "s", "h", "u", "u", "t", "o", "u", " ", "D", "a", "i", "k", "o", "u", "s", "h", "a", " ", "-", " ", "H", "a", "r", "u", " ", "n", "o", " ", "M", "a", "i", "]", "[", "0", "5", "]", "[", "1", "0", "8", "0", "P", "]", "[", "G", "B", "]", "[", "M", "P", "4", "]" ] }, { "file_id": 6, "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][06][1080P][GB][MP4]", "dataset_tokens": [ "[", "KissSub", "]", "[", "Shunkashuutou", " ", "Daikousha", " ", "-", " ", "Haru", " ", "no", " ", "Mai", "]", "[06]", "[1080P]", "[GB]", "[MP4]" ], "diagnosed_tokens": [ "[KissSub]", "[Shunkashuutou Daikousha - Haru no Mai]", "[06]", "[1080P]", "[GB]", "[MP4]" ], "regex_tokens": [ "[KissSub]", "[Shunkashuutou Daikousha - Haru no Mai]", "[06]", "[1080P]", "[GB]", "[MP4]" ], "char_tokens": [ "[", "K", "i", "s", "s", "S", "u", "b", "]", "[", "S", "h", "u", "n", "k", "a", "s", "h", "u", "u", "t", "o", "u", " ", "D", "a", "i", "k", "o", "u", "s", "h", "a", " ", "-", " ", "H", "a", "r", "u", " ", "n", "o", " ", "M", "a", "i", "]", "[", "0", "6", "]", "[", "1", "0", "8", "0", "P", "]", "[", "G", "B", "]", "[", "M", "P", "4", "]" ] }, { "file_id": 7, "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][06][1080P][BIG5][MP4]", "dataset_tokens": [ "[", "KissSub", "]", "[", "Shunkashuutou", " ", "Daikousha", " ", "-", " ", "Haru", " ", "no", " ", "Mai", "]", "[06]", "[1080P]", "[BIG5]", "[MP4]" ], "diagnosed_tokens": [ "[KissSub]", "[Shunkashuutou Daikousha - Haru no Mai]", "[06]", "[1080P]", "[BIG5]", "[MP4]" ], "regex_tokens": [ "[KissSub]", "[Shunkashuutou Daikousha - Haru no Mai]", "[06]", "[1080P]", "[BIG5]", "[MP4]" ], "char_tokens": [ "[", "K", "i", "s", "s", "S", "u", "b", "]", "[", "S", "h", "u", "n", "k", "a", "s", "h", "u", "u", "t", "o", "u", " ", "D", "a", "i", "k", "o", "u", "s", "h", "a", " ", "-", " ", "H", "a", "r", "u", " ", "n", "o", " ", "M", "a", "i", "]", "[", "0", "6", "]", "[", "1", "0", "8", "0", "P", "]", "[", "B", "I", "G", "5", "]", "[", "M", "P", "4", "]" ] }, { "file_id": 8, "filename": "[KissSub][Shunkashuutou Daikousha - Haru no Mai][05][1080P][BIG5][MP4]", "dataset_tokens": [ "[", "KissSub", "]", "[", "Shunkashuutou", " ", "Daikousha", " ", "-", " ", "Haru", " ", "no", " ", "Mai", "]", "[05]", "[1080P]", "[BIG5]", "[MP4]" ], "diagnosed_tokens": [ "[KissSub]", "[Shunkashuutou Daikousha - Haru no Mai]", "[05]", "[1080P]", "[BIG5]", "[MP4]" ], "regex_tokens": [ "[KissSub]", "[Shunkashuutou Daikousha - Haru no Mai]", "[05]", "[1080P]", "[BIG5]", "[MP4]" ], "char_tokens": [ "[", "K", "i", "s", "s", "S", "u", "b", "]", "[", "S", "h", "u", "n", "k", "a", "s", "h", "u", "u", "t", "o", "u", " ", "D", "a", "i", "k", "o", "u", "s", "h", "a", " ", "-", " ", "H", "a", "r", "u", " ", "n", "o", " ", "M", "a", "i", "]", "[", "0", "5", "]", "[", "1", "0", "8", "0", "P", "]", "[", "B", "I", "G", "5", "]", "[", "M", "P", "4", "]" ] }]```
### Vocabulary coverage```json{ "total": 85312, "unk": 5900, "unk_rate": 0.06915791447861966, "top_unk": [ [ "(BDRip 720p x264)", 66 ], [ "Partie", 59 ], [ "incantevole", 54 ], [ "Muxed", 54 ], [ "nonscordarmi", 54 ], [ "NEET", 52 ], [ "Dousei", 52 ], [ "[krikoun68]", 52 ], [ "[Blu-Ray - MUX - 960p - x264 - AC3 ITA-JAP - SUB ITA]", 51 ], [ "CTR", 45 ], [ "joseol", 45 ], [ "e99", 45 ], [ "(1440x1080 h264 AC3 AAC)", 45 ], [ "VERS", 37 ], [ "脙", 37 ], [ "Shunkashuutou", 36 ], [ "Daikousha", 36 ], [ "houbatsu", 36 ], [ "DEFINITIVA", 36 ], [ "Crash", 35 ], [ "Realm", 31 ], [ "UHD", 31 ], [ "[BDrip 1080P HEVC-10bit AAC]", 29 ], [ "Choroi", 28 ], [ "완", 28 ] ]}```
## Train Inference Tokenizer Comparison
- Model dir: `checkpoints\dmhy-finetune\final`- Model tokenizer variant: `regex`- Dataset tokenizer variant: `regex`- Diagnostic tokenizer variant: `regex`- Model tokenizer vocab size: 3,000- Diagnostic tokenizer vocab size: 8,000
If dataset and model tokenizer variants differ, validation loss can be low while real inference sees different token IDs and boundaries.
## Model Confusion Analysis
- Evaluated samples: 128- Entity precision: 0.9568- Entity recall: 0.9530- Entity F1: 0.9549
### Boundary error classes- `B-boundary`: 26 (56.52%)- `entity-type`: 20 (43.48%)
### Top token-label confusions| true | pred | count || --- | --- | --- || O | I-TITLE | 17 || O | B-EPISODE | 6 || B-SOURCE | O | 4 || I-TITLE | O | 3 || B-EPISODE | O | 3 || B-SEASON | O | 2 || B-RESOLUTION | B-SOURCE | 2 || B-EPISODE | I-TITLE | 2 || O | B-TITLE | 2 || B-TITLE | I-TITLE | 2 || O | B-SOURCE | 1 || B-SEASON | I-TITLE | 1 || O | B-SEASON | 1 |
### Top entity-type confusions| true | pred | count || --- | --- | --- || O | TITLE | 19 || O | EPISODE | 6 || SOURCE | O | 4 || TITLE | O | 3 || EPISODE | O | 3 || SEASON | O | 2 || RESOLUTION | SOURCE | 2 || EPISODE | TITLE | 2 || O | SOURCE | 1 || SEASON | TITLE | 1 || O | SEASON | 1 |
### Seqeval report```text precision recall f1-score support
EPISODE 0.9535 0.9609 0.9572 128 GROUP 1.0000 1.0000 1.0000 53 RESOLUTION 1.0000 0.9545 0.9767 44 SEASON 0.9630 0.8966 0.9286 29 SOURCE 0.9703 0.9608 0.9655 102 SPECIAL 1.0000 1.0000 1.0000 5 TITLE 0.9211 0.9333 0.9272 150
micro avg 0.9568 0.9530 0.9549 511 macro avg 0.9725 0.9580 0.9650 511weighted avg 0.9571 0.9530 0.9550 511
```
## Recommended Pipeline
1. Use one tokenizer variant end to end and save it in the checkpoint metadata.2. Prefer char-level or a deterministic hybrid tokenizer for DMHY filenames; avoid generic subword tokenization for labels.3. For char-level runs, use `--tokenizer char --max-seq-length 128` with `vocab.char.json`.4. Add CRF decoding or constrained BIO decoding so illegal I-X transitions and impossible boundary jumps are blocked.5. Keep rule-assisted post-processing for high-confidence structural anchors: leading group bracket, ` - 07`, `S01E07`, source, and resolution.6. Track entity-level F1 and field exact-match on real filenames; do not accept low validation loss alone.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952"""Export weakly-labeled anime filename samples from a DMHY crawler SQLite DB.
The crawler database is append-only while it runs, so this script snapshots ahigh-water mark (`files.id <= last_file_id`) and writes that value to a manifest.Future exports can pass `--min-id last_file_id + 1` to label only newly crawledrows."""
import argparseimport jsonimport osimport randomimport reimport sqlite3from collections import Counterfrom dataclasses import dataclassfrom datetime import datetime, timezonefrom pathlib import Pathfrom typing import Iterable, List, Optional, Sequence
from data_generator import LABEL_MAP, categorize_meta_tokenfrom label_repairs import season_marker_numberfrom tokenizer import AnimeTokenizer
VIDEO_EXTENSIONS = { ".mkv", ".mp4", ".avi", ".mov", ".wmv", ".flv", ".rmvb", ".ts", ".m2ts", ".webm", ".mpg", ".mpeg", ".m4v",}
NOISE_BRACKETS = { "mp4", "mkv", "avi", "webm", "mov", "wmv", "flv", "rmvb", "ts", "m2ts", "raw", "raws", "rip", "10bit", "8bit", "hi10p", "ma10p", "ass", "assx2", "tc", "sc", "gb", "big5", "cht", "chs", "jpn", "jp", "jap", "eng", "繁中", "简中", "繁日", "简日", "日语", "日文", "外挂", "内封", "字幕",}CATEGORY_BRACKETS = { "国漫", "國漫", "国产", "國產", "国产动漫", "國產動漫", "国产动画", "國產動畫", "国创", "國創", "中国动漫", "中國動漫", "中国动画", "中國動畫",}
SPECIAL_RE = re.compile(r"^(?:ova\d*|oad\d*|sp\d*|movie|the\s*movie|op|ed|pv|cm|ncop|nced|剧场版|劇場版|特别篇|特別篇)$", re.I)SPECIAL_SEARCH_RE = re.compile(r"^(?:檢索|检索|搜索|搜寻|搜尋|别名|別名|alias|search|keyword)\s*[::].+", re.I)EPISODE_RE = re.compile(r"^(?:[Ee][Pp]?|#)?(\d{1,4})(?:v\d+|END)?$", re.I)SEASON_RE = re.compile( r"^(?:" r"[Ss](\d{1,2})|" r"Seasons?\s*(\d{1,2})|" r"第([一二三四五六七八九十\d]+)[季期部]|" r"(\d+)(?:st|nd|rd|th)\s+[Ss]eason" r")$", re.I)READING_SEASON_RE = re.compile( r"^(?:Ni\s+no\s+(?:Sara|Shou|Sho|Syo|Shō)|Ni\s+Gakki|Sono\s+Ni|" r"San\s+no\s+(?:Sara|Shou|Sho|Syo)|(?:Yon|Shi|Shin)\s+no\s+Sara|" r"(?:Go|Gou)\s+no\s+Sara)$", re.I,)CJK_SEQUEL_SEASON_RE = re.compile( r"^(?:[一二三四五六七八九十兩两貳贰弐弍參叁参肆伍陸陆柒捌玖](?:\s*(?:ノ|の|之)\s*(?:章|期|季|部))?|" r"[ⅡⅢⅣⅤⅥⅦⅧⅨ]|II|III|IV|V|VI|VII|VIII|IX)$", re.I,)SXE_RE = re.compile(r"^([Ss]\d{1,2})([Ee]\d{1,4})(?:v\d+)?$")DATE_RE = re.compile(r"^(?:19|20)\d{2}[.\-_年]?(?:0?[1-9]|1[0-2])?[.\-_月]?(?:0?[1-9]|[12]\d|3[01])?日?$")HASH_RE = re.compile(r"^[A-Fa-f0-9]{8,}$")DIMENSION_RE = re.compile(r"^\d{3,4}[xX×]\d{3,4}$")RESOLUTION_RE = re.compile(r"^(?:\d{3,4}[pP]|\d[Kk]|\d{3,4}[xX×]\d{3,4})$")RESOLUTION_SEARCH_RE = re.compile(r"(?<![A-Za-z0-9])(?:\d{3,4}[pP]|\d[Kk]|\d{3,4}[xX×]\d{3,4})(?![A-Za-z0-9])")SOURCE_RE = re.compile( r"^(?:WEB[-_ ]?DL|WEB[-_ ]?Rip|BDRip|BluRay|BDMV|BD|DVDRip|DVD|TVRip|HDTV|" r"Netflix|NF|AMZN|Baha|CR|ABEMA|DSNP|U[-_ ]?NEXT|Hulu|AT[-_ ]?X|" r"x26[45]|h\.?26[45]|HEVC|AVC|AV1|AAC\d*(?:\.\d+)?|AAC|FLAC|MP3|DTS|Opus|" r"CHS|CHT|BIG5|GB|JPN?|JPSC|JPTC|简[体體]?|繁[体體]?|简日双语|繁日双语|内封|外挂|MSubs?)$", re.I,)GROUP_HINT_RE = re.compile( r"(?:字幕|字幕组|字幕組|sub|subs|raws?|fansub|studio|house|team|project|" r"loli|ani|baha|vcb|airota|kiss|dmhy|mabors|lilith|ohys|erai|subsplease)", re.I,)TRAILING_DECORATION_RE = re.compile( r"(?:新番|月番|合集|合輯|全集|完结|完結|检索|檢索|招募|字幕|内封|內封|" r"年齡|年龄|限制|版本|版|" r"简中|繁中|GB|BIG5|CHS|CHT|JPN?|MP4|MKV|HEVC|AVC|AAC|FLAC|WEB-DL|1080[Pp]|720[Pp])")
@dataclassclass ExportStats: scanned_rows: int = 0 video_rows: int = 0 duplicate_basenames: int = 0 labeled_samples: int = 0 skipped_no_episode: int = 0 skipped_no_title: int = 0 skipped_too_short: int = 0 skipped_too_long: int = 0
def normalize_path_basename(filename: str) -> str: return re.split(r"[\\/]", filename)[-1].strip()
def strip_video_extension(basename: str) -> tuple[str, str]: stem, ext = os.path.splitext(basename) return stem.strip(), ext.lower()
def clean_bracket(token: str) -> str: return token.strip().strip("[]()【】《》()").strip()
def cn_number_to_int(text: str) -> Optional[int]: if text.isdigit(): return int(text) values = {"一": 1, "二": 2, "三": 3, "四": 4, "五": 5, "六": 6, "七": 7, "八": 8, "九": 9} if text == "十": return 10 if text.startswith("十") and len(text) == 2: return 10 + values.get(text[1], 0) if text.endswith("十") and len(text) == 2: return values.get(text[0], 0) * 10 if "十" in text and len(text) == 3: return values.get(text[0], 0) * 10 + values.get(text[2], 0) return values.get(text)
def season_number(token: str) -> Optional[int]: clean = clean_bracket(token) match = SEASON_RE.match(clean) if match: value = next((g for g in match.groups() if g), None) if value is None: return None return cn_number_to_int(value) if READING_SEASON_RE.match(clean) or CJK_SEQUEL_SEASON_RE.match(clean): return season_marker_number(clean) return None
def is_explicit_season(token: str) -> bool: """Return True for unambiguous season syntax such as S02 or 第2季.""" clean = clean_bracket(token) return bool(SEASON_RE.match(clean))
def episode_number(token: str) -> Optional[int]: clean = clean_bracket(token) if season_number(clean) is not None: return None if DIMENSION_RE.match(clean) or DATE_RE.match(clean) or HASH_RE.match(clean): return None if re.match(r"^第\d{1,4}(?:\(\d{1,4}\))?[话話集]$", clean): return int(re.search(r"\d+", clean).group()) if re.match(r"^(?:OVA|OAD|SP)\d{1,4}$", clean, re.I): return int(re.search(r"\d+", clean).group()) if re.match(r"^\d{1,4}\s*END$", clean, re.I): return int(re.search(r"\d+", clean).group()) if re.match(r"^\d{1,4}[._]\d+$", clean): return int(re.search(r"\d+", clean).group()) match = EPISODE_RE.match(clean) if not match: return None number = int(match.group(1)) if number == 0 or number > 2000: return None return number
def has_wrapping_brackets(token: str) -> bool: return len(token) >= 2 and token[0] in "[【(《" and token[-1] in "]】)》"
def is_resolution(token: str) -> bool: clean = clean_bracket(token) return bool(RESOLUTION_RE.match(clean) or (has_wrapping_brackets(token) and RESOLUTION_SEARCH_RE.search(clean)))
def is_source(token: str) -> bool: clean = clean_bracket(token) if not clean: return False if categorize_meta_token(token) in {"RESOLUTION", "SOURCE"} and ( is_resolution(clean) or SOURCE_RE.match(clean) ): return True if SOURCE_RE.match(clean): return True if has_wrapping_brackets(token): parts = [part for part in re.split(r"[\s&+/,._-]+", clean) if part] has_source_part = any(SOURCE_RE.match(part) for part in parts) return has_source_part and all(SOURCE_RE.match(part) or is_noise_bracket(part) for part in parts) return False
def is_special(token: str) -> bool: clean = clean_bracket(token) return bool(SPECIAL_RE.match(clean) or SPECIAL_SEARCH_RE.match(clean))
def is_category_bracket(token: str) -> bool: clean = re.sub(r"[\s._-]+", "", clean_bracket(token)) return has_wrapping_brackets(token) and clean in CATEGORY_BRACKETS
def is_noise_bracket(token: str) -> bool: clean = clean_bracket(token) if not clean: return True normalized = re.sub(r"[\s._-]+", "", clean).lower() if normalized in NOISE_BRACKETS: return True if is_category_bracket(token): return True if DATE_RE.match(clean) or HASH_RE.match(clean): return True return False
def is_group_bracket(token: str, index: int, tokens: Sequence[str]) -> bool: if not (token.startswith("[") or token.startswith("(") or token.startswith("【") or token.startswith("《")): return False clean = clean_bracket(token) if not clean or is_noise_bracket(token): return False if is_resolution(clean) or is_source(clean) or is_special(clean) or episode_number(clean) is not None: return False first_content_index = next((i for i, t in enumerate(tokens) if t not in {" ", "-", "_", "|", "~", "~", "."}), 0) if index == first_content_index: return True if index <= first_content_index + 2 and GROUP_HINT_RE.search(clean): return True return False
def is_title_token(token: str) -> bool: if not token.strip(): return False if token in {" ", "-", "_", "|", "~", "~", "."}: return False clean = clean_bracket(token) if not clean: return False if is_resolution(clean) or is_source(clean) or is_special(clean): return False if is_explicit_season(clean) or episode_number(clean) is not None: return False if DATE_RE.match(clean) or HASH_RE.match(clean): return False if (token.startswith("[") or token.startswith("(") or token.startswith("【") or token.startswith("《")) and TRAILING_DECORATION_RE.search(clean): return False return True
def trim_title_span(tokens: Sequence[str], start: int, end: int) -> tuple[int, int]: while start < end and not is_title_token(tokens[start]): start += 1 while end > start and not is_title_token(tokens[end - 1]): end -= 1 while start < end and TRAILING_DECORATION_RE.search(clean_bracket(tokens[end - 1])): end -= 1 while end > start and tokens[end - 1] in {" ", "-", "_", "|", "~", "~", "."}: end -= 1 return start, end
def find_episode_index(tokens: Sequence[str]) -> Optional[int]: candidates: list[tuple[int, int]] = [] for idx, token in enumerate(tokens): number = episode_number(token) if number is None: continue clean = clean_bracket(token) if idx > 0 and tokens[idx - 1] == "." and re.fullmatch(r"\d+", clean): previous_clean = clean_bracket(tokens[idx - 2]) if idx >= 2 else "" if previous_clean.lower() in VIDEO_EXTENSIONS or f".{clean}".lower() in VIDEO_EXTENSIONS: continue score = 0 if re.match(r"^(?:[Ee][Pp]?|#|第|OVA|OAD|SP)", clean, re.I): score += 4 if token.startswith("[") or token.startswith("(") or token.startswith("【"): score += 3 if idx > 0 and tokens[idx - 1] in {"-", "_", "|"}: score += 2 if idx >= len(tokens) // 2: score += 1 if 1 <= number <= 200: score += 1 candidates.append((score, idx)) if not candidates: return None return max(candidates, key=lambda item: (item[0], item[1]))[1]
def is_separator_token(token: str) -> bool: return token in {" ", "-", "_", "|", "~", "~", ".", "+", "&", "/", ","}
def has_only_separators_between(tokens: Sequence[str], start: int, end: int) -> bool: return all(is_separator_token(token) for token in tokens[start:end])
def is_context_season_token(tokens: Sequence[str], idx: int, episode_idx: int) -> bool: """Detect compact season markers only when they structurally lead into an episode.""" if idx >= episode_idx: return False
token = tokens[idx] clean = clean_bracket(token) if not clean: return False if is_explicit_season(clean): return True
if season_number(clean) is None: return False if not has_only_separators_between(tokens, idx + 1, episode_idx): return False
# A bare V is often the volume prefix in V02E01, not season five. if clean.upper() == "V": return False return True
def label_context_season_tokens( tokens: Sequence[str], categories: List[str], episode_idx: int,) -> None: if ( episode_idx >= 2 and clean_bracket(tokens[episode_idx]).upper().startswith("E") and clean_bracket(tokens[episode_idx - 2]).upper() == "V" and clean_bracket(tokens[episode_idx - 1]).isdigit() ): categories[episode_idx - 2] = "season" categories[episode_idx - 1] = "season" return
for idx in range(episode_idx): if categories[idx] in {"group", "episode", "resolution", "source", "special"}: continue if is_context_season_token(tokens, idx, episode_idx): categories[idx] = "season"
def repair_structured_bracket_title_aliases( tokens: Sequence[str], categories: List[str], episode_idx: int,) -> None: """Keep the primary title in category-prefixed bracket series.
GM-Team-style rows often look like: [GROUP][国漫][中文标题 第2季][English Alias Ⅱ][2026][04][meta] The category, alias, and year brackets are metadata for parsing purposes; the first real title bracket after the category is the canonical title. """ if not any(is_category_bracket(tokens[idx]) for idx in range(min(episode_idx, len(tokens)))): return
title_candidates = [ idx for idx in range(episode_idx) if categories[idx] == "title" and has_wrapping_brackets(tokens[idx]) and is_title_token(tokens[idx]) ] if not title_candidates: return
primary_idx = title_candidates[0] for idx in title_candidates[1:]: categories[idx] = "sep"
for idx in range(episode_idx): if idx == primary_idx: continue if is_category_bracket(tokens[idx]) or DATE_RE.match(clean_bracket(tokens[idx])): categories[idx] = "sep"
def embedded_bracket_episode(token: str) -> Optional[tuple[str, str, str]]: """Split malformed tokens such as '[Group}Title[658]' into title + episode.""" if episode_number(token) is not None: return None match = re.match(r"^(?P<prefix>.+?)\[(?P<episode>\d{1,4}(?:v\d+)?)(?P<close>\])?$", token, re.I) if match is None and has_wrapping_brackets(token): match = re.match(r"^(?P<prefix>.+?)(?P<episode>\d{2,4})(?P<close>[\]\)】》])$", token, re.I) if not match: return None prefix = match.group("prefix") episode = match.group("episode") close = match.group("close") or "" if not clean_bracket(prefix): return None number = int(re.search(r"\d+", episode).group()) if number == 0 or number > 2000: return None return prefix, episode, close
def append_tokenized_category( tokens: List[str], categories: List[str], text: str, category: str, tokenizer: AnimeTokenizer,) -> None: for piece in tokenizer.tokenize(text): if not piece: continue if is_separator_token(piece) or piece in {"[", "]", "(", ")", "【", "】", "《", "》"}: piece_category = "sep" else: piece_category = category tokens.append(piece) categories.append(piece_category)
def finalize_weak_sample( tokens: Sequence[str], categories: Sequence[str], tokenizer: AnimeTokenizer, require_episode: bool = True,) -> Optional[dict]: expanded_tokens, expanded_categories = expand_tokens_and_categories(tokens, categories, tokenizer)
# Only unambiguous season forms are promoted here. Compact sequel markers # such as 貳, II, or Ni no Sara need episode context and are repaired by # label_repairs from character spans; treating every single CJK numeral as # season would corrupt titles like 魯邦三世. for idx, token in enumerate(expanded_tokens): if expanded_categories[idx] in {"sep", "episode", "group", "source", "resolution", "special", "season"}: continue if is_explicit_season(token): expanded_categories[idx] = "season" prev_idx = idx - 1 while prev_idx >= 0 and is_separator_token(expanded_tokens[prev_idx]) and expanded_categories[prev_idx] == "title": expanded_categories[prev_idx] = "sep" prev_idx -= 1
labels = assign_iob2(expanded_categories) if len(expanded_tokens) != len(labels): return None if not any(label.endswith("TITLE") for label in labels): return None if require_episode and not any(label.endswith("EPISODE") for label in labels): return None return {"tokens": expanded_tokens, "labels": labels}
def assign_iob2(categories: Sequence[str]) -> List[str]: labels: List[str] = [] previous_entity: Optional[str] = None for category in categories: entity = LABEL_MAP.get(category, "O") if entity == "O": labels.append("O") previous_entity = None continue prefix = "I" if previous_entity == entity else "B" labels.append(f"{prefix}-{entity}") previous_entity = entity return labels
def fallback_embedded_episode_sample( tokens: Sequence[str], tokenizer: AnimeTokenizer,) -> Optional[dict]: rebuilt_tokens: List[str] = [] rebuilt_categories: List[str] = [] used_episode = False
for token in tokens: embedded = embedded_bracket_episode(token) if embedded and not used_episode: prefix, episode, close = embedded append_tokenized_category(rebuilt_tokens, rebuilt_categories, prefix, "title", tokenizer) rebuilt_tokens.append(episode) rebuilt_categories.append("episode") if close: rebuilt_tokens.append(close) rebuilt_categories.append("sep") used_episode = True continue
if not used_episode: category = "sep" if is_separator_token(token) else "title" elif is_resolution(token): category = "resolution" elif is_source(token): category = "source" elif is_special(token): category = "special" else: category = "sep" rebuilt_tokens.append(token) rebuilt_categories.append(category)
if not used_episode: return None return finalize_weak_sample(rebuilt_tokens, rebuilt_categories, tokenizer)
def has_embedded_episode_candidate(tokens: Sequence[str]) -> bool: return any(embedded_bracket_episode(token) is not None for token in tokens)
def fallback_episode_first_sample( tokens: Sequence[str], categories: Sequence[str], episode_idx: int, tokenizer: AnimeTokenizer,) -> Optional[dict]: fallback_categories = ["sep"] * len(tokens)
# V02E01-style catalog rows are episode-first. The tokenizer currently # exposes them as V, 02, E01, so keep V02 together as a season span. if ( episode_idx >= 2 and clean_bracket(tokens[episode_idx]).upper().startswith("E") and clean_bracket(tokens[episode_idx - 2]).upper() == "V" and clean_bracket(tokens[episode_idx - 1]).isdigit() ): fallback_categories[episode_idx - 2] = "season" fallback_categories[episode_idx - 1] = "season" else: label_context_season_tokens(tokens, fallback_categories, episode_idx)
fallback_categories[episode_idx] = "episode"
title_indices: List[int] = [] for idx in range(episode_idx + 1, len(tokens)): token = tokens[idx] if is_separator_token(token): continue if is_resolution(token) or is_source(token) or is_special(token) or is_noise_bracket(token): fallback_categories[idx] = "resolution" if is_resolution(token) else "source" if is_source(token) else "special" if is_special(token) else "sep" continue title_indices.append(idx)
if not title_indices: # Some rows are title-only brackets followed by season/episode, # e.g. [伊蘇] II-01. If the leading bracket was guessed as GROUP but # no real title exists, use it as TITLE to keep the row useful. for idx in range(episode_idx): if categories[idx] == "group" and clean_bracket(tokens[idx]): title_indices.append(idx) break
for idx in title_indices: fallback_categories[idx] = "title" if title_indices: for idx in range(title_indices[0], title_indices[-1] + 1): if is_separator_token(tokens[idx]): fallback_categories[idx] = "title"
return finalize_weak_sample(tokens, fallback_categories, tokenizer)
def fallback_minimal_sample( tokens: Sequence[str], episode_idx: int, tokenizer: AnimeTokenizer,) -> Optional[dict]: """Keep malformed low-information rows instead of silently dropping them.""" categories: List[str] = [] title_idx: Optional[int] = None
for idx, token in enumerate(tokens): if idx == episode_idx: categories.append("episode") elif is_resolution(token): categories.append("resolution") elif is_source(token): categories.append("source") elif is_special(token): categories.append("special") if title_idx is None: title_idx = idx else: categories.append("sep")
if title_idx is None: for idx, token in enumerate(tokens): if idx == episode_idx or is_separator_token(token): continue if categories[idx] not in {"resolution", "source"}: title_idx = idx break if title_idx is None: return None
categories[title_idx] = "title" return finalize_weak_sample(tokens, categories, tokenizer)
def fallback_no_episode_sample(tokens: Sequence[str], tokenizer: AnimeTokenizer) -> Optional[dict]: """Label movies, OP/ED/SP, and malformed rows that have no true episode token.""" categories: List[str] = [] seen_title = False title_allowed = True
for idx, token in enumerate(tokens): if is_separator_token(token): categories.append("title" if seen_title and title_allowed else "sep") continue if idx == 0 and is_group_bracket(token, idx, tokens): categories.append("group") continue if is_resolution(token): categories.append("resolution") title_allowed = False continue if is_source(token): categories.append("source") title_allowed = False continue if is_special(token): categories.append("special") title_allowed = False continue if is_noise_bracket(token): categories.append("sep") continue categories.append("title") seen_title = True
return finalize_weak_sample(tokens, categories, tokenizer, require_episode=False)
def bracket_delimiters(token: str) -> tuple[str, str]: open_char = token[0] if token and token[0] in "[【(《" else "" close_char = token[-1] if token and token[-1] in "]】)》" else "" return open_char, close_char
def label_bracket_contents(token: str, category: str, tokenizer: AnimeTokenizer) -> tuple[List[str], List[str]]: inner = clean_bracket(token) if not inner: return [token], [category] open_char, close_char = bracket_delimiters(token) inner_tokens = tokenizer.tokenize(inner) tokens: List[str] = [] cats: List[str] = [] if open_char: tokens.append(open_char) cats.append("sep") tokens.extend(inner_tokens) cats.extend([category] * len(inner_tokens)) if close_char: tokens.append(close_char) cats.append("sep") return tokens, cats
def label_meta_bracket_contents(token: str, tokenizer: AnimeTokenizer) -> tuple[List[str], List[str]]: inner = clean_bracket(token) if not inner: return [token], ["sep"] open_char, close_char = bracket_delimiters(token) inner_tokens = tokenizer.tokenize(inner) tokens: List[str] = [] cats: List[str] = [] if open_char: tokens.append(open_char) cats.append("sep") for inner_token in inner_tokens: if inner_token in {" ", "-", "_", "|", "~", "~", ".", "+", "&", "/", ","}: cat = "sep" elif is_resolution(inner_token) or RESOLUTION_SEARCH_RE.fullmatch(inner_token): cat = "resolution" elif is_source(inner_token): cat = "source" elif is_special(inner_token): cat = "special" elif is_noise_bracket(inner_token): cat = "sep" else: cat = "sep" tokens.append(inner_token) cats.append(cat) if close_char: tokens.append(close_char) cats.append("sep") return tokens, cats
def expand_tokens_and_categories( tokens: Sequence[str], categories: Sequence[str], tokenizer: AnimeTokenizer,) -> tuple[List[str], List[str]]: expanded_tokens: List[str] = [] expanded_categories: List[str] = [] for token, category in zip(tokens, categories): clean = clean_bracket(token) if category == "season": match = SXE_RE.match(clean) if match: expanded_tokens.extend([match.group(1), match.group(2)]) expanded_categories.extend(["season", "episode"]) continue if category in {"group", "title"} and ( token.startswith("[") or token.startswith("(") or token.startswith("【") or token.startswith("《") ): split_tokens, split_categories = label_bracket_contents(token, category, tokenizer) expanded_tokens.extend(split_tokens) expanded_categories.extend(split_categories) continue if category in {"source", "resolution", "special", "sep"} and ( token.startswith("[") or token.startswith("(") or token.startswith("【") or token.startswith("《") ): split_tokens, split_categories = label_meta_bracket_contents(token, tokenizer) if any(cat != "sep" for cat in split_categories): expanded_tokens.extend(split_tokens) expanded_categories.extend(split_categories) continue expanded_tokens.append(token) expanded_categories.append(category) return expanded_tokens, expanded_categories
def weak_label_filename(filename: str, tokenizer: AnimeTokenizer) -> Optional[dict]: basename = normalize_path_basename(str(filename)) stem, ext = strip_video_extension(basename) if ext in VIDEO_EXTENSIONS: filename = stem else: filename = basename
tokens = tokenizer.tokenize(filename) if not tokens: return None if has_embedded_episode_candidate(tokens): embedded_sample = fallback_embedded_episode_sample(tokens, tokenizer) if embedded_sample is not None: return embedded_sample
categories = ["sep" if token in {" ", "-", "_", "|", "~", "~", "."} else "title" for token in tokens]
for idx, token in enumerate(tokens): if is_group_bracket(token, idx, tokens): categories[idx] = "group"
for idx, token in enumerate(tokens): if categories[idx] == "group": continue if is_category_bracket(token): categories[idx] = "sep" elif is_resolution(token): categories[idx] = "resolution" elif is_source(token): categories[idx] = "source" elif is_special(token): categories[idx] = "special" elif is_explicit_season(token): categories[idx] = "season" elif is_noise_bracket(token): categories[idx] = "sep"
episode_idx = find_episode_index(tokens) if episode_idx is None: return fallback_embedded_episode_sample(tokens, tokenizer) or fallback_no_episode_sample(tokens, tokenizer) categories[episode_idx] = "episode" label_context_season_tokens(tokens, categories, episode_idx) repair_structured_bracket_title_aliases(tokens, categories, episode_idx)
# S01E07 is tokenized as S01 + E07 after tokenizer changes. If an older # token slips through, expand_tokens_and_categories will split it. clean_episode = clean_bracket(tokens[episode_idx]) sxe_match = SXE_RE.match(clean_episode) if sxe_match: categories[episode_idx] = "season" elif not any(cat == "season" for cat in categories[:episode_idx]): for idx in range(episode_idx - 1, -1, -1): if categories[idx] == "sep": continue clean = clean_bracket(tokens[idx]) if re.fullmatch(r"[0-9]+", clean) and 1 <= int(clean) <= 20 and not ( tokens[idx].startswith("[") or tokens[idx].startswith("(") or tokens[idx].startswith("【") ): categories[idx] = "season" break
title_end = episode_idx while title_end > 0 and categories[title_end - 1] in {"season", "sep"}: title_end -= 1 title_start = 0 while title_start < title_end and categories[title_start] in {"group", "sep", "source", "resolution", "special"}: title_start += 1 title_start, title_end = trim_title_span(tokens, title_start, title_end) if title_start >= title_end: return fallback_embedded_episode_sample(tokens, tokenizer) or fallback_episode_first_sample( tokens, categories, episode_idx, tokenizer ) or fallback_minimal_sample( tokens, episode_idx, tokenizer )
for idx, token in enumerate(tokens): if title_start <= idx < title_end: if categories[idx] not in {"group", "season", "episode", "resolution", "source", "special"}: categories[idx] = "title" elif categories[idx] == "title": categories[idx] = "sep"
if not any(cat == "title" for cat in categories) or not any(cat == "episode" for cat in categories): return fallback_embedded_episode_sample(tokens, tokenizer) or fallback_episode_first_sample( tokens, categories, episode_idx, tokenizer ) or fallback_minimal_sample( tokens, episode_idx, tokenizer )
return finalize_weak_sample(tokens, categories, tokenizer)
def iter_db_rows(db_path: Path, min_id: int, max_id: int) -> Iterable[tuple[int, str]]: uri = f"file:{db_path}?mode=ro" conn = sqlite3.connect(uri, uri=True, timeout=30) conn.execute("PRAGMA query_only=ON") try: query = "SELECT id, filename FROM files WHERE id >= ? AND id <= ? ORDER BY id" yield from conn.execute(query, (min_id, max_id)) finally: conn.close()
def export_dataset(args: argparse.Namespace) -> None: db_path = Path(args.db) output_path = Path(args.output) output_path.parent.mkdir(parents=True, exist_ok=True)
conn = sqlite3.connect(f"file:{db_path}?mode=ro", uri=True, timeout=30) conn.execute("PRAGMA query_only=ON") try: db_max_id = conn.execute("SELECT MAX(id) FROM files").fetchone()[0] or 0 max_id = min(args.max_id if args.max_id is not None else db_max_id, db_max_id) finally: conn.close()
base_vocab = None if args.base_vocab: base_tokenizer = AnimeTokenizer(vocab_file=args.base_vocab) base_vocab = base_tokenizer.get_vocab() tokenizer = AnimeTokenizer() stats = ExportStats() seen_basenames: set[str] = set() token_lists: List[List[str]] = [] label_counter: Counter[str] = Counter() examples: List[dict] = []
with output_path.open("w", encoding="utf-8") as out: for file_id, raw_filename in iter_db_rows(db_path, args.min_id, max_id): stats.scanned_rows += 1 basename = normalize_path_basename(raw_filename) stem, ext = strip_video_extension(basename) if ext not in VIDEO_EXTENSIONS: continue stats.video_rows += 1 if stem in seen_basenames: stats.duplicate_basenames += 1 continue seen_basenames.add(stem) if len(stem) < args.min_chars: stats.skipped_too_short += 1 continue if len(stem) > args.max_chars: stats.skipped_too_long += 1 continue
sample = weak_label_filename(stem, tokenizer) if sample is None: # Most failures are no confident episode or no title; keep the # manifest aggregate conservative instead of over-classifying. stats.skipped_no_episode += 1 continue
labels = sample["labels"] if not any(label.endswith("TITLE") for label in labels): stats.skipped_no_title += 1 continue if not any(label.endswith("EPISODE") for label in labels): stats.skipped_no_episode += 1 continue
record = { "file_id": file_id, "filename": stem, "tokens": sample["tokens"], "labels": labels, } out.write(json.dumps(record, ensure_ascii=False) + "\n") stats.labeled_samples += 1 token_lists.append(sample["tokens"]) label_counter.update(labels) if len(examples) < args.example_count: examples.append(record) if args.limit and stats.labeled_samples >= args.limit: break
tokenizer.build_vocab(token_lists, max_size=args.max_vocab_size, base_vocab=base_vocab) tokenizer.save_vocabulary(output_path.parent)
manifest = { "created_at": datetime.now(timezone.utc).isoformat(), "source_db": str(db_path), "output": str(output_path), "min_file_id": args.min_id, "last_file_id": max_id, "db_max_file_id_at_export_start": db_max_id, "limit": args.limit, "stats": stats.__dict__, "label_counts": dict(label_counter), "vocab_size": tokenizer.vocab_size, "notes": [ "Rows are a snapshot of files.id <= last_file_id.", "Future incremental export can use --min-id last_file_id+1.", "Weak labels target GROUP, TITLE, SEASON, and EPISODE; media tags are boundary labels/noise.", ], "examples": examples, } manifest_path = output_path.with_suffix(".manifest.json") manifest_path.write_text(json.dumps(manifest, ensure_ascii=False, indent=2), encoding="utf-8")
print(json.dumps({k: v for k, v in manifest.items() if k != "examples"}, ensure_ascii=False, indent=2))
def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser(description="Export weakly-labeled DMHY filename dataset") parser.add_argument("--db", default=r"D:\WorkSpace\Python\dmhy-parser\dmhy_anime.db", help="DMHY SQLite database") parser.add_argument("--output", default="data/dmhy_weak.jsonl", help="Output JSONL path") parser.add_argument("--min-id", type=int, default=1, help="Minimum files.id to export") parser.add_argument("--max-id", type=int, default=None, help="Maximum files.id to export; defaults to current DB max") parser.add_argument("--limit", type=int, default=None, help="Maximum labeled samples to write") parser.add_argument("--min-chars", type=int, default=4, help="Minimum stem length") parser.add_argument("--max-chars", type=int, default=180, help="Maximum stem length") parser.add_argument("--example-count", type=int, default=20, help="Examples to include in manifest") parser.add_argument("--base-vocab", default=None, help="Optional vocab whose IDs should be preserved") parser.add_argument("--max-vocab-size", type=int, default=3000, help="Maximum vocab size including special tokens") parser.add_argument("--seed", type=int, default=42, help="Random seed") return parser.parse_args()
if __name__ == "__main__": parsed_args = parse_args() random.seed(parsed_args.seed) export_dataset(parsed_args)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163"""Evaluate parser checkpoints on fixed real-world filename cases."""
import argparseimport jsonimport osfrom typing import Dict, List, Optional
import torchfrom transformers import BertForTokenClassification
from config import Configfrom inference import parse_filenamefrom tokenizer import load_tokenizer
DEFAULT_CASE_FILE = os.path.join("data", "parser_regression_cases.json")
def normalize_field_value(field: str, value) -> Optional[str]: if value is None: return None if field in {"episode", "season"}: try: return str(int(value)) except (TypeError, ValueError): return str(value).strip().lower() text = str(value).strip() if field in {"resolution", "source"}: return text.lower().replace("_", "-") return " ".join(text.lower().split())
def load_cases(path: str) -> List[Dict]: with open(path, "r", encoding="utf-8") as f: cases = json.load(f) if not isinstance(cases, list): raise ValueError(f"{path} must contain a JSON list") return cases
def evaluate_cases( model_dir: str, case_file: str, tokenizer_variant: Optional[str], max_length: Optional[int], use_rules: bool, constrain_bio: bool,) -> Dict: cfg = Config() tokenizer = load_tokenizer(model_dir, tokenizer_variant) model = BertForTokenClassification.from_pretrained(model_dir) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model.to(device) model.eval()
id2label = {int(k): v for k, v in getattr(model.config, "id2label", cfg.id2label).items()} resolved_max_length = max_length or int(getattr(model.config, "max_seq_length", 64)) cases = load_cases(case_file)
field_totals: Dict[str, int] = {} field_correct: Dict[str, int] = {} results = [] full_correct = 0
for case in cases: expected = case.get("expected", {}) pred = parse_filename( case["filename"], model, tokenizer, id2label, max_length=resolved_max_length, debug=False, use_rules=use_rules, constrain_bio=constrain_bio, ) errors = {} for field, expected_value in expected.items(): field_totals[field] = field_totals.get(field, 0) + 1 expected_norm = normalize_field_value(field, expected_value) pred_norm = normalize_field_value(field, pred.get(field)) if expected_norm == pred_norm: field_correct[field] = field_correct.get(field, 0) + 1 else: errors[field] = { "expected": expected_value, "pred": pred.get(field), } if not errors: full_correct += 1 results.append( { "id": case.get("id"), "filename": case["filename"], "ok": not errors, "errors": errors, "expected": expected, "pred": {field: pred.get(field) for field in sorted(expected)}, } )
field_accuracy = { field: field_correct.get(field, 0) / total for field, total in sorted(field_totals.items()) } return { "model_dir": model_dir, "case_file": case_file, "tokenizer_variant": getattr(tokenizer, "tokenizer_variant", "regex"), "max_length": resolved_max_length, "use_rules": use_rules, "constrain_bio": constrain_bio, "case_count": len(cases), "full_correct": full_correct, "full_accuracy": full_correct / len(cases) if cases else 0.0, "field_correct": field_correct, "field_total": field_totals, "field_accuracy": field_accuracy, "failures": [result for result in results if not result["ok"]], "results": results, }
def main() -> None: parser = argparse.ArgumentParser(description="Evaluate parser on fixed filename regression cases") parser.add_argument("--model-dir", required=True) parser.add_argument("--case-file", default=DEFAULT_CASE_FILE) parser.add_argument("--tokenizer", choices=["regex", "char"], default=None) parser.add_argument("--max-length", type=int, default=None) parser.add_argument("--output", default=None, help="Optional JSON output path") parser.add_argument("--no-rule-assist", action="store_true") parser.add_argument("--no-constrained-bio", action="store_true") args = parser.parse_args()
metrics = evaluate_cases( model_dir=args.model_dir, case_file=args.case_file, tokenizer_variant=args.tokenizer, max_length=args.max_length, use_rules=not args.no_rule_assist, constrain_bio=not args.no_constrained_bio, )
print( f"Full case accuracy: {metrics['full_correct']}/{metrics['case_count']} " f"({metrics['full_accuracy']:.4f})" ) for field, total in metrics["field_total"].items(): correct = metrics["field_correct"].get(field, 0) print(f" {field}: {correct}/{total} ({correct / total:.4f})") if metrics["failures"]: print("\nFailures:") for failure in metrics["failures"]: print(json.dumps(failure, ensure_ascii=False))
if args.output: os.makedirs(os.path.dirname(args.output) or ".", exist_ok=True) with open(args.output, "w", encoding="utf-8") as f: json.dump(metrics, f, ensure_ascii=False, indent=2)
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143"""Export the trained anime filename BERT checkpoint to ONNX for Android.
The Android parser pads every filename to a fixed sequence length, so the ONNXgraph is exported with a static [1, max_length] input shape. This keeps mobileruntime setup simple and predictable."""
import argparseimport jsonimport osimport shutilimport sysfrom pathlib import Path
import numpy as npimport onnximport onnxruntime as ortimport torchfrom transformers import BertForTokenClassification
from tokenizer import AnimeTokenizer, load_tokenizer
if hasattr(sys.stdout, "reconfigure"): sys.stdout.reconfigure(encoding="utf-8")if hasattr(sys.stderr, "reconfigure"): sys.stderr.reconfigure(encoding="utf-8")
class TokenClassificationWrapper(torch.nn.Module): def __init__(self, model: BertForTokenClassification): super().__init__() self.model = model
def forward(self, input_ids: torch.Tensor, attention_mask: torch.Tensor) -> torch.Tensor: return self.model(input_ids=input_ids, attention_mask=attention_mask).logits
def encode_sample(tokenizer: AnimeTokenizer, text: str, max_length: int) -> tuple[np.ndarray, np.ndarray]: tokens = tokenizer.tokenize(text) input_ids = [tokenizer.cls_token_id] + tokenizer.convert_tokens_to_ids(tokens) + [tokenizer.sep_token_id] attention_mask = [1] * len(input_ids)
if len(input_ids) > max_length: input_ids = input_ids[:max_length] attention_mask = attention_mask[:max_length]
pad_len = max_length - len(input_ids) if pad_len > 0: input_ids += [tokenizer.pad_token_id] * pad_len attention_mask += [0] * pad_len
return ( np.array([input_ids], dtype=np.int64), np.array([attention_mask], dtype=np.int64), )
def copy_android_assets(model_dir: Path, onnx_path: Path, assets_dir: Path) -> None: assets_dir.mkdir(parents=True, exist_ok=True) shutil.copy2(onnx_path, assets_dir / "anime_filename_parser.onnx") shutil.copy2(model_dir / "vocab.json", assets_dir / "vocab.json") shutil.copy2(model_dir / "config.json", assets_dir / "config.json")
def main() -> None: parser = argparse.ArgumentParser(description="Export anime filename parser to ONNX") parser.add_argument("--model-dir", default="checkpoints/final", help="HuggingFace checkpoint directory") parser.add_argument("--output", default="exports/anime_filename_parser.onnx", help="Output ONNX file") parser.add_argument("--max-length", type=int, default=64, help="Fixed sequence length used on Android") parser.add_argument( "--android-assets-dir", help="Optional Android assets directory that receives the ONNX model, vocab, and config", ) parser.add_argument( "--sample", default="[ANi] 葬送的芙莉莲 S2 - 03 [1080P][WEB-DL]", help="Sample filename used for PyTorch/ONNX parity verification", ) args = parser.parse_args()
model_dir = Path(args.model_dir) output_path = Path(args.output) output_path.parent.mkdir(parents=True, exist_ok=True) output_path.with_suffix(output_path.suffix + ".data").unlink(missing_ok=True)
tokenizer = load_tokenizer(os.fspath(model_dir)) model = BertForTokenClassification.from_pretrained(model_dir) model.eval()
input_ids_np, attention_mask_np = encode_sample(tokenizer, args.sample, args.max_length) input_ids = torch.from_numpy(input_ids_np) attention_mask = torch.from_numpy(attention_mask_np)
wrapper = TokenClassificationWrapper(model).eval() with torch.no_grad(): torch_logits = wrapper(input_ids, attention_mask).detach().cpu().numpy()
torch.onnx.export( wrapper, (input_ids, attention_mask), output_path, input_names=["input_ids", "attention_mask"], output_names=["logits"], opset_version=18, do_constant_folding=True, dynamo=True, external_data=False, )
onnx_model = onnx.load(output_path) onnx.checker.check_model(onnx_model)
session = ort.InferenceSession(os.fspath(output_path), providers=["CPUExecutionProvider"]) onnx_logits = session.run( ["logits"], { "input_ids": input_ids_np, "attention_mask": attention_mask_np, }, )[0] max_diff = float(np.max(np.abs(torch_logits - onnx_logits)))
metadata = { "model_dir": os.fspath(model_dir), "output": os.fspath(output_path), "max_length": args.max_length, "sample": args.sample, "logits_shape": list(onnx_logits.shape), "max_abs_diff": max_diff, } metadata_path = output_path.with_suffix(".metadata.json") metadata_path.write_text(json.dumps(metadata, ensure_ascii=False, indent=2), encoding="utf-8")
if args.android_assets_dir: copy_android_assets(model_dir, output_path, Path(args.android_assets_dir))
print(json.dumps(metadata, ensure_ascii=False, indent=2))
if __name__ == "__main__": main()123456789101112{ "model_dir": ".", "output": "exports\\anime_filename_parser.onnx", "max_length": 128, "sample": "[ANi] 葬送的芙莉莲 S2 - 03 [1080P][WEB-DL]", "logits_shape": [ 1, 128, 15 ], "max_abs_diff": 5.65648078918457e-05}123version https://git-lfs.github.com/spec/v1oid sha256:6d967c5c2305e6737c9e791956a174655deebef2cfa477e081890ebddd56e004size 19633926123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991"""Inference script for anime filename parser.
Loads a trained model and tokenizer, parses anime filenames,and outputs structured metadata.
Usage: python inference.py "[ANi] 葬送的芙莉莲 S2 - 03 [1080P][WEB-DL]" python inference.py --input-file filenames.txt --output-file results.jsonl"""
import argparseimport jsonimport osimport reimport sysfrom typing import Dict, List, Optional, Tuple
import torchfrom transformers import BertForTokenClassification
from config import Configfrom label_repairs import season_marker_numberfrom tokenizer import AnimeTokenizer, load_tokenizer
# Chinese number mappingCN_NUM_MAP: Dict[str, int] = { "一": 1, "二": 2, "三": 3, "四": 4, "五": 5, "六": 6, "七": 7, "八": 8, "九": 9, "十": 10,}
def extract_season_number(text: str) -> Optional[int]: """ Extract season number from various season formats.
Examples: "S2" → 2, "Season 2" → 2, "第二季" → 2, "1st Season" → 1 """ marker_value = season_marker_number(text) if marker_value is not None: return marker_value
# Arabic digits match = re.search(r'(\d+)', text) if match: return int(match.group(1))
# Chinese digits for cn, num in CN_NUM_MAP.items(): if cn in text: return num
return None
def extract_episode_number(text: str) -> Optional[int]: """ Extract episode number from various episode formats.
Examples: "03" → 3, "EP21" → 21, "第7话" → 7, "#01" → 1 """ match = re.search(r'(\d+)', text) if match: return int(match.group(1)) return None
def extract_resolution(text: str) -> Optional[str]: """Extract resolution string (e.g., '1080P', '4K', '1920x1080').""" # Strip brackets for matching clean = text.strip("[]()【】") return clean if clean else None
def display_token(token: str) -> str: """Make whitespace tokens visible in debug output.""" if token == " ": return "<SPACE>" if token == "\t": return "<TAB>" return token
def trim_decorations(text: str) -> str: """Trim outer release brackets from an extracted entity.""" return text.strip().strip("[]()【】《》()").strip()
def join_entity_tokens(tokens: List[str], tokenizer: Optional[AnimeTokenizer] = None) -> str: """Join entity tokens according to the tokenizer granularity.""" if tokenizer is not None and getattr(tokenizer, "tokenizer_variant", "regex") == "char": return "".join(tokens) text = "".join(tokens) if " " in tokens: return text return text
def labels_to_entities( tokens: List[str], labels: List[str], tokenizer: Optional[AnimeTokenizer] = None,) -> List[Tuple[str, str]]: """ Convert BIO labels into entity spans.
Illegal orphan I-X labels start a new entity so debug output exposes the model behavior instead of silently dropping tokens. """ entities: List[Tuple[str, str]] = [] current_entity: Optional[str] = None current_tokens: List[str] = []
for token, label in zip(tokens, labels): if label.startswith("B-"): if current_entity: entities.append((current_entity, join_entity_tokens(current_tokens, tokenizer))) current_entity = label[2:] current_tokens = [token] elif label.startswith("I-"): entity_type = label[2:] if current_entity == entity_type: current_tokens.append(token) else: if current_entity: entities.append((current_entity, join_entity_tokens(current_tokens, tokenizer))) current_entity = entity_type current_tokens = [token] else: if current_entity: entities.append((current_entity, join_entity_tokens(current_tokens, tokenizer))) current_entity = None current_tokens = []
if current_entity: entities.append((current_entity, join_entity_tokens(current_tokens, tokenizer))) return entities
def is_allowed_bio_transition(previous_label: str, label: str) -> bool: """Return whether previous_label -> label is valid under IOB2.""" if label.startswith("I-"): entity = label[2:] return previous_label in {f"B-{entity}", f"I-{entity}"} return True
def constrained_bio_decode(emissions: torch.Tensor, id2label: Dict[int, str]) -> List[int]: """ Decode token logits with hard BIO transition constraints.
This is a lightweight CRF-style Viterbi decoder without learned transition weights. It prevents impossible orphan I-X spans at inference time. """ if emissions.numel() == 0: return []
num_tokens, num_labels = emissions.shape scores = emissions.detach().cpu() backpointers = torch.zeros((num_tokens, num_labels), dtype=torch.long) dp = torch.full((num_labels,), float("-inf"))
for label_id in range(num_labels): label = id2label.get(label_id, "O") if not label.startswith("I-"): dp[label_id] = scores[0, label_id]
for idx in range(1, num_tokens): next_dp = torch.full((num_labels,), float("-inf")) for label_id in range(num_labels): label = id2label.get(label_id, "O") best_score = float("-inf") best_prev = 0 for prev_id in range(num_labels): prev_label = id2label.get(prev_id, "O") if not is_allowed_bio_transition(prev_label, label): continue candidate = dp[prev_id] + scores[idx, label_id] if candidate > best_score: best_score = float(candidate) best_prev = prev_id next_dp[label_id] = best_score backpointers[idx, label_id] = best_prev dp = next_dp
best_last = int(torch.argmax(dp).item()) decoded = [best_last] for idx in range(num_tokens - 1, 0, -1): decoded.append(int(backpointers[idx, decoded[-1]].item())) decoded.reverse() return decoded
def postprocess( tokens: List[str], labels: List[str], tokenizer: Optional[AnimeTokenizer] = None, filename: Optional[str] = None, use_rules: bool = True,) -> Dict: """ Convert BIO-labeled tokens into structured metadata.
Merges consecutive B- / I- tokens of the same entity type, then extracts structured fields. """ result: Dict = { "title": None, "season": None, "episode": None, "group": None, "resolution": None, "source": None, "special": None, }
entities = labels_to_entities(tokens, labels, tokenizer)
# Fill result for entity_type, text in entities: if entity_type == "TITLE": result["title"] = result["title"] or trim_decorations(text) # If we find multiple title fragments, concatenate them # (handles "That" + ... + "Time" etc.) elif entity_type == "SEASON": season_num = extract_season_number(text) if season_num is not None: # Keep the highest/last season number if multiple result["season"] = season_num elif entity_type == "EPISODE": ep_num = extract_episode_number(text) if ep_num is not None: if result["episode"] is None: result["episode"] = ep_num elif entity_type == "GROUP": group = text.strip("[]()【】") if result["group"] is None: result["group"] = group elif entity_type == "SPECIAL": special = text.strip("[]()【】") result["special"] = special elif entity_type == "RESOLUTION": res = extract_resolution(text) if res: result["resolution"] = res elif entity_type == "SOURCE": src = text.strip("[]()【】") result["source"] = src
# Handle multi-fragment titles: concatenate all TITLE fragments # (This is needed because O tokens between words break entity continuity) title_fragments = [t for e, t in entities if e == "TITLE"] if title_fragments: result["title"] = " ".join( trimmed for f in title_fragments if (trimmed := trim_decorations(f)) )
if use_rules and filename: result = apply_rule_assists(filename, result)
return result
BRACKET_RE = re.compile(r"\[([^\]]+)\]|\(([^)]+)\)|【([^】]+)】|《([^》]+)》")RESOLUTION_RE = re.compile(r"(?<![A-Za-z0-9])(?:\d{3,4}[pP]|\d[Kk]|\d{3,4}[xX×]\d{3,4})(?![A-Za-z0-9])")SOURCE_TOKEN_PATTERN = ( r"WEB[-_ ]?DL|WEB[-_ ]?Rip|BDRip|BluRay|BDMV|BD|DVDRip|DVD|TVRip|HDTV|" r"Netflix|NF|AMZN|Baha|CR|ABEMA|DSNP|U[-_ ]?NEXT|Hulu|AT[-_ ]?X|" r"CHS|CHT|GB|BIG5|JPN?|繁中|简中")SOURCE_RE = re.compile(rf"\b(?:{SOURCE_TOKEN_PATTERN})\b", re.I)SOURCE_TAG_RE = re.compile( rf"^(?:{SOURCE_TOKEN_PATTERN})(?:\s*(?:[&+/]|,\s*)\s*(?:{SOURCE_TOKEN_PATTERN}))*$", re.I,)SPECIAL_TAG_RE = re.compile( r"^(?:檢索|检索|搜索|搜寻|搜尋|别名|別名|alias|search|keyword)\s*[::].+", re.I,)EPISODE_PATTERNS = [ ("season_episode", re.compile(r"[Ss]\d{1,2}[Ee](?P<ep>\d{1,4})(?:v\d+)?", re.I)), ("dash_episode", re.compile(r"(?:^|[\s._])[-_]\s*(?P<ep>\d{1,4})(?:v\d+)?(?=$|[\s._\-\]\)】》\[])")), ("bracket_episode", re.compile(r"[\[\(【《](?:EP?|#)?(?P<ep>\d{1,4})(?:v\d+)?[\]\)】》]", re.I)), ("explicit_episode", re.compile(r"(?:^|[\s._\-\[\(【《#])(?:EP?|第|#)(?P<ep>\d{1,4})(?:v\d+)?(?:[话話集])?(?=$|[\s._\-\]\)】》])", re.I)), ( "long_episode", re.compile( r"(?:^|[\s._\-\[\(【《])(?P<ep>\d{3,4})(?:v\d+)?" r"(?=[\s._\-\]\)】》\[]+(?:\d{3,4}[pP]|WEB|BD|BluRay|HDTV|NF|AMZN|CR|Baha))", re.I, ), ), ("generic_episode", re.compile(r"(?:^|[\s._\-\[\(【《#])(?P<ep>\d{1,3})(?:v\d+)?(?=$|[\s._\-\]\)】》])", re.I)),]SEASON_RE = re.compile(r"(?:^|[\s._\-\[\(【《])(?:[Ss](?P<s1>\d{1,2})|Season\s*(?P<s2>\d{1,2})|第(?P<s3>[一二三四五六七八九十\d]+)[季期部])", re.I)SEQUEL_MARKER_RE = re.compile( r"(?<![A-Za-z0-9])" r"(?P<marker>" r"Ni\s+no\s+(?:Sara|Shou|Sho|Syo|Shō)|" r"San\s+no\s+(?:Sara|Shou|Sho|Syo)|" r"(?:Yon|Shi|Shin)\s+no\s+Sara|" r"(?:Go|Gou)\s+no\s+Sara|" r"Ni\s+Gakki|Sono\s+Ni|Ni|" r"II|III|IV|V|VI|VII|VIII|IX|[ⅡⅢⅣⅤⅥⅦⅧⅨ]|" r"[一二三四五六七八九十兩两貳贰弐弍參叁参肆伍陸陆柒捌玖](?:\s*(?:ノ|の|之)\s*(?:章|期|季|部))?" r")" r"(?![A-Za-z0-9])", re.I,)TRAILING_SEQUEL_MARKER_RE = re.compile( r"(?:^|[\s._-])" r"(?P<marker>" r"Ni\s+no\s+(?:Sara|Shou|Sho|Syo|Shō)|" r"San\s+no\s+(?:Sara|Shou|Sho|Syo)|" r"(?:Yon|Shi|Shin)\s+no\s+Sara|" r"(?:Go|Gou)\s+no\s+Sara|" r"Ni\s+Gakki|Sono\s+Ni|Ni|" r"II|III|IV|V|VI|VII|VIII|IX|[ⅡⅢⅣⅤⅥⅦⅧⅨ]|" r"[一二三四五六七八九十兩两貳贰弐弍參叁参肆伍陸陆柒捌玖](?:\s*(?:ノ|の|之)\s*(?:章|期|季|部))?" r")$", re.I,)NOISE_META_RE = re.compile( r"^(?:\d{3,4}[pP]|\d[Kk]|WEB[-_ ]?DL|WEB[-_ ]?Rip|BDRip|BluRay|BDMV|BD|DVDRip|DVD|TVRip|" r"HDTV|Netflix|NF|AMZN|Baha|CR|HEVC|AVC|AV1|x26[45]|h\.?26[45]|AAC.*|FLAC|MP3|DTS|" r"Opus|ASS.*|CHS|CHT|BIG5|GB|JPN?|MP4|MKV|繁中|简中|内封|外挂)$", re.I,)DATE_RE = re.compile(r"^(?:19|20)\d{2}(?:[.\-_年]?(?:0?[1-9]|1[0-2]))?(?:[.\-_月]?(?:0?[1-9]|[12]\d|3[01]))?日?$")CATEGORY_BRACKETS = { "国漫", "國漫", "国产", "國產", "国产动漫", "國產動漫", "国产动画", "國產動畫", "国创", "國創", "中国动漫", "中國動漫", "中国动画", "中國動畫",}
def cn_number_to_int(text: str) -> Optional[int]: if text.isdigit(): return int(text) values = {"一": 1, "二": 2, "三": 3, "四": 4, "五": 5, "六": 6, "七": 7, "八": 8, "九": 9} if text == "十": return 10 if text.startswith("十") and len(text) == 2: return 10 + values.get(text[1], 0) if text.endswith("十") and len(text) == 2: return values.get(text[0], 0) * 10 if "十" in text and len(text) == 3: return values.get(text[0], 0) * 10 + values.get(text[2], 0) return values.get(text)
def bracket_parts(filename: str) -> List[Tuple[str, int, int]]: parts: List[Tuple[str, int, int]] = [] for match in BRACKET_RE.finditer(filename): text = next(group for group in match.groups() if group is not None) parts.append((text.strip(), match.start(), match.end())) return parts
def looks_like_group(text: str) -> bool: if not text or NOISE_META_RE.search(text): return False return bool( re.search( r"(?:字幕|字幕组|字幕組|sub|subs|raws?|fansub|studio|house|team|project|" r"loli|ani|vcb|airota|kiss|dmhy|erai|subsplease)", text, re.I, ) )
def looks_like_episode_or_meta(text: str) -> bool: if not text: return False clean = text.strip() normalized = re.sub(r"[\s._-]+", "", clean) return bool( re.fullmatch(r"(?:EP?|#)?\d{1,4}(?:v\d+)?", clean, re.I) or DATE_RE.fullmatch(clean) or normalized in CATEGORY_BRACKETS or RESOLUTION_RE.search(clean) or SOURCE_TAG_RE.fullmatch(clean) or SOURCE_RE.search(clean) or SPECIAL_TAG_RE.search(clean) or NOISE_META_RE.search(clean) )
def looks_like_structural_group(text: str, filename: str, bracket_end: int) -> bool: """Heuristic for short leading release-group brackets not in the name list.""" if looks_like_group(text): return True if not text or looks_like_episode_or_meta(text): return False
after = filename[bracket_end:].lstrip(" \t._") if after.startswith("-"): return False next_bracket = BRACKET_RE.match(after) if next_bracket: next_text = next(group for group in next_bracket.groups() if group is not None) if looks_like_episode_or_meta(next_text): return False
words = re.findall(r"[A-Za-z0-9]+", text) if not words: if re.search(r"[\u3400-\u9fff]", text) and len(text) <= 32: return True return False if len(text) > 32: return False if len(words) == 1: return True if any(sep in text for sep in "-_"): return True if words[0].isupper() and len(words[0]) <= 4 and len(words) <= 3: return True return False
def apply_rule_assists(filename: str, result: Dict) -> Dict: """ Fill high-confidence structural fields from filename conventions.
The model remains the primary tagger; rules only fill missing obvious fields or repair common boundary drift around leading group brackets and episodes. """ repaired = dict(result) brackets = bracket_parts(filename)
if (not repaired.get("group") or (repaired.get("title") and repaired["group"] in repaired["title"])) and brackets: first_text, first_start, first_end = brackets[0] if first_start == 0 and looks_like_structural_group(first_text, filename, first_end): repaired["group"] = first_text
if not repaired.get("resolution"): match = RESOLUTION_RE.search(filename) if match: repaired["resolution"] = match.group(0)
source_matches = source_candidates(filename) current_source = repaired.get("source") preferred_source = source_matches[0] if source_matches else None if source_matches and ( not current_source or not SOURCE_RE.fullmatch(str(current_source)) or len(str(current_source)) <= 3 and str(current_source).lower() not in {"nf", "cr"} or ( preferred_source and str(current_source).lower().replace("_", "-") in {"web-dl", "webdl", "webrip", "web-rip"} and preferred_source.lower().replace("_", "-") not in {"web-dl", "webdl", "webrip", "web-rip"} ) ): repaired["source"] = preferred_source
if not repaired.get("special"): for text, _start, _end in brackets: clean = text.strip() if SPECIAL_TAG_RE.search(clean): repaired["special"] = clean break
episode = best_structural_episode(filename) if episode is not None and ( repaired.get("episode") is None or not plausible_episode_context(filename, int(repaired["episode"])) ): repaired["episode"] = episode
if repaired.get("season") is None: match = SEASON_RE.search(filename) if match: value = next(group for group in match.groups() if group) season = cn_number_to_int(value) if season is not None: repaired["season"] = season if repaired.get("season") is None and repaired.get("episode") is not None: sequel = structural_sequel_marker(filename, repaired.get("group"), repaired.get("episode")) if sequel is not None: repaired["season"] = sequel[1] elif repaired.get("episode") == repaired.get("season") and not SEASON_RE.search(filename): repaired["season"] = None
title = repaired.get("title") group = repaired.get("group") if group and (NOISE_META_RE.search(str(group)) or SOURCE_RE.fullmatch(str(group)) or RESOLUTION_RE.fullmatch(str(group))): repaired["group"] = None group = None
if title and group and title.startswith(group): title = title[len(group):].lstrip("]】)>})》 \t-_.") repaired["title"] = title or repaired["title"]
if repaired.get("episode"): repaired_title = infer_title_span(filename, group, repaired["episode"]) if repaired_title: repaired["title"] = repaired_title
structured_title = infer_structured_bracket_title(filename, group, repaired.get("episode")) if structured_title: repaired["title"] = structured_title
if repaired.get("title") and repaired.get("season") is not None: repaired["title"] = strip_trailing_season_from_title(repaired["title"], repaired["season"])
return repaired
def structural_sequel_marker( filename: str, group: Optional[str], episode: Optional[int],) -> Optional[Tuple[str, int]]: if episode is None: return None title_end = None if episode is not None: ep_patterns = [ rf"[Ss]\d{{1,2}}[Ee]0*{episode}(?:v\d+)?", rf"\s[-_]\s*0*{episode}(?:v\d+)?(?=$|[\s\[\(【《._-])", rf"[\[\(【《]0*{episode}(?:v\d+)?[\]\)】》]", rf"#\s*0*{episode}(?:v\d+)?(?=$|[\s\[\(【《._-])", rf"(?:^|[\s._\-\[\(【《])第0*{episode}(?:[话話集])?(?=$|[\s._\-\]\)】》])", ] start = 0 if group: first = BRACKET_RE.match(filename) if first and group in first.group(0): start = first.end() for pattern in ep_patterns: match = re.search(pattern, filename[start:], re.I) if match: title_end = start + match.start() break if title_end is None: return None
prefix = filename[:title_end].rstrip(" \t-_.") for match in reversed(list(SEQUEL_MARKER_RE.finditer(prefix))): marker = match.group("marker") value = season_marker_number(marker) if value is None: continue tail = prefix[match.end():].strip(" \t-_.") if tail: continue if marker.lower() == "ni" and "Kakuriyo no Yadomeshi Ni" not in prefix: continue return marker, value return None
def normalize_source_text(text: str) -> str: text = re.sub(r"\s+", "", text.strip()) text = re.sub(r"(?i)WEB[_ ]?DL", "WEB-DL", text) text = re.sub(r"(?i)WEB[_ ]?Rip", "WebRip", text) text = re.sub(r"(?i)U[_ ]?NEXT", "U-NEXT", text) text = re.sub(r"(?i)AT[_ ]?X", "AT-X", text) return text.replace("_", "-")
def source_priority(source: str) -> int: normalized = source.lower().replace("_", "-").replace(" ", "") parts = re.split(r"[&+/,]", normalized) if any(part in {"nf", "netflix", "amzn", "baha", "cr", "abema", "dsnp", "u-next", "hulu", "at-x"} for part in parts): return 90 if any(part in {"web-dl", "webdl", "webrip", "web-rip", "bdrip", "bluray", "bdmv", "bd", "dvdrip", "dvd", "tvrip", "hdtv"} for part in parts): return 60 if len(parts) > 1: return 40 return 20
def source_candidates(filename: str) -> List[str]: candidates: List[Tuple[int, int, str]] = [] for text, start, _end in bracket_parts(filename): clean = text.strip() if SOURCE_TAG_RE.fullmatch(clean): normalized = normalize_source_text(clean) candidates.append((source_priority(normalized), -start, normalized))
for match in SOURCE_RE.finditer(filename): normalized = normalize_source_text(match.group(0)) candidates.append((source_priority(normalized), -match.start(), normalized))
deduped: Dict[str, Tuple[int, int, str]] = {} for priority, neg_start, value in candidates: key = value.lower() if key not in deduped or (priority, neg_start) > (deduped[key][0], deduped[key][1]): deduped[key] = (priority, neg_start, value)
return [value for _priority, _neg_start, value in sorted(deduped.values(), reverse=True)]
def is_category_text(text: str) -> bool: normalized = re.sub(r"[\s._-]+", "", text.strip()) return normalized in CATEGORY_BRACKETS
def infer_structured_bracket_title( filename: str, group: Optional[str], episode: Optional[int],) -> Optional[str]: """Pick the primary title from [group][category][title][alias][year][episode] rows.""" brackets = bracket_parts(filename) if len(brackets) < 4 or episode is None: return None
start_index = 0 if group and brackets and brackets[0][0] == group: start_index = 1
search = brackets[start_index:] if not search or not any(is_category_text(text) for text, _start, _end in search[:2]): return None
episode_index = None for idx, (text, _start, _end) in enumerate(brackets): if re.fullmatch(rf"(?:EP?|#)?0*{episode}(?:v\d+)?", text.strip(), re.I): episode_index = idx break if episode_index is None: return None
candidates: List[Tuple[int, str]] = [] for idx in range(start_index, episode_index): text = brackets[idx][0].strip() if not text or looks_like_episode_or_meta(text): continue score = 0 if SEASON_RE.search(text) or TRAILING_SEQUEL_MARKER_RE.search(text): score += 50 if re.search(r"[\u3400-\u9fff]", text): score += 20 if idx > start_index: score += 10 candidates.append((score, text))
if not candidates: return None return max(candidates, key=lambda item: item[0])[1]
def best_structural_episode(filename: str) -> Optional[int]: priorities = { "season_episode": 1000, "dash_episode": 900, "bracket_episode": 850, "explicit_episode": 800, "long_episode": 750, "generic_episode": 100, } candidates: List[Tuple[int, int, int]] = [] for name, pattern in EPISODE_PATTERNS: for match in pattern.finditer(filename): ep_text = match.group("ep") ep = int(ep_text) if ep == 0 or ep > 2000: continue context = filename[max(0, match.start() - 5):match.end() + 5] if RESOLUTION_RE.search(context) or re.search(r"AAC|DDP|AC3|H\.?26[45]|x26[45]", context, re.I): continue priority = priorities[name] if 1 <= ep <= 200: priority += 20 candidates.append((priority, match.start(), ep)) if not candidates: return None return max(candidates, key=lambda item: (item[0], item[1]))[2]
def plausible_episode_context(filename: str, episode: int) -> bool: ep_text = str(episode) padded = f"{episode:02d}" if re.search(rf"(?<![A-Za-z0-9])(?:H|x)\.?0*{re.escape(ep_text)}(?!\d)", filename, re.I): return False patterns = [ rf"[Ss]\d{{1,2}}[Ee]0*{episode}(?:v\d+)?", rf"(?:^|[\s._])[-_]\s*0*{episode}(?:v\d+)?(?=$|[\s._\-\]\)】》\[])", rf"[\[\(【《](?:EP?|#)?0*{episode}(?:v\d+)?[\]\)】》]", rf"(?:^|[\s._\-\[\(【《#])(?:EP?|第|#)0*{episode}(?:v\d+)?(?:[话話集])?(?=$|[\s._\-\]\)】》])", rf"(?:^|[\s._\-\[\(【《])0*{episode}(?:v\d+)?(?=[\s._\-\]\)】》\[]+(?:\d{{3,4}}[pP]|WEB|BD|BluRay|HDTV|NF|AMZN|CR|Baha))", ] return any(re.search(pattern, filename, re.I) for pattern in patterns) or bool( re.search(rf"(?:^|[\s._\-\[\(【《])(?:{re.escape(ep_text)}|{re.escape(padded)})(?=$|[\s._\-\]\)】》])", filename) )
def strip_trailing_season_from_title(title: str, season: int) -> str: season_text = str(season) patterns = [ rf"\s+[Ss]0*{season_text}$", rf"\s+Season\s*0*{season_text}$", rf"\s+0*{season_text}$", rf"\s+第(?:0*{season_text}|{season_text})[季期部章]$", ] cleaned = title for pattern in patterns: cleaned = re.sub(pattern, "", cleaned, flags=re.I).strip(" \t-_.") match = TRAILING_SEQUEL_MARKER_RE.search(cleaned) if match and season_marker_number(match.group("marker")) == season: cleaned = cleaned[:match.start()].strip(" \t-_.") return cleaned or title
def clean_inferred_title(title: str) -> str: raw_title = title.strip(" \t-_.") bracket_matches = list(BRACKET_RE.finditer(raw_title)) if bracket_matches: first = bracket_matches[0] prefix = raw_title[:first.start()].strip(" \t-_.★☆") text = next(group for group in first.groups() if group is not None).strip() if text and not looks_like_episode_or_meta(text) and ( not prefix or re.search(r"(?:新番|月|合集|繁|简|字幕|先行|合集|★|☆)", prefix, re.I) ): return text return raw_title.strip("[]()【】《》()")
def infer_title_span(filename: str, group: Optional[str], episode: Optional[int]) -> Optional[str]: start = 0 if group: first = BRACKET_RE.match(filename) if first and group in first.group(0): start = first.end() else: # Some releases put leading metadata before the actual title, e.g. # `[1080p] Title - 01`. Do not keep that wrapper as title text. while True: leading = BRACKET_RE.match(filename[start:].lstrip(" \t._-")) if not leading: break skipped_ws = len(filename[start:]) - len(filename[start:].lstrip(" \t._-")) text = next(group for group in leading.groups() if group is not None) if not looks_like_episode_or_meta(text): break start += skipped_ws + leading.end()
end = None if episode is not None: ep_patterns = [ rf"[Ss]\d{{1,2}}[Ee]0*{episode}(?:v\d+)?", rf"\s[-_]\s*0*{episode}(?:v\d+)?(?=$|[\s\[\(【《._-])", rf"[\[\(【《]0*{episode}(?:v\d+)?[\]\)】》]", rf"#\s*0*{episode}(?:v\d+)?(?=$|[\s\[\(【《._-])", rf"(?:^|[\s._\-\[\(【《])第0*{episode}(?:[话話集])?(?=$|[\s._\-\]\)】》])", rf"[Ee]0*{episode}(?:v\d+)?", ] for pattern in ep_patterns: match = re.search(pattern, filename[start:], re.I) if match: end = start + match.start() break
if end is None: for text, bracket_start, _bracket_end in bracket_parts(filename): if bracket_start <= start: continue if NOISE_META_RE.search(text) or RESOLUTION_RE.search(text) or SOURCE_RE.search(text): end = bracket_start break
if end is None or end <= start: return None title = clean_inferred_title(filename[start:end]) return title or None
def parse_filename( filename: str, model: BertForTokenClassification, tokenizer: AnimeTokenizer, id2label: Dict[int, str], max_length: int = 64, debug: bool = False, use_rules: bool = True, constrain_bio: bool = True,) -> Dict: """ Parse an anime filename and extract structured metadata.
Args: filename: Raw anime filename string. model: Trained BertForTokenClassification model. tokenizer: AnimeTokenizer instance. id2label: Mapping from label ID to label string. max_length: Maximum sequence length (including special tokens).
Returns: Dict with parsed fields (title, season, episode, etc.). """ # Tokenize tokens = tokenizer.tokenize(filename) if not tokens: return {"title": None, "season": None, "episode": None, "group": None, "resolution": None, "source": None, "special": None}
# Convert to input IDs input_ids = tokenizer.convert_tokens_to_ids(tokens) embedding_size = model.get_input_embeddings().weight.shape[0] out_of_range_tokens = [ token for token, token_id in zip(tokens, input_ids) if token_id >= embedding_size ] if out_of_range_tokens: input_ids = [ token_id if token_id < embedding_size else tokenizer.unk_token_id for token_id in input_ids ] unk_token_id = tokenizer.unk_token_id unk_tokens = [token for token, token_id in zip(tokens, input_ids) if token_id == unk_token_id]
# Add special tokens input_ids = [tokenizer.cls_token_id] + input_ids + [tokenizer.sep_token_id] attention_mask = [1] * len(input_ids)
# Truncate if needed if len(input_ids) > max_length: input_ids = [input_ids[0]] + input_ids[1:max_length - 1] + [tokenizer.sep_token_id] attention_mask = [1] * len(input_ids)
# Pad pad_len = max_length - len(input_ids) if pad_len > 0: input_ids += [tokenizer.pad_token_id] * pad_len attention_mask += [0] * pad_len
# Predict device = next(model.parameters()).device input_tensor = torch.tensor([input_ids], device=device) mask_tensor = torch.tensor([attention_mask], device=device)
# Remove special token predictions # Count real tokens used (minus CLS/SEP) real_token_count = len(tokens) # Truncate real tokens if we had to truncate available = min(real_token_count, max_length - 2) if available <= 0: return {"title": None, "season": None, "episode": None, "group": None, "resolution": None, "source": None, "special": None}
with torch.no_grad(): logits = model(input_ids=input_tensor, attention_mask=mask_tensor).logits token_logits = logits[0, 1:1 + available, :] probabilities = torch.softmax(token_logits, dim=-1) scores, greedy_predictions = torch.max(probabilities, dim=-1) if constrain_bio: pred_labels = constrained_bio_decode(token_logits, id2label) selected_scores = [ probabilities[idx, label_id].detach().cpu().item() for idx, label_id in enumerate(pred_labels) ] else: pred_labels = greedy_predictions.detach().cpu().tolist() selected_scores = scores.detach().cpu().tolist() label_strings = [id2label.get(p, "O") for p in pred_labels]
# Post-process result = postprocess( tokens[:available], label_strings, tokenizer=tokenizer, filename=filename, use_rules=use_rules, ) if debug: result["_debug"] = { "tokenizer_variant": getattr(tokenizer, "tokenizer_variant", "regex"), "decoder": "constrained_bio" if constrain_bio else "greedy", "max_length": max_length, "token_count": len(tokens), "available_token_count": available, "truncated": len(tokens) > available, "unk_count": len(unk_tokens), "unk_rate": len(unk_tokens) / len(tokens) if tokens else 0.0, "unk_tokens": unk_tokens[:50], "vocab_mismatch": bool(out_of_range_tokens), "model_embedding_size": int(embedding_size), "tokenizer_vocab_size": int(tokenizer.vocab_size), "out_of_range_tokens": out_of_range_tokens[:50], "tokens": tokens[:available], "labels": label_strings, "scores": [round(float(score), 4) for score in selected_scores], "token_table": [ { "i": i, "token": display_token(token), "id": int(token_id), "label": label, "score": round(float(score), 4), } for i, (token, token_id, label, score) in enumerate( zip(tokens[:available], input_ids[1:1 + available], label_strings, selected_scores) ) ], "entities": [ {"type": entity_type, "text": text} for entity_type, text in labels_to_entities(tokens[:available], label_strings, tokenizer) ], } return result
def main(): parser = argparse.ArgumentParser(description="Anime filename parser") parser.add_argument("filename", nargs="?", type=str, help="Anime filename to parse") parser.add_argument("--input-file", type=str, help="File with filenames (one per line)") parser.add_argument("--output-file", type=str, help="Output file for results (JSONL)") parser.add_argument("--model-dir", type=str, default=".", help="Path to trained model directory") parser.add_argument("--tokenizer", choices=["regex", "char"], default=None, help="Tokenizer variant override. Defaults to checkpoint metadata") parser.add_argument("--max-length", type=int, default=64, help="Maximum sequence length") parser.add_argument("--debug", action="store_true", help="Include tokenizer, labels, scores, and entity spans in JSON output") parser.add_argument("--no-rule-assist", action="store_true", help="Disable high-confidence structural post-processing rules") parser.add_argument("--no-constrained-bio", action="store_true", help="Use greedy per-token decoding instead of constrained BIO Viterbi") args = parser.parse_args()
# Load config cfg = Config()
# Load tokenizer print(f"Loading tokenizer from {args.model_dir}...", file=sys.stderr) tokenizer = load_tokenizer(args.model_dir, args.tokenizer)
# Load model print(f"Loading model from {args.model_dir}...", file=sys.stderr) model = BertForTokenClassification.from_pretrained(args.model_dir) model.eval()
id2label = {int(k): v for k, v in getattr(model.config, "id2label", cfg.id2label).items()} max_length = args.max_length if max_length == 64: max_length = int(getattr(model.config, "max_seq_length", max_length))
# Process filenames filenames_to_parse: List[str] = []
if args.filename: filenames_to_parse.append(args.filename)
if args.input_file: with open(args.input_file, 'r', encoding='utf-8') as f: filenames_to_parse.extend(line.strip() for line in f if line.strip())
if not filenames_to_parse: # Read from stdin filenames_to_parse.extend(sys.stdin.read().strip().splitlines())
# Parse and output results: List[Dict] = [] for fn in filenames_to_parse: if not fn.strip(): continue result = parse_filename( fn, model, tokenizer, id2label, max_length, debug=args.debug, use_rules=not args.no_rule_assist, constrain_bio=not args.no_constrained_bio, ) result["_input"] = fn results.append(result)
if args.output_file is None: print(json.dumps(result, ensure_ascii=False))
if args.output_file: with open(args.output_file, 'w', encoding='utf-8') as f: for r in results: f.write(json.dumps(r, ensure_ascii=False) + '\n') print(f"Results saved to {args.output_file}", file=sys.stderr)
if __name__ == "__main__": main().gitattributesAdded+36
.gitignoreAdded+16
.gitmodulesAdded+3
AGENTS.mdAdded+169
ANDROID.mdAdded+58
MAINTENANCE.mdAdded+121
README.mdAdded+210
build_repair_focus_dataset.pyAdded+187
case_metrics.jsonAdded+481
check_f1.pyAdded+33
colab/README.mdAdded+75
colab/configs/dmhy_char_train.jsonAdded+42
colab/configs/dmhy_regex_finetune.jsonAdded+42
colab/start_worker.ipynbAdded+45
colab_client.pyAdded+184
colab_train.pyAdded+543
colab_worker.pyAdded+446
config.jsonAdded+64
config.pyAdded+74
convert_to_char_dataset.pyAdded+201
data/dmhy/README.mdAdded+21
data/dmhy/ab_mix_100k.manifest.jsonAdded+9
data/dmhy/dmhy_weak.manifest.jsonAdded+531
data/dmhy/dmhy_weak_new.manifest.jsonAdded+38
data/dmhy/llm_batches/_summary.jsonAdded+9
data/dmhy/llm_batches/hardcases_00.jsonAdded+1
data/dmhy/llm_batches/hardcases_01.jsonAdded+1
data/dmhy/llm_batches/hardcases_02.jsonAdded+1
data/dmhy/llm_batches/hardcases_03.jsonAdded+1
data/dmhy/llm_batches/hardcases_04.jsonAdded+1
data/dmhy/llm_batches/prompt_00000.txtAdded+110
data/dmhy/llm_batches/prompt_00001.txtAdded+110
data/dmhy/mixed_train.manifest.jsonAdded+9
data/dmhy/vocab.jsonAdded
data/parser_regression_cases.jsonAdded+244
data/synthetic_small.jsonlAdded
data/test_smoke.jsonlAdded+100
data/vocab.jsonAdded
data_generator.pyAdded+757
dataset.pyAdded+358
datasets/AnimeNameAdded+1
diagnose_pipeline.pyAdded+885
diagnostics_report.mdAdded+277
diagnostics_report_word.mdAdded+2678
dmhy_dataset.pyAdded+952
evaluate_parser_cases.pyAdded+163
export_onnx.pyAdded+143
exports/anime_filename_parser.metadata.jsonAdded+12
exports/anime_filename_parser.onnxAdded+3
inference.pyAdded+991