Token Classification
Transformers
ONNX
Safetensors
English
Japanese
Chinese
bert
anime
filename-parsing
Eval Results (legacy)
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
File size: 42,387 Bytes
33bb11c 7509455 33bb11c 7509455 33bb11c 7509455 33bb11c 7509455 33bb11c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 | """Smoke tests for annotated DMHY graph dataset helpers."""
from __future__ import annotations
import tempfile
import json
import subprocess
import sys
import unittest
from pathlib import Path
from tools.annotate_dmhy_prefix_graph import normalize_generated_tokens
from tools.annotate_dmhy_prefix_tree_agent import (
Args as PrefixTreeAgentArgs,
annotate_terminal_with_llm,
annotate_selected,
enrich_patch,
request_body,
validate_strict_annotation,
)
from tools.convert_annotated_dmhy_dataset import (
iter_validated_jsonl,
validate_record,
)
from tools.convert_to_char_dataset import convert_record
class AnnotatedDmhyWorkflowTests(unittest.TestCase):
def prefix_tree_agent_args(self) -> PrefixTreeAgentArgs:
return PrefixTreeAgentArgs(
graph=Path("graph.json"),
source_list=Path("source.jsonl"),
output=Path("out.jsonl"),
patch_output=Path("patch.jsonl"),
units_output=Path("units.jsonl"),
limit=None,
min_weight=None,
only_needs_review=False,
llm=True,
base_url="http://example.test/v1",
api_key="test-key",
model="gpt-5.4-mini",
max_requests=None,
http_timeout=1,
preserve_i_labels=False,
examples_only=False,
workers=8,
retries=2,
resume=False,
reasoning_effort="medium",
)
def prefix_selected(self, count: int = 3) -> list[tuple[int, dict, dict]]:
selected: list[tuple[int, dict, dict]] = []
for index in range(count):
terminal_id = f"t{index}"
terminal = {
"terminal_id": terminal_id,
"prefix": f"Show {index} - ",
"suffix_examples": [f"0{index} - Title [1080P]"],
"value_examples": [f"Show {index} - 0{index} - Title [1080P].mkv"],
"weight": index + 1,
}
patch = enrich_patch(
{
"terminal_id": terminal_id,
"needs_llm_review": True,
"episode_title_suffixes": [],
"media_suffixes": ["[1080P]"],
"title_candidates": [],
"llm_label": None,
"notes": "heuristic",
"source": "heuristic-v1",
}
)
selected.append((index, terminal, patch))
return selected
def test_prefix_tree_agent_max_requests_does_not_fallback_unprocessed_terminals(self) -> None:
args = self.prefix_tree_agent_args()
args = PrefixTreeAgentArgs(**{**args.__dict__, "max_requests": 1, "workers": 1})
selected = self.prefix_selected(3)
def fake_annotate(_terminal, patch, _args):
merged = dict(patch)
merged.update({"source": "responses-v1", "status": "ok", "fallback": False, "errors": []})
return merged, False
import tools.annotate_dmhy_prefix_tree_agent as agent
original_annotate = agent.annotate_terminal_with_llm
try:
agent.annotate_terminal_with_llm = fake_annotate
completed, resume_skipped, llm_requests, llm_fallbacks, pending_unprocessed = annotate_selected(selected, args)
finally:
agent.annotate_terminal_with_llm = original_annotate
self.assertEqual([patch["terminal_id"] for _ord, _idx, _term, patch in completed], ["t0"])
self.assertEqual(resume_skipped, 0)
self.assertEqual(llm_requests, 1)
self.assertEqual(llm_fallbacks, 0)
self.assertEqual(pending_unprocessed, 2)
self.assertFalse(any(patch["fallback"] for _ord, _idx, _term, patch in completed))
def test_prefix_tree_agent_resume_continues_next_batch(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
patch_path = Path(tmpdir) / "patches.jsonl"
patch_path.write_text(
json.dumps(
{
"terminal_id": "t0",
"unit_id": "t0",
"terminal_ids": ["t0"],
"episode_title_suffixes": ["old"],
"media_suffixes": ["[1080P]"],
"title_candidates": ["old"],
"llm_label": "old",
"notes": "previous",
"source": "responses-v1",
"status": "ok",
"fallback": False,
"errors": [],
},
ensure_ascii=False,
separators=(",", ":"),
)
+ "\n",
encoding="utf-8",
)
args = self.prefix_tree_agent_args()
args = PrefixTreeAgentArgs(
**{**args.__dict__, "patch_output": patch_path, "resume": True, "max_requests": 1, "workers": 1}
)
selected = self.prefix_selected(3)
def fake_annotate(_terminal, patch, _args):
merged = dict(patch)
merged.update(
{
"episode_title_suffixes": [patch["terminal_id"]],
"source": "responses-v1",
"status": "ok",
"fallback": False,
"errors": [],
}
)
return merged, False
import tools.annotate_dmhy_prefix_tree_agent as agent
original_annotate = agent.annotate_terminal_with_llm
try:
agent.annotate_terminal_with_llm = fake_annotate
completed, resume_skipped, llm_requests, _llm_fallbacks, pending_unprocessed = annotate_selected(
selected, args
)
finally:
agent.annotate_terminal_with_llm = original_annotate
patches = [patch for _ord, _idx, _term, patch in completed]
self.assertEqual([patch["terminal_id"] for patch in patches], ["t0", "t1"])
self.assertEqual(patches[0]["notes"], "previous")
self.assertEqual(patches[1]["episode_title_suffixes"], ["t1"])
self.assertEqual(resume_skipped, 1)
self.assertEqual(llm_requests, 1)
self.assertEqual(pending_unprocessed, 1)
def test_prefix_tree_agent_completed_order_is_deterministic(self) -> None:
args = self.prefix_tree_agent_args()
args = PrefixTreeAgentArgs(**{**args.__dict__, "max_requests": 3, "workers": 3})
selected = self.prefix_selected(3)
def fake_annotate(_terminal, patch, _args):
merged = dict(patch)
merged.update({"source": "responses-v1", "status": "ok", "fallback": False, "errors": []})
return merged, False
import tools.annotate_dmhy_prefix_tree_agent as agent
original_annotate = agent.annotate_terminal_with_llm
try:
agent.annotate_terminal_with_llm = fake_annotate
completed, _resume_skipped, _llm_requests, _llm_fallbacks, _pending_unprocessed = annotate_selected(
selected, args
)
finally:
agent.annotate_terminal_with_llm = original_annotate
self.assertEqual([patch["terminal_id"] for _ord, _idx, _term, patch in completed], ["t0", "t1", "t2"])
def test_prefix_tree_agent_writes_llm_failures_without_completing_patches(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
failure_path = Path(tmpdir) / "failures.jsonl"
args = self.prefix_tree_agent_args()
args = PrefixTreeAgentArgs(
**{**args.__dict__, "max_requests": 1, "workers": 1, "failure_output": failure_path}
)
selected = self.prefix_selected(2)
def fake_annotate(_terminal, patch, _args):
failed = dict(patch)
failed.update(
{
"status": "fallback",
"fallback": True,
"errors": ["Responses API HTTP 429: busy"],
}
)
return failed, True
import tools.annotate_dmhy_prefix_tree_agent as agent
original_annotate = agent.annotate_terminal_with_llm
try:
agent.annotate_terminal_with_llm = fake_annotate
completed, _resume_skipped, llm_requests, llm_fallbacks, pending_unprocessed = annotate_selected(
selected, args
)
finally:
agent.annotate_terminal_with_llm = original_annotate
self.assertEqual(completed, [])
self.assertEqual(llm_requests, 1)
self.assertEqual(llm_fallbacks, 1)
self.assertEqual(pending_unprocessed, 2)
failures = [
json.loads(line)
for line in failure_path.read_text(encoding="utf-8").splitlines()
if line.strip()
]
self.assertEqual(len(failures), 1)
self.assertEqual(failures[0]["terminal_id"], "t0")
self.assertEqual(failures[0]["status"], "pending")
self.assertEqual(failures[0]["source"], "responses-v1")
self.assertIn("Responses API HTTP 429", failures[0]["errors"][0])
def test_generated_tokens_split_punctuation_and_use_b_only_labels(self) -> None:
tokens, labels = normalize_generated_tokens(
["[ANi]", " ", "Title-Name", "07"],
["B-GROUP", "O", "I-TITLE", "B-EPISODE"],
)
self.assertEqual(tokens, ["[", "ANi", "]", " ", "Title", "-", "Name", "07"])
self.assertEqual(
labels,
["O", "B-GROUP", "O", "O", "B-TITLE", "O", "B-TITLE", "B-EPISODE"],
)
self.assertTrue(all(label == "O" or label.startswith("B-") for label in labels))
def test_preserve_i_labels_keeps_i_on_non_separator_pieces(self) -> None:
tokens, labels = normalize_generated_tokens(
["Title-Name"],
["I-TITLE"],
preserve_i_labels=True,
)
self.assertEqual(tokens, ["Title", "-", "Name"])
self.assertEqual(labels, ["I-TITLE", "O", "I-TITLE"])
def test_prefix_tree_agent_strict_schema_payload(self) -> None:
args = self.prefix_tree_agent_args()
terminal = {
"terminal_id": "t0",
"prefix": "Show - 01",
"suffix_examples": [" [1080P][WEB-DL]"],
"value_examples": ["Show - 01 [1080P][WEB-DL].mkv"],
}
patch = {
"terminal_id": "t0",
"episode_title_suffixes": [],
"media_suffixes": ["[1080P]", "[WEB-DL]"],
"title_candidates": [],
"notes": "heuristic",
}
body = request_body(args=args, terminal=terminal, patch=patch, previous_error=None, mode="reasoning_json_schema")
self.assertEqual(body["model"], "gpt-5.4-mini")
self.assertEqual(body["reasoning"], {"effort": "medium"})
schema_format = body["text"]["format"]
self.assertEqual(schema_format["type"], "json_schema")
self.assertTrue(schema_format["strict"])
self.assertIn("terminal_id", schema_format["schema"]["required"])
self.assertIn("episode_title_suffixes", schema_format["schema"]["required"])
def test_prefix_tree_agent_reasoning_effort_is_configurable(self) -> None:
args = self.prefix_tree_agent_args()
args = PrefixTreeAgentArgs(**{**args.__dict__, "reasoning_effort": "xhigh"})
body = request_body(
args=args,
terminal={"terminal_id": "t0"},
patch={"terminal_id": "t0", "notes": ""},
previous_error=None,
mode="reasoning_json_schema",
)
self.assertEqual(body["reasoning"], {"effort": "xhigh"})
def test_prefix_tree_agent_terminal_id_mismatch_validation(self) -> None:
with self.assertRaisesRegex(ValueError, "terminal_id mismatch"):
validate_strict_annotation(
{
"terminal_id": "wrong",
"episode_title_suffixes": [],
"media_suffixes": [],
"title_candidates": [],
"llm_label": "uncertain",
"notes": "bad id",
},
expected_terminal_id="expected",
)
def test_prefix_tree_agent_retries_after_terminal_id_mismatch(self) -> None:
args = self.prefix_tree_agent_args()
terminal = {
"terminal_id": "t0",
"prefix": "Show - 01",
"suffix_examples": [" - Finale [1080P]"],
"value_examples": ["Show - 01 - Finale [1080P].mkv"],
}
patch = {
"terminal_id": "t0",
"needs_llm_review": True,
"episode_title_suffixes": [],
"media_suffixes": ["[1080P]"],
"title_candidates": [],
"llm_label": None,
"notes": "heuristic",
"source": "heuristic-v1",
}
calls: list[dict] = []
def fake_post(body, _args):
calls.append(body)
terminal_id = "wrong" if len(calls) == 1 else "t0"
return {
"output_text": json.dumps(
{
"terminal_id": terminal_id,
"episode_title_suffixes": ["Finale"],
"media_suffixes": ["[1080P]"],
"title_candidates": ["Finale"],
"llm_label": "mixed",
"notes": "validated",
}
)
}
import tools.annotate_dmhy_prefix_tree_agent as agent
original_post = agent.post_responses
try:
agent.post_responses = fake_post
merged, used_fallback = annotate_terminal_with_llm(terminal, patch, args)
finally:
agent.post_responses = original_post
self.assertFalse(used_fallback)
self.assertEqual(len(calls), 2)
self.assertIn("Previous attempt failed validation", calls[1]["input"][0]["content"])
self.assertEqual(merged["terminal_id"], "t0")
self.assertEqual(merged["source"], "responses-v1")
self.assertEqual(merged["episode_title_suffixes"], ["Finale"])
def test_prefix_tree_agent_cli_writes_validator_compatible_units(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
tmp = Path(tmpdir)
graph_path = tmp / "graph.json"
dataset_path = tmp / "dmhy_weak.prefix_sample.jsonl"
patch_path = tmp / "patches.jsonl"
units_path = tmp / "units.jsonl"
graph_path.write_text(
json.dumps(
{
"terminals": [
{
"terminal_id": "t0",
"node_id": 42,
"prefix": "[ANi] Prefix Show - ",
"digit_skeleton": "[ANi] Prefix Show - ##",
"weight": 7,
"value_examples": [
"[ANi] Prefix Show - 01 [1080P][WEB-DL].mkv"
],
"suffix_examples": ["01 [1080P][WEB-DL]"],
}
]
},
ensure_ascii=False,
),
encoding="utf-8",
)
annotate = subprocess.run(
[
sys.executable,
"-m",
"tools.annotate_dmhy_prefix_tree_agent",
"--graph",
str(graph_path),
"--output",
str(dataset_path),
"--patch-output",
str(patch_path),
"--units-output",
str(units_path),
"--examples-only",
"--workers",
"1",
],
check=False,
capture_output=True,
text=True,
)
self.assertEqual(annotate.returncode, 0, annotate.stderr)
units = [
json.loads(line)
for line in units_path.read_text(encoding="utf-8").splitlines()
if line.strip()
]
self.assertEqual(len(units), 1)
self.assertEqual(
set(units[0]),
{
"unit_id",
"source_kind",
"source_id",
"terminal_ids",
"weight",
"context",
"examples",
"expected_output",
},
)
self.assertEqual(units[0]["unit_id"], "t0")
self.assertEqual(units[0]["source_kind"], "prefix_tree")
self.assertEqual(units[0]["source_id"], 42)
self.assertEqual(units[0]["terminal_ids"], ["t0"])
self.assertEqual(units[0]["weight"], 7)
self.assertEqual(units[0]["context"]["prefixes"], ["[ANi] Prefix Show - "])
self.assertEqual(units[0]["context"]["digit_skeletons"], ["[ANi] Prefix Show - ##"])
self.assertEqual(units[0]["context"]["edge_labels"], [])
self.assertEqual(
units[0]["examples"]["values"],
["[ANi] Prefix Show - 01 [1080P][WEB-DL].mkv"],
)
self.assertEqual(units[0]["examples"]["suffixes"], ["01 [1080P][WEB-DL]"])
self.assertEqual(units[0]["expected_output"], {"schema_version": "dmhy-annotation-v1"})
patches = [
json.loads(line)
for line in patch_path.read_text(encoding="utf-8").splitlines()
if line.strip()
]
self.assertEqual(patches[0]["unit_id"], "t0")
self.assertEqual(patches[0]["terminal_ids"], ["t0"])
self.assertEqual(patches[0]["status"], "ok")
self.assertEqual(patches[0]["errors"], [])
self.assertFalse(patches[0]["fallback"])
validate_units = subprocess.run(
[
sys.executable,
"-m",
"tools.validate_dmhy_annotation_pipeline",
"validate-units",
str(units_path),
],
check=False,
capture_output=True,
text=True,
)
self.assertEqual(validate_units.returncode, 0, validate_units.stdout + validate_units.stderr)
validate_patches = subprocess.run(
[
sys.executable,
"-m",
"tools.validate_dmhy_annotation_pipeline",
"validate-patches",
str(patch_path),
"--units",
str(units_path),
],
check=False,
capture_output=True,
text=True,
)
self.assertEqual(validate_patches.returncode, 0, validate_patches.stdout + validate_patches.stderr)
def test_validation_rejects_embedded_punctuation(self) -> None:
record = {
"filename": "Title-Name 07",
"tokens": ["Title-Name", "07"],
"labels": ["B-TITLE", "B-EPISODE"],
}
with self.assertRaisesRegex(ValueError, "contains punctuation"):
validate_record(record, Path("sample.jsonl"), 1)
def test_validation_rejects_embedded_symbol_separator(self) -> None:
record = {
"filename": "Title 1920×1080 07",
"tokens": ["Title", "1920×1080", "07"],
"labels": ["B-TITLE", "B-RESOLUTION", "B-EPISODE"],
}
with self.assertRaisesRegex(ValueError, "contains punctuation"):
validate_record(record, Path("sample.jsonl"), 1)
def test_b_only_input_converts_to_char_i_labels(self) -> None:
record = {
"filename": "Title-Name 07",
"tokens": ["Title", "-", "Name", " ", "07"],
"labels": ["B-TITLE", "O", "B-TITLE", "O", "B-EPISODE"],
}
validate_record(record, Path("sample.jsonl"), 1)
converted = convert_record(record)
self.assertIn("I-TITLE", converted["labels"])
self.assertEqual(converted["tokens"][:5], ["T", "i", "t", "l", "e"])
def test_iter_validated_jsonl_accepts_generated_shape(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
path = Path(tmpdir) / "records.jsonl"
path.write_text(
'{"filename":"A 01","tokens":["A"," ","01"],"labels":["B-TITLE","O","B-EPISODE"]}\n',
encoding="utf-8",
)
rows = list(iter_validated_jsonl(path))
self.assertEqual(len(rows), 1)
self.assertEqual(rows[0]["filename"], "A 01")
def test_cli_smoke_annotate_then_convert_with_temp_files(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
tmp = Path(tmpdir)
graph_path = tmp / "graph.json"
dataset_path = tmp / "dmhy_weak.generated.jsonl"
char_path = tmp / "dmhy_weak.generated_char.jsonl"
vocab_path = tmp / "vocab.generated.char.json"
manifest_path = tmp / "manifest.json"
graph_path.write_text(
json.dumps(
{
"terminals": [
{
"terminal_id": "t0",
"weight": 1,
"value_examples": [
"[ANi] Test Show - 01 [1080P][WEB-DL].mkv"
],
"suffix_examples": [" [1080P][WEB-DL]"],
}
]
},
ensure_ascii=False,
),
encoding="utf-8",
)
annotate = subprocess.run(
[
sys.executable,
"-m",
"tools.annotate_dmhy_prefix_graph",
"--graph",
str(graph_path),
"--output",
str(dataset_path),
"--patch-output",
"",
"--examples-only",
],
check=False,
capture_output=True,
text=True,
)
self.assertEqual(annotate.returncode, 0, annotate.stderr)
rows = [
json.loads(line)
for line in dataset_path.read_text(encoding="utf-8").splitlines()
if line.strip()
]
self.assertEqual(len(rows), 1)
self.assertIn("annotations", rows[0])
self.assertEqual(rows[0]["tokens"][0], "[")
self.assertEqual(rows[0]["labels"][0], "O")
convert = subprocess.run(
[
sys.executable,
"-m",
"tools.convert_annotated_dmhy_dataset",
"--input",
str(dataset_path),
"--output",
str(char_path),
"--vocab-output",
str(vocab_path),
"--manifest-output",
str(manifest_path),
"--progress",
"0",
],
check=False,
capture_output=True,
text=True,
)
self.assertEqual(convert.returncode, 0, convert.stderr)
self.assertTrue(char_path.exists())
self.assertTrue(vocab_path.exists())
self.assertTrue(manifest_path.exists())
def test_cli_source_list_mode_expands_beyond_value_examples(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
tmp = Path(tmpdir)
graph_path = tmp / "graph.json"
source_path = tmp / "dmhy_list.jsonl"
dataset_path = tmp / "dmhy_weak.generated.jsonl"
graph_path.write_text(
json.dumps(
{
"terminals": [
{
"terminal_id": "t0",
"prefix": "[ANi] Full Show - ",
"weight": 10,
"value_examples": [
"[ANi] Full Show - 01 [1080P][WEB-DL].mkv"
],
"suffix_examples": ["01 [1080P][WEB-DL]"],
},
{
"terminal_id": "t1",
"prefix": "[ANi] Other Show - ",
"weight": 10,
"value_examples": [
"[ANi] Other Show - 01 [1080P][WEB-DL].mkv"
],
"suffix_examples": ["01 [1080P][WEB-DL]"],
},
]
},
ensure_ascii=False,
),
encoding="utf-8",
)
source_path.write_text(
"\n".join(
json.dumps({"value": value}, ensure_ascii=False)
for value in [
"[ANi] Full Show - 01 [1080P][WEB-DL].mkv",
"[ANi] Full Show - 02 [1080P][WEB-DL].mkv",
"[ANi] Full Show - 03 [1080P][WEB-DL].mkv",
"[ANi] Other Show - 01 [1080P][WEB-DL].mkv",
]
)
+ "\n",
encoding="utf-8",
)
annotate = subprocess.run(
[
sys.executable,
"-m",
"tools.annotate_dmhy_prefix_graph",
"--graph",
str(graph_path),
"--source-list",
str(source_path),
"--output",
str(dataset_path),
"--patch-output",
"",
"--limit",
"1",
],
check=False,
capture_output=True,
text=True,
)
self.assertEqual(annotate.returncode, 0, annotate.stderr)
rows = [
json.loads(line)
for line in dataset_path.read_text(encoding="utf-8").splitlines()
if line.strip()
]
self.assertEqual(len(rows), 3)
self.assertEqual([row["filename"] for row in rows], [
"[ANi] Full Show - 01 [1080P][WEB-DL].mkv",
"[ANi] Full Show - 02 [1080P][WEB-DL].mkv",
"[ANi] Full Show - 03 [1080P][WEB-DL].mkv",
])
self.assertTrue(all(row["terminal_id"] == "t0" for row in rows))
def test_cli_examples_only_uses_terminal_value_examples(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
tmp = Path(tmpdir)
graph_path = tmp / "graph.json"
source_path = tmp / "dmhy_list.jsonl"
dataset_path = tmp / "dmhy_weak.generated.jsonl"
graph_path.write_text(
json.dumps(
{
"terminals": [
{
"terminal_id": "t0",
"prefix": "[ANi] Example Show - ",
"weight": 10,
"value_examples": [
"[ANi] Example Show - 01 [1080P][WEB-DL].mkv"
],
"suffix_examples": ["01 [1080P][WEB-DL]"],
}
]
},
ensure_ascii=False,
),
encoding="utf-8",
)
source_path.write_text(
"\n".join(
json.dumps({"value": value}, ensure_ascii=False)
for value in [
"[ANi] Example Show - 01 [1080P][WEB-DL].mkv",
"[ANi] Example Show - 02 [1080P][WEB-DL].mkv",
]
)
+ "\n",
encoding="utf-8",
)
annotate = subprocess.run(
[
sys.executable,
"-m",
"tools.annotate_dmhy_prefix_graph",
"--graph",
str(graph_path),
"--source-list",
str(source_path),
"--output",
str(dataset_path),
"--patch-output",
"",
"--examples-only",
],
check=False,
capture_output=True,
text=True,
)
self.assertEqual(annotate.returncode, 0, annotate.stderr)
rows = [
json.loads(line)
for line in dataset_path.read_text(encoding="utf-8").splitlines()
if line.strip()
]
self.assertEqual(len(rows), 1)
self.assertEqual(rows[0]["filename"], "[ANi] Example Show - 01 [1080P][WEB-DL].mkv")
def test_cli_dag_annotation_units_include_shared_node_terminals(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
tmp = Path(tmpdir)
dag_path = tmp / "dmhy_prefix_dag.json"
output_path = tmp / "dmhy_prefix_dag.annotation_units.jsonl"
dag_path.write_text(
json.dumps(
{
"meta": {"version": "prefix-dag-v1"},
"root": 0,
"nodes": [
{
"id": 0,
"terminal": False,
"children": [
{"label": "A", "target": 1},
{"label": "B", "target": 2},
],
"incoming_count": 0,
"reachable_terminals": 2,
"reachable_weight": 20,
},
{
"id": 1,
"terminal": False,
"children": [{"label": " shared", "target": 3}],
"incoming_count": 1,
"reachable_terminals": 1,
"reachable_weight": 10,
},
{
"id": 2,
"terminal": False,
"children": [{"label": " shared", "target": 3}],
"incoming_count": 1,
"reachable_terminals": 1,
"reachable_weight": 10,
},
{
"id": 3,
"terminal": False,
"children": [
{"label": " 01", "target": 4},
{"label": " 02", "target": 5},
],
"incoming_count": 2,
"reachable_terminals": 2,
"reachable_weight": 20,
},
{
"id": 4,
"terminal": True,
"children": [],
"incoming_count": 1,
"reachable_terminals": 1,
"reachable_weight": 10,
},
{
"id": 5,
"terminal": True,
"children": [],
"incoming_count": 1,
"reachable_terminals": 1,
"reachable_weight": 10,
},
],
"terminals": [
{
"terminal_id": "t0",
"node_id": 4,
"prefix": "Show A shared 01",
"digit_skeleton": "Show A shared <NUM>",
"count": 10,
"weight": 10,
"suffix_examples": [" [1080P][WEB-DL]"],
"value_examples": ["Show A shared 01 [1080P][WEB-DL].mkv"],
"annotations": {},
},
{
"terminal_id": "t1",
"node_id": 5,
"prefix": "Show B shared 02",
"digit_skeleton": "Show B shared <NUM>",
"count": 10,
"weight": 10,
"suffix_examples": [" [1080P][WEB-DL]"],
"value_examples": ["Show B shared 02 [1080P][WEB-DL].mkv"],
"annotations": {},
},
],
},
ensure_ascii=False,
),
encoding="utf-8",
)
annotate = subprocess.run(
[
sys.executable,
"-m",
"tools.annotate_dmhy_prefix_dag",
"--dag",
str(dag_path),
"--output",
str(output_path),
"--min-reachable-terminals",
"2",
"--min-incoming-count",
"2",
"--limit",
"1",
],
check=False,
capture_output=True,
text=True,
)
self.assertEqual(annotate.returncode, 0, annotate.stderr)
rows = [
json.loads(line)
for line in output_path.read_text(encoding="utf-8").splitlines()
if line.strip()
]
self.assertEqual(len(rows), 1)
self.assertEqual(rows[0]["unit_id"], "dag-node-3")
self.assertEqual(rows[0]["kind"], "shared_suffix")
self.assertEqual(rows[0]["terminal_ids"], ["t0", "t1"])
self.assertEqual(
rows[0]["prefix_examples"],
["Show A shared 01", "Show B shared 02"],
)
self.assertEqual(rows[0]["common_edge_labels"], [" 01", " 02"])
self.assertIn("annotations", rows[0])
def test_cli_dag_annotation_units_skip_terminal_sink_by_default(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
tmp = Path(tmpdir)
dag_path = tmp / "dmhy_prefix_dag.json"
output_path = tmp / "dmhy_prefix_dag.annotation_units.jsonl"
dag_path.write_text(
json.dumps(
{
"meta": {"version": "prefix-dag-v1"},
"root": 0,
"nodes": [
{
"id": 0,
"terminal": False,
"children": [{"label": "shared", "target": 2}],
"incoming_count": 0,
"reachable_terminals": 1,
"reachable_weight": 12908,
},
{
"id": 2,
"terminal": True,
"children": [],
"incoming_count": 12908,
"reachable_terminals": 1,
"reachable_weight": 12908,
},
],
"terminals": [
{
"terminal_id": "t-sink",
"node_id": 2,
"prefix": "Shared Sink",
"digit_skeleton": "Shared Sink",
"count": 12908,
"weight": 12908,
"suffix_examples": [],
"value_examples": ["Shared Sink.mkv"],
"annotations": {},
}
],
},
ensure_ascii=False,
),
encoding="utf-8",
)
annotate = subprocess.run(
[
sys.executable,
"-m",
"tools.annotate_dmhy_prefix_dag",
"--dag",
str(dag_path),
"--output",
str(output_path),
"--min-reachable-terminals",
"1",
"--min-incoming-count",
"2",
],
check=False,
capture_output=True,
text=True,
)
self.assertEqual(annotate.returncode, 0, annotate.stderr)
rows = [
json.loads(line)
for line in output_path.read_text(encoding="utf-8").splitlines()
if line.strip()
]
self.assertEqual(rows, [])
def test_cli_dag_annotation_units_include_terminal_sink_when_requested(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
tmp = Path(tmpdir)
dag_path = tmp / "dmhy_prefix_dag.json"
output_path = tmp / "dmhy_prefix_dag.annotation_units.jsonl"
dag_path.write_text(
json.dumps(
{
"meta": {"version": "prefix-dag-v1"},
"root": 0,
"nodes": [
{
"id": 0,
"terminal": False,
"children": [{"label": "shared", "target": 2}],
"incoming_count": 0,
"reachable_terminals": 1,
"reachable_weight": 12908,
},
{
"id": 2,
"terminal": True,
"children": [],
"incoming_count": 12908,
"reachable_terminals": 1,
"reachable_weight": 12908,
},
],
"terminals": [
{
"terminal_id": "t-sink",
"node_id": 2,
"prefix": "Shared Sink",
"digit_skeleton": "Shared Sink",
"count": 12908,
"weight": 12908,
"suffix_examples": [],
"value_examples": ["Shared Sink.mkv"],
"annotations": {},
}
],
},
ensure_ascii=False,
),
encoding="utf-8",
)
annotate = subprocess.run(
[
sys.executable,
"-m",
"tools.annotate_dmhy_prefix_dag",
"--dag",
str(dag_path),
"--output",
str(output_path),
"--min-reachable-terminals",
"1",
"--min-incoming-count",
"2",
"--include-terminal-sink-units",
],
check=False,
capture_output=True,
text=True,
)
self.assertEqual(annotate.returncode, 0, annotate.stderr)
rows = [
json.loads(line)
for line in output_path.read_text(encoding="utf-8").splitlines()
if line.strip()
]
self.assertEqual(len(rows), 1)
self.assertEqual(rows[0]["unit_id"], "dag-node-2")
self.assertEqual(rows[0]["kind"], "shared_suffix")
self.assertEqual(rows[0]["terminal_ids"], ["t-sink"])
self.assertEqual(rows[0]["common_edge_labels"], [])
if __name__ == "__main__":
unittest.main()
|