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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
"""
Export weakly-labeled anime filename samples from a DMHY crawler SQLite DB.

The crawler database is append-only while it runs, so this script snapshots a
high-water mark (`files.id <= last_file_id`) and writes that value to a manifest.
Future exports can pass `--min-id last_file_id + 1` to label only newly crawled
rows.
"""

import argparse
import json
import os
import random
import re
import sqlite3
from collections import Counter
from dataclasses import dataclass
from datetime import datetime, timezone
from pathlib import Path
from typing import Iterable, List, Optional, Sequence

from data_generator import LABEL_MAP, categorize_meta_token
from label_repairs import season_marker_number
from tokenizer import AnimeTokenizer


VIDEO_EXTENSIONS = {
    ".mkv", ".mp4", ".avi", ".mov", ".wmv", ".flv", ".rmvb",
    ".ts", ".m2ts", ".webm", ".mpg", ".mpeg", ".m4v",
}

NOISE_BRACKETS = {
    "mp4", "mkv", "avi", "webm", "mov", "wmv", "flv", "rmvb", "ts", "m2ts",
    "raw", "raws", "rip", "10bit", "8bit", "hi10p", "ma10p", "ass", "assx2",
    "tc", "sc", "gb", "big5", "cht", "chs", "jpn", "jp", "jap", "eng",
    "繁中", "简中", "繁日", "简日", "日语", "日文", "外挂", "内封", "字幕",
}
CATEGORY_BRACKETS = {
    "国漫", "國漫", "国产", "國產", "国产动漫", "國產動漫", "国产动画", "國產動畫",
    "国创", "國創", "中国动漫", "中國動漫", "中国动画", "中國動畫",
}

SPECIAL_RE = re.compile(r"^(?:ova\d*|oad\d*|sp\d*|movie|the\s*movie|op|ed|pv|cm|ncop|nced|剧场版|劇場版|特别篇|特別篇)$", re.I)
SPECIAL_SEARCH_RE = re.compile(r"^(?:檢索|检索|搜索|搜寻|搜尋|别名|別名|alias|search|keyword)\s*[::].+", re.I)
EPISODE_RE = re.compile(r"^(?:[Ee][Pp]?|#)?(\d{1,4})(?:v\d+|END)?$", re.I)
SEASON_RE = re.compile(
    r"^(?:"
    r"[Ss](\d{1,2})|"
    r"Seasons?\s*(\d{1,2})|"
    r"第([一二三四五六七八九十\d]+)[季期部]|"
    r"(\d+)(?:st|nd|rd|th)\s+[Ss]eason"
    r")$", re.I
)
READING_SEASON_RE = re.compile(
    r"^(?:Ni\s+no\s+(?:Sara|Shou|Sho|Syo|Shō)|Ni\s+Gakki|Sono\s+Ni|"
    r"San\s+no\s+(?:Sara|Shou|Sho|Syo)|(?:Yon|Shi|Shin)\s+no\s+Sara|"
    r"(?:Go|Gou)\s+no\s+Sara)$",
    re.I,
)
CJK_SEQUEL_SEASON_RE = re.compile(
    r"^(?:[一二三四五六七八九十兩两貳贰弐弍參叁参肆伍陸陆柒捌玖](?:\s*(?:ノ|の|之)\s*(?:章|期|季|部))?|"
    r"[ⅡⅢⅣⅤⅥⅦⅧⅨ]|II|III|IV|V|VI|VII|VIII|IX)$",
    re.I,
)
SXE_RE = re.compile(r"^([Ss]\d{1,2})([Ee]\d{1,4})(?:v\d+)?$")
DATE_RE = re.compile(r"^(?:19|20)\d{2}[.\-_年]?(?:0?[1-9]|1[0-2])?[.\-_月]?(?:0?[1-9]|[12]\d|3[01])?日?$")
HASH_RE = re.compile(r"^[A-Fa-f0-9]{8,}$")
DIMENSION_RE = re.compile(r"^\d{3,4}[xX×]\d{3,4}$")
RESOLUTION_RE = re.compile(r"^(?:\d{3,4}[pP]|\d[Kk]|\d{3,4}[xX×]\d{3,4})$")
RESOLUTION_SEARCH_RE = re.compile(r"(?<![A-Za-z0-9])(?:\d{3,4}[pP]|\d[Kk]|\d{3,4}[xX×]\d{3,4})(?![A-Za-z0-9])")
SOURCE_RE = re.compile(
    r"^(?:WEB[-_ ]?DL|WEB[-_ ]?Rip|BDRip|BluRay|BDMV|BD|DVDRip|DVD|TVRip|HDTV|"
    r"Netflix|NF|AMZN|Baha|CR|ABEMA|DSNP|U[-_ ]?NEXT|Hulu|AT[-_ ]?X|"
    r"x26[45]|h\.?26[45]|HEVC|AVC|AV1|AAC\d*(?:\.\d+)?|AAC|FLAC|MP3|DTS|Opus|"
    r"CHS|CHT|BIG5|GB|JPN?|JPSC|JPTC|简[体體]?|繁[体體]?|简日双语|繁日双语|内封|外挂|MSubs?)$",
    re.I,
)
GROUP_HINT_RE = re.compile(
    r"(?:字幕|字幕组|字幕組|sub|subs|raws?|fansub|studio|house|team|project|"
    r"loli|ani|baha|vcb|airota|kiss|dmhy|mabors|lilith|ohys|erai|subsplease)",
    re.I,
)
TRAILING_DECORATION_RE = re.compile(
    r"(?:新番|月番|合集|合輯|全集|完结|完結|检索|檢索|招募|字幕|内封|內封|"
    r"年齡|年龄|限制|版本|版|"
    r"简中|繁中|GB|BIG5|CHS|CHT|JPN?|MP4|MKV|HEVC|AVC|AAC|FLAC|WEB-DL|1080[Pp]|720[Pp])"
)


@dataclass
class ExportStats:
    scanned_rows: int = 0
    video_rows: int = 0
    duplicate_basenames: int = 0
    labeled_samples: int = 0
    skipped_no_episode: int = 0
    skipped_no_title: int = 0
    skipped_too_short: int = 0
    skipped_too_long: int = 0


def normalize_path_basename(filename: str) -> str:
    return re.split(r"[\\/]", filename)[-1].strip()


def strip_video_extension(basename: str) -> tuple[str, str]:
    stem, ext = os.path.splitext(basename)
    return stem.strip(), ext.lower()


def clean_bracket(token: str) -> str:
    return token.strip().strip("[]()【】《》()").strip()


def cn_number_to_int(text: str) -> Optional[int]:
    if text.isdigit():
        return int(text)
    values = {"一": 1, "二": 2, "三": 3, "四": 4, "五": 5, "六": 6, "七": 7, "八": 8, "九": 9}
    if text == "十":
        return 10
    if text.startswith("十") and len(text) == 2:
        return 10 + values.get(text[1], 0)
    if text.endswith("十") and len(text) == 2:
        return values.get(text[0], 0) * 10
    if "十" in text and len(text) == 3:
        return values.get(text[0], 0) * 10 + values.get(text[2], 0)
    return values.get(text)


def season_number(token: str) -> Optional[int]:
    clean = clean_bracket(token)
    match = SEASON_RE.match(clean)
    if match:
        value = next((g for g in match.groups() if g), None)
        if value is None:
            return None
        return cn_number_to_int(value)
    if READING_SEASON_RE.match(clean) or CJK_SEQUEL_SEASON_RE.match(clean):
        return season_marker_number(clean)
    return None


def is_explicit_season(token: str) -> bool:
    """Return True for unambiguous season syntax such as S02 or 第2季."""
    clean = clean_bracket(token)
    return bool(SEASON_RE.match(clean))


def episode_number(token: str) -> Optional[int]:
    clean = clean_bracket(token)
    if season_number(clean) is not None:
        return None
    if DIMENSION_RE.match(clean) or DATE_RE.match(clean) or HASH_RE.match(clean):
        return None
    if re.match(r"^第\d{1,4}(?:\(\d{1,4}\))?[话話集]$", clean):
        return int(re.search(r"\d+", clean).group())
    if re.match(r"^(?:OVA|OAD|SP)\d{1,4}$", clean, re.I):
        return int(re.search(r"\d+", clean).group())
    if re.match(r"^\d{1,4}\s*END$", clean, re.I):
        return int(re.search(r"\d+", clean).group())
    if re.match(r"^\d{1,4}[._]\d+$", clean):
        return int(re.search(r"\d+", clean).group())
    match = EPISODE_RE.match(clean)
    if not match:
        return None
    number = int(match.group(1))
    if number == 0 or number > 2000:
        return None
    return number


def has_wrapping_brackets(token: str) -> bool:
    return len(token) >= 2 and token[0] in "[【(《" and token[-1] in "]】)》"


def is_resolution(token: str) -> bool:
    clean = clean_bracket(token)
    return bool(RESOLUTION_RE.match(clean) or (has_wrapping_brackets(token) and RESOLUTION_SEARCH_RE.search(clean)))


def is_source(token: str) -> bool:
    clean = clean_bracket(token)
    if not clean:
        return False
    if categorize_meta_token(token) in {"RESOLUTION", "SOURCE"} and (
        is_resolution(clean) or SOURCE_RE.match(clean)
    ):
        return True
    if SOURCE_RE.match(clean):
        return True
    if has_wrapping_brackets(token):
        parts = [part for part in re.split(r"[\s&+/,._-]+", clean) if part]
        has_source_part = any(SOURCE_RE.match(part) for part in parts)
        return has_source_part and all(SOURCE_RE.match(part) or is_noise_bracket(part) for part in parts)
    return False


def is_special(token: str) -> bool:
    clean = clean_bracket(token)
    return bool(SPECIAL_RE.match(clean) or SPECIAL_SEARCH_RE.match(clean))


def is_category_bracket(token: str) -> bool:
    clean = re.sub(r"[\s._-]+", "", clean_bracket(token))
    return has_wrapping_brackets(token) and clean in CATEGORY_BRACKETS


def is_noise_bracket(token: str) -> bool:
    clean = clean_bracket(token)
    if not clean:
        return True
    normalized = re.sub(r"[\s._-]+", "", clean).lower()
    if normalized in NOISE_BRACKETS:
        return True
    if is_category_bracket(token):
        return True
    if DATE_RE.match(clean) or HASH_RE.match(clean):
        return True
    return False


def is_group_bracket(token: str, index: int, tokens: Sequence[str]) -> bool:
    if not (token.startswith("[") or token.startswith("(") or token.startswith("【") or token.startswith("《")):
        return False
    clean = clean_bracket(token)
    if not clean or is_noise_bracket(token):
        return False
    if is_resolution(clean) or is_source(clean) or is_special(clean) or episode_number(clean) is not None:
        return False
    first_content_index = next((i for i, t in enumerate(tokens) if t not in {" ", "-", "_", "|", "~", "~", "."}), 0)
    if index == first_content_index:
        return True
    if index <= first_content_index + 2 and GROUP_HINT_RE.search(clean):
        return True
    return False


def is_title_token(token: str) -> bool:
    if not token.strip():
        return False
    if token in {" ", "-", "_", "|", "~", "~", "."}:
        return False
    clean = clean_bracket(token)
    if not clean:
        return False
    if is_resolution(clean) or is_source(clean) or is_special(clean):
        return False
    if is_explicit_season(clean) or episode_number(clean) is not None:
        return False
    if DATE_RE.match(clean) or HASH_RE.match(clean):
        return False
    if (token.startswith("[") or token.startswith("(") or token.startswith("【") or token.startswith("《")) and TRAILING_DECORATION_RE.search(clean):
        return False
    return True


def trim_title_span(tokens: Sequence[str], start: int, end: int) -> tuple[int, int]:
    while start < end and not is_title_token(tokens[start]):
        start += 1
    while end > start and not is_title_token(tokens[end - 1]):
        end -= 1
    while start < end and TRAILING_DECORATION_RE.search(clean_bracket(tokens[end - 1])):
        end -= 1
        while end > start and tokens[end - 1] in {" ", "-", "_", "|", "~", "~", "."}:
            end -= 1
    return start, end


def find_episode_index(tokens: Sequence[str]) -> Optional[int]:
    candidates: list[tuple[int, int]] = []
    for idx, token in enumerate(tokens):
        number = episode_number(token)
        if number is None:
            continue
        clean = clean_bracket(token)
        if idx > 0 and tokens[idx - 1] == "." and re.fullmatch(r"\d+", clean):
            previous_clean = clean_bracket(tokens[idx - 2]) if idx >= 2 else ""
            if previous_clean.lower() in VIDEO_EXTENSIONS or f".{clean}".lower() in VIDEO_EXTENSIONS:
                continue
        score = 0
        if re.match(r"^(?:[Ee][Pp]?|#|第|OVA|OAD|SP)", clean, re.I):
            score += 4
        if token.startswith("[") or token.startswith("(") or token.startswith("【"):
            score += 3
        if idx > 0 and tokens[idx - 1] in {"-", "_", "|"}:
            score += 2
        if idx >= len(tokens) // 2:
            score += 1
        if 1 <= number <= 200:
            score += 1
        candidates.append((score, idx))
    if not candidates:
        return None
    return max(candidates, key=lambda item: (item[0], item[1]))[1]


def is_separator_token(token: str) -> bool:
    return token in {" ", "-", "_", "|", "~", "~", ".", "+", "&", "/", ","}


def has_only_separators_between(tokens: Sequence[str], start: int, end: int) -> bool:
    return all(is_separator_token(token) for token in tokens[start:end])


def is_context_season_token(tokens: Sequence[str], idx: int, episode_idx: int) -> bool:
    """Detect compact season markers only when they structurally lead into an episode."""
    if idx >= episode_idx:
        return False

    token = tokens[idx]
    clean = clean_bracket(token)
    if not clean:
        return False
    if is_explicit_season(clean):
        return True

    if season_number(clean) is None:
        return False
    if not has_only_separators_between(tokens, idx + 1, episode_idx):
        return False

    # A bare V is often the volume prefix in V02E01, not season five.
    if clean.upper() == "V":
        return False
    return True


def label_context_season_tokens(
    tokens: Sequence[str],
    categories: List[str],
    episode_idx: int,
) -> None:
    if (
        episode_idx >= 2
        and clean_bracket(tokens[episode_idx]).upper().startswith("E")
        and clean_bracket(tokens[episode_idx - 2]).upper() == "V"
        and clean_bracket(tokens[episode_idx - 1]).isdigit()
    ):
        categories[episode_idx - 2] = "season"
        categories[episode_idx - 1] = "season"
        return

    for idx in range(episode_idx):
        if categories[idx] in {"group", "episode", "resolution", "source", "special"}:
            continue
        if is_context_season_token(tokens, idx, episode_idx):
            categories[idx] = "season"


def repair_structured_bracket_title_aliases(
    tokens: Sequence[str],
    categories: List[str],
    episode_idx: int,
) -> None:
    """Keep the primary title in category-prefixed bracket series.

    GM-Team-style rows often look like:
    [GROUP][国漫][中文标题 第2季][English Alias Ⅱ][2026][04][meta]
    The category, alias, and year brackets are metadata for parsing purposes;
    the first real title bracket after the category is the canonical title.
    """
    if not any(is_category_bracket(tokens[idx]) for idx in range(min(episode_idx, len(tokens)))):
        return

    title_candidates = [
        idx
        for idx in range(episode_idx)
        if categories[idx] == "title"
        and has_wrapping_brackets(tokens[idx])
        and is_title_token(tokens[idx])
    ]
    if not title_candidates:
        return

    primary_idx = title_candidates[0]
    for idx in title_candidates[1:]:
        categories[idx] = "sep"

    for idx in range(episode_idx):
        if idx == primary_idx:
            continue
        if is_category_bracket(tokens[idx]) or DATE_RE.match(clean_bracket(tokens[idx])):
            categories[idx] = "sep"


def embedded_bracket_episode(token: str) -> Optional[tuple[str, str, str]]:
    """Split malformed tokens such as '[Group}Title[658]' into title + episode."""
    if episode_number(token) is not None:
        return None
    match = re.match(r"^(?P<prefix>.+?)\[(?P<episode>\d{1,4}(?:v\d+)?)(?P<close>\])?$", token, re.I)
    if match is None and has_wrapping_brackets(token):
        match = re.match(r"^(?P<prefix>.+?)(?P<episode>\d{2,4})(?P<close>[\]\)】》])$", token, re.I)
    if not match:
        return None
    prefix = match.group("prefix")
    episode = match.group("episode")
    close = match.group("close") or ""
    if not clean_bracket(prefix):
        return None
    number = int(re.search(r"\d+", episode).group())
    if number == 0 or number > 2000:
        return None
    return prefix, episode, close


def append_tokenized_category(
    tokens: List[str],
    categories: List[str],
    text: str,
    category: str,
    tokenizer: AnimeTokenizer,
) -> None:
    for piece in tokenizer.tokenize(text):
        if not piece:
            continue
        if is_separator_token(piece) or piece in {"[", "]", "(", ")", "【", "】", "《", "》"}:
            piece_category = "sep"
        else:
            piece_category = category
        tokens.append(piece)
        categories.append(piece_category)


def finalize_weak_sample(
    tokens: Sequence[str],
    categories: Sequence[str],
    tokenizer: AnimeTokenizer,
    require_episode: bool = True,
) -> Optional[dict]:
    expanded_tokens, expanded_categories = expand_tokens_and_categories(tokens, categories, tokenizer)

    # Only unambiguous season forms are promoted here. Compact sequel markers
    # such as 貳, II, or Ni no Sara need episode context and are repaired by
    # label_repairs from character spans; treating every single CJK numeral as
    # season would corrupt titles like 魯邦三世.
    for idx, token in enumerate(expanded_tokens):
        if expanded_categories[idx] in {"sep", "episode", "group", "source", "resolution", "special", "season"}:
            continue
        if is_explicit_season(token):
            expanded_categories[idx] = "season"
            prev_idx = idx - 1
            while prev_idx >= 0 and is_separator_token(expanded_tokens[prev_idx]) and expanded_categories[prev_idx] == "title":
                expanded_categories[prev_idx] = "sep"
                prev_idx -= 1

    labels = assign_iob2(expanded_categories)
    if len(expanded_tokens) != len(labels):
        return None
    if not any(label.endswith("TITLE") for label in labels):
        return None
    if require_episode and not any(label.endswith("EPISODE") for label in labels):
        return None
    return {"tokens": expanded_tokens, "labels": labels}


def assign_iob2(categories: Sequence[str]) -> List[str]:
    labels: List[str] = []
    previous_entity: Optional[str] = None
    for category in categories:
        entity = LABEL_MAP.get(category, "O")
        if entity == "O":
            labels.append("O")
            previous_entity = None
            continue
        prefix = "I" if previous_entity == entity else "B"
        labels.append(f"{prefix}-{entity}")
        previous_entity = entity
    return labels


def fallback_embedded_episode_sample(
    tokens: Sequence[str],
    tokenizer: AnimeTokenizer,
) -> Optional[dict]:
    rebuilt_tokens: List[str] = []
    rebuilt_categories: List[str] = []
    used_episode = False

    for token in tokens:
        embedded = embedded_bracket_episode(token)
        if embedded and not used_episode:
            prefix, episode, close = embedded
            append_tokenized_category(rebuilt_tokens, rebuilt_categories, prefix, "title", tokenizer)
            rebuilt_tokens.append(episode)
            rebuilt_categories.append("episode")
            if close:
                rebuilt_tokens.append(close)
                rebuilt_categories.append("sep")
            used_episode = True
            continue

        if not used_episode:
            category = "sep" if is_separator_token(token) else "title"
        elif is_resolution(token):
            category = "resolution"
        elif is_source(token):
            category = "source"
        elif is_special(token):
            category = "special"
        else:
            category = "sep"
        rebuilt_tokens.append(token)
        rebuilt_categories.append(category)

    if not used_episode:
        return None
    return finalize_weak_sample(rebuilt_tokens, rebuilt_categories, tokenizer)


def has_embedded_episode_candidate(tokens: Sequence[str]) -> bool:
    return any(embedded_bracket_episode(token) is not None for token in tokens)


def fallback_episode_first_sample(
    tokens: Sequence[str],
    categories: Sequence[str],
    episode_idx: int,
    tokenizer: AnimeTokenizer,
) -> Optional[dict]:
    fallback_categories = ["sep"] * len(tokens)

    # V02E01-style catalog rows are episode-first. The tokenizer currently
    # exposes them as V, 02, E01, so keep V02 together as a season span.
    if (
        episode_idx >= 2
        and clean_bracket(tokens[episode_idx]).upper().startswith("E")
        and clean_bracket(tokens[episode_idx - 2]).upper() == "V"
        and clean_bracket(tokens[episode_idx - 1]).isdigit()
    ):
        fallback_categories[episode_idx - 2] = "season"
        fallback_categories[episode_idx - 1] = "season"
    else:
        label_context_season_tokens(tokens, fallback_categories, episode_idx)

    fallback_categories[episode_idx] = "episode"

    title_indices: List[int] = []
    for idx in range(episode_idx + 1, len(tokens)):
        token = tokens[idx]
        if is_separator_token(token):
            continue
        if is_resolution(token) or is_source(token) or is_special(token) or is_noise_bracket(token):
            fallback_categories[idx] = "resolution" if is_resolution(token) else "source" if is_source(token) else "special" if is_special(token) else "sep"
            continue
        title_indices.append(idx)

    if not title_indices:
        # Some rows are title-only brackets followed by season/episode,
        # e.g. [伊蘇] II-01. If the leading bracket was guessed as GROUP but
        # no real title exists, use it as TITLE to keep the row useful.
        for idx in range(episode_idx):
            if categories[idx] == "group" and clean_bracket(tokens[idx]):
                title_indices.append(idx)
                break

    for idx in title_indices:
        fallback_categories[idx] = "title"
    if title_indices:
        for idx in range(title_indices[0], title_indices[-1] + 1):
            if is_separator_token(tokens[idx]):
                fallback_categories[idx] = "title"

    return finalize_weak_sample(tokens, fallback_categories, tokenizer)


def fallback_minimal_sample(
    tokens: Sequence[str],
    episode_idx: int,
    tokenizer: AnimeTokenizer,
) -> Optional[dict]:
    """Keep malformed low-information rows instead of silently dropping them."""
    categories: List[str] = []
    title_idx: Optional[int] = None

    for idx, token in enumerate(tokens):
        if idx == episode_idx:
            categories.append("episode")
        elif is_resolution(token):
            categories.append("resolution")
        elif is_source(token):
            categories.append("source")
        elif is_special(token):
            categories.append("special")
            if title_idx is None:
                title_idx = idx
        else:
            categories.append("sep")

    if title_idx is None:
        for idx, token in enumerate(tokens):
            if idx == episode_idx or is_separator_token(token):
                continue
            if categories[idx] not in {"resolution", "source"}:
                title_idx = idx
                break
    if title_idx is None:
        return None

    categories[title_idx] = "title"
    return finalize_weak_sample(tokens, categories, tokenizer)


def fallback_no_episode_sample(tokens: Sequence[str], tokenizer: AnimeTokenizer) -> Optional[dict]:
    """Label movies, OP/ED/SP, and malformed rows that have no true episode token."""
    categories: List[str] = []
    seen_title = False
    title_allowed = True

    for idx, token in enumerate(tokens):
        if is_separator_token(token):
            categories.append("title" if seen_title and title_allowed else "sep")
            continue
        if idx == 0 and is_group_bracket(token, idx, tokens):
            categories.append("group")
            continue
        if is_resolution(token):
            categories.append("resolution")
            title_allowed = False
            continue
        if is_source(token):
            categories.append("source")
            title_allowed = False
            continue
        if is_special(token):
            categories.append("special")
            title_allowed = False
            continue
        if is_noise_bracket(token):
            categories.append("sep")
            continue
        categories.append("title")
        seen_title = True

    return finalize_weak_sample(tokens, categories, tokenizer, require_episode=False)


def bracket_delimiters(token: str) -> tuple[str, str]:
    open_char = token[0] if token and token[0] in "[【(《" else ""
    close_char = token[-1] if token and token[-1] in "]】)》" else ""
    return open_char, close_char


def label_bracket_contents(token: str, category: str, tokenizer: AnimeTokenizer) -> tuple[List[str], List[str]]:
    inner = clean_bracket(token)
    if not inner:
        return [token], [category]
    open_char, close_char = bracket_delimiters(token)
    inner_tokens = tokenizer.tokenize(inner)
    tokens: List[str] = []
    cats: List[str] = []
    if open_char:
        tokens.append(open_char)
        cats.append("sep")
    tokens.extend(inner_tokens)
    cats.extend([category] * len(inner_tokens))
    if close_char:
        tokens.append(close_char)
        cats.append("sep")
    return tokens, cats


def label_meta_bracket_contents(token: str, tokenizer: AnimeTokenizer) -> tuple[List[str], List[str]]:
    inner = clean_bracket(token)
    if not inner:
        return [token], ["sep"]
    open_char, close_char = bracket_delimiters(token)
    inner_tokens = tokenizer.tokenize(inner)
    tokens: List[str] = []
    cats: List[str] = []
    if open_char:
        tokens.append(open_char)
        cats.append("sep")
    for inner_token in inner_tokens:
        if inner_token in {" ", "-", "_", "|", "~", "~", ".", "+", "&", "/", ","}:
            cat = "sep"
        elif is_resolution(inner_token) or RESOLUTION_SEARCH_RE.fullmatch(inner_token):
            cat = "resolution"
        elif is_source(inner_token):
            cat = "source"
        elif is_special(inner_token):
            cat = "special"
        elif is_noise_bracket(inner_token):
            cat = "sep"
        else:
            cat = "sep"
        tokens.append(inner_token)
        cats.append(cat)
    if close_char:
        tokens.append(close_char)
        cats.append("sep")
    return tokens, cats


def expand_tokens_and_categories(
    tokens: Sequence[str],
    categories: Sequence[str],
    tokenizer: AnimeTokenizer,
) -> tuple[List[str], List[str]]:
    expanded_tokens: List[str] = []
    expanded_categories: List[str] = []
    for token, category in zip(tokens, categories):
        clean = clean_bracket(token)
        if category == "season":
            match = SXE_RE.match(clean)
            if match:
                expanded_tokens.extend([match.group(1), match.group(2)])
                expanded_categories.extend(["season", "episode"])
                continue
        if category in {"group", "title"} and (
            token.startswith("[") or token.startswith("(") or token.startswith("【") or token.startswith("《")
        ):
            split_tokens, split_categories = label_bracket_contents(token, category, tokenizer)
            expanded_tokens.extend(split_tokens)
            expanded_categories.extend(split_categories)
            continue
        if category in {"source", "resolution", "special", "sep"} and (
            token.startswith("[") or token.startswith("(") or token.startswith("【") or token.startswith("《")
        ):
            split_tokens, split_categories = label_meta_bracket_contents(token, tokenizer)
            if any(cat != "sep" for cat in split_categories):
                expanded_tokens.extend(split_tokens)
                expanded_categories.extend(split_categories)
                continue
        expanded_tokens.append(token)
        expanded_categories.append(category)
    return expanded_tokens, expanded_categories


def weak_label_filename(filename: str, tokenizer: AnimeTokenizer) -> Optional[dict]:
    basename = normalize_path_basename(str(filename))
    stem, ext = strip_video_extension(basename)
    if ext in VIDEO_EXTENSIONS:
        filename = stem
    else:
        filename = basename

    tokens = tokenizer.tokenize(filename)
    if not tokens:
        return None
    if has_embedded_episode_candidate(tokens):
        embedded_sample = fallback_embedded_episode_sample(tokens, tokenizer)
        if embedded_sample is not None:
            return embedded_sample

    categories = ["sep" if token in {" ", "-", "_", "|", "~", "~", "."} else "title" for token in tokens]

    for idx, token in enumerate(tokens):
        if is_group_bracket(token, idx, tokens):
            categories[idx] = "group"

    for idx, token in enumerate(tokens):
        if categories[idx] == "group":
            continue
        if is_category_bracket(token):
            categories[idx] = "sep"
        elif is_resolution(token):
            categories[idx] = "resolution"
        elif is_source(token):
            categories[idx] = "source"
        elif is_special(token):
            categories[idx] = "special"
        elif is_explicit_season(token):
            categories[idx] = "season"
        elif is_noise_bracket(token):
            categories[idx] = "sep"

    episode_idx = find_episode_index(tokens)
    if episode_idx is None:
        return fallback_embedded_episode_sample(tokens, tokenizer) or fallback_no_episode_sample(tokens, tokenizer)
    categories[episode_idx] = "episode"
    label_context_season_tokens(tokens, categories, episode_idx)
    repair_structured_bracket_title_aliases(tokens, categories, episode_idx)

    # S01E07 is tokenized as S01 + E07 after tokenizer changes. If an older
    # token slips through, expand_tokens_and_categories will split it.
    clean_episode = clean_bracket(tokens[episode_idx])
    sxe_match = SXE_RE.match(clean_episode)
    if sxe_match:
        categories[episode_idx] = "season"
    elif not any(cat == "season" for cat in categories[:episode_idx]):
        for idx in range(episode_idx - 1, -1, -1):
            if categories[idx] == "sep":
                continue
            clean = clean_bracket(tokens[idx])
            if re.fullmatch(r"[0-9]+", clean) and 1 <= int(clean) <= 20 and not (
                tokens[idx].startswith("[") or tokens[idx].startswith("(") or tokens[idx].startswith("【")
            ):
                categories[idx] = "season"
            break

    title_end = episode_idx
    while title_end > 0 and categories[title_end - 1] in {"season", "sep"}:
        title_end -= 1
    title_start = 0
    while title_start < title_end and categories[title_start] in {"group", "sep", "source", "resolution", "special"}:
        title_start += 1
    title_start, title_end = trim_title_span(tokens, title_start, title_end)
    if title_start >= title_end:
        return fallback_embedded_episode_sample(tokens, tokenizer) or fallback_episode_first_sample(
            tokens, categories, episode_idx, tokenizer
        ) or fallback_minimal_sample(
            tokens, episode_idx, tokenizer
        )

    for idx, token in enumerate(tokens):
        if title_start <= idx < title_end:
            if categories[idx] not in {"group", "season", "episode", "resolution", "source", "special"}:
                categories[idx] = "title"
        elif categories[idx] == "title":
            categories[idx] = "sep"

    if not any(cat == "title" for cat in categories) or not any(cat == "episode" for cat in categories):
        return fallback_embedded_episode_sample(tokens, tokenizer) or fallback_episode_first_sample(
            tokens, categories, episode_idx, tokenizer
        ) or fallback_minimal_sample(
            tokens, episode_idx, tokenizer
        )

    return finalize_weak_sample(tokens, categories, tokenizer)


def iter_db_rows(db_path: Path, min_id: int, max_id: int) -> Iterable[tuple[int, str]]:
    uri = f"file:{db_path}?mode=ro"
    conn = sqlite3.connect(uri, uri=True, timeout=30)
    conn.execute("PRAGMA query_only=ON")
    try:
        query = "SELECT id, filename FROM files WHERE id >= ? AND id <= ? ORDER BY id"
        yield from conn.execute(query, (min_id, max_id))
    finally:
        conn.close()


def export_dataset(args: argparse.Namespace) -> None:
    db_path = Path(args.db)
    output_path = Path(args.output)
    output_path.parent.mkdir(parents=True, exist_ok=True)

    conn = sqlite3.connect(f"file:{db_path}?mode=ro", uri=True, timeout=30)
    conn.execute("PRAGMA query_only=ON")
    try:
        db_max_id = conn.execute("SELECT MAX(id) FROM files").fetchone()[0] or 0
        max_id = min(args.max_id if args.max_id is not None else db_max_id, db_max_id)
    finally:
        conn.close()

    base_vocab = None
    if args.base_vocab:
        base_tokenizer = AnimeTokenizer(vocab_file=args.base_vocab)
        base_vocab = base_tokenizer.get_vocab()
    tokenizer = AnimeTokenizer()
    stats = ExportStats()
    seen_basenames: set[str] = set()
    token_lists: List[List[str]] = []
    label_counter: Counter[str] = Counter()
    examples: List[dict] = []

    with output_path.open("w", encoding="utf-8") as out:
        for file_id, raw_filename in iter_db_rows(db_path, args.min_id, max_id):
            stats.scanned_rows += 1
            basename = normalize_path_basename(raw_filename)
            stem, ext = strip_video_extension(basename)
            if ext not in VIDEO_EXTENSIONS:
                continue
            stats.video_rows += 1
            if stem in seen_basenames:
                stats.duplicate_basenames += 1
                continue
            seen_basenames.add(stem)
            if len(stem) < args.min_chars:
                stats.skipped_too_short += 1
                continue
            if len(stem) > args.max_chars:
                stats.skipped_too_long += 1
                continue

            sample = weak_label_filename(stem, tokenizer)
            if sample is None:
                # Most failures are no confident episode or no title; keep the
                # manifest aggregate conservative instead of over-classifying.
                stats.skipped_no_episode += 1
                continue

            labels = sample["labels"]
            if not any(label.endswith("TITLE") for label in labels):
                stats.skipped_no_title += 1
                continue
            if not any(label.endswith("EPISODE") for label in labels):
                stats.skipped_no_episode += 1
                continue

            record = {
                "file_id": file_id,
                "filename": stem,
                "tokens": sample["tokens"],
                "labels": labels,
            }
            out.write(json.dumps(record, ensure_ascii=False) + "\n")
            stats.labeled_samples += 1
            token_lists.append(sample["tokens"])
            label_counter.update(labels)
            if len(examples) < args.example_count:
                examples.append(record)
            if args.limit and stats.labeled_samples >= args.limit:
                break

    tokenizer.build_vocab(token_lists, max_size=args.max_vocab_size, base_vocab=base_vocab)
    tokenizer.save_vocabulary(output_path.parent)

    manifest = {
        "created_at": datetime.now(timezone.utc).isoformat(),
        "source_db": str(db_path),
        "output": str(output_path),
        "min_file_id": args.min_id,
        "last_file_id": max_id,
        "db_max_file_id_at_export_start": db_max_id,
        "limit": args.limit,
        "stats": stats.__dict__,
        "label_counts": dict(label_counter),
        "vocab_size": tokenizer.vocab_size,
        "notes": [
            "Rows are a snapshot of files.id <= last_file_id.",
            "Future incremental export can use --min-id last_file_id+1.",
            "Weak labels target GROUP, TITLE, SEASON, and EPISODE; media tags are boundary labels/noise.",
        ],
        "examples": examples,
    }
    manifest_path = output_path.with_suffix(".manifest.json")
    manifest_path.write_text(json.dumps(manifest, ensure_ascii=False, indent=2), encoding="utf-8")

    print(json.dumps({k: v for k, v in manifest.items() if k != "examples"}, ensure_ascii=False, indent=2))


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description="Export weakly-labeled DMHY filename dataset")
    parser.add_argument("--db", default=r"D:\WorkSpace\Python\dmhy-parser\dmhy_anime.db", help="DMHY SQLite database")
    parser.add_argument("--output", default="data/dmhy_weak.jsonl", help="Output JSONL path")
    parser.add_argument("--min-id", type=int, default=1, help="Minimum files.id to export")
    parser.add_argument("--max-id", type=int, default=None, help="Maximum files.id to export; defaults to current DB max")
    parser.add_argument("--limit", type=int, default=None, help="Maximum labeled samples to write")
    parser.add_argument("--min-chars", type=int, default=4, help="Minimum stem length")
    parser.add_argument("--max-chars", type=int, default=180, help="Maximum stem length")
    parser.add_argument("--example-count", type=int, default=20, help="Examples to include in manifest")
    parser.add_argument("--base-vocab", default=None, help="Optional vocab whose IDs should be preserved")
    parser.add_argument("--max-vocab-size", type=int, default=3000, help="Maximum vocab size including special tokens")
    parser.add_argument("--seed", type=int, default=42, help="Random seed")
    return parser.parse_args()


if __name__ == "__main__":
    parsed_args = parse_args()
    random.seed(parsed_args.seed)
    export_dataset(parsed_args)