Spaces:
Sleeping
Sleeping
Create clarity.py
Browse files- nlp/clarity.py +35 -0
nlp/clarity.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .detectors import jargon_density
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def _structure_presence(text: str) -> int:
|
| 5 |
+
lines = [l for l in text.splitlines() if l.strip()]
|
| 6 |
+
if not lines:
|
| 7 |
+
return 0
|
| 8 |
+
# Headings or steps indicate structure
|
| 9 |
+
has_head = any(l.strip().endswith(":") for l in lines)
|
| 10 |
+
has_steps = any(l.strip().lower().startswith(tuple(str(i)+"." for i in range(1,6))) for l in lines)
|
| 11 |
+
return 1 if (has_head or has_steps) else 0
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def _glossary_coverage(text: str) -> float:
|
| 15 |
+
# heuristic: if parentheses appear after a term, assume explained once
|
| 16 |
+
# This is a placeholder; can be replaced by dictionary lookups and pattern checks
|
| 17 |
+
opens = text.count('(')
|
| 18 |
+
closes = text.count(')')
|
| 19 |
+
pairs = min(opens, closes)
|
| 20 |
+
return min(1.0, pairs / 5.0)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def score(text: str):
|
| 24 |
+
jd = jargon_density(text)
|
| 25 |
+
gc = _glossary_coverage(text)
|
| 26 |
+
sp = _structure_presence(text)
|
| 27 |
+
|
| 28 |
+
sub = 0.4*(1 - min(1.0, jd)) + 0.4*gc + 0.2*sp
|
| 29 |
+
sub = max(0.0, min(1.0, sub))
|
| 30 |
+
return {
|
| 31 |
+
'subscore': sub,
|
| 32 |
+
'jargon_density': jd,
|
| 33 |
+
'glossary_coverage': gc,
|
| 34 |
+
'structure_presence': sp,
|
| 35 |
+
}
|