← back to Japan Enrich
upgrade_lilycolor_titles.py
57 lines
#!/usr/bin/env python3
"""
Upgrade the 151 derived-fallback Lilycolor titles to real English translations
now that ollama has recovered. Targets rows with title_en_source=='derived-fallback'
that still have a Japanese title_ja pattern name (first '|' segment). Per-item
translation (reliable, no count-mismatch). Atomic temp+rename. $0 local.
"""
import os, json, re, urllib.request
SRC = "staging/lilycolor-unified-staging.jsonl"
OLLAMA = "http://127.0.0.1:11434/api/generate"
MODEL = "qwen2.5:latest"
PIPE = "|"
CJK = re.compile(r'[-ゟ゠-ヿ一-鿿]')
def translate_one(name):
prompt = ("Translate this Japanese wallcovering pattern name to a concise English "
"design term (1-3 words, e.g. Boucle, Velour, Damask, Stucco, Pottery). "
"Output ONLY the English term, nothing else.\n" + name)
body = json.dumps({"model": MODEL, "stream": False, "prompt": prompt,
"options": {"temperature": 0, "num_predict": 24}}).encode()
req = urllib.request.Request(OLLAMA, data=body, headers={"Content-Type": "application/json"})
raw = json.loads(urllib.request.urlopen(req, timeout=60).read())["response"]
if "</think>" in raw: raw = raw.split("</think>")[-1]
return next((l.strip().strip('"\'`.').strip() for l in raw.splitlines() if l.strip()), "")
def main():
rows = [json.loads(l) for l in open(SRC) if l.strip()]
def pname(r):
t = r.get("title_ja")
return t.split(PIPE)[0].strip() if t else None
targets = [r for r in rows if r.get("title_en_source") == "derived-fallback" and pname(r)]
uniq = sorted({pname(r) for r in targets})
print(f"{len(targets)} derived-fallback rows w/ JP source across {len(uniq)} unique names", flush=True)
fixed = {}
for i, jp in enumerate(uniq):
try:
en = translate_one(jp)
if en and not CJK.search(en): fixed[jp] = en
except Exception as ex:
print(f" '{jp}' failed ({ex})", flush=True)
if (i + 1) % 25 == 0: print(f" {i+1}/{len(uniq)}", flush=True)
n = 0
for r in rows:
p = pname(r)
if r.get("title_en_source") == "derived-fallback" and p in fixed:
r["title_en"] = fixed[p]; r["title_en_source"] = "qwen-mt-draft-peritem"; n += 1
TMP = SRC + ".tmp"
with open(TMP, "w") as f:
for r in rows: f.write(json.dumps(r, ensure_ascii=False) + "\n")
os.replace(TMP, SRC)
remain = sum(1 for r in rows if r.get("title_en_source") == "derived-fallback" and pname(r))
print(f"\nDONE: upgraded {n} rows to real EN; {remain} derived-fallback w/ JP source remain -> {SRC}", flush=True)
if __name__ == "__main__":
main()