Token Classification
Transformers
ONNX
Safetensors
English
Japanese
Chinese
bert
anime
filename-parsing
Eval Results (legacy)
Run this model with HF Jobs
Run ModerRAS/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/ModerRAS/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/ModerRAS/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 ModerRAS/AniFileBERT with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ModerRAS/AniFileBERT with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("token-classification", model="ModerRAS/AniFileBERT")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForTokenClassification tokenizer = AutoTokenizer.from_pretrained("ModerRAS/AniFileBERT") model = AutoModelForTokenClassification.from_pretrained("ModerRAS/AniFileBERT", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Add parser diagnostics and inference debugging
Browse files- add diagnose_pipeline.py for BIO validation, tokenizer alignment, entity stats, truncation, UNK, and confusion analysis
- add diagnostics reports for char and regex DMHY datasets
- add inference debug output, constrained BIO decoding, checkpoint max-length handling, and rule-assisted parsing
Files changed (4)
- diagnose_pipeline.pyAdded+709
- diagnostics_report.mdAdded+277
- diagnostics_report_word.mdAdded+2678
- inference.py+355-35
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709"""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 align_tokens_for_tokenizerfrom 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": if previous_label.startswith("B-"): violations.append( { "type": "B_DIRECT_TO_O", "index": idx, "prev_label": previous_label, "label": label, "token": token, } ) 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 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 = align_tokens_for_tokenizer(sample["tokens"], sample["labels"], 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( tokens: List[str], labels: List[str], tokenizer: AnimeTokenizer, label2id: Dict[str, int], max_length: int,) -> Tuple[List[int], List[int], List[int], List[str]]: tokens, labels = align_tokens_for_tokenizer(tokens, labels, 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 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()
with torch.no_grad(): for sample in eval_samples: input_ids, attention_mask, label_ids, _tokens = prepare_inputs( sample["tokens"], sample["labels"], 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 pred_ids = torch.argmax(logits, dim=-1)[0].detach().cpu().tolist()
true_labels: List[str] = [] pred_labels: List[str] = [] for pred_id, label_id in zip(pred_ids, label_ids): if label_id == -100: continue 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)
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, }
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] = [] 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 = align_tokens_for_tokenizer(tokens, labels, 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 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) sections.append( ( "BIO Violations And Boundary Drift", "\n".join( [ "### Violation counts", format_counter(violation_counter), "", "### Boundary drift heuristics", format_counter(boundary_drift_counter), "", "### Sample violations", markdown_json(violations[: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"] ] 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", "", "### 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.13 unmodified lines141516171718192049 unmodified lines7071727374757677787980818283848586787980878889909192939495828396979899100101102858687888990919293103104959610510610710810911011111211310211410410511511611711811912012112211212311412412512611712712811912913013113213312413413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721835 unmodified lines25425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943017 unmodified lines4484494504514524534544551 unmodified line4574584592052064604614624634646 unmodified lines4714724732192202212224744754764 unmodified lines4814824832334844854864874884894904914924934944954964974984995002375015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415427 unmodified lines5505515525535545555565575585595605618 unmodified lines57057157226557357457557657757857914 unmodified lines59459559628659759859960060160260360460560660760860913 unmodified linesimport osimport reimport sysfrom typing import Dict, List, Optionalfrom typing import Dict, List, Optional, Tuple
import torchfrom transformers import BertForTokenClassification49 unmodified lines 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 postprocess(tokens: List[str], labels: List[str]) -> Dict: """ Convert BIO-labeled tokens into structured metadata.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
Merges consecutive B- / I- tokens of the same entity type, then extracts structured fields.
def labels_to_entities( tokens: List[str], labels: List[str], tokenizer: Optional[AnimeTokenizer] = None,) -> List[Tuple[str, str]]: """ result: Dict = { "title": None, "season": None, "episode": None, "group": None, "resolution": None, "source": None, "special": None, } Convert BIO labels into entity spans.
# Merge consecutive B- / I- tokens into entities entities: List[tuple] = [] 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-"): # Finalize previous entity if current_entity: entities.append((current_entity, "".join(current_tokens))) current_entity = label[2:] # Remove "B-" 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: # Orphaned I- — start new entity if current_entity: entities.append((current_entity, "".join(current_tokens))) entities.append((current_entity, join_entity_tokens(current_tokens, tokenizer))) current_entity = entity_type current_tokens = [token] else: # O else: if current_entity: entities.append((current_entity, "".join(current_tokens))) entities.append((current_entity, join_entity_tokens(current_tokens, tokenizer))) current_entity = None current_tokens = []
if current_entity: entities.append((current_entity, "".join(current_tokens))) 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:35 unmodified lines 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"\b(?:\d{3,4}[pP]|\d[Kk]|\d{3,4}[xX×]\d{3,4})\b")SOURCE_RE = re.compile( r"\b(?:WEB[-_ ]?DL|WEB[-_ ]?Rip|BDRip|BluRay|BDMV|DVDRip|DVD|TVRip|HDTV|" r"Netflix|NF|AMZN|Baha|CR|ABEMA|DSNP|U[-_ ]?NEXT|Hulu|AT[-_ ]?X)\b", re.I,)EPISODE_PATTERNS = [ re.compile(r"(?:^|[\s._\-\[\(【《#])(?:EP?|第)?(?P<ep>\d{1,4})(?:v\d+)?(?:[话話集])?(?=$|[\s._\-\]\)】》])", re.I), re.compile(r"[Ss]\d{1,2}[Ee](?P<ep>\d{1,4})(?:v\d+)?", 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)NOISE_META_RE = re.compile( r"^(?:\d{3,4}[pP]|\d[Kk]|WEB[-_ ]?DL|WEB[-_ ]?Rip|BDRip|BluRay|BDMV|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,)
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 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_group(first_text): repaired["group"] = first_text
if not repaired.get("resolution"): match = RESOLUTION_RE.search(filename) if match: repaired["resolution"] = match.group(0)
if not repaired.get("source"): match = SOURCE_RE.search(filename) if match: repaired["source"] = match.group(0).replace("_", "-")
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("episode") is None: candidates: List[Tuple[int, int, str]] = [] for 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 score = match.start() if 1 <= ep <= 200: score += 10000 if "-" in filename[max(0, match.start() - 3):match.start() + 1]: score += 1000 if match.start() > len(filename) // 3: score += 200 candidates.append((score, ep, ep_text)) if candidates: repaired["episode"] = max(candidates, key=lambda item: item[0])[1]
title = repaired.get("title") group = repaired.get("group") if title and group and title.startswith(group): title = title[len(group):].lstrip("]】)>})》 \t-_.") repaired["title"] = title or repaired["title"]
if (not repaired.get("title") or (group and repaired["title"].startswith(group))) and repaired.get("episode"): repaired_title = infer_title_span(filename, group, repaired["episode"]) if repaired_title: repaired["title"] = repaired_title
return repaired
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()
end = None if episode is not None: ep_patterns = [ rf"\s[-_]\s*0*{episode}(?:v\d+)?(?=$|[\s\[\(【《._-])", rf"[\[\(【《]0*{episode}(?:v\d+)?[\]\)】》]", 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 = filename[start:end].strip(" \t-_.[]()【】《》()") 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.17 unmodified lines
# Convert to input IDs input_ids = tokenizer.convert_tokens_to_ids(tokens) 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]1 unmodified line
# Truncate if needed if len(input_ids) > max_length: input_ids = input_ids[:max_length] attention_mask = attention_mask[: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)6 unmodified lines input_tensor = torch.tensor([input_ids], device=device) mask_tensor = torch.tensor([attention_mask], device=device)
with torch.no_grad(): logits = model(input_ids=input_tensor, attention_mask=mask_tensor).logits predictions = torch.argmax(logits, dim=-1)[0]
# Remove special token predictions # Count real tokens used (minus CLS/SEP) real_token_count = len(tokens)4 unmodified lines "group": None, "resolution": None, "source": None, "special": None}
pred_labels = predictions[1:1 + available].tolist() 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 return postprocess(tokens[:available], label_strings) 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], "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():7 unmodified lines 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 config8 unmodified lines model = BertForTokenClassification.from_pretrained(args.model_dir) model.eval()
id2label = cfg.id2label 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] = []14 unmodified lines for fn in filenames_to_parse: if not fn.strip(): continue result = parse_filename(fn, model, tokenizer, id2label, args.max_length) 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)
diagnose_pipeline.pyAdded+709
diagnostics_report.mdAdded+277
diagnostics_report_word.mdAdded+2678
inference.py+355-35