Files
mama-box/tools/whcm/build.py

62 lines
3.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""孕妈悄悄话置顶笔记的 100 条模拟祝福评论(11-07 用户:「评论里按正常评论放上 100 个」)。
lines.txt 一行一条,「||」后面是楼中楼回复(其他孕妈/宝妈)。人设从 cm.json 的 1000 人里挑:主评论用孕期人设、回复混宝妈;
尽量挑在别的帖子下出现少的人。确定性(固定种子),重跑结果不变;只追加/覆盖 c[TARGET],不动其他帖子。"""
import json, random
from collections import Counter
TARGET = 'wh-20260928'
P = 'guangchang/cm.json'
d = json.load(open(P, encoding='utf-8'))
used = Counter(c[0] for k, L in d['c'].items() if k != TARGET for c in L)
rnd = random.Random(20261107)
import re
def wk(p):
m = re.match(r'孕 ?(\d+)', p[2]); return int(m.group(1)) if m else 0
# 内容多是胎动、产检、待产(孕中晚期的事)→ 主评论只用孕 24 周以上的人设,免得「孕 6 周」说「头朝下了」
preg = sorted([i for i, p in enumerate(d['p']) if wk(p) >= 24], key=lambda i: (used[i], i))
anyp = sorted(range(len(d['p'])), key=lambda i: (used[i], i))
mains = preg[:200]; rnd.shuffle(mains)
reps = anyp[:300]; rnd.shuffle(reps)
lines = [l.strip() for l in open('tools/whcm/lines.txt', encoding='utf-8') if l.strip()]
assert len(lines) == 100, len(lines)
out, ri = [], 0
for k, l in enumerate(lines):
parts = l.split('||')
likes = max(3, int(520 * (0.965 ** k)) + rnd.randint(-18, 18))
rr = []
for t in parts[1:]:
rr.append([reps[ri], t]); ri += 1
out.append([mains[k], parts[0], likes, rr])
# 11-07 用户:「评论里也加一些孕早期的孕妈,看到别人有胎动也有期待,也有自己的话对孩子说;都要真人感」
# early.txt = 孕 6~13 周人设的留言(回复用孕中晚期/宝妈人设,像过来人);均匀插进前 100 条中间,不全堆在后面
# early_rep.txt = 「原留言开头|早孕妈妈的回复」,挂在已有留言楼里(「看你们说胎动好羡慕」这类)
early_p = sorted([i for i, p in enumerate(d['p']) if 6 <= wk(p) <= 13 and i not in set(reps)], key=lambda i: (used[i], i))
er = random.Random(20261108); er.shuffle(early_p)
ei = 0
for t in [l.strip() for l in open('tools/whcm/early_rep.txt', encoding='utf-8') if l.strip()]:
head, txt = t.split('|', 1)
hit = [c for c in out if c[1].startswith(head)]
assert len(hit) == 1, head
hit[0][3].append([early_p[ei], txt]); ei += 1
early = [l.strip() for l in open('tools/whcm/early.txt', encoding='utf-8') if l.strip()]
eo = []
for k, l in enumerate(early):
parts = l.split('||')
rr = []
for t in parts[1:]:
rr.append([reps[ri], t]); ri += 1
eo.append([early_p[ei], parts[0], 0, rr]); ei += 1
# 插进去:从第 3 条起每隔约 2~3 条放一条早孕的,赞数取前后两条之间
merged = []
step = len(out) / len(eo)
j = 0
for k, c in enumerate(out):
merged.append(c)
while j < len(eo) and k >= 2 + j * step * .96:
e = eo[j]; nxt = out[k + 1][2] if k + 1 < len(out) else 3
e[2] = max(3, (c[2] + nxt) // 2 + rnd.randint(-4, 4)); merged.append(e); j += 1
merged += eo[j:]
out = merged
d['c'][TARGET] = out
json.dump(d, open(P, 'w', encoding='utf-8'), ensure_ascii=False, separators=(',', ':'))
print('ok', len(out), 'replies', ri, 'early', len(eo), 'early-replies', ei - len(eo))