coderound-task-backend / app /pipeline /skill_parser.py
AnishDev's picture
initial code and logic pushed
ff7b177
Raw History Blame Contribute Delete
9.6 kB
"""
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