Files
Claude 291ac58217 feat(cleaner): empty groups, merge weights and LoRA spacing rules
Three prompt-syntax rules for WP Prompt Cleaner, run before whitespace so
the existing rules tidy the seams they leave:

- empty_groups drops (), [], ( , ), (:1.2) and groups left empty
- merge_weights folds nested ( ) weights into one group, ComfyUI-style
  (((a:1.1):1.2) -> (a:1.32)), and unwraps a weight of 1
- lora_spacing tidies <lora:...> tags and the space around them

Balanced turns on empty groups + LoRA spacing; aggressive adds merge
weights. The negative column follows the same presets.
2026-10-06 02:31:42 +00:00

43 lines
1.3 KiB
Python

"""Cleaner rule registry.
Each rule module exports a single `apply(text, mode, config)` function
returning a `RuleResult`. The registry below pins the canonical
execution order — the pipeline iterates rules in this order regardless
of how the widget toggles them, so output stays deterministic.
"""
from __future__ import annotations
from collections.abc import Callable
from engine.cleaner.rules import (
blocklist,
dedupe,
lora,
punctuation,
syntax,
whitespace,
)
from engine.cleaner.types import RuleId, RuleResult
ApplyFn = Callable[[str, str, dict], RuleResult]
# The bracket and LoRA rules run first: the commas and spaces they leave
# behind are then tidied by whitespace / punctuation in the same run.
RULE_REGISTRY: list[tuple[RuleId, ApplyFn]] = [
("empty_groups", syntax.apply_empty_groups),
("merge_weights", syntax.apply_merge_weights),
("lora_spacing", lora.apply),
("whitespace", whitespace.apply),
("punctuation", punctuation.apply),
("dedupe_exact", dedupe.apply_exact),
("fuzzy_dedupe", dedupe.apply_fuzzy),
("blocklist", blocklist.apply),
]
def get_rule(rule_id: RuleId) -> ApplyFn:
for rid, fn in RULE_REGISTRY:
if rid == rule_id:
return fn
raise KeyError(f"unknown rule id: {rule_id!r}")