videogen/apps/api/tools/moderation/refresh_hate_terms.py
caelum0x d56639a594
Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

69 lines
2.8 KiB
Python
Raw Permalink 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.

"""Diff the curated hate-terms list against the upstream LDNOOBW lists.
Usage::
python apps/api/tools/moderation/refresh_hate_terms.py # fetch upstream, print the diff
python apps/api/tools/moderation/refresh_hate_terms.py --offline DIR # use local copies of en/tr/de
It never edits the list. Upstream mixes slurs with general profanity, so a
person reviews each "new upstream term" and adds the slurs to
``assets/moderation/hate_terms.json`` (category ``upstream``), bumping
``version`` and ``sources[0].commit``. Licence of the upstream data: CC-BY-4.0.
"""
from __future__ import annotations
import argparse
import json
import sys
import unicodedata
import urllib.request
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
LIST_FILE = ROOT / "assets" / "moderation" / "hate_terms.json"
RAW = "https://raw.githubusercontent.com/LDNOOBW/List-of-Dirty-Naughty-Obscene-and-Otherwise-Bad-Words/master/{lang}"
def fold(text: str) -> str:
lowered = str(text or "").replace("İ", "i").replace("I", "i").replace("ı", "i").casefold()
return "".join(c for c in unicodedata.normalize("NFKD", lowered) if not unicodedata.combining(c)).strip()
def upstream_terms(langs: list[str], offline: Path | None) -> dict[str, set[str]]:
out: dict[str, set[str]] = {}
for lang in langs:
if offline:
text = (offline / lang).read_text(encoding="utf-8")
else:
with urllib.request.urlopen(RAW.format(lang=lang), timeout=20) as resp: # noqa: S310 - fixed https URL
text = resp.read().decode("utf-8")
out[lang] = {fold(line) for line in text.splitlines() if line.strip()}
return out
def diff(data: dict, upstream: dict[str, set[str]]) -> dict[str, list[str]]:
kept = {fold(t) for group in data["categories"].values() for t in group.get("upstream", [])}
excluded = {fold(t) for t in data.get("excluded_from_upstream", {})}
every = set().union(*upstream.values()) if upstream else set()
return {
"gone_upstream": sorted(kept - every),
"not_reviewed": sorted(every - kept - excluded),
}
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument("--langs", default="en,tr,de")
parser.add_argument("--offline", type=Path, default=None)
args = parser.parse_args(argv)
data = json.loads(LIST_FILE.read_text(encoding="utf-8"))
result = diff(data, upstream_terms([x for x in args.langs.split(",") if x], args.offline))
print(json.dumps({"version": data["version"], "gone_upstream": result["gone_upstream"],
"not_reviewed_count": len(result["not_reviewed"])}, indent=2))
return 1 if result["gone_upstream"] else 0
if __name__ == "__main__":
sys.exit(main())