Spaces:
Sleeping
Sleeping
Rajan Sharma
commited on
Create column_resolver.py
Browse files- column_resolver.py +22 -0
column_resolver.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 build_alias_map(columns: List[str]) -> Dict[str, str]:
|
| 8 |
+
m = {}
|
| 9 |
+
for c in columns:
|
| 10 |
+
n = _norm(c)
|
| 11 |
+
m[n] = c
|
| 12 |
+
if n.endswith('s') and n[:-1] not in m: m[n[:-1]] = c
|
| 13 |
+
if not n.endswith('s') and n+'s' not in m: m[n+'s'] = c
|
| 14 |
+
return m
|
| 15 |
+
|
| 16 |
+
def resolve_cols(requested: List[str], available: List[str]) -> List[str]:
|
| 17 |
+
alias = build_alias_map(available)
|
| 18 |
+
out = []
|
| 19 |
+
for r in requested:
|
| 20 |
+
n = _norm(r)
|
| 21 |
+
out.append(alias.get(n, r))
|
| 22 |
+
return out
|