File size: 7,472 Bytes
a0aaf60
feb1b1c
 
a0aaf60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0894e2
a0aaf60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
feb1b1c
a0aaf60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b9db06
 
 
a0aaf60
 
 
0b9db06
 
 
a0aaf60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
830d137
a0aaf60
 
 
830d137
a0aaf60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

from __future__ import annotations

from collections.abc import Mapping
from types import MappingProxyType
from typing import final

from pydantic import BaseModel, ConfigDict, Field

from redstack.domain.candidate.logistics import LogisticsProfile
from redstack.domain.enums import EvidenceKind, LocationFit, NoticeFit
from redstack.domain.ids import LpaAmount
from redstack.domain.source import RawCandidate
from redstack.features.normalize import normalize_text
from redstack.features.parsing import (
    FeatureCell,
    FeatureId,
    feature_id,
    make_cell,
    mint_evidence,
)

__feature_version__ = "1.1.0"

_STRICT = ConfigDict(
    frozen=True, extra="forbid", str_strip_whitespace=True, validate_default=True
)

GEO_HUB_MATCH: FeatureId = feature_id("geo", "hub_match")
GEO_INDIA_RELOCATABLE: FeatureId = feature_id("geo", "india_relocatable")
GEO_OUTSIDE_INDIA_NO_SPONSOR: FeatureId = feature_id("geo", "outside_india_no_sponsor")
RELOC_WILLING: FeatureId = feature_id("reloc", "willing")
RELOC_NEEDED: FeatureId = feature_id("reloc", "needed")
NOTICE_FIT: FeatureId = feature_id("notice", "fit")
NOTICE_OVER_30: FeatureId = feature_id("notice", "over_30")
SAL_FIT: FeatureId = feature_id("sal", "fit")
SAL_IS_INVERTED: FeatureId = feature_id("sal", "is_inverted")


DEFAULT_JD_HUBS: frozenset[str] = frozenset(
    {
        "pune",
        "noida",
        "hyderabad",
        "mumbai",
        "delhi",
        "new delhi",
        "delhi ncr",
        "ncr",
        "gurgaon",
        "gurugram",
        "ghaziabad",
        "faridabad",
        "greater noida",
    }
)

_NOTICE_FIT_SCORE: Mapping[NoticeFit, float] = MappingProxyType(
    {
        NoticeFit.SUB_30_IDEAL: 1.0,
        NoticeFit.BUYOUTABLE: 0.6,
        NoticeFit.OVER_30_HIGHER_BAR: 0.25,
    }
)

_NOTICE_IDEAL_MAX_DAYS = 30


@final
class SalaryTarget(BaseModel):
    """The JD's target compensation band (INR lpa), if any."""

    model_config = _STRICT

    min_lpa: LpaAmount = Field(ge=0.0, allow_inf_nan=False)
    max_lpa: LpaAmount = Field(ge=0.0, allow_inf_nan=False)


def _hub_match_value(logistics: LogisticsProfile, jd_hubs: frozenset[str]) -> float:
    """1.0 when the candidate's city is a JD hub.

    Authoritative signal is the pre-derived ``LocationFit.PREFERRED_HUB``; the
    injected ``jd_hubs`` set is a corroborating cross-check on the normalized
    city string (so an updated hub set is honoured without re-deriving the enum).
    ``location`` is consistently "<City>, <State>" in this dataset and
    ``jd_hubs`` holds bare city names, so the full-string check alone never
    matches a hub resident — also check the substring before the first comma.
    """
    if logistics.location_fit is LocationFit.PREFERRED_HUB:
        return 1.0
    normalized = normalize_text(logistics.location)
    city_only = normalized.split(",", 1)[0].strip()
    return 1.0 if normalized in jd_hubs or city_only in jd_hubs else 0.0


def _salary_overlap(
    candidate_min: float,
    candidate_max: float,
    target: SalaryTarget,
) -> float:
    """Jaccard-style overlap of two bands in ``[0, 1]``.

    Inverted candidate bands are first normalized to ``[lo, hi]`` so a sanity
    inversion does not corrupt the fit score (inversion is reported separately).
    """
    lo = min(candidate_min, candidate_max)
    hi = max(candidate_min, candidate_max)
    overlap = max(0.0, min(hi, target.max_lpa) - max(lo, target.min_lpa))
    union = max(hi, target.max_lpa) - min(lo, target.min_lpa)
    if union <= 0.0:
        # Both bands collapse to a point; fit iff they coincide.
        return 1.0 if lo == target.min_lpa else 0.0
    return overlap / union


def extract_geography(
    raw: RawCandidate,
    logistics: LogisticsProfile,
    jd_hubs: frozenset[str] = DEFAULT_JD_HUBS,
    jd_salary: SalaryTarget | None = None,
) -> Mapping[FeatureId, FeatureCell]:
    """Extract the ``geo.* / reloc.* / notice.* / sal.*`` cells for one candidate.

    Deterministic and total. Confidence drops only when the city is unrecognized
    (``LocationFit`` resolved to a non-hub bucket on an unknown city) or when no
    JD salary target is supplied.
    """
    cells: dict[FeatureId, FeatureCell] = {}

    loc_ev = mint_evidence(raw, EvidenceKind.PROFILE_FIELD, "profile.location")
    country_ev = mint_evidence(raw, EvidenceKind.PROFILE_FIELD, "profile.country")
    reloc_ev = mint_evidence(
        raw, EvidenceKind.SIGNAL, "redrob_signals.willing_to_relocate"
    )
    fit = logistics.location_fit
    city_known = fit is not LocationFit.INDIA_NON_RELOCATABLE or logistics.willing_to_relocate
    geo_conf = 0.95 if fit is LocationFit.PREFERRED_HUB else (0.85 if city_known else 0.5)

    # --- geo.* -------------------------------------------------------------- #
    cells[GEO_HUB_MATCH] = make_cell(
        _hub_match_value(logistics, jd_hubs), geo_conf, (loc_ev,)
    )
    india_relocatable = (
        fit is LocationFit.INDIA_RELOCATABLE
        or (fit is LocationFit.PREFERRED_HUB)
        or (fit is LocationFit.INDIA_NON_RELOCATABLE and logistics.willing_to_relocate)
    )
    cells[GEO_INDIA_RELOCATABLE] = make_cell(
        1.0 if india_relocatable else 0.0, geo_conf, (loc_ev, reloc_ev)
    )
    cells[GEO_OUTSIDE_INDIA_NO_SPONSOR] = make_cell(
        1.0 if fit is LocationFit.OUTSIDE_INDIA_NO_SPONSOR else 0.0,
        geo_conf,
        (country_ev,),
    )

    # --- reloc.* ------------------------------------------------------------ #
    cells[RELOC_WILLING] = make_cell(
        1.0 if logistics.willing_to_relocate else 0.0, 0.95, (reloc_ev,)
    )
    # "needed" iff not already in a preferred hub.
    needed = fit is not LocationFit.PREFERRED_HUB
    cells[RELOC_NEEDED] = make_cell(1.0 if needed else 0.0, geo_conf, (loc_ev,))

    # --- notice.* ----------------------------------------------------------- #
    notice_ev = mint_evidence(
        raw, EvidenceKind.SIGNAL, "redrob_signals.notice_period_days"
    )
    cells[NOTICE_FIT] = make_cell(
        _NOTICE_FIT_SCORE[logistics.notice_fit], 0.95, (notice_ev,)
    )
    cells[NOTICE_OVER_30] = make_cell(
        1.0 if logistics.notice_period_days > _NOTICE_IDEAL_MAX_DAYS else 0.0,
        0.95,
        (notice_ev,),
    )

    # --- sal.* -------------------------------------------------------------- #
    sal_min_ev = mint_evidence(
        raw, EvidenceKind.SIGNAL,
        "redrob_signals.expected_salary_range_inr_lpa.min",
    )
    sal_max_ev = mint_evidence(
        raw, EvidenceKind.SIGNAL,
        "redrob_signals.expected_salary_range_inr_lpa.max",
    )
    if jd_salary is not None:
        sal_fit = _salary_overlap(
            float(logistics.salary.min_lpa),
            float(logistics.salary.max_lpa),
            jd_salary,
        )
        sal_conf = 0.7
    else:
        # No JD band: neutral fit, reduced confidence (cannot judge alignment).
        sal_fit = 0.5
        sal_conf = 0.3
    cells[SAL_FIT] = make_cell(sal_fit, sal_conf, (sal_min_ev, sal_max_ev))
    cells[SAL_IS_INVERTED] = make_cell(
        1.0 if logistics.salary.is_inverted else 0.0, 0.9, (sal_min_ev, sal_max_ev)
    )

    return MappingProxyType(cells)


__all__: tuple[str, ...] = (
    "DEFAULT_JD_HUBS",
    "GEO_HUB_MATCH",
    "GEO_INDIA_RELOCATABLE",
    "GEO_OUTSIDE_INDIA_NO_SPONSOR",
    "NOTICE_FIT",
    "NOTICE_OVER_30",
    "RELOC_NEEDED",
    "RELOC_WILLING",
    "SAL_FIT",
    "SAL_IS_INVERTED",
    "SalaryTarget",
    "extract_geography",
)