-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_parser.py
More file actions
134 lines (109 loc) · 4.97 KB
/
Copy pathtext_parser.py
File metadata and controls
134 lines (109 loc) · 4.97 KB
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
# text_parser.py
# CV PDF Project
# Copyright 2025 pyrus-code
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re
class TextParser:
"""
Parses a block of text containing professional experience, skills,
and software skills into a structured dictionary.
"""
def __init__(self, all_jobs, all_software_skills):
"""
Initializes the parser with the necessary data for lookups.
Args:
all_jobs (list): A list of job dictionaries from the database.
all_software_skills (list): A list of software skill dictionaries.
"""
self.all_jobs = all_jobs
self.all_software_skills = all_software_skills
self.job_lookup = {str(job['title']).lower().strip().replace("–", "-"): job for job in self.all_jobs}
self.software_skills_map = {s['skill'].lower(): s['skill'] for s in self.all_software_skills}
def parse(self, text):
"""
The main public method to parse the text.
Args:
text (str): The raw text to parse.
Returns:
dict: A dictionary containing the parsed data.
"""
if not text or not text.strip():
return {'experience': [], 'skills': [], 'software_skills': []}
sections = self._split_sections(text)
parsed_experience = self._parse_experience(sections.get('experience_text', ''))
parsed_skills = self._parse_skills(sections.get('skills_text', ''))
parsed_software_skills = self._parse_software_skills(sections.get('software_skills_text', ''))
return {
'experience': parsed_experience,
'skills': parsed_skills,
'software_skills': parsed_software_skills
}
def _split_sections(self, text):
"""Splits the text into experience, skills, and software skills sections."""
sections = {}
parts = re.split(r'\n(software skills|skills)\n', text, flags=re.IGNORECASE)
sections['experience_text'] = parts[0]
i = 1
while i < len(parts):
header = parts[i].lower().strip()
content = parts[i + 1]
if "software skills" in header:
sections['software_skills_text'] = content
elif "skills" in header:
sections['skills_text'] = content
i += 2
return sections
def _parse_skills(self, skills_text):
"""Parses the skills section into a list of strings."""
if not skills_text:
return []
return [s.strip().lstrip("-* ").strip() for s in skills_text.split('\n') if s.strip()]
def _parse_software_skills(self, software_skills_text):
"""Parses the software skills section and maps them to canonical names."""
if not software_skills_text:
return [s['skill'] for s in self.all_software_skills[:5]]
parsed_skills_from_text = [s.strip().lstrip("-* ").strip() for s in software_skills_text.split('\n') if s.strip()]
canonical_skills = [
self.software_skills_map.get(skill.lower(), skill.title())
for skill in parsed_skills_from_text
]
return canonical_skills
def _parse_experience(self, experience_text):
"""Parses the professional experience section."""
if not experience_text:
return []
parsed_experience = []
current_job_block = None
for line in experience_text.split('\n'):
clean_line_lower = line.strip().lower().replace("–", "-")
if not clean_line_lower:
if current_job_block:
parsed_experience.append(current_job_block)
current_job_block = None
continue
match_job = self.job_lookup.get(clean_line_lower)
if match_job:
if current_job_block:
parsed_experience.append(current_job_block)
current_job_block = {
'title': match_job['title'],
'company': match_job['company'],
'dates': match_job['dates'],
'tasks': []
}
elif current_job_block and (line.strip().startswith("-") or line.strip().startswith("•")):
current_job_block['tasks'].append(line)
if current_job_block:
parsed_experience.append(current_job_block)
return parsed_experience