# -*- coding: utf-8 -*-
"""达人商投品：从评论与口播抽取收益结构、投流预算、选品标准"""
import glob, json, os, re, sys
from collections import Counter

sys.stdout.reconfigure(encoding="utf-8")
ROOT = r"D:\Deepseek\shangtou_corpus"
CMT, SUB = os.path.join(ROOT, "comments"), os.path.join(ROOT, "subtitles")

n_c = 0
cms = []
for fp in glob.glob(os.path.join(CMT, "*.json")):
    j = json.load(open(fp, encoding="utf-8"))
    for c in j.get("comments", []):
        cms.append(c)
        n_c += 1

texts = [open(f, encoding="utf-8").read() for f in glob.glob(os.path.join(SUB, "*.txt"))]
print(f"评论 {n_c} 条 | 转写 {len(texts)} 条 / {sum(len(t) for t in texts)} 字")

CATS = {
    "双佣金/佣金率": ["双佣金", "佣金率", "5%", "5个点", "佣金只有", "佣金低", "佣金多少", "30%", "20%", "10%"],
    "投流预算/消耗": ["投了", "投流", "消耗", "预算", "随心推", "千川", "ROI", "保本"],
    "出单/收益": ["出单", "出了", "单没出", "一单", "几单", "赚", "收益"],
    "选品": ["选品", "选什么", "什么品", "退货率", "客单", "爆品", "新品"],
    "授权/合规": ["授权", "违规", "侵权", "责任"],
}
cnt = Counter()
for c in cms:
    t = c.get("text") or ""
    for k, kws in CATS.items():
        if any(w in t for w in kws):
            cnt[k] += 1
print("评论话题分布:", cnt.most_common())

pat = re.compile(r"(\d+(\.\d+)?\s*(万|千|w|k)?\s*(单|元|块|%|个点))")
picked = []
for c in cms:
    t = (c.get("text") or "").strip()
    if t and pat.search(t) and any(w in t for w in ["投", "佣金", "单", "赚", "收益"]):
        picked.append((c.get("digg") or 0, t))
print(f"\n含数字的投流/佣金/单量评论 {len(picked)} 条，按赞 Top 30：")
for d, t in sorted(picked, key=lambda x: -x[0])[:30]:
    print(f"[{d}] {t[:110]}")

print("\n=== 口播中的选品/佣金句 ===")
n = 0
for t in texts:
    for s in re.split(r"[，,。！？!?\n]+", t):
        s = s.strip()
        if 10 < len(s) < 80 and re.search(r"(选品|佣金|投流|商家|退货|客单)", s) and re.search(r"\d", s):
            print(" -", s)
            n += 1
            if n >= 25:
                break
    if n >= 25:
        break
