File size: 1,175 Bytes
3831e97 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | """Night diary line picker from fixed pool (not NLG)."""
from __future__ import annotations
import json
import random
from pathlib import Path
from typing import List, Optional, Sequence
class DiaryPool:
def __init__(self, lines: List[dict], rng: Optional[random.Random] = None):
self.lines = lines
self.rng = rng or random.Random()
@classmethod
def load(cls, path: Path, rng: Optional[random.Random] = None) -> "DiaryPool":
data = json.loads(path.read_text(encoding="utf-8"))
return cls(list(data.get("lines") or []), rng=rng)
def pick(self, tags: Sequence[str]) -> str:
tagset = set(tags)
scored = []
for line in self.lines:
lt = set(line.get("tags") or [])
score = len(lt & tagset)
if "default" in lt:
score = max(score, 0)
scored.append((score, line.get("text", "")))
scored.sort(key=lambda x: x[0], reverse=True)
best = scored[0][0] if scored else 0
top = [t for s, t in scored if s == best and t]
if not top:
return "巷子很安静。店也是。"
return self.rng.choice(top)
|