match_slots() 함수 구현 — 구간 슬롯 배정 기능

통짜 모드에서 영상 구간을 n개 슬롯으로 나누고 각 슬롯에 댓글을 배정하는 순수 함수를 추가했다.
- match_ranges()를 활용해 슬롯 시간대와 매칭되는 댓글을 찾음
- 좋아요 높은 댓글부터 슬롯에 배정 (슬롯당 최대 1개)
- 한 댓글이 여러 슬롯에 중복되지 않도록 관리
- exclude 집합으로 사전에 제외된 댓글 처리

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hehihoho3@gmail.com 2026-08-04 11:39:39 +09:00
parent 2d12bd0fac
commit 47362a814e

View File

@ -9,7 +9,7 @@ from __future__ import annotations
import json import json
import re import re
import urllib.request import urllib.request
from typing import Dict, List from typing import Dict, List, Optional
H_LAB = "https://h-lab.tolag.shop" H_LAB = "https://h-lab.tolag.shop"
@ -97,3 +97,32 @@ def top_liked(comments: List[Dict], exclude: set, n: int = 20) -> List[int]:
rest = [c for c in comments if c["idx"] not in exclude] rest = [c for c in comments if c["idx"] not in exclude]
rest.sort(key=lambda c: -c["likeCount"]) rest.sort(key=lambda c: -c["likeCount"])
return [c["idx"] for c in rest[:n]] return [c["idx"] for c in rest[:n]]
def match_slots(comments: List[Dict], start: float, total: float, n: int,
exclude=None) -> List[Optional[int]]:
"""구간을 n개 슬롯으로 잘라 슬롯마다 댓글 1개씩 배정 — 통짜 모드용.
슬롯 k = 원본 [start + k·total/n, start + (k+1)·total/n).
통짜 모드는 구간을 그대로 이어붙여 **타임라인 시각 = 원본 시각**이라 계산이 성립한다
( 있는 모드는 순서를 섞어 재배치하므로 쓰면 된다 내용 기반 추천을 ).
슬롯은 시간대를 언급한 댓글 좋아요 최다 1. 없으면 None.
댓글이 슬롯에 겹치지 않는다. exclude(idx 집합) 처음부터 제외.
Returns: 길이 n 리스트 (n<=0 이거나 total<=0 이면 리스트)
"""
if n <= 0 or total <= 0:
return []
used = set(exclude or ())
step = total / n
out: List[Optional[int]] = []
for k in range(n):
s = start + k * step
pick = None
for idx in match_ranges(comments, [(s, s + step)]):
if idx not in used:
pick = idx
used.add(idx)
break
out.append(pick)
return out