Spaces:
Sleeping
Sleeping
Rajan Sharma
commited on
Update column_resolver.py
Browse files- column_resolver.py +23 -12
column_resolver.py
CHANGED
|
@@ -1,18 +1,29 @@
|
|
| 1 |
import re
|
| 2 |
-
from typing import List, Dict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def _norm(s: str) -> str:
|
| 5 |
return re.sub(r'[^a-z0-9]+', '', s.lower())
|
| 6 |
|
| 7 |
-
def
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
for c in columns:
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
return [alias.get(_norm(r), r) for r in requested]
|
|
|
|
| 1 |
import re
|
| 2 |
+
from typing import List, Dict, Tuple
|
| 3 |
+
|
| 4 |
+
ROLE_SYNONYMS = {
|
| 5 |
+
"facility": ["facility", "hospital", "site", "centre", "clinic", "settlement", "community"],
|
| 6 |
+
"zone": ["zone", "region", "area", "district"],
|
| 7 |
+
"specialty": ["specialty", "service", "discipline"],
|
| 8 |
+
"population": ["population", "residents", "members"],
|
| 9 |
+
"cost": ["cost", "expense", "spend"],
|
| 10 |
+
"outcome": ["outcome", "improvement", "result"]
|
| 11 |
+
}
|
| 12 |
|
| 13 |
def _norm(s: str) -> str:
|
| 14 |
return re.sub(r'[^a-z0-9]+', '', s.lower())
|
| 15 |
|
| 16 |
+
def resolve_role(role: str, columns: List[str]) -> Tuple[str | None, float]:
|
| 17 |
+
"""Return best matching column for semantic role with confidence score."""
|
| 18 |
+
target = _norm(role)
|
| 19 |
+
syns = ROLE_SYNONYMS.get(target, [role])
|
| 20 |
+
best, score = None, 0.0
|
| 21 |
for c in columns:
|
| 22 |
+
nc = _norm(c)
|
| 23 |
+
for s in syns:
|
| 24 |
+
ns = _norm(s)
|
| 25 |
+
if ns in nc or nc in ns:
|
| 26 |
+
sc = len(ns) / max(len(nc), 1)
|
| 27 |
+
if sc > score:
|
| 28 |
+
best, score = c, sc
|
| 29 |
+
return best, score
|
|
|