import re from typing import Dict, List class WorkExperienceExtractor: def init(self):…
- User
import re from typing import Dict, List
class WorkExperienceExtractor: def init(self): self.exp_keys = [ 'EXPERIENCE', 'WORK EXPERIENCE', 'EMPLOYMENT HISTORY', 'PROFESSIONAL EXPERIENCE', 'RELEVANT EXPERIENCE', 'RESEARCH EXPERIENCE', 'CAREER HISTORY', 'WORK HISTORY', 'INDUSTRY EXPERIENCE', 'FREELANCE EXPERIENCE', 'INTERNSHIP EXPERIENCE' ]
def extract_work_experience(self, resume_dict: Dict[str, str]) -> List[Dict[str, str]]: work_experience = [] # Final output list if 'sections' in resume_dict: sections = resume_dict['sections'] # Step 1: Find the first matching key from exp_keys matching_key = next((key for key in self.exp_keys if key in sections), None) if matching_key and matching_key in sections: work_text = sections[matching_key] # Get the text of the matching section if work_text: # Ensure it's not empty lines = [line.strip() for line in work_text.split('\n') if line.strip()] # Split into lines and remove empty ones current_job = {} # Temp dict for the current job responsibilities = [] # List to collect responsibilities for line in lines: # Step 2: Check if this line contains a title and duration (e.g., "Title at Company | Duration") match = re.search(r'^(.+?)\s*[|]\s*(.+)$', line) # Pattern for "Title | Duration" if match: # If we have a previous job, save it first if current_job and responsibilities: current_job['responsibilities'] = '\n'.join(responsibilities) work_experience.append(current_job) title_part = match.group(1).strip() # Extract title duration_part = match.group(2).strip() # Extract duration (e.g., "May’21-Jun’22") # Set the current job current_job = { 'title': title_part, 'duration': duration_part, } responsibilities = [] # Reset responsibilities elif self.is_duration(line): # Check if this line is just a duration (e.g., "Aug 2024 - Present") if not current_job: # If no title yet, assume this line might be a title (rare case), but skip for now continue current_job['duration'] = line # Set or overwrite duration else: # Treat as responsibility (e.g., bullet points) cleaned_line = re.sub(r'^[\u2022\-●]\s?', '', line).strip() # Remove bullets like '•', '-', or '●' if cleaned_line: # Only add non-empty lines responsibilities.append(cleaned_line) # Add the last job if it exists if current_job and responsibilities: current_job['responsibilities'] = '\n'.join(responsibilities) work_experience.append(current_job) return work_experience def is_duration(self, line: str) -> bool: # Regex for common date formats: e.g., "Aug 2024 - Present", "May’21-Jun’22", "Dec’17-Jun’21", "2010 - 2015" date_patterns = [ r'^\w+ \d{4} - \w+ \d{4}', # e.g., "Aug 2024 - May 2025" r'^\w+ \d{4} - Present', # e.g., "Aug 2024 - Present" r'^\w+’\d{2}-\w+’\d{2}', # e.g., "May’21-Jun’22" (curly apostrophe) r'^\w+’\d{2}-Present', # e.g., "May’21-Present" r'^\d{4} - \d{4}', # e.g., "2017 - 2023" r'^\d{4} - Present' # e.g., "2017 - Present" ] for pattern in date_patterns: if re.match(pattern, line, re.IGNORECASE): return True return Falsei only want the duration and responsibilities
- Assistant
Below is an updated version of your code that extracts and returns only the duration and responsibilities from the work experience section. The modifications remove the title from the extracted data and only save the duration (from a matching line or duration-only line) along with any following responsibilities.
import re from typing import Dict, List class WorkExperienceExtractor: def __init__(self): self.exp_keys = [ 'EXPERIENCE', 'WORK EXPERIENCE', 'EMPLOYMENT HISTORY', 'PROFESSIONAL EXPERIENCE', 'RELEVANT EXPERIENCE', 'RESEARCH EXPERIENCE', 'CAREER HISTORY', 'WORK HISTORY', 'INDUSTRY EXPERIENCE', 'FREELANCE EXPERIENCE', 'INTERNSHIP EXPERIENCE' ] def extract_work_experience(self, resume_dict: Dict[str, str]) -> List[Dict[str, str]]: work_experience = [] # Final output list if 'sections' in resume_dict: sections = resume_dict['sections'] # Find the first matching key from exp_keys matching_key = next((key for key in self.exp_keys if key in sections), None) if matching_key and matching_key in sections: work_text = sections[matching_key] # Get the text from the matching section if work_text: # Make sure it's not empty lines = [line.strip() for line in work_text.split('\n') if line.strip()] current_job = {} # Temporary dict for current job responsibilities = [] # List for responsibilities for line in lines: # Check if this line matches a pattern with a separator ("Title | Duration") match = re.search(r'^(.+?)\s*[|]\s*(.+)$', line) if match: # Before starting a new job, save the previous one if it has both duration and responsibilities if current_job and responsibilities: current_job['responsibilities'] = '\n'.join(responsibilities) work_experience.append(current_job) # We only want duration; ignore the title part. duration_part = match.group(2).strip() current_job = { 'duration': duration_part, } responsibilities = [] # Reset responsibilities for the new job elif self.is_duration(line): # Check if the line is solely a duration if not current_job: # If no job started yet, skip this line. continue current_job['duration'] = line # Overwrite or set the duration else: # Treat this line as a responsibility (after cleaning any common bullet characters) cleaned_line = re.sub(r'^[\u2022\-●]\s?', '', line).strip() if cleaned_line: responsibilities.append(cleaned_line) # Add the last job if it exists and has responsibilities if current_job and responsibilities: current_job['responsibilities'] = '\n'.join(responsibilities) work_experience.append(current_job) return work_experience def is_duration(self, line: str) -> bool: # Regex patterns for common date formats: e.g., "Aug 2024 - Present", "May’21-Jun’22", or "2010 - 2015" date_patterns = [ r'^\w+ \d{4} - \w+ \d{4}', # e.g., "Aug 2024 - May 2025" r'^\w+ \d{4} - Present', # e.g., "Aug 2024 - Present" r'^\w+’\d{2}-\w+’\d{2}', # e.g., "May’21-Jun’22" (using curly apostrophe) r'^\w+’\d{2}-Present', # e.g., "May’21-Present" r'^\d{4} - \d{4}', # e.g., "2017 - 2023" r'^\d{4} - Present' # e.g., "2017 - Present" ] for pattern in date_patterns: if re.match(pattern, line, re.IGNORECASE): return True return FalseExplanation of Changes
-
Removed Title Information:
- When a line matches the
"Title | Duration"pattern, the code now only extracts the duration (ignoring the title).
- When a line matches the
-
Preserving Duration and Responsibilities Only:
- The
current_jobdictionary is created with only thedurationkey. - Responsibilities are gathered and added under the
responsibilitieskey.
- The
-
Appending Jobs:
- Only jobs with both a duration and responsibilities are added to the final list.
This updated implementation now returns a list of dictionaries where each dictionary contains only the
durationand theresponsibilitiesextracted from the resume. -
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.