Spaces:
Sleeping
Sleeping
mahrooshishaq commited on
Commit ·
7c66219
1
Parent(s): 97e667f
Improve sparse PDF extraction
Browse files- services/pdf_extractor.py +32 -0
services/pdf_extractor.py
CHANGED
|
@@ -50,6 +50,8 @@ def extract_resume_from_stream(stream) -> dict:
|
|
| 50 |
font_sizes.append(float(size))
|
| 51 |
|
| 52 |
page_text = page.extract_text() or ""
|
|
|
|
|
|
|
| 53 |
text_pages.append(page_text)
|
| 54 |
|
| 55 |
full_text = "\n".join(text_pages).strip()
|
|
@@ -185,6 +187,36 @@ def _detect_multiple_columns(page) -> bool:
|
|
| 185 |
return split_rows >= max(4, round(len(rows) * 0.15))
|
| 186 |
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
def _detect_sections(text: str) -> dict:
|
| 189 |
"""
|
| 190 |
Splits the resume text into named sections by detecting section headers.
|
|
|
|
| 50 |
font_sizes.append(float(size))
|
| 51 |
|
| 52 |
page_text = page.extract_text() or ""
|
| 53 |
+
if _is_sparse_page_text(page_text, page):
|
| 54 |
+
page_text = _extract_words_as_lines(page) or page_text
|
| 55 |
text_pages.append(page_text)
|
| 56 |
|
| 57 |
full_text = "\n".join(text_pages).strip()
|
|
|
|
| 187 |
return split_rows >= max(4, round(len(rows) * 0.15))
|
| 188 |
|
| 189 |
|
| 190 |
+
def _is_sparse_page_text(page_text: str, page) -> bool:
|
| 191 |
+
"""True when the regular extractor produced much less text than the word layer."""
|
| 192 |
+
words = page.extract_words(use_text_flow=False, keep_blank_chars=False)
|
| 193 |
+
if not words:
|
| 194 |
+
return False
|
| 195 |
+
word_chars = sum(len(str(word.get("text", ""))) for word in words)
|
| 196 |
+
return len(page_text.strip()) < max(80, word_chars * 0.35)
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
def _extract_words_as_lines(page) -> str:
|
| 200 |
+
"""Rebuild readable lines from positioned words when extract_text is sparse."""
|
| 201 |
+
words = page.extract_words(use_text_flow=False, keep_blank_chars=False)
|
| 202 |
+
if not words:
|
| 203 |
+
return ""
|
| 204 |
+
|
| 205 |
+
rows: dict[int, list[dict]] = {}
|
| 206 |
+
for word in words:
|
| 207 |
+
row_key = round(float(word.get("top", 0)) / 4)
|
| 208 |
+
rows.setdefault(row_key, []).append(word)
|
| 209 |
+
|
| 210 |
+
lines = []
|
| 211 |
+
for _, row_words in sorted(rows.items(), key=lambda item: item[0]):
|
| 212 |
+
ordered = sorted(row_words, key=lambda word: float(word.get("x0", 0)))
|
| 213 |
+
line = " ".join(str(word.get("text", "")).strip() for word in ordered).strip()
|
| 214 |
+
if line:
|
| 215 |
+
lines.append(line)
|
| 216 |
+
|
| 217 |
+
return "\n".join(lines)
|
| 218 |
+
|
| 219 |
+
|
| 220 |
def _detect_sections(text: str) -> dict:
|
| 221 |
"""
|
| 222 |
Splits the resume text into named sections by detecting section headers.
|