File size: 1,510 Bytes
37b0787
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import re
from typing import Any

from src.extraction.career_history_utils import latest_role

_SENIORITY_PREFIXES = [
    "senior", "sr", "junior", "jr", "lead", "staff",
    "principal", "chief", "head", "vp", "vp of", "director of",
    "associate", "assistant", "principal",
]

_SENIORITY_PATTERN = re.compile(
    r"^(?:" + "|".join(_SENIORITY_PREFIXES) + r")\s+",
    re.IGNORECASE,
)


def extract_title(
    prof: dict[str, Any],
    history: list[dict[str, Any]],
) -> tuple[str | None, str]:
    direct = prof.get("current_title")
    if direct and isinstance(direct, str) and direct.strip():
        return direct.strip(), "direct"

    latest = latest_role(history)
    if latest:
        title = latest.get("title")
        if title and isinstance(title, str) and title.strip():
            return title.strip(), "history"

    headline = prof.get("headline", "")
    if headline and isinstance(headline, str) and headline.strip():
        extracted = _parse_headline(headline)
        if extracted:
            return extracted, "headline"

    return None, "not_found"


def _parse_headline(headline: str) -> str | None:
    candidate = headline.strip()
    if "|" in candidate:
        candidate = candidate.split("|")[0].strip()
    if " at " in candidate.lower():
        candidate = candidate.lower().split(" at ")[0].strip().title()
    candidate = _SENIORITY_PATTERN.sub("", candidate).strip()
    if candidate:
        return candidate
    return None