File size: 12,514 Bytes
c2f9ec8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Simplified Hugging Face Resume Extractor

This module provides resume extraction using primarily regex patterns
with minimal Hugging Face model usage for specific tasks only.
This approach is more reliable and faster than full model-based extraction.
"""

import json
import re
import logging
from typing import Dict, Any, List, Optional

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class SimpleHFResumeExtractor:
    """
    Simplified resume extractor using primarily regex with minimal HF model usage
    """
    
    def __init__(self):
        """Initialize the simple extractor"""
        self.model_available = False
        
        # Try to load a lightweight model for name extraction only
        try:
            # Only load if really needed and use the smallest possible model
            logger.info("Simple HF extractor initialized (regex-based)")
            self.model_available = False  # Disable model usage for now
        except Exception as e:
            logger.info(f"No HF model loaded, using pure regex approach: {e}")
            self.model_available = False
    
    def extract_sections_hf_simple(self, text: str) -> Dict[str, Any]:
        """
        Extract resume sections using simplified approach
        
        Args:
            text: Raw resume text
            
        Returns:
            Structured resume data
        """
        logger.info("Starting simplified HF extraction...")
        
        try:
            # Extract different sections using optimized regex patterns
            name = self._extract_name_simple(text)
            summary = self._extract_summary_simple(text)
            skills = self._extract_skills_simple(text)
            experiences = self._extract_experiences_simple(text)
            education = self._extract_education_simple(text)
            
            result = {
                "Name": name,
                "Summary": summary,
                "Skills": skills,
                "StructuredExperiences": experiences,
                "Education": education,
                "Training": []
            }
            
            logger.info("βœ… Simplified HF extraction completed")
            return result
            
        except Exception as e:
            logger.error(f"Simplified HF extraction failed: {e}")
            # Fallback to regex-based extraction
            from utils.extractor_fixed import extract_sections_spacy_fixed
            return extract_sections_spacy_fixed(text)
    
    def _extract_name_simple(self, text: str) -> str:
        """Extract name using optimized regex patterns"""
        lines = text.split('\n')[:5]  # Check first 5 lines
        
        for line in lines:
            line = line.strip()
            # Skip lines with contact info
            if re.search(r'@|phone|email|linkedin|github|πŸ“§|πŸ“ž|πŸ“', line.lower()):
                continue
            # Skip lines with too many special characters
            if len(re.findall(r'[^\w\s]', line)) > 3:
                continue
            # Look for name-like patterns
            name_match = re.match(r'^([A-Z][a-z]+ [A-Z][a-z]+(?:\s+[A-Z][a-z]+)?)', line)
            if name_match:
                return name_match.group(1)
        
        return ""
    
    def _extract_summary_simple(self, text: str) -> str:
        """Extract professional summary using improved regex"""
        # Look for summary section with better boundary detection
        summary_patterns = [
            r'(?i)(?:professional\s+)?summary[:\s]*\n(.*?)(?=\n\s*(?:technical\s+skills?|skills?|experience|education))',
            r'(?i)objective[:\s]*\n(.*?)(?=\n\s*(?:technical\s+skills?|skills?|experience|education))',
            r'(?i)profile[:\s]*\n(.*?)(?=\n\s*(?:technical\s+skills?|skills?|experience|education))'
        ]
        
        for pattern in summary_patterns:
            match = re.search(pattern, text, re.DOTALL)
            if match:
                summary = match.group(1).strip()
                # Clean up the summary
                summary = re.sub(r'\n+', ' ', summary)
                summary = re.sub(r'\s+', ' ', summary)
                if len(summary) > 50:  # Ensure it's substantial
                    return summary
        
        return ""
    
    def _extract_skills_simple(self, text: str) -> List[str]:
        """Extract skills using enhanced regex patterns"""
        skills = set()
        
        # Look for technical skills section with better parsing
        skills_pattern = r'(?i)technical\s+skills?[:\s]*\n(.*?)(?=\n\s*(?:professional\s+experience|experience|education|projects?))'
        match = re.search(skills_pattern, text, re.DOTALL)
        
        if match:
            skills_text = match.group(1)
            
            # Parse bullet-pointed skills with improved cleaning
            bullet_lines = re.findall(r'●\s*([^●\n]+)', skills_text)
            for line in bullet_lines:
                if ':' in line:
                    # Format: "Category: skill1, skill2, skill3"
                    skills_part = line.split(':', 1)[1].strip()
                    individual_skills = re.split(r',\s*', skills_part)
                    for skill in individual_skills:
                        skill = skill.strip()
                        # Clean up parenthetical information
                        skill = re.sub(r'\([^)]*\)', '', skill).strip()
                        if skill and len(skill) > 1 and len(skill) < 50:  # Reasonable length
                            skills.add(skill)
        
        # Enhanced common technical skills detection
        common_skills = [
            'Python', 'Java', 'JavaScript', 'TypeScript', 'C++', 'C#', 'SQL', 'NoSQL',
            'React', 'Angular', 'Vue', 'Node.js', 'Django', 'Flask', 'Spring',
            'AWS', 'Azure', 'GCP', 'Docker', 'Kubernetes', 'Jenkins',
            'Git', 'GitHub', 'GitLab', 'Jira', 'Confluence',
            'TensorFlow', 'PyTorch', 'Scikit-learn', 'Pandas', 'NumPy', 'Matplotlib', 'Seaborn',
            'MySQL', 'PostgreSQL', 'MongoDB', 'Redis',
            'Linux', 'Windows', 'MacOS', 'Ubuntu',
            'Selenium', 'Pytest', 'TestNG', 'Postman',
            'AWS Glue', 'AWS SageMaker', 'REST APIs', 'Apex', 'Bash'
        ]
        
        for skill in common_skills:
            if re.search(rf'\b{re.escape(skill)}\b', text, re.IGNORECASE):
                skills.add(skill)
        
        return sorted(list(skills))
    
    def _extract_experiences_simple(self, text: str) -> List[Dict[str, Any]]:
        """Extract work experiences using improved regex patterns"""
        experiences = []
        
        # Look for experience section
        exp_pattern = r'(?i)(?:professional\s+)?experience[:\s]*\n(.*?)(?=\n\s*(?:education|projects?|certifications?|$))'
        match = re.search(exp_pattern, text, re.DOTALL)
        
        if not match:
            return experiences
        
        exp_text = match.group(1)
        
        # Parse job entries with improved patterns
        # Pattern 1: Company | Location | Title | Date
        pattern1 = r'([^|\n]+)\s*\|\s*([^|\n]+)\s*\|\s*([^|\n]+)\s*\|\s*([^|\n]+)'
        matches1 = re.findall(pattern1, exp_text)
        
        processed_companies = set()  # Track to avoid duplicates
        
        for match in matches1:
            company, location, title, dates = match
            company_key = f"{company.strip()}, {location.strip()}"
            
            # Skip if we've already processed this company
            if company_key in processed_companies:
                continue
            processed_companies.add(company_key)
            
            # Extract responsibilities for this specific job
            responsibilities = self._extract_responsibilities_simple(exp_text, company.strip(), title.strip())
            
            experience = {
                "title": title.strip(),
                "company": company_key,
                "date_range": dates.strip(),
                "responsibilities": responsibilities
            }
            experiences.append(experience)
        
        return experiences
    
    def _extract_responsibilities_simple(self, exp_text: str, company: str, title: str) -> List[str]:
        """Extract responsibilities for a specific job using improved regex"""
        responsibilities = []
        
        # Create a pattern to find the job entry and extract bullet points after it
        # Look for the company and title, then capture bullet points until next job or section
        job_pattern = rf'{re.escape(company)}.*?{re.escape(title)}.*?\n(.*?)(?=\n[A-Z][^|\n]*\s*\||$)'
        match = re.search(job_pattern, exp_text, re.DOTALL | re.IGNORECASE)
        
        if match:
            resp_text = match.group(1)
            # Extract bullet points with improved cleaning
            bullets = re.findall(r'●\s*([^●\n]+)', resp_text)
            for bullet in bullets:
                bullet = bullet.strip()
                # Clean up the bullet point
                bullet = re.sub(r'\s+', ' ', bullet)  # Normalize whitespace
                if bullet and len(bullet) > 15:  # Ensure substantial content
                    responsibilities.append(bullet)
        
        return responsibilities
    
    def _extract_education_simple(self, text: str) -> List[str]:
        """Extract education information using improved regex"""
        education = []
        
        # Look for education section with better boundary detection
        edu_pattern = r'(?i)education[:\s]*\n(.*?)(?=\n\s*(?:certifications?|projects?|$))'
        match = re.search(edu_pattern, text, re.DOTALL)
        
        if match:
            edu_text = match.group(1)
            
            # Extract bullet points or lines with improved cleaning
            edu_lines = re.findall(r'●\s*([^●\n]+)', edu_text)
            if not edu_lines:
                # Try line-by-line for non-bulleted education
                edu_lines = [line.strip() for line in edu_text.split('\n') if line.strip()]
            
            for line in edu_lines:
                line = line.strip()
                # Clean up the education entry
                line = re.sub(r'\s+', ' ', line)  # Normalize whitespace
                if line and len(line) > 3:  # Reduced to catch short entries like "8 years"
                    education.append(line)
        
        return education

# Convenience function for easy usage
def extract_sections_hf_simple(text: str) -> Dict[str, Any]:
    """
    Extract resume sections using simplified Hugging Face approach
    
    Args:
        text: Raw resume text
        
    Returns:
        Structured resume data
    """
    extractor = SimpleHFResumeExtractor()
    return extractor.extract_sections_hf_simple(text)

# Test function
def test_simple_hf_extraction():
    """Test the simplified HF extraction with sample resume"""
    
    sample_text = """
    Jonathan Edward Nguyen
    πŸ“San Diego, CA | 858-900-5036 | πŸ“§ jonatngu@icloud.com
    
    Summary
    Sun Diego-based Software Engineer, and Developer Hackathon 2025 winner who loves building scalable
    automation solutions, AI development, and optimizing workflows.
    
    Technical Skills
    ● Programming Languages: Python, Java, SQL, Apex, Bash
    ● Frameworks & Libraries: TensorFlow, PyTorch, Scikit-learn, NumPy, Pandas
    ● Cloud Platforms: AWS Glue, AWS SageMaker, AWS Orchestration, REST APIs
    
    Professional Experience
    TalentLens.AI | Remote | AI Developer | Feb 2025 – Present
    ● Built an automated test suite for LLM prompts that export reports with performance metrics
    ● Architected and developed an AI-powered resume screening application using Streamlit
    
    GoFundMe | San Diego, CA | Senior Developer in Test | Oct 2021 – Dec 2024
    ● Built and maintained robust API and UI test suites in Python, reducing defects by 37%
    ● Automated environment builds using Apex and Bash, improving deployment times by 30%
    
    Education
    ● California State San Marcos (May 2012): Bachelor of Arts, Literature and Writing
    """
    
    extractor = SimpleHFResumeExtractor()
    result = extractor.extract_sections_hf_simple(sample_text)
    
    print("Simplified HF Extraction Results:")
    print(json.dumps(result, indent=2))
    
    return result

if __name__ == "__main__":
    test_simple_hf_extraction()