Spaces:
Paused
Paused
File size: 11,262 Bytes
471f933 33fa314 45f0a42 33fa314 288175b 45f0a42 288175b 45f0a42 288175b 45f0a42 288175b 45f0a42 471f933 288175b 45f0a42 288175b b8deff5 288175b b8deff5 288175b 45f0a42 6248af7 45f0a42 6248af7 45f0a42 6248af7 45f0a42 6248af7 45f0a42 6248af7 45f0a42 b336194 45f0a42 288175b 45f0a42 288175b 45f0a42 |
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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
import re
from pathlib import Path
from typing import Dict, List, Tuple
import spacy
from pdfminer.high_level import extract_text as pdf_extract_text
from docx import Document
from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
import nltk
from nltk.corpus import stopwords
from dateutil.parser import parse as date_parse
# Download required NLTK data
try:
nltk.download('stopwords', quiet=True)
nltk.download('punkt', quiet=True)
except:
pass
# Load spaCy model for better NER
try:
nlp = spacy.load("en_core_web_sm")
except:
print("Please install spacy model: python -m spacy download en_core_web_sm")
nlp = None
MODEL_NAME = "manishiitg/resume-ner"
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForTokenClassification.from_pretrained(MODEL_NAME)
ner_pipeline = pipeline("ner", model=model, tokenizer=tokenizer, aggregation_strategy="simple")
# Expanded keyword lists
SKILL_KEYWORDS = {
# Programming Languages
"python", "java", "javascript", "typescript", "c++", "c#", "ruby", "go", "rust", "kotlin", "swift",
"php", "r", "matlab", "scala", "perl", "bash", "powershell", "sql", "html", "css",
# Frameworks & Libraries
"react", "angular", "vue", "node.js", "express", "django", "flask", "spring", "spring boot",
".net", "laravel", "rails", "fastapi", "pytorch", "tensorflow", "keras", "scikit-learn",
# Databases
"mysql", "postgresql", "mongodb", "redis", "elasticsearch", "cassandra", "oracle", "sql server",
# Cloud & DevOps
"aws", "azure", "gcp", "docker", "kubernetes", "jenkins", "terraform", "ansible", "ci/cd",
# Other Technical Skills
"machine learning", "deep learning", "data science", "nlp", "computer vision", "ai",
"rest api", "graphql", "microservices", "agile", "scrum", "git", "linux", "windows"
}
EDUCATION_PATTERNS = [
# Degrees
r"\b(bachelor|b\.?s\.?c?\.?|b\.?a\.?|b\.?tech|b\.?e\.?)\b",
r"\b(master|m\.?s\.?c?\.?|m\.?a\.?|m\.?tech|m\.?e\.?|mba)\b",
r"\b(ph\.?d\.?|doctorate|doctoral)\b",
r"\b(diploma|certificate|certification)\b",
# Fields of Study
r"\b(computer science|software engineering|information technology|it|cs)\b",
r"\b(electrical engineering|mechanical engineering|civil engineering)\b",
r"\b(data science|artificial intelligence|machine learning)\b",
r"\b(business administration|finance|accounting|marketing)\b",
# Institution indicators
r"\b(university|college|institute|school)\s+of\s+\w+",
r"\b\w+\s+(university|college|institute)\b"
]
JOB_TITLE_PATTERNS = [
r"\b(software|senior|junior|lead|principal|staff)\s*(engineer|developer|programmer)\b",
r"\b(data|business|system|security)\s*(analyst|scientist|engineer)\b",
r"\b(project|product|program|engineering)\s*manager\b",
r"\b(devops|cloud|ml|ai|backend|frontend|full[\s-]?stack)\s*(engineer|developer)\b",
r"\b(consultant|architect|specialist|coordinator|administrator)\b"
]
def extract_text(file_path: str) -> str:
"""Extract text from PDF or DOCX files"""
path = Path(file_path)
if path.suffix.lower() == ".pdf":
text = pdf_extract_text(file_path)
elif path.suffix.lower() == ".docx":
doc = Document(file_path)
text = "\n".join([p.text for p in doc.paragraphs])
else:
raise ValueError("Unsupported file format")
return text
def clean_text(text: str) -> str:
"""Clean and normalize text"""
# Remove multiple spaces and normalize
text = re.sub(r'\s+', ' ', text)
# Keep line breaks for section detection
text = re.sub(r'\n{3,}', '\n\n', text)
return text.strip()
def extract_sections(text: str) -> Dict[str, str]:
"""Extract different sections from resume"""
sections = {
'education': '',
'experience': '',
'skills': '',
'summary': ''
}
# Common section headers
section_patterns = {
'education': r'(education|academic|qualification|degree)',
'experience': r'(experience|employment|work\s*history|professional\s*experience|career)',
'skills': r'(skills|technical\s*skills|competencies|expertise)',
'summary': r'(summary|objective|profile|about)'
}
lines = text.split('\n')
current_section = None
for i, line in enumerate(lines):
line_lower = line.lower().strip()
# Check if this line is a section header
for section, pattern in section_patterns.items():
if re.search(pattern, line_lower) and len(line_lower) < 50:
current_section = section
break
# Add content to current section
if current_section and i > 0:
sections[current_section] += line + '\n'
return sections
def extract_name(text: str, entities: List) -> str:
"""Extract name using multiple methods"""
# Method 1: Use transformer model
name_parts = []
for ent in entities:
if ent["entity_group"].upper() in ["NAME", "PERSON", "PER"]:
name_parts.append(ent["word"].strip())
if name_parts:
# Clean and join name parts
full_name = " ".join(dict.fromkeys(name_parts))
full_name = re.sub(r'\s+', ' ', full_name).strip()
if len(full_name) > 3 and len(full_name.split()) <= 4:
return full_name
# Method 2: Use spaCy if available
if nlp:
doc = nlp(text[:500]) # Check first 500 chars
for ent in doc.ents:
if ent.label_ == "PERSON":
name = ent.text.strip()
if len(name) > 3 and len(name.split()) <= 4:
return name
# Method 3: Pattern matching for first few lines
first_lines = text.split('\n')[:5]
for line in first_lines:
line = line.strip()
# Look for name pattern (2-4 words, title case)
if re.match(r'^[A-Z][a-z]+(\s+[A-Z][a-z]+){1,3}$', line):
return line
return "Not Found"
def extract_skills(text: str, skill_section: str = "") -> List[str]:
"""Extract skills using multiple methods"""
skills_found = set()
# Prioritize skills section if available
search_text = skill_section + " " + text if skill_section else text
search_text = search_text.lower()
# Method 1: Direct keyword matching
for skill in SKILL_KEYWORDS:
if re.search(rf'\b{re.escape(skill.lower())}\b', search_text):
skills_found.add(skill)
# Method 2: Pattern-based extraction
# Look for skills in bullet points or comma-separated lists
skill_patterns = [
r'[•·▪▫◦‣⁃]\s*([A-Za-z\s\+\#\.]+)', # Bullet points
r'(?:skills?|technologies|tools?)[\s:]*([A-Za-z\s,\+\#\.]+)', # After keywords
]
for pattern in skill_patterns:
matches = re.findall(pattern, search_text, re.IGNORECASE)
for match in matches:
# Check each word/phrase in the match
potential_skills = re.split(r'[,;]', match)
for ps in potential_skills:
ps = ps.strip().lower()
if ps in SKILL_KEYWORDS:
skills_found.add(ps)
return list(skills_found)
def extract_education(text: str, edu_section: str = "") -> List[str]:
"""Extract education information"""
education_info = []
search_text = edu_section + " " + text if edu_section else text
# Extract degrees
for pattern in EDUCATION_PATTERNS:
matches = re.findall(pattern, search_text, re.IGNORECASE)
for match in matches:
if isinstance(match, tuple):
match = match[0]
education_info.append(match)
# Extract years (graduation years)
year_pattern = r'\b(19[0-9]{2}|20[0-9]{2})\b'
years = re.findall(year_pattern, search_text)
# Extract GPA if mentioned
gpa_pattern = r'(?:gpa|cgpa|grade)[\s:]*([0-9]\.[0-9]+)'
gpa_matches = re.findall(gpa_pattern, search_text, re.IGNORECASE)
return list(dict.fromkeys(education_info)) # Remove duplicates
def extract_experience(text: str, exp_section: str = "") -> List[str]:
"""Extract experience information"""
experience_info = []
search_text = exp_section + " " + text if exp_section else text
# Extract job titles
for pattern in JOB_TITLE_PATTERNS:
matches = re.findall(pattern, search_text, re.IGNORECASE)
for match in matches:
if isinstance(match, tuple):
match = ' '.join(match).strip()
experience_info.append(match)
# Extract years of experience
exp_patterns = [
r'(\d+)\+?\s*(?:years?|yrs?)(?:\s+of)?\s+experience',
r'experience\s*:?\s*(\d+)\+?\s*(?:years?|yrs?)',
r'(\d+)\+?\s*(?:years?|yrs?)\s+(?:as|in|of)',
]
for pattern in exp_patterns:
matches = re.findall(pattern, search_text, re.IGNORECASE)
if matches:
years = max(map(int, matches))
experience_info.append(f"{years}+ years experience")
break
# Extract company names (common patterns)
company_patterns = [
r'(?:at|@|company|employer)\s*:?\s*([A-Z][A-Za-z\s&\.\-]+)',
r'([A-Z][A-Za-z\s&\.\-]+)\s*(?:inc|llc|ltd|corp|company)',
]
for pattern in company_patterns:
matches = re.findall(pattern, search_text)
experience_info.extend(matches[:3]) # Limit to avoid false positives
return list(dict.fromkeys(experience_info))
def parse_resume(file_path: str, filename: str = None) -> Dict[str, str]:
"""Main function to parse resume"""
# Extract and clean text
raw_text = extract_text(file_path)
text = clean_text(raw_text)
# Extract sections
sections = extract_sections(text)
# Get NER entities
entities = ner_pipeline(text[:1024]) # Limit for performance
# Extract information
name = extract_name(text, entities)
skills = extract_skills(text, sections.get('skills', ''))
education = extract_education(text, sections.get('education', ''))
experience = extract_experience(text, sections.get('experience', ''))
return {
"name": name,
"skills": ", ".join(skills[:15]) if skills else "Not Found", # Limit to 15 skills
"education": ", ".join(education[:5]) if education else "Not Found",
"experience": ", ".join(experience[:5]) if experience else "Not Found"
}
# Optional: Add confidence scores
def parse_resume_with_confidence(file_path: str) -> Dict[str, Tuple[str, float]]:
"""Parse resume with confidence scores for each field"""
result = parse_resume(file_path)
# Simple confidence calculation based on whether data was found
confidence_scores = {
"name": 0.9 if result["name"] != "Not Found" else 0.1,
"skills": min(0.9, len(result["skills"].split(",")) * 0.1) if result["skills"] != "Not Found" else 0.1,
"education": 0.8 if result["education"] != "Not Found" else 0.2,
"experience": 0.8 if result["experience"] != "Not Found" else 0.2
}
return {
key: (value, confidence_scores[key])
for key, value in result.items()
} |