File size: 2,712 Bytes
eb6064a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
    }