Files
omni-parse/tools/rescore.py

44 lines
1.6 KiB
Python

"""Пересчёт score всех записей текущей формулой rank.py (после правок веса).
python tools/rescore.py # показать топ-15 без записи
python tools/rescore.py --apply # обновить items.score
"""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from omni import config, rank, store # noqa: E402
class _It:
__slots__ = ("provider", "kind", "title", "text", "published", "price", "score")
def __init__(self, provider, kind, title, text, published, price):
self.provider, self.kind = provider, kind
self.title, self.text = title or "", text or ""
self.published, self.price, self.score = published or "", price, 0.0
def main():
apply = "--apply" in sys.argv
c = store.conn()
rows = c.execute("SELECT id, provider, kind, title, text, published, price, score FROM items").fetchall()
upd, top = [], []
for iid, prov, kind, title, text, pub, price, old in rows:
it = rank.score(_It(prov, kind, title, text, pub, price))
upd.append((it.score, iid))
top.append((it.score, old, prov, title or ""))
top.sort(reverse=True)
print(f"записей {len(rows)}; топ по новой формуле:")
for sc, old, prov, title in top[:15]:
print(f" {sc:6.2f} (было {old:5.2f}) {prov[:10]:10} {title[:72]}")
if apply:
c.executemany("UPDATE items SET score=? WHERE id=?", upd)
c.commit()
print("обновлено", len(upd))
c.close()
if __name__ == "__main__":
main()