File size: 14,437 Bytes
958fece
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ea7f74
958fece
4ea7f74
958fece
 
 
be87d41
958fece
 
 
327b929
 
 
958fece
4ea7f74
958fece
 
 
 
 
 
 
 
 
 
 
327b929
 
 
 
 
 
 
 
 
 
be87d41
 
 
7c66219
 
958fece
 
 
be87d41
958fece
 
 
 
 
 
 
327b929
 
 
 
 
 
958fece
 
 
 
 
 
 
 
 
327b929
 
 
 
 
 
 
be87d41
 
 
 
 
 
327b929
 
 
 
 
 
 
 
 
 
 
be87d41
327b929
 
 
 
be87d41
 
 
 
 
327b929
be87d41
 
 
 
 
327b929
 
 
 
 
be87d41
 
 
 
 
 
327b929
 
 
 
 
 
 
be87d41
327b929
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be87d41
 
958fece
 
 
 
be87d41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327b929
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c66219
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
958fece
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import re
import pdfplumber


# Section keywords to detect standard resume sections
SECTION_KEYWORDS = {
    "experience":  ["experience", "work experience", "employment", "work history"],
    "education":   ["education", "academic background", "qualifications"],
    "skills":      ["skills", "technical skills", "core competencies", "expertise"],
    "summary":     ["summary", "objective", "profile", "about me"],
    "contact":     ["contact", "personal information", "personal details"],
    "projects":    ["projects", "personal projects", "key projects"],
    "certifications": ["certifications", "certificates", "licenses"],
}


def extract_resume_from_stream(stream) -> dict:
    """
    Opens a PDF with pdfplumber from a stream, extracts text page by page,
    detects basic metadata, and identifies resume sections.
    """
    text_pages = []
    hidden_pages = []
    has_tables = False
    has_images = False
    page_count = 0
    font_names = set()
    font_sizes = []
    has_multiple_columns = False

    with pdfplumber.open(stream) as pdf:
        page_count = len(pdf.pages)

        for page in pdf.pages:
            # Detect tables
            if page.find_tables():
                has_tables = True

            # Detect images
            if page.images:
                has_images = True

            has_multiple_columns = has_multiple_columns or _detect_multiple_columns(page)

            for char in page.chars:
                font_name = _normalize_font_name(str(char.get("fontname", "")))
                if font_name:
                    font_names.add(font_name)
                size = char.get("size")
                if isinstance(size, (int, float)) and size > 0:
                    font_sizes.append(float(size))

            page_text, page_hidden = _split_hidden(page)
            if page_hidden:
                hidden_pages.append(page_hidden)
            if _is_sparse_page_text(page_text, page):
                page_text = _extract_words_as_lines(page) or page_text
            text_pages.append(page_text)

    full_text = "\n".join(text_pages).strip()
    hidden_text = "\n".join(hidden_pages).strip()

    # Confidence: penalise if text is very short relative to page count
    expected_min_chars = page_count * 200
    confidence = min(1.0, len(full_text) / max(expected_min_chars, 1))
    confidence = round(confidence, 2)

    sections = _detect_sections(full_text)
    non_empty_lines = [line.strip() for line in full_text.splitlines() if line.strip()]
    long_line_count = sum(len(line) > 120 for line in non_empty_lines)
    bullet_count = sum(
        bool(re.match(r"^[\u2022\u25e6\u25aa\u25cf\-\u2013\u2014*]\s+", line))
        for line in non_empty_lines
    )

    return {
        "text": full_text,
        "sections": sections,
        "metadata": {
            "hasTables":  has_tables,
            "hasImages":  has_images,
            "pageCount":  page_count,
            "confidence": confidence,
            "hasMultipleColumns": has_multiple_columns,
            "fontCount": len(font_names),
            "minFontSize": round(min(font_sizes), 1) if font_sizes else None,
            "averageFontSize": round(sum(font_sizes) / len(font_sizes), 1) if font_sizes else None,
            "averageCharsPerPage": round(len(full_text) / max(page_count, 1)),
            "longLineCount": long_line_count,
            "bulletCount": bullet_count,
            # Text the page draws but nobody can read. The backend treats this
            # as a high-severity integrity finding rather than as CV content:
            # skills only present here were written for the scorer, not for the
            # person doing the hiring.
            "hiddenTextFound": bool(hidden_text),
            "hiddenTextSample": hidden_text[:300] or None,
        },
    }


def extract_docx_from_stream(stream) -> dict:
    """Extract ordered text and layout signals from a DOCX document."""
    from docx import Document
    from docx.oxml.ns import qn

    document = Document(stream)
    text_lines = []
    hidden_runs = []
    font_names = set()
    font_sizes = []

    for paragraph in document.paragraphs:
        # Word has a literal hidden-text attribute (w:vanish) as well as the
        # white-on-white trick, and python-docx exposes both. A run hidden by
        # either is dropped from the text and kept as evidence: it is not
        # content the candidate is showing the employer.
        visible_runs = []
        for run in paragraph.runs:
            if _is_hidden_run(run):
                if run.text.strip():
                    hidden_runs.append(run.text.strip())
                continue
            visible_runs.append(run)
            if run.font.name:
                font_names.add(_normalize_font_name(run.font.name))
            if run.font.size:
                font_sizes.append(float(run.font.size.pt))

        text = ("".join(run.text for run in visible_runs) or paragraph.text).strip()
        if hidden_runs and not visible_runs:
            text = ""
        if text:
            text_lines.append(text)

    for table in document.tables:
        for row in table.rows:
            cells = [cell.text.strip() for cell in row.cells if cell.text.strip()]
            if cells:
                text_lines.append(" | ".join(cells))

    full_text = "\n".join(text_lines).strip()
    hidden_text = "\n".join(hidden_runs).strip()
    estimated_pages = max(1, round(len(full_text) / 3500))
    has_multiple_columns = False
    for section in document.sections:
        columns = section._sectPr.xpath('./w:cols')
        if columns:
            count = columns[0].get(qn('w:num'))
            if count and int(count) > 1:
                has_multiple_columns = True

    long_line_count = sum(len(line) > 120 for line in text_lines)
    bullet_count = sum(
        bool(re.match(r"^[\u2022\u25e6\u25aa\u25cf\-\u2013\u2014*]\s+", line))
        for line in text_lines
    )
    confidence = 1.0 if len(full_text) >= 100 else round(len(full_text) / 100, 2)

    return {
        "text": full_text,
        "sections": _detect_sections(full_text),
        "metadata": {
            "hasTables": bool(document.tables),
            "hasImages": bool(document.inline_shapes),
            "pageCount": estimated_pages,
            "confidence": confidence,
            "hasMultipleColumns": has_multiple_columns,
            "fontCount": len(font_names),
            "minFontSize": round(min(font_sizes), 1) if font_sizes else None,
            "averageFontSize": round(sum(font_sizes) / len(font_sizes), 1) if font_sizes else None,
            "averageCharsPerPage": round(len(full_text) / estimated_pages),
            "longLineCount": long_line_count,
            "bulletCount": bullet_count,
            "hiddenTextFound": bool(hidden_text),
            "hiddenTextSample": hidden_text[:300] or None,
        },
    }


# ----------------------------------------------------------------- hiding --

# Below this, nothing is readable on a screen or on paper. A CV does not contain
# text nobody is meant to read unless somebody meant it.
_UNREADABLE_PT = 3.5

# How close to the page colour counts as invisible. Not exact equality: a CV
# built to hide text often uses 0.98 grey rather than pure white, precisely
# because an exact-match check is the obvious one to write.
_INK_EPSILON = 0.04


def _is_background_colour(color) -> bool:
    """True when a character is drawn in (near) the page's own colour.

    pdfplumber reports `non_stroking_color` in whatever colourspace the PDF
    used, so this has to handle all of them:

        None            unknown - treated as visible, never as evidence
        0.97            greyscale, 1 is white
        (0.99,1,1)      RGB
        (0,0,0,0)       CMYK, where 0 ink is white
    """
    if color is None:
        return False
    if isinstance(color, (int, float)):
        return float(color) >= 1 - _INK_EPSILON
    try:
        channels = [float(c) for c in color]
    except (TypeError, ValueError):
        return False
    if not channels:
        return False
    if len(channels) == 4:  # CMYK: no ink at all
        return all(c <= _INK_EPSILON for c in channels)
    return all(c >= 1 - _INK_EPSILON for c in channels)


def _is_hidden_char(char) -> bool:
    """A character the page renders but a person cannot read."""
    size = char.get("size")
    if isinstance(size, (int, float)) and 0 < float(size) < _UNREADABLE_PT:
        return True
    return _is_background_colour(char.get("non_stroking_color"))


def _is_hidden_run(run) -> bool:
    """A DOCX run Word will render invisibly, or not at all."""
    font = run.font
    # w:vanish - Word's own "hidden text" checkbox.
    if getattr(font, "hidden", None):
        return True
    size = getattr(font, "size", None)
    if size is not None and getattr(size, "pt", None) and float(size.pt) < _UNREADABLE_PT:
        return True
    color = getattr(font, "color", None)
    rgb = getattr(color, "rgb", None) if color is not None else None
    if rgb is None:
        return False
    try:
        r, g, b = int(str(rgb)[0:2], 16), int(str(rgb)[2:4], 16), int(str(rgb)[4:6], 16)
    except (ValueError, IndexError):
        return False
    return r >= 245 and g >= 245 and b >= 245


def _split_hidden(page):
    """Return (visible_text, hidden_text) for one page.

    The important subtlety is the RATIO. A CV designed with a dark background
    and white type is a legitimate design choice, and on such a page nearly
    every character is 'white' - flagging it would accuse a graphic designer of
    fraud for owning a portfolio template. Hiding, by contrast, is a small block
    of invisible text on an otherwise ordinary page.

    So white text is only treated as hidden when it is the MINORITY of the page.
    Above half, the page is read as a design and nothing is stripped.
    """
    chars = page.chars or []
    if not chars:
        return page.extract_text() or "", ""

    hidden = [c for c in chars if _is_hidden_char(c)]
    if not hidden:
        return page.extract_text() or "", ""

    if len(hidden) > len(chars) * 0.5:
        # A dark-background or light-on-colour design, not a hiding place.
        return page.extract_text() or "", ""

    hidden_text = "".join(str(c.get("text", "")) for c in hidden).strip()
    visible = page.filter(
        lambda obj: obj.get("object_type") != "char" or not _is_hidden_char(obj)
    )
    return (visible.extract_text() or ""), hidden_text


def _normalize_font_name(font_name: str) -> str:
    """Collapse embedded font prefixes and style suffixes into a font family."""
    family = font_name.split("+")[-1].lower()
    family = re.sub(r"(bold|italic|oblique|regular|medium|light|semibold|mt)", "", family)
    return re.sub(r"[^a-z0-9]", "", family)


def _detect_multiple_columns(page) -> bool:
    """Detect repeated, independently populated left/right text regions."""
    words = page.extract_words(use_text_flow=False, keep_blank_chars=False)
    if len(words) < 20 or not page.width:
        return False

    rows: dict[int, list[dict]] = {}
    for word in words:
        row_key = round(float(word.get("top", 0)) / 5)
        rows.setdefault(row_key, []).append(word)

    midpoint = float(page.width) / 2
    split_rows = 0
    for row_words in rows.values():
        left = [w for w in row_words if float(w.get("x1", 0)) < midpoint]
        right = [w for w in row_words if float(w.get("x0", 0)) > midpoint]
        if not left or not right:
            continue

        left_text = " ".join(str(w.get("text", "")) for w in left)
        right_text = " ".join(str(w.get("text", "")) for w in right)
        gap = min(float(w.get("x0", 0)) for w in right) - max(
            float(w.get("x1", 0)) for w in left
        )
        if len(left_text) >= 8 and len(right_text) >= 8 and gap > float(page.width) * 0.08:
            split_rows += 1

    return split_rows >= max(4, round(len(rows) * 0.15))


def _is_sparse_page_text(page_text: str, page) -> bool:
    """True when the regular extractor produced much less text than the word layer."""
    words = page.extract_words(use_text_flow=False, keep_blank_chars=False)
    if not words:
        return False
    word_chars = sum(len(str(word.get("text", ""))) for word in words)
    return len(page_text.strip()) < max(80, word_chars * 0.35)


def _extract_words_as_lines(page) -> str:
    """Rebuild readable lines from positioned words when extract_text is sparse."""
    words = page.extract_words(use_text_flow=False, keep_blank_chars=False)
    if not words:
        return ""

    rows: dict[int, list[dict]] = {}
    for word in words:
        row_key = round(float(word.get("top", 0)) / 4)
        rows.setdefault(row_key, []).append(word)

    lines = []
    for _, row_words in sorted(rows.items(), key=lambda item: item[0]):
        ordered = sorted(row_words, key=lambda word: float(word.get("x0", 0)))
        line = " ".join(str(word.get("text", "")).strip() for word in ordered).strip()
        if line:
            lines.append(line)

    return "\n".join(lines)


def _detect_sections(text: str) -> dict:
    """
    Splits the resume text into named sections by detecting section headers.
    Returns a dict of section_name → list of content lines.
    """
    lines = text.split("\n")
    sections: dict[str, list[str]] = {}
    current_section: str | None = None

    for line in lines:
        stripped = line.strip()
        if not stripped:
            continue

        detected = _match_section_header(stripped)

        if detected:
            current_section = detected
            if current_section not in sections:
                sections[current_section] = []
        elif current_section:
            sections[current_section].append(stripped)

    return sections


def _match_section_header(line: str) -> str | None:
    """
    Returns the canonical section name if the line looks like a section header,
    otherwise returns None.
    A line is treated as a header if it's short (≤50 chars) and matches a keyword.
    """
    if len(line) > 50:
        return None

    lower = line.lower().rstrip(":").strip()

    for section_name, keywords in SECTION_KEYWORDS.items():
        for kw in keywords:
            # Exact match or the line IS the keyword phrase
            if re.fullmatch(re.escape(kw), lower):
                return section_name

    return None