File size: 9,595 Bytes
ff7b177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
"""
Proficiency-aware skill parsing.

Solves: "Core Java" (level 4) β‰  "Basic Java" (level 2) β‰  "Java" (level 3 default).

Each skill token is parsed into (normalized_name, level 1-5, qualifier_found).
Level scale:
  5 = Expert/Mastery
  4 = Core/Advanced/Strong/Deep
  3 = Intermediate/Proficient (DEFAULT for unqualified)
  2 = Basic/Beginner/Exposure
  1 = Theoretical/Academic/Awareness
"""
import re
from app.models.domain import SkillEntry

# ─── Proficiency map ──────────────────────────────────────────────────────────

_QUALIFIER_LEVELS: list[tuple[int, list[str]]] = [
    (5, ["expert", "mastery", "mastered", "guru", "specialist"]),
    (4, [
        "core", "advanced", "strong", "deep", "extensive", "senior",
        "production", "in-depth", "high proficiency", "highly proficient",
    ]),
    (3, [
        "proficient", "intermediate", "working knowledge", "solid",
        "good", "hands-on", "hands on", "experienced", "skilled",
        "comfortable", "competent",
    ]),
    (2, [
        "basic", "basics", "beginner", "elementary", "familiar",
        "exposure", "learning", "introductory", "intro",
        "fundamental", "foundational",
    ]),
    (1, [
        "theoretical", "academic", "theory", "awareness",
        "conceptual", "introductory knowledge",
    ]),
]

# Build flat lookup: qualifier_text β†’ level  (longer phrases first)
_QUALIFIER_MAP: dict[str, int] = {}
for _lvl, _quals in _QUALIFIER_LEVELS:
    for _q in _quals:
        _QUALIFIER_MAP[_q] = _lvl

_SORTED_QUALIFIERS = sorted(_QUALIFIER_MAP.keys(), key=len, reverse=True)

# ─── Skill alias map ──────────────────────────────────────────────────────────

SKILL_ALIASES: dict[str, str] = {
    # JavaScript ecosystem
    "react.js": "react", "reactjs": "react", "react js": "react",
    "node.js": "nodejs", "node js": "nodejs",
    "vue.js": "vue", "vuejs": "vue",
    "express.js": "express", "expressjs": "express",
    "next.js": "nextjs", "next js": "nextjs",
    "angular.js": "angularjs",
    "typescript": "typescript", "ts": "typescript",
    "javascript": "javascript", "js": "javascript",
    # Python ecosystem
    "fast api": "fastapi", "fast-api": "fastapi",
    "django rest framework": "django rest framework", "drf": "django rest framework",
    "scikit learn": "scikit-learn", "sklearn": "scikit-learn",
    "sci-kit learn": "scikit-learn",
    "langchain": "langchain", "lang chain": "langchain",
    "langraph": "langgraph", "lang graph": "langgraph",
    # AI/ML
    "large language models": "llm", "large language model": "llm",
    "llms": "llm", "llm": "llm",
    "retrieval augmented generation": "rag",
    "retrieval-augmented generation": "rag",
    "vector database": "vector databases", "vector db": "vector databases",
    "vector store": "vector databases",
    "generative ai": "generative ai", "gen ai": "generative ai",
    "genai": "generative ai", "gen-ai": "generative ai",
    "tensorflow": "tensorflow", "tf": "tensorflow",
    "pytorch": "pytorch", "torch": "pytorch",
    "hugging face": "huggingface", "hf": "huggingface",
    # Databases
    "postgresql": "postgresql", "postgres": "postgresql", "postgres sql": "postgresql",
    "mongodb": "mongodb", "mongo": "mongodb",
    "elasticsearch": "elasticsearch", "elastic search": "elasticsearch",
    # DevOps/Cloud
    "kubernetes": "kubernetes", "k8s": "kubernetes",
    "amazon web services": "aws", "amazon aws": "aws",
    "google cloud platform": "gcp", "google cloud": "gcp",
    "microsoft azure": "azure",
    "github actions": "github actions", "gh actions": "github actions",
    # REST
    "rest api": "rest apis", "restful api": "rest apis",
    "restful apis": "rest apis", "rest": "rest apis",
    "rest api development": "rest apis",
    # Java
    "spring boot": "springboot", "spring-boot": "springboot",
    "java spring": "springboot",
    # Misc
    "message queue": "message queues", "mq": "message queues",
    "rabbitmq": "rabbitmq", "rabbit mq": "rabbitmq",
    "ci/cd": "ci/cd", "cicd": "ci/cd",
    "websocket": "websockets", "web socket": "websockets",
    "natural language processing": "nlp",
    "computer vision": "computer vision", "cv": "computer vision",
}


def normalize_skill_name(raw: str) -> str:
    """Lowercase, strip parens/qualifiers, apply alias."""
    s = raw.lower().strip()
    # Remove parenthetical suffixes: "Python (Advanced)" β†’ "python"
    s = re.sub(r"\s*\(.*?\)", "", s).strip()
    # Remove colon suffixes: "SQL: Advanced Queries" β†’ "sql"
    s = re.sub(r":.*$", "", s).strip()
    # Apply alias
    return SKILL_ALIASES.get(s, s)


def parse_skill_token(token: str) -> SkillEntry:
    """
    Parse one skill token from a comma-separated skills list.

    Returns SkillEntry with normalized name, level 1-5, qualifier string.

    Examples:
      "Core Java"      β†’ name="java",  level=4, qualifier="core"
      "Basic Python"   β†’ name="python", level=2, qualifier="basic"
      "Python"         β†’ name="python", level=3, qualifier=None  (default)
      "Python (Advanced)" β†’ name="python", level=4, qualifier="advanced"
      "SQL: Advanced"  β†’ name="sql",   level=4, qualifier="advanced"
    """
    token = token.strip()
    lower = token.lower()

    # 1. Check prefix qualifiers (longest first to avoid "basic" matching "basics")
    for qualifier in _SORTED_QUALIFIERS:
        if lower.startswith(qualifier + " ") or lower.startswith(qualifier + "-"):
            base_raw = token[len(qualifier):].strip().lstrip("-").strip()
            level = _QUALIFIER_MAP[qualifier]
            return SkillEntry(
                name=normalize_skill_name(base_raw),
                raw=token,
                level=level,
                qualifier=qualifier,
            )

    # 2. Check parenthetical suffix: "Python (Advanced)"
    m = re.match(r"^(.+?)\s*\(([^)]+)\)\s*$", lower)
    if m:
        base_lower, paren_lower = m.group(1).strip(), m.group(2).strip()
        for qualifier in _SORTED_QUALIFIERS:
            if paren_lower == qualifier or paren_lower.startswith(qualifier):
                base_raw = token[: token.lower().find("(")].strip()
                return SkillEntry(
                    name=normalize_skill_name(base_raw),
                    raw=token,
                    level=_QUALIFIER_MAP[qualifier],
                    qualifier=qualifier,
                )

    # 3. Check colon pattern: "SQL: Advanced Queries"
    m = re.match(r"^(.+?)\s*:\s*(\w[\w\s]*)$", lower)
    if m:
        base_lower, suffix_lower = m.group(1).strip(), m.group(2).strip()
        for qualifier in _SORTED_QUALIFIERS:
            if suffix_lower.startswith(qualifier):
                base_raw = token[: token.lower().find(":")].strip()
                return SkillEntry(
                    name=normalize_skill_name(base_raw),
                    raw=token,
                    level=_QUALIFIER_MAP[qualifier],
                    qualifier=qualifier,
                )

    # 4. No qualifier β†’ default intermediate (3)
    return SkillEntry(
        name=normalize_skill_name(token),
        raw=token,
        level=3,
        qualifier=None,
    )


def parse_skills_string(skills_text: str) -> list[SkillEntry]:
    """Parse comma-separated skills string into structured list."""
    if not skills_text or not skills_text.strip():
        return []

    tokens = [t.strip() for t in skills_text.split(",") if t.strip()]
    entries: list[SkillEntry] = []
    seen: set[str] = set()

    for token in tokens:
        entry = parse_skill_token(token)
        if entry.name and entry.name not in seen:
            seen.add(entry.name)
            entries.append(entry)

    return entries


def upgrade_levels_from_work_exp(
    skills: list[SkillEntry],
    work_experiences: list,  # list[WorkExperience]
) -> list[SkillEntry]:
    """
    Upgrade unqualified skills (level=3) based on depth in work experience.

    Rules:
      - Skill appears in job title across any role β†’ upgrade to 4
      - Skill mentioned in descriptions of 2+ roles β†’ upgrade to 4
      - Skill mentioned in 1 role description β†’ keep 3
      - Not found β†’ keep 3 (skill may be peripheral to their work)
    """
    # Build work experience corpus for fast lookup
    role_corpus: list[str] = []
    desc_corpus: list[str] = []

    for exp in work_experiences:
        if hasattr(exp, "role"):
            role_corpus.append(exp.role.lower())
            desc_corpus.append(exp.description.lower())

    upgraded: list[SkillEntry] = []
    for skill in skills:
        if skill.qualifier is not None or skill.level != 3:
            # Already has explicit qualifier β€” don't override
            upgraded.append(skill)
            continue

        name = skill.name.lower()
        # Also check the original raw text for matching
        raw_lower = skill.raw.lower()

        in_title = any(name in role or raw_lower in role for role in role_corpus)
        desc_matches = sum(
            1 for desc in desc_corpus if name in desc or raw_lower in desc
        )

        if in_title or desc_matches >= 2:
            upgraded.append(SkillEntry(
                name=skill.name,
                raw=skill.raw,
                level=4,
                qualifier="inferred_from_experience",
            ))
        else:
            upgraded.append(skill)

    return upgraded