"""Job description understanding. Turns a free-text job description into a structured ``JobRequirements`` object: role family, must-have / nice-to-have skills, seniority and an experience band. The goal is to *understand the role*, not to keyword match. We extract a normalized requirement profile that the scorer can reason about, and we also keep the raw text so the semantic encoder can embed the full intent of the JD. """ from __future__ import annotations import re from dataclasses import dataclass, field from typing import Dict, List, Optional # --------------------------------------------------------------------------- # Canonical skill vocabulary grouped by role family. # This lets us recognise a skill even when the JD phrases it differently, and # lets us tell a genuine ML engineer apart from someone who merely lists "AI". # --------------------------------------------------------------------------- ROLE_FAMILIES: Dict[str, Dict[str, List[str]]] = { "ai_ml_engineer": { "titles": [ "ai engineer", "ml engineer", "machine learning engineer", "deep learning engineer", "data scientist", "applied scientist", "research engineer", "nlp engineer", "mlops engineer", "computer vision engineer", "ai/ml", "ai / ml", ], "core_skills": [ "machine learning", "deep learning", "pytorch", "tensorflow", "scikit-learn", "sklearn", "nlp", "natural language processing", "llm", "large language model", "transformers", "embeddings", "vector search", "semantic search", "rag", "retrieval augmented generation", "recommendation", "ranking", "computer vision", "neural network", "model deployment", "feature engineering", "mlops", "hugging face", "fine-tuning", "prompt engineering", "xgboost", "lightgbm", "gradient boosting", ], "support_skills": [ "python", "pandas", "numpy", "docker", "kubernetes", "aws", "gcp", "azure", "sql", "spark", "airflow", "ci/cd", "git", "rest api", "fastapi", "flask", "data pipeline", "model monitoring", "a/b testing", ], }, "software_engineer": { "titles": [ "software engineer", "software developer", "backend engineer", "frontend engineer", "fullstack engineer", "sde", "swe", "platform engineer", "devops engineer", "site reliability", ], "core_skills": [ "java", "golang", "rust", "c++", "c#", "node.js", "react", "microservices", "distributed systems", "system design", "api design", "backend", "frontend", "full stack", ], "support_skills": [ "python", "docker", "kubernetes", "aws", "gcp", "azure", "git", "ci/cd", "sql", "nosql", "redis", "kafka", ], }, "data_engineer": { "titles": [ "data engineer", "analytics engineer", "etl engineer", "data platform", "data infrastructure", ], "core_skills": [ "spark", "airflow", "kafka", "etl", "data pipeline", "dbt", "data warehouse", "snowflake", "bigquery", "redshift", "data lake", "databricks", ], "support_skills": [ "python", "sql", "aws", "gcp", "azure", "docker", "kubernetes", "git", "ci/cd", ], }, } SENIORITY_PATTERNS: Dict[str, List[str]] = { "lead": ["lead", "principal", "staff", "head of", "director", "vp of", "vp,"], "senior": ["senior", "sr.", "sr ", "level 5", "l5", "level 6", "l6"], "mid": ["mid", "intermediate", "level 3", "l3", "level 4", "l4"], "junior": ["junior", "jr.", "jr ", "entry", "associate", "graduate", "level 1", "l1", "level 2", "l2"], } # Tokens that strongly suggest a senior/lead role even without explicit words SENIOR_SIGNALS = [ "5+ years", "6+ years", "7+ years", "8+ years", "10+ years", "own the full", "own and lead", "technical lead", "mentor", "architect", "design and build", "production systems", ] @dataclass class JobRequirements: raw_text: str role_family: str title_keywords: List[str] core_skills: List[str] support_skills: List[str] seniority: str min_years: float max_years: float locations: List[str] = field(default_factory=list) work_modes: List[str] = field(default_factory=list) # Extra derived fields used by the scorer nice_to_have: List[str] = field(default_factory=list) requires_india: bool = False def normalized_skill(self, name: str) -> str: return _normalize(name) def _normalize(text: str) -> str: return re.sub(r"[^a-z0-9+#.\-/ ]", " ", text.lower()).strip() def _detect_role_family(text: str) -> str: best, best_hits = "ai_ml_engineer", -1 for family, spec in ROLE_FAMILIES.items(): hits = sum(2 for t in spec["titles"] if t in text) hits += sum(1 for s in spec["core_skills"] if s in text) if hits > best_hits: best, best_hits = family, hits return best def _detect_seniority(text: str) -> str: for level, pats in SENIORITY_PATTERNS.items(): if any(p in text for p in pats): return level # implicit senior signals if any(sig in text for sig in SENIOR_SIGNALS): return "senior" return "mid" def _detect_years(text: str) -> tuple[float, float]: """Extract experience range from patterns like '5+ years', '3-5 years'.""" # Range: "3 to 5 years", "3-5 years" rng = re.search(r"(\d+)\s*[-–to]+\s*(\d+)\s*\+?\s*years", text) if rng: return float(rng.group(1)), float(rng.group(2)) # Plus: "5+ years" plus = re.search(r"(\d+)\s*\+\s*years", text) if plus: lo = float(plus.group(1)) return lo, lo + 6 # Minimum: "minimum 4 years", "at least 4 years" mini = re.search(r"(?:minimum|at least|more than)\s+(\d+)\s*years", text) if mini: lo = float(mini.group(1)) return lo, lo + 5 # Single: "5 years" single = re.search(r"(\d+)\s*years", text) if single: lo = float(single.group(1)) return lo, lo + 4 return 0.0, 50.0 def _extract_nice_to_have(text: str, family: str) -> List[str]: """Pull skills from 'nice to have' / 'preferred' sections.""" # Find nice-to-have section pattern = re.compile( r"(?:nice[\s\-]to[\s\-]have|preferred|bonus|plus|good to have)[:\s]+(.*?)(?:\n\n|\Z)", re.DOTALL | re.IGNORECASE, ) m = pattern.search(text) if not m: return [] block = _normalize(m.group(1)) spec = ROLE_FAMILIES.get(family, {}) all_vocab = spec.get("core_skills", []) + spec.get("support_skills", []) return [s for s in all_vocab if s in block] def _present_skills(text: str, vocab: List[str]) -> List[str]: return [s for s in vocab if s in text] def parse_job_description(raw_text: str) -> JobRequirements: text = _normalize(raw_text) family = _detect_role_family(text) spec = ROLE_FAMILIES[family] core = _present_skills(text, spec["core_skills"]) or spec["core_skills"] support = _present_skills(text, spec["support_skills"]) or spec["support_skills"] title_kw = [t for t in spec["titles"] if t in text] or spec["titles"][:4] nice = _extract_nice_to_have(raw_text, family) min_y, max_y = _detect_years(text) seniority = _detect_seniority(text) if seniority in ("senior", "lead") and min_y < 5: min_y = max(min_y, 5.0) work_modes = [m for m in ("remote", "hybrid", "onsite", "flexible") if m in text] locations = [c for c in ( "india", "bangalore", "bengaluru", "hyderabad", "pune", "mumbai", "delhi", "chennai", "remote", "new delhi", "noida", "gurgaon", "gurugram", ) if c in text] requires_india = any( tok in text for tok in ("india", "indian", "bangalore", "bengaluru", "hyderabad", "mumbai", "pune", "delhi", "chennai") ) return JobRequirements( raw_text=raw_text, role_family=family, title_keywords=title_kw, core_skills=core, support_skills=support, seniority=seniority, min_years=min_y, max_years=max_y, locations=locations, work_modes=work_modes, nice_to_have=nice, requires_india=requires_india, )