Spaces:
Sleeping
Sleeping
File size: 1,418 Bytes
161cb23 419a527 161cb23 419a527 a1415e8 161cb23 419a527 | 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 35 36 37 38 39 40 41 42 43 44 45 46 | from __future__ import annotations
import hashlib
import importlib.util
from pathlib import Path
import sys
from typing import Any
def _find_repo_root() -> Path:
current = Path(__file__).resolve().parent
for parent in current.parents:
if (parent / "codehygiene" / "__init__.py").is_file():
return parent
return current.parent.parent
REPO_ROOT = _find_repo_root()
LOCAL_PACKAGE = REPO_ROOT / "codehygiene" / "__init__.py"
if importlib.util.find_spec("codehygiene") is None and LOCAL_PACKAGE.is_file():
sys.path.insert(0, str(REPO_ROOT))
import codehygiene
WEIGHTS_PATH = Path(codehygiene.__file__).with_name("weights.npz")
SCORER_VERSION = str(codehygiene.__version__)
# Version plus a weights digest: a recompile that keeps the version string still counts as a new scorer.
SCORER_ID = f"{SCORER_VERSION}+{hashlib.sha256(WEIGHTS_PATH.read_bytes()).hexdigest()[:12]}"
def score_dict(content: str, threshold: float) -> dict[str, Any]:
return codehygiene.score(content, threshold=threshold).to_dict()
def score_value(content: str) -> float:
return float(codehygiene.score(content).score)
def score_file(content: str) -> tuple[float, dict[str, int]]:
result = codehygiene.score(content)
counts: dict[str, int] = {}
for squiggle in result.squiggles:
counts[squiggle.kind] = counts.get(squiggle.kind, 0) + 1
return float(result.score), counts
|