eval_dataset.py 41.1 KB
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 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
"""Generate production-style labeled evaluation samples from a lyric library."""

from __future__ import annotations

import csv
import hashlib
import json
import random
import re
import sys
from collections import Counter
from dataclasses import dataclass
from pathlib import Path

from lyric_dedup.checker import LyricRecord
from lyric_dedup.file_import import iter_lyric_files
from lyric_dedup.file_import import record_from_file
from lyric_dedup.normalization import NormalizedLyrics
from lyric_dedup.normalization import fingerprint_text
from lyric_dedup.normalization import normalize_lyrics


STANDARD_SAMPLE_MIX = {
    "positive_full_duplicate": 0.30,
    "negative_real_holdout_full_song": 0.40,
    "negative_fragment": 0.10,
    "negative_shared_chorus": 0.05,
    "negative_translation_only": 0.05,
    "negative_same_theme_synthetic": 0.05,
    "edge_short_or_placeholder": 0.05,
}
DEFAULT_SAMPLE_MIX = STANDARD_SAMPLE_MIX

HARD_SAMPLE_MIX = {
    "positive_realistic_variant": 0.30,
    "negative_real_holdout_full_song": 0.20,
    "negative_near_neighbor_holdout_full_song": 0.20,
    "negative_long_fragment": 0.15,
    "negative_shared_chorus": 0.05,
    "negative_translation_only": 0.04,
    "negative_catalog_mashup": 0.04,
    "edge_short_or_placeholder": 0.02,
}


def _progress(message: str) -> None:
    print(f"[eval-gen] {message}", file=sys.stderr, flush=True)


def _progress_count(label: str, current: int, total: int, *, step: int = 1000) -> None:
    if total <= 0:
        return
    if current == 1 or current == total or current % step == 0:
        _progress(f"{label}: {current}/{total}")


@dataclass(frozen=True)
class LyricProfile:
    path: Path
    record_id: str
    raw_text: str
    title: str
    artist: str
    normalized: NormalizedLyrics
    line_count: int
    char_count: int
    line_count_bucket: str
    language_bucket: str
    source_bucket: str
    normalized_hash: str
    has_translation: bool


@dataclass(frozen=True)
class GeneratedSample:
    sample_id: str
    file: str
    expected: str
    sample_type: str
    source: str
    source_record_id: str = ""
    candidate_record_id: str = ""
    line_count_bucket: str = ""
    language_bucket: str = ""
    source_bucket: str = ""
    title: str = ""
    artist: str = ""
    notes: str = ""


def generate_eval_set(
    *,
    library_dir: Path,
    output_dir: Path,
    csv_path: Path,
    size: int = 100,
    positive_ratio: float = 0.30,
    seed: int = 20260602,
    index_path: Path | None = None,
    eval_index_path: Path | None = None,
    profile: str = "standard",
) -> dict[str, object]:
    """Generate a stratified production evaluation set.

    ``positive_ratio`` is kept for CLI compatibility. It overrides the default
    positive quota while keeping the remaining negative categories proportional.
    """
    if size <= 0:
        raise ValueError("size must be positive")

    if profile not in {"standard", "hard"}:
        raise ValueError("profile must be 'standard' or 'hard'")

    _progress(f"start generation: profile={profile}, size={size}, positive_ratio={positive_ratio}, seed={seed}")
    rng = random.Random(seed)
    profiles = profile_library(library_dir)
    if not profiles:
        raise ValueError(f"{library_dir} 下没有 .lrc/.txt 歌词文件")

    output_dir.mkdir(parents=True, exist_ok=True)
    csv_path.parent.mkdir(parents=True, exist_ok=True)
    _progress(f"clean output dir: {output_dir}")
    _clean_generated_output_dir(output_dir)

    plan = _sample_plan(size, positive_ratio=positive_ratio, profile=profile)
    _progress(f"sample plan: {plan}")
    holdout_count = min(_holdout_plan_count(plan), max(1, len(profiles) // 2))
    holdout_profiles = _stratified_unique_sample(
        profiles,
        holdout_count,
        rng,
    )
    holdout_ids = {profile.record_id for profile in holdout_profiles}
    indexed_profiles = [profile for profile in profiles if profile.record_id not in holdout_ids] or profiles
    groups = _profile_groups(indexed_profiles)
    samples: list[GeneratedSample] = []

    if profile == "hard":
        samples.extend(
            _build_hard_samples(
                plan,
                groups=groups,
                holdout_profiles=holdout_profiles,
                indexed_profiles=indexed_profiles,
                output_dir=output_dir,
                csv_base=csv_path.parent,
                rng=rng,
                start_index=len(samples) + 1,
            )
        )
    else:
        _progress("build positive_full_duplicate samples")
        samples.extend(
            _build_positive_samples(
                _stratified_sample(groups["normal"], plan["positive_full_duplicate"], rng),
                output_dir,
                csv_path.parent,
                rng,
                start_index=len(samples) + 1,
            )
        )
        _progress(f"built samples: {len(samples)}/{size}")
        _progress("build negative_real_holdout_full_song samples")
        samples.extend(
            _build_holdout_full_song_samples(
                holdout_profiles[: plan["negative_real_holdout_full_song"]],
                output_dir,
                csv_path.parent,
                start_index=len(samples) + 1,
            )
        )
        _progress(f"built samples: {len(samples)}/{size}")
        _progress("build negative_fragment samples")
        samples.extend(
            _build_fragment_samples(
                _stratified_sample(groups["fragmentable"], plan["negative_fragment"], rng),
                output_dir,
                csv_path.parent,
                rng,
                start_index=len(samples) + 1,
            )
        )
        _progress(f"built samples: {len(samples)}/{size}")
        _progress("build negative_shared_chorus samples")
        samples.extend(
            _build_shared_chorus_samples(
                _stratified_sample(groups["normal"], plan["negative_shared_chorus"], rng),
                output_dir,
                csv_path.parent,
                rng,
                start_index=len(samples) + 1,
            )
        )
        _progress(f"built samples: {len(samples)}/{size}")
        _progress("build negative_translation_only samples")
        samples.extend(
            _build_translation_only_samples(
                _stratified_sample(groups["foreign"], plan["negative_translation_only"], rng),
                output_dir,
                csv_path.parent,
                rng,
                start_index=len(samples) + 1,
            )
        )
        _progress(f"built samples: {len(samples)}/{size}")
        _progress("build negative_same_theme_synthetic samples")
        samples.extend(
            _build_same_theme_synthetic_samples(
                plan["negative_same_theme_synthetic"],
                output_dir,
                csv_path.parent,
                rng,
                start_index=len(samples) + 1,
            )
        )
        _progress(f"built samples: {len(samples)}/{size}")
        _progress("build edge_short_or_placeholder samples")
        samples.extend(
            _build_edge_samples(
                _stratified_sample(groups["edge"], plan["edge_short_or_placeholder"], rng),
                output_dir,
                csv_path.parent,
                rng,
                start_index=len(samples) + 1,
            )
        )
    _progress(f"built samples: {len(samples)}/{size}")

    if len(samples) < size:
        _progress(f"top up with negative_same_theme_synthetic samples: {size - len(samples)}")
        samples.extend(
            _build_same_theme_synthetic_samples(
                size - len(samples),
                output_dir,
                csv_path.parent,
                rng,
                start_index=len(samples) + 1,
            )
        )
    samples = samples[:size]
    rng.shuffle(samples)

    _progress(f"write csv: {csv_path}")
    _write_csv(samples, csv_path, seed=seed)
    _progress("write manifest")
    manifest = _write_manifest(
        profiles=profiles,
        samples=samples,
        csv_path=csv_path,
        output_dir=output_dir,
        seed=seed,
        plan=plan,
        index_path=index_path,
        eval_index_path=eval_index_path,
        holdout_count=len(holdout_profiles),
        profile=profile,
    )
    _progress("generation complete")
    return manifest


def profile_library(library_dir: Path) -> list[LyricProfile]:
    profiles: list[LyricProfile] = []
    paths = iter_lyric_files(library_dir)
    _progress(f"profile library: 0/{len(paths)}")
    for index, path in enumerate(paths, start=1):
        record = record_from_file(path, base_dir=library_dir)
        raw_text = record.lyrics
        normalized = normalize_lyrics(raw_text)
        lines = normalized.primary_lines or normalized.unique_lines
        line_count = len(lines)
        normalized_text = fingerprint_text(normalized) or normalized.normalized_full_text
        source_bucket = _source_bucket(path)
        profiles.append(
            LyricProfile(
                path=path,
                record_id=record.record_id,
                raw_text=raw_text,
                title=record.title or "",
                artist=record.artist or "",
                normalized=normalized,
                line_count=line_count,
                char_count=len(normalized_text),
                line_count_bucket=_line_count_bucket(line_count),
                language_bucket=_language_bucket(lines),
                source_bucket=source_bucket,
                normalized_hash=hashlib.sha256(normalized_text.encode("utf-8")).hexdigest(),
                has_translation=bool(normalized.translation_lines),
            )
        )
        _progress_count("profile library", index, len(paths), step=5000)
    return profiles


def _sample_plan(size: int, *, positive_ratio: float, profile: str) -> dict[str, int]:
    positive_ratio = max(0.0, min(1.0, positive_ratio))
    mix = dict(HARD_SAMPLE_MIX if profile == "hard" else STANDARD_SAMPLE_MIX)
    positive_key = "positive_realistic_variant" if profile == "hard" else "positive_full_duplicate"
    negative_total = sum(value for key, value in mix.items() if key != positive_key)
    mix[positive_key] = positive_ratio
    for key in list(mix):
        if key != positive_key:
            base_mix = HARD_SAMPLE_MIX if profile == "hard" else STANDARD_SAMPLE_MIX
            mix[key] = (1.0 - positive_ratio) * (base_mix[key] / negative_total)

    plan = {key: int(size * value) for key, value in mix.items()}
    remainder = size - sum(plan.values())
    for key in sorted(mix, key=mix.get, reverse=True):
        if remainder <= 0:
            break
        plan[key] += 1
        remainder -= 1
    return plan


def _holdout_plan_count(plan: dict[str, int]) -> int:
    return plan.get("negative_real_holdout_full_song", 0) + plan.get("negative_near_neighbor_holdout_full_song", 0)


def _profile_groups(profiles: list[LyricProfile]) -> dict[str, list[LyricProfile]]:
    normal = [profile for profile in profiles if profile.line_count >= 6]
    edge = [profile for profile in profiles if profile.line_count <= 5]
    return {
        "normal": normal or profiles,
        "fragmentable": [profile for profile in profiles if profile.line_count >= 12] or normal or profiles,
        "foreign": [
            profile
            for profile in profiles
            if profile.language_bucket in {"latin", "mixed", "jp_kr"} and profile.line_count >= 4
        ]
        or normal
        or profiles,
        "edge": edge or normal or profiles,
    }


def _stratified_sample(profiles: list[LyricProfile], count: int, rng: random.Random) -> list[LyricProfile]:
    if count <= 0 or not profiles:
        return []
    buckets: dict[tuple[str, str, str], list[LyricProfile]] = {}
    for profile in profiles:
        key = (profile.line_count_bucket, profile.language_bucket, profile.source_bucket)
        buckets.setdefault(key, []).append(profile)

    selected: list[LyricProfile] = []
    bucket_keys = list(buckets)
    rng.shuffle(bucket_keys)
    cursors = {key: rng.sample(items, len(items)) for key, items in buckets.items()}
    while len(selected) < count and bucket_keys:
        progressed = False
        for key in list(bucket_keys):
            if len(selected) >= count:
                break
            items = cursors[key]
            if not items:
                bucket_keys.remove(key)
                continue
            selected.append(items.pop())
            progressed = True
        if not progressed:
            break
    while len(selected) < count:
        selected.append(rng.choice(profiles))
    return selected


def _stratified_unique_sample(profiles: list[LyricProfile], count: int, rng: random.Random) -> list[LyricProfile]:
    if count <= 0 or not profiles:
        return []
    return _stratified_sample(profiles, min(count, len(profiles)), rng)


def _build_positive_samples(
    profiles: list[LyricProfile],
    output_dir: Path,
    csv_base: Path,
    rng: random.Random,
    *,
    start_index: int,
) -> list[GeneratedSample]:
    samples: list[GeneratedSample] = []
    for offset, profile in enumerate(profiles):
        raw = profile.raw_text
        lines = _content_lines(raw)
        variants = [
            ("positive_exact_copy", raw),
            ("positive_timestamped", _add_timestamps(lines)),
            ("positive_punctuation_noise", _add_punctuation_noise(lines, rng)),
            ("positive_platform_noise", _with_platform_noise(lines)),
            ("positive_blank_line_noise", _add_blank_line_noise(lines)),
            ("positive_chorus_count_changed", _change_repeated_line_counts(lines)),
            ("positive_translation_added", _translation_added(lines)),
            ("positive_typo_noise", _add_typo_noise(lines, rng)),
        ]
        sample_type, text = variants[offset % len(variants)]
        index = start_index + offset
        path = _write_sample_file(output_dir, f"pos_{index:05d}_{sample_type}.txt", text)
        samples.append(_sample_from_profile(index, path, csv_base, "应去重", sample_type, profile))
        _progress_count("positive_full_duplicate", len(samples), len(profiles))
    return samples


def _build_hard_samples(
    plan: dict[str, int],
    *,
    groups: dict[str, list[LyricProfile]],
    holdout_profiles: list[LyricProfile],
    indexed_profiles: list[LyricProfile],
    output_dir: Path,
    csv_base: Path,
    rng: random.Random,
    start_index: int,
) -> list[GeneratedSample]:
    samples: list[GeneratedSample] = []

    _progress("build positive_realistic_variant samples")
    samples.extend(
        _build_realistic_positive_samples(
            _stratified_sample(groups["normal"], plan["positive_realistic_variant"], rng),
            output_dir,
            csv_base,
            rng,
            start_index=start_index + len(samples),
        )
    )
    _progress(f"built samples: {len(samples)}")

    real_holdout_count = plan.get("negative_real_holdout_full_song", 0)
    _progress("build negative_real_holdout_full_song samples")
    samples.extend(
        _build_holdout_full_song_samples(
            holdout_profiles[:real_holdout_count],
            output_dir,
            csv_base,
            start_index=start_index + len(samples),
        )
    )
    _progress(f"built samples: {len(samples)}")

    near_count = plan.get("negative_near_neighbor_holdout_full_song", 0)
    _progress("build negative_near_neighbor_holdout_full_song samples")
    near_holdouts = _near_neighbor_holdouts(
        holdout_profiles[real_holdout_count:],
        indexed_profiles,
        near_count,
    )
    samples.extend(
        _build_holdout_full_song_samples(
            near_holdouts,
            output_dir,
            csv_base,
            start_index=start_index + len(samples),
            sample_type="negative_near_neighbor_holdout_full_song",
            notes="full real holdout lyric selected for catalog line overlap with indexed songs",
        )
    )
    _progress(f"built samples: {len(samples)}")

    _progress("build negative_long_fragment samples")
    samples.extend(
        _build_fragment_samples(
            _stratified_sample(groups["fragmentable"], plan.get("negative_long_fragment", 0), rng),
            output_dir,
            csv_base,
            rng,
            start_index=start_index + len(samples),
            sample_type="negative_long_fragment",
            long_fragment=True,
            notes="realistic long partial lyric upload, not a full-song duplicate",
        )
    )
    _progress(f"built samples: {len(samples)}")

    _progress("build negative_shared_chorus samples")
    samples.extend(
        _build_shared_chorus_samples(
            _stratified_sample(groups["normal"], plan.get("negative_shared_chorus", 0), rng),
            output_dir,
            csv_base,
            rng,
            start_index=start_index + len(samples),
        )
    )
    _progress(f"built samples: {len(samples)}")

    _progress("build negative_translation_only samples")
    samples.extend(
        _build_translation_only_samples(
            _stratified_sample(groups["foreign"], plan.get("negative_translation_only", 0), rng),
            output_dir,
            csv_base,
            rng,
            start_index=start_index + len(samples),
        )
    )
    _progress(f"built samples: {len(samples)}")

    _progress("build negative_catalog_mashup samples")
    samples.extend(
        _build_catalog_mashup_samples(
            _stratified_sample(groups["normal"], plan.get("negative_catalog_mashup", 0) * 3, rng),
            plan.get("negative_catalog_mashup", 0),
            output_dir,
            csv_base,
            rng,
            start_index=start_index + len(samples),
        )
    )
    _progress(f"built samples: {len(samples)}")

    _progress("build edge_short_or_placeholder samples")
    samples.extend(
        _build_edge_samples(
            _stratified_sample(groups["edge"], plan.get("edge_short_or_placeholder", 0), rng),
            output_dir,
            csv_base,
            rng,
            start_index=start_index + len(samples),
        )
    )
    return samples


def _build_realistic_positive_samples(
    profiles: list[LyricProfile],
    output_dir: Path,
    csv_base: Path,
    rng: random.Random,
    *,
    start_index: int,
) -> list[GeneratedSample]:
    samples: list[GeneratedSample] = []
    for offset, profile in enumerate(profiles):
        content_lines = _content_lines(profile.raw_text)
        primary_lines = list(profile.normalized.primary_lines or profile.normalized.unique_lines) or content_lines
        variants = [
            ("positive_platform_mixed_noise", _platform_mixed_noise(content_lines, rng)),
            ("positive_near_full_missing_section", _near_full_missing_section(primary_lines, rng)),
            ("positive_block_translation_added", _block_translation_added(primary_lines)),
            ("positive_typo_and_punctuation_noise", _stronger_typo_and_punctuation_noise(content_lines, rng)),
            ("positive_timestamped_platform_variant", _timestamped_platform_variant(content_lines)),
            ("positive_chorus_count_changed", _change_repeated_line_counts(content_lines)),
        ]
        sample_type, text = variants[offset % len(variants)]
        index = start_index + offset
        path = _write_sample_file(output_dir, f"pos_{index:05d}_{sample_type}.txt", text)
        samples.append(_sample_from_profile(index, path, csv_base, "应去重", sample_type, profile))
        _progress_count("positive_realistic_variant", len(samples), len(profiles))
    return samples


def _build_holdout_full_song_samples(
    profiles: list[LyricProfile],
    output_dir: Path,
    csv_base: Path,
    *,
    start_index: int,
    sample_type: str = "negative_real_holdout_full_song",
    notes: str = "full real lyric held out from the generated eval index",
) -> list[GeneratedSample]:
    samples: list[GeneratedSample] = []
    for offset, profile in enumerate(profiles):
        index = start_index + offset
        text = profile.raw_text
        path = _write_sample_file(output_dir, f"neg_{index:05d}_{sample_type}.txt", text)
        samples.append(
            _sample_from_profile(
                index,
                path,
                csv_base,
                "不应去重",
                sample_type,
                profile,
                notes=notes,
            )
        )
        _progress_count(sample_type, len(samples), len(profiles))
    return samples


def _build_same_theme_synthetic_samples(
    count: int,
    output_dir: Path,
    csv_base: Path,
    rng: random.Random,
    *,
    start_index: int,
) -> list[GeneratedSample]:
    samples: list[GeneratedSample] = []
    for offset in range(count):
        index = start_index + offset
        text = _same_theme_synthetic(index, rng)
        path = _write_sample_file(output_dir, f"neg_{index:05d}_negative_same_theme_synthetic.txt", text)
        samples.append(
            GeneratedSample(
                sample_id=f"sample-{index:05d}",
                file=str(path.relative_to(csv_base)),
                expected="不应去重",
                sample_type="negative_same_theme_synthetic",
                source="synthetic",
                notes="same-theme synthetic full lyric not copied from library",
            )
        )
        _progress_count("negative_same_theme_synthetic", len(samples), count)
    return samples


def _build_fragment_samples(
    profiles: list[LyricProfile],
    output_dir: Path,
    csv_base: Path,
    rng: random.Random,
    *,
    start_index: int,
    sample_type: str = "negative_fragment",
    long_fragment: bool = False,
    notes: str = "partial lyric fragment only",
) -> list[GeneratedSample]:
    samples: list[GeneratedSample] = []
    for offset, profile in enumerate(profiles):
        lines = list(profile.normalized.primary_lines or profile.normalized.unique_lines)
        text = _long_song_fragment(lines, rng) if long_fragment else _single_song_fragment(lines, rng)
        index = start_index + offset
        path = _write_sample_file(output_dir, f"neg_{index:05d}_{sample_type}.txt", text)
        samples.append(
            _sample_from_profile(
                index,
                path,
                csv_base,
                "不应去重",
                sample_type,
                profile,
                notes=notes,
            )
        )
        _progress_count(sample_type, len(samples), len(profiles))
    return samples


def _build_catalog_mashup_samples(
    profiles: list[LyricProfile],
    count: int,
    output_dir: Path,
    csv_base: Path,
    rng: random.Random,
    *,
    start_index: int,
) -> list[GeneratedSample]:
    samples: list[GeneratedSample] = []
    if count <= 0 or not profiles:
        return samples
    for offset in range(count):
        index = start_index + offset
        picked = rng.sample(profiles, k=min(3, len(profiles)))
        text = _catalog_mashup_text(picked, rng)
        path = _write_sample_file(output_dir, f"neg_{index:05d}_negative_catalog_mashup.txt", text)
        samples.append(
            GeneratedSample(
                sample_id=f"sample-{index:05d}",
                file=str(path.relative_to(csv_base)),
                expected="不应去重",
                sample_type="negative_catalog_mashup",
                source=" | ".join(str(profile.path) for profile in picked),
                notes="medley-style partial lyric assembled from multiple catalog songs",
            )
        )
        _progress_count("negative_catalog_mashup", len(samples), count)
    return samples


def _build_shared_chorus_samples(
    profiles: list[LyricProfile],
    output_dir: Path,
    csv_base: Path,
    rng: random.Random,
    *,
    start_index: int,
) -> list[GeneratedSample]:
    samples: list[GeneratedSample] = []
    for offset, profile in enumerate(profiles):
        lines = list(profile.normalized.primary_lines or profile.normalized.unique_lines)
        repeated = _repeated_or_sampled_lines(profile.normalized, rng)
        text = "\n".join(
            [
                "清晨的光落在新的街口",
                "我把故事重新写给以后",
                *repeated,
                *repeated,
                "所有答案都从这里开始",
            ]
        )
        index = start_index + offset
        path = _write_sample_file(output_dir, f"neg_{index:05d}_negative_shared_chorus.txt", text)
        samples.append(
            _sample_from_profile(
                index,
                path,
                csv_base,
                "不应去重",
                "negative_shared_chorus",
                profile,
                notes="shared repeated lines with new surrounding content",
            )
        )
        _progress_count("negative_shared_chorus", len(samples), len(profiles))
    return samples


def _build_translation_only_samples(
    profiles: list[LyricProfile],
    output_dir: Path,
    csv_base: Path,
    rng: random.Random,
    *,
    start_index: int,
) -> list[GeneratedSample]:
    samples: list[GeneratedSample] = []
    for offset, profile in enumerate(profiles):
        lines = list(profile.normalized.translation_lines) or [
            _pseudo_translation(idx) for idx in range(1, min(8, max(profile.line_count, 4)) + 1)
        ]
        rng.shuffle(lines)
        text = "\n".join(lines[:8])
        index = start_index + offset
        path = _write_sample_file(output_dir, f"neg_{index:05d}_negative_translation_only.txt", text)
        samples.append(
            _sample_from_profile(
                index,
                path,
                csv_base,
                "不应去重",
                "negative_translation_only",
                profile,
                notes="translation-like text without matching original lyric",
            )
        )
        _progress_count("negative_translation_only", len(samples), len(profiles))
    return samples


def _build_edge_samples(
    profiles: list[LyricProfile],
    output_dir: Path,
    csv_base: Path,
    rng: random.Random,
    *,
    start_index: int,
) -> list[GeneratedSample]:
    samples: list[GeneratedSample] = []
    for offset, profile in enumerate(profiles):
        lines = list(profile.normalized.primary_lines or profile.normalized.unique_lines)
        if profile.line_count <= 1:
            text = _same_theme_synthetic(start_index + offset, rng)
            notes = "zero or one effective line; use synthetic edge negative"
        else:
            text = _short_shared_snippet(lines, rng)
            notes = "short lyric edge case with limited overlap"
        index = start_index + offset
        path = _write_sample_file(output_dir, f"neg_{index:05d}_edge_short_or_placeholder.txt", text)
        samples.append(
            _sample_from_profile(
                index,
                path,
                csv_base,
                "不应去重",
                "edge_short_or_placeholder",
                profile,
                notes=notes,
            )
        )
        _progress_count("edge_short_or_placeholder", len(samples), len(profiles))
    return samples


def _sample_from_profile(
    index: int,
    path: Path,
    csv_base: Path,
    expected: str,
    sample_type: str,
    profile: LyricProfile,
    *,
    candidate_record_id: str = "",
    notes: str = "",
) -> GeneratedSample:
    return GeneratedSample(
        sample_id=f"sample-{index:05d}",
        file=str(path.relative_to(csv_base)),
        expected=expected,
        sample_type=sample_type,
        source=str(profile.path),
        source_record_id=profile.record_id,
        candidate_record_id=candidate_record_id,
        line_count_bucket=profile.line_count_bucket,
        language_bucket=profile.language_bucket,
        source_bucket=profile.source_bucket,
        title=profile.title,
        artist=profile.artist,
        notes=notes,
    )


def _write_sample_file(output_dir: Path, name: str, text: str) -> Path:
    path = output_dir / name
    path.write_text(text.strip() + "\n", encoding="utf-8")
    return path


def _write_csv(samples: list[GeneratedSample], csv_path: Path, *, seed: int) -> None:
    fieldnames = [
        "id",
        "file",
        "expected",
        "sample_type",
        "source",
        "source_record_id",
        "candidate_record_id",
        "line_count_bucket",
        "language_bucket",
        "source_bucket",
        "title",
        "artist",
        "seed",
        "notes",
    ]
    with csv_path.open("w", encoding="utf-8", newline="") as file:
        writer = csv.DictWriter(file, fieldnames=fieldnames)
        writer.writeheader()
        for sample in samples:
            writer.writerow(
                {
                    "id": sample.sample_id,
                    "file": sample.file,
                    "expected": sample.expected,
                    "sample_type": sample.sample_type,
                    "source": sample.source,
                    "source_record_id": sample.source_record_id,
                    "candidate_record_id": sample.candidate_record_id,
                    "line_count_bucket": sample.line_count_bucket,
                    "language_bucket": sample.language_bucket,
                    "source_bucket": sample.source_bucket,
                    "title": sample.title,
                    "artist": sample.artist,
                    "seed": seed,
                    "notes": sample.notes,
                }
            )


def _write_manifest(
    *,
    profiles: list[LyricProfile],
    samples: list[GeneratedSample],
    csv_path: Path,
    output_dir: Path,
    seed: int,
    plan: dict[str, int],
    index_path: Path | None,
    eval_index_path: Path,
    holdout_count: int,
    profile: str,
) -> dict[str, object]:
    manifest = {
        "profile": profile,
        "seed": seed,
        "library_files": len(profiles),
        "sample_size": len(samples),
        "plan": plan,
        "source_index": str(index_path) if index_path else "",
        "eval_index": str(eval_index_path) if eval_index_path else "",
        "holdout_records": holdout_count,
        "lyrics_dir": str(output_dir),
        "csv": str(csv_path),
        "manifest": str(csv_path.with_suffix(csv_path.suffix + ".manifest.json")),
        "sample_type_counts": dict(Counter(sample.sample_type for sample in samples)),
        "expected_counts": dict(Counter(sample.expected for sample in samples)),
        "line_count_bucket_counts": dict(Counter(profile.line_count_bucket for profile in profiles)),
        "language_bucket_counts": dict(Counter(profile.language_bucket for profile in profiles)),
        "source_bucket_counts": dict(Counter(profile.source_bucket for profile in profiles).most_common(50)),
        "unique_source_records": len({sample.source_record_id for sample in samples if sample.source_record_id}),
    }
    csv_path.with_suffix(csv_path.suffix + ".manifest.json").write_text(
        json.dumps(manifest, ensure_ascii=False, indent=2),
        encoding="utf-8",
    )
    return manifest


def _near_neighbor_holdouts(
    holdout_profiles: list[LyricProfile],
    indexed_profiles: list[LyricProfile],
    count: int,
) -> list[LyricProfile]:
    if count <= 0 or not holdout_profiles:
        return []
    if not indexed_profiles:
        return holdout_profiles[:count]

    line_to_indexed_count: Counter[str] = Counter()
    for profile in indexed_profiles:
        for line in set(profile.normalized.primary_lines or profile.normalized.unique_lines):
            if len(line) >= 4:
                line_to_indexed_count[line] += 1

    scored: list[tuple[float, LyricProfile]] = []
    for profile in holdout_profiles:
        lines = set(profile.normalized.primary_lines or profile.normalized.unique_lines)
        useful_lines = {line for line in lines if len(line) >= 4}
        if not useful_lines:
            score = 0.0
        else:
            shared = sum(1 for line in useful_lines if line_to_indexed_count[line] > 0)
            common_weight = sum(min(line_to_indexed_count[line], 5) for line in useful_lines)
            score = (shared / len(useful_lines)) + (common_weight / (len(useful_lines) * 20))
        scored.append((score, profile))

    scored.sort(key=lambda item: item[0], reverse=True)
    return [profile for _, profile in scored[:count]]


def _content_lines(text: str) -> list[str]:
    lines = [line.strip() for line in text.splitlines() if line.strip()]
    return lines or [text.strip()]


def _clean_generated_output_dir(output_dir: Path) -> None:
    for path in output_dir.iterdir():
        if path.is_file() and path.suffix.lower() in {".txt", ".lrc"}:
            path.unlink()


def _line_count_bucket(line_count: int) -> str:
    if line_count == 0:
        return "zero"
    if line_count <= 5:
        return "short"
    if line_count <= 40:
        return "normal"
    return "long"


def _language_bucket(lines: tuple[str, ...]) -> str:
    text = "\n".join(lines)
    cjk = len(re.findall(r"[\u4e00-\u9fff]", text))
    latin = len(re.findall(r"[A-Za-z]", text))
    kana = len(re.findall(r"[\u3040-\u30ff]", text))
    hangul = len(re.findall(r"[\uac00-\ud7af]", text))
    if kana or hangul:
        return "jp_kr"
    if cjk and latin:
        return "mixed"
    if cjk:
        return "zh"
    if latin:
        return "latin"
    return "other"


def _source_bucket(path: Path) -> str:
    stem = path.stem
    parts = stem.split("_")
    if len(parts) >= 2:
        code = re.sub(r"\d+$", "", parts[-1])
        return code or "unknown"
    return "unknown"


def _add_timestamps(lines: list[str]) -> str:
    return "\n".join(f"[00:{idx % 60:02d}.00]{line}" for idx, line in enumerate(lines, start=1))


def _platform_mixed_noise(lines: list[str], rng: random.Random) -> str:
    noisy = _add_blank_line_noise(lines).splitlines()
    if noisy:
        noisy = _add_punctuation_noise(noisy, rng).splitlines()
    return "\n".join(["作词:未知", "歌词来自平台同步", *noisy, "未经著作权人许可 不得商业使用"])


def _timestamped_platform_variant(lines: list[str]) -> str:
    timestamped = _add_timestamps(lines).splitlines()
    return "\n".join(["[00:00.00]歌词贡献者:用户上传", *timestamped])


def _add_punctuation_noise(lines: list[str], rng: random.Random) -> str:
    marks = ["!", "?", "...", ",", "。"]
    return "\n".join(f"{line}{rng.choice(marks)}" for line in lines)


def _with_platform_noise(lines: list[str]) -> str:
    return "\n".join(["歌词来自QQ音乐", "作词:测试", *lines, "未经著作权人许可 不得翻唱"])


def _add_blank_line_noise(lines: list[str]) -> str:
    result: list[str] = []
    for idx, line in enumerate(lines, start=1):
        result.append(line)
        if idx % 4 == 0:
            result.append("")
    return "\n".join(result)


def _change_repeated_line_counts(lines: list[str]) -> str:
    seen: set[str] = set()
    result: list[str] = []
    for line in lines:
        if line in seen:
            continue
        seen.add(line)
        result.append(line)
    return "\n".join(result or lines)


def _translation_added(lines: list[str]) -> str:
    result: list[str] = []
    for idx, line in enumerate(lines, start=1):
        result.append(line)
        if _looks_foreign(line) and idx <= 24:
            result.append(_pseudo_translation(idx))
    return "\n".join(result)


def _block_translation_added(lines: list[str]) -> str:
    body = "\n".join(lines)
    translation_count = min(8, max(4, len(lines) // 4))
    translations = [_pseudo_translation(index) for index in range(1, translation_count + 1)]
    return "\n".join([body, "", *translations])


def _near_full_missing_section(lines: list[str], rng: random.Random) -> str:
    if len(lines) <= 8:
        return "\n".join(lines)
    drop_count = max(1, min(max(1, len(lines) // 5), 8))
    start = rng.randrange(0, max(1, len(lines) - drop_count + 1))
    kept = lines[:start] + lines[start + drop_count :]
    return "\n".join(kept or lines)


def _add_typo_noise(lines: list[str], rng: random.Random) -> str:
    if not lines:
        return ""
    result = list(lines)
    editable_indexes = [index for index, line in enumerate(result) if _can_typo_line(line)]
    if not editable_indexes:
        return "\n".join(result)
    typo_count = max(1, min(4, len(editable_indexes) // 8 or 1))
    for index in rng.sample(editable_indexes, k=min(typo_count, len(editable_indexes))):
        result[index] = _typo_line(result[index], rng)
    return "\n".join(result)


def _stronger_typo_and_punctuation_noise(lines: list[str], rng: random.Random) -> str:
    if not lines:
        return ""
    result = _add_punctuation_noise(lines, rng).splitlines()
    editable_indexes = [index for index, line in enumerate(result) if _can_typo_line(line)]
    typo_count = max(1, min(8, len(editable_indexes) // 6 or 1))
    for index in rng.sample(editable_indexes, k=min(typo_count, len(editable_indexes))):
        result[index] = _typo_line(result[index], rng)
    return "\n".join(result)


def _can_typo_line(line: str) -> bool:
    return bool(re.search(r"[A-Za-z]{4,}|[\u4e00-\u9fff]{4,}", line))


def _typo_line(line: str, rng: random.Random) -> str:
    words = list(re.finditer(r"[A-Za-z]{4,}", line))
    if words and rng.random() < 0.65:
        match = rng.choice(words)
        typo = _typo_english_word(match.group(0), rng)
        return line[: match.start()] + typo + line[match.end() :]
    cjk_positions = [index for index, char in enumerate(line) if "\u4e00" <= char <= "\u9fff"]
    if cjk_positions:
        index = rng.choice(cjk_positions)
        return line[:index] + _typo_cjk_char(line[index]) + line[index + 1 :]
    return line


def _typo_english_word(word: str, rng: random.Random) -> str:
    if len(word) <= 4 or rng.random() < 0.55:
        remove_at = rng.randrange(1, max(2, len(word) - 1))
        return word[:remove_at] + word[remove_at + 1 :]
    swap_at = rng.randrange(1, max(2, len(word) - 2))
    chars = list(word)
    chars[swap_at], chars[swap_at + 1] = chars[swap_at + 1], chars[swap_at]
    return "".join(chars)


def _typo_cjk_char(char: str) -> str:
    replacements = {
        "你": "妳",
        "爱": "爰",
        "夜": "液",
        "里": "裏",
        "风": "凤",
        "雨": "兩",
        "听": "昕",
        "说": "説",
        "想": "相",
        "梦": "夣",
        "心": "芯",
        "光": "先",
        "城": "诚",
        "远": "迩",
        "回": "囬",
        "走": "赱",
        "海": "毎",
        "天": "夭",
    }
    return replacements.get(char, char)


def _single_song_fragment(lines: list[str], rng: random.Random) -> str:
    if len(lines) <= 4:
        return "\n".join(lines[: max(1, len(lines) // 2)])
    fragment_len = max(2, min(8, len(lines) // rng.choice([3, 4, 5])))
    start = rng.randrange(0, max(1, len(lines) - fragment_len + 1))
    return "\n".join(lines[start : start + fragment_len])


def _long_song_fragment(lines: list[str], rng: random.Random) -> str:
    if len(lines) <= 8:
        return _single_song_fragment(lines, rng)
    fragment_len = max(6, min(len(lines) - 1, int(len(lines) * rng.uniform(0.35, 0.60))))
    start = rng.randrange(0, max(1, len(lines) - fragment_len + 1))
    return "\n".join(lines[start : start + fragment_len])


def _catalog_mashup_text(profiles: list[LyricProfile], rng: random.Random) -> str:
    sections: list[str] = []
    for profile in profiles:
        lines = list(profile.normalized.primary_lines or profile.normalized.unique_lines)
        if not lines:
            continue
        section_len = min(max(2, len(lines) // 8), 5)
        start = rng.randrange(0, max(1, len(lines) - section_len + 1))
        sections.extend(lines[start : start + section_len])
    if not sections:
        return _same_theme_synthetic(0, rng)
    return "\n".join(sections)


def _short_shared_snippet(lines: list[str], rng: random.Random) -> str:
    snippet = rng.sample(lines, k=min(2, len(lines))) if lines else []
    synthetic = [
        "清晨的风吹过新的街口",
        "我把昨天放进安静的口袋",
        *snippet,
        "故事从这里重新开始",
        "灯光落下我继续往前走",
    ]
    return "\n".join(synthetic)


def _repeated_or_sampled_lines(normalized: NormalizedLyrics, rng: random.Random) -> list[str]:
    repeated = [line for line, count in normalized.line_counts.items() if count >= 2]
    if repeated:
        return rng.sample(repeated, k=min(2, len(repeated)))
    lines = list(normalized.primary_lines or normalized.unique_lines)
    return rng.sample(lines, k=min(2, len(lines))) if lines else []


def _same_theme_synthetic(index: int, rng: random.Random) -> str:
    starts = ["我在夜里想起远方的你", "城市灯火陪我走过雨季", "风把旧名字吹向清晨"]
    middles = ["那些没说完的话留在风里", "新的路口慢慢亮起", "时间把答案交给下一站"]
    ends = ["明天醒来我们各自继续", "我会把今天写成新的旋律", "故事从这里重新开始"]
    return "\n".join(
        [
            rng.choice(starts),
            rng.choice(middles),
            rng.choice(ends),
            f"这是第 {index} 个全新测试样本",
        ]
    )


def _pseudo_translation(index: int) -> str:
    translations = [
        "今晚我仍然想念你",
        "风会带走所有疲惫",
        "黑暗里也会有光",
        "别让昨天困住自己",
        "我们终会继续向前",
        "雨停以后天空会亮",
        "把遗憾留在旧时光",
        "你已经足够好了",
    ]
    return translations[(index - 1) % len(translations)]


def _looks_foreign(line: str) -> bool:
    latin = len(re.findall(r"[A-Za-z]", line))
    cjk = len(re.findall(r"[\u4e00-\u9fff]", line))
    return latin > 0 and cjk == 0