import os import re import PyPDF2 from docx import Document from typing import Dict, List def extract_text_from_pdf(file_path: str) -> str: """Extract text from PDF file.""" with open(file_path, 'rb') as file: reader = PyPDF2.PdfReader(file) text = "" for page in reader.pages: text += page.extract_text() + "\n" return text def extract_text_from_docx(file_path: str) -> str: """Extract text from DOCX file.""" doc = Document(file_path) return "\n".join([paragraph.text for paragraph in doc.paragraphs]) def extract_contact_info(text: str) -> Dict[str, str]: """Extract contact information from text.""" # Email pattern email_pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}' # Phone pattern (supports various formats) phone_pattern = r'(\+\d{1,3}[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}' email = re.search(email_pattern, text) phone = re.search(phone_pattern, text) return { 'email': email.group(0) if email else '', 'phone': phone.group(0) if phone else '' } def extract_skills(text: str) -> List[str]: """Extract skills by keyword scan (lightweight, no external NLP).""" skills_list = [ 'python', 'java', 'javascript', 'c++', 'c#', 'sql', 'html', 'css', 'machine learning', 'deep learning', 'data science', 'data analysis', 'project management', 'team leadership', 'agile', 'scrum', 'devops', 'cloud', 'aws', 'azure', 'gcp', 'docker', 'kubernetes', 'git', 'rest api', 'graphql', 'mongodb', 'postgresql', 'mysql', 'tensorflow', 'pytorch', 'pandas', 'numpy', 'spark', 'hadoop', 'nlp' ] text_lower = text.lower() found = set() for skill in skills_list: if skill in text_lower: found.add(skill) return sorted(found) def parse_resume(file_path: str) -> Dict: """Main function to parse resume and extract information.""" # Determine file type and extract text file_ext = os.path.splitext(file_path)[1].lower() if file_ext == '.pdf': text = extract_text_from_pdf(file_path) elif file_ext == '.docx': text = extract_text_from_docx(file_path) else: raise ValueError("Unsupported file format. Please upload a PDF or DOCX file.") # Extract information contact_info = extract_contact_info(text) skills = extract_skills(text) # Extract name (simple approach - first line is often the name) name = text.split('\n')[0].strip() return { 'name': name, 'email': contact_info['email'], 'phone': contact_info['phone'], 'skills': skills, 'raw_text': text }