-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcv_parser.py
More file actions
200 lines (168 loc) · 6.51 KB
/
cv_parser.py
File metadata and controls
200 lines (168 loc) · 6.51 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
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
"""CV Parser module for extracting text from PDF CVs."""
import pymupdf
from langdetect import detect, DetectorFactory
from typing import Optional
from docx import Document
import os
# Set seed for consistent language detection
DetectorFactory.seed = 0
def extract_text_from_pdf(pdf_path: str) -> dict:
"""
Extract text content from a PDF file.
"""
try:
doc = pymupdf.open(pdf_path)
full_text = ""
page_texts = []
for page_num, page in enumerate(doc):
page_text = page.get_text()
page_texts.append({
"page_number": page_num + 1,
"text": page_text
})
full_text += page_text + "\n"
metadata = doc.metadata
doc.close()
return {
"success": True,
"full_text": full_text.strip(),
"page_count": len(page_texts),
"pages": page_texts,
"metadata": metadata,
"file_path": pdf_path
}
except Exception as e:
return {
"success": False,
"error": str(e),
"file_path": pdf_path
}
def extract_text_from_docx(docx_path: str) -> dict:
"""
Extract text content from a DOCX file.
"""
try:
doc = Document(docx_path)
full_text = []
for para in doc.paragraphs:
full_text.append(para.text)
# Heuristic for page count in DOCX, not accurate but gives a rough idea
# Could be improved with more sophisticated methods if needed
word_count = len(" ".join(full_text).split())
page_count_estimate = max(1, word_count // 500) # Estimate 500 words per page
return {
"success": True,
"full_text": "\n".join(full_text).strip(),
"page_count": page_count_estimate,
"pages": [{"page_number": i + 1, "text": p} for i, p in enumerate(full_text)], # Simplified
"metadata": {}, # DOCX metadata is different, can be extracted if needed
"file_path": docx_path
}
except Exception as e:
return {
"success": False,
"error": str(e),
"file_path": docx_path
}
def extract_text_from_file(file_path: str) -> dict:
"""
Extract text content from a PDF or DOCX file based on file extension.
"""
file_extension = os.path.splitext(file_path)[1].lower()
if file_extension == ".pdf":
return extract_text_from_pdf(file_path)
elif file_extension == ".docx":
return extract_text_from_docx(file_path)
else:
return {
"success": False,
"error": "Unsupported file type. Only PDF and DOCX are supported.",
"file_path": file_path
}
def detect_language(text: str) -> str:
"""
Detect the language of the CV text.
Args:
text: CV text content
Returns:
Language code ('en', 'de', etc.) or 'unknown'
"""
try:
if len(text.strip()) < 20:
return "unknown"
return detect(text)
except Exception:
return "unknown"
def analyze_cv_structure(text: str, language: Optional[str] = None) -> dict:
"""
Analyze basic structural elements of the CV text.
Args:
text: Extracted CV text
language: Optional language code ('en', 'de', etc.)
Returns:
Dictionary with structural analysis
"""
lines = text.split('\n')
non_empty_lines = [line for line in lines if line.strip()]
# Detect language if not provided
if language is None:
language = detect_language(text)
# Basic structural analysis
import re
# Simple phone regex: looks for patterns like +1-555..., (123) 456..., or groups of digits separated by dashes/spaces
# This is a basic heuristic, not a perfect validator
phone_pattern = r'(\+\d{1,3}[- ]?)?\(?\d{3}\)?[- ]?\d{3}[- ]?\d{4}'
analysis = {
"total_lines": len(lines),
"non_empty_lines": len(non_empty_lines),
"total_characters": len(text),
"total_words": len(text.split()),
"has_email": "@" in text and "." in text,
"has_phone": bool(re.search(phone_pattern, text)),
"has_urls": "http" in text.lower() or "www." in text.lower(),
"detected_language": language
}
# Define section keywords by language
section_keywords = {
"en": {
"experience": ["experience", "work history", "employment", "professional experience"],
"education": ["education", "academic", "qualification", "degree"],
"skills": ["skills", "competencies", "expertise", "technical skills"],
"summary": ["summary", "profile", "about me", "professional summary"],
"objective": ["objective", "career objective"],
"projects": ["projects", "portfolio"],
"certifications": ["certifications", "certificates", "licenses"],
"awards": ["awards", "achievements", "honors"],
"languages": ["languages", "language skills"],
"interests": ["interests", "hobbies"]
},
"de": {
"personal_info": ["persönliche daten", "personalien", "zur person"],
"experience": ["berufserfahrung", "beruflicher werdegang", "karriere", "tätigkeiten"],
"education": ["ausbildung", "bildung", "schulbildung", "studium", "akademischer werdegang"],
"skills": ["kenntnisse", "fähigkeiten", "kompetenzen", "qualifikationen", "fertigkeiten"],
"summary": ["profil", "zusammenfassung", "persönliches profil", "kurzprofil"],
"projects": ["projekte", "projektarbeit"],
"certifications": ["zertifikate", "zertifizierungen", "weiterbildung"],
"awards": ["auszeichnungen", "ehrungen", "preise"],
"languages": ["sprachen", "sprachkenntnisse"],
"interests": ["interessen", "hobbys", "freizeitaktivitäten"],
"references": ["referenzen", "empfehlungen"]
}
}
# Select appropriate keywords based on language
if language == "de":
keywords = section_keywords["de"]
else:
keywords = section_keywords["en"]
# Find sections
text_lower = text.lower()
found_sections = []
for section_type, terms in keywords.items():
for term in terms:
if term in text_lower:
found_sections.append(section_type)
break
analysis["found_sections"] = list(set(found_sections)) # Remove duplicates
analysis["section_count"] = len(analysis["found_sections"])
return analysis