# -*- coding: utf-8 -*-
"""矩阵账号 × 商投佣金：语料合并"""
import glob, json, os, re, sys
from collections import Counter

sys.stdout.reconfigure(encoding="utf-8")
SRC = r"D:\Deepseek\juzhen_corpus\_search"
OUT = r"D:\Deepseek\juzhen_corpus"
REL = ["矩阵", "多账号", "多个号", "商投", "投流", "带货", "达人", "佣金", "账号"]

items = {}
for fp in glob.glob(os.path.join(SRC, "*.json")):
    j = json.load(open(fp, encoding="utf-8"))
    kw = j.get("keyword", "")
    for it in j.get("items", []):
        if it["id"] in items:
            items[it["id"]]["keywords"].append(kw)
            continue
        t = it.get("txt", "")
        parts = [p.strip() for p in t.split("⏐")]
        dur = next((p for p in parts if re.fullmatch(r"\d{1,2}:\d{2}", p)), "")
        likes = next((p for p in parts if re.fullmatch(r"[\d.]+万?", p)), "")
        author = next((p for p in parts if p.startswith("@")), "").lstrip("@")
        desc = next((p for p in parts if p not in (dur, likes) and not p.startswith("@") and not p.startswith("·")), "")
        items[it["id"]] = {"id": it["id"], "desc": desc[:120], "author": author, "dur": dur,
                           "likes": likes, "keywords": [kw], "raw": t[:200]}

rows = [v for v in items.values() if any(k in (v["desc"] + v["raw"]) for k in REL)]
print(f"去重 {len(items)}，过滤后 {len(rows)}")
print("作者 Top:", Counter(r["author"] for r in rows).most_common(8))
json.dump(rows, open(os.path.join(OUT, "_corpus_cards.json"), "w", encoding="utf-8"), ensure_ascii=False, indent=1)
