Spaces:
Runtime error
Runtime error
File size: 1,153 Bytes
cc15101 | 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 | """Phase W - the lightweight, from-scratch classifier we own.
A transparent weighted-lexical scorer: it runs the rule sets in rules.py over a
normalized copy of the text and returns a per-category score in [0, 1] (the max
weight of any matching signal for that category). It is intentionally simple and
dependency-free; the `score()` method is the seam a trained model can later
replace without touching any caller.
"""
from server.safety import rules
from server.safety.config import CATEGORIES
class LexicalClassifier:
"""Rule-driven scorer. Swap-compatible with a future learned model."""
def score(self, text: str) -> dict:
scores = {c: 0.0 for c in CATEGORIES}
norm = rules.normalize(text)
if not norm:
return scores
for rx, weight, cat in rules.RULES:
if weight > scores[cat] and rx.search(norm):
scores[cat] = weight
for cat, a_rx, b_rx, weight in rules.COOCCURRENCE:
if weight > scores[cat] and a_rx.search(norm) and b_rx.search(norm):
scores[cat] = weight
return scores
default_classifier = LexicalClassifier()
|