File size: 8,219 Bytes
7a9cfa1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Shared constants, helpers, and data loaders for the runtime pipeline.
Reference date: 2026-06-10.
"""

import json
import math
import re
from datetime import date
from pathlib import Path
from typing import Iterator

REFERENCE_DATE = date(2026, 6, 10)

# --------------------------------------------------------------------------- #
# Company lists
# --------------------------------------------------------------------------- #

IT_SERVICES: set[str] = {
    "tcs", "tata consultancy", "infosys", "wipro", "accenture", "cognizant",
    "capgemini", "hcl", "hcl technologies", "tech mahindra", "mphasis",
    "hexaware", "ltimindtree", "mindtree", "persistent", "persistent systems",
    "coforge", "birlasoft", "niit technologies", "l&t infotech",
    "lti", "mastech", "kpit", "sonata software",
}

# Fictional companies used as honeypot markers (with founding year)
FICTIONAL_COMPANIES: dict[str, int] = {
    "pied piper": 2014,
    "initech": 1999,
    "wayne enterprises": 1939,
    "stark industries": 1940,
    "dunder mifflin": 1949,
    "umbrella corporation": 1968,
    "soylent": 2010,
    "globex": 1996,
}

# --------------------------------------------------------------------------- #
# JD skill lists (used by feature_engineering.py)
# --------------------------------------------------------------------------- #

JD_CORE_SKILLS: list[str] = [
    "embeddings", "retrieval", "vector", "ranking", "search",
    "sentence-transformers", "sentence_transformers", "faiss", "elasticsearch",
    "pinecone", "weaviate", "ndcg", "evaluation", "a/b", "python",
    "llm", "fine-tuning", "finetuning", "lora", "recommendation",
    "nlp", "ir", "information retrieval",
]

JD_NICE_SKILLS: list[str] = [
    "xgboost", "learning-to-rank", "ltr", "distributed systems",
    "inference optimization", "open source", "hr tech", "recruitment",
    "qdrant", "milvus", "opensearch",
]

# City names mapped for preferred-city matching
PREFERRED_CITIES: list[str] = [
    "pune", "noida", "hyderabad", "mumbai", "delhi", "bangalore", "bengaluru",
    "gurgaon", "gurugram",
]

# Production-action words used for production_signal_count
PRODUCTION_KEYWORDS: list[str] = [
    "deployed", "deploy", "productionized", "shipped", "ship",
    "scaled", "scale", "served", "serving", "production", "prod",
    "users", "latency", "throughput", "real-time", "realtime", "live",
    "live traffic", "a/b test", "a/b tested", "rollout", "launched",
    "in production", "at scale", "millions", "billion", "qps", "rps",
    "uptime", "reliability", "serving infrastructure",
]

# --------------------------------------------------------------------------- #
# Education tier mapping
# --------------------------------------------------------------------------- #

EDU_TIER_SCORES: dict[str, int] = {
    "tier_1": 4,
    "tier_2": 3,
    "tier_3": 2,
    "tier_4": 1,
}

STEM_FIELDS: set[str] = {
    "computer science", "cs", "cse", "information technology", "it",
    "electronics", "ece", "electrical", "eee", "mathematics", "statistics",
    "physics", "data science", "machine learning", "artificial intelligence",
    "ai", "ml", "information systems", "software engineering", "software",
}

# --------------------------------------------------------------------------- #
# Company-size ordinal encoding
# --------------------------------------------------------------------------- #

COMPANY_SIZE_ORDINAL: dict[str, int] = {
    "1-10": 1,
    "11-50": 2,
    "51-200": 3,
    "201-500": 4,
    "501-1000": 5,
    "1001-5000": 6,
    "5001-10000": 7,
    "10001+": 8,
}

# --------------------------------------------------------------------------- #
# Date / duration helpers
# --------------------------------------------------------------------------- #

def days_since(date_str: str, ref: date = REFERENCE_DATE) -> int:
    """Return days between date_str (ISO) and REFERENCE_DATE. Negative = future."""
    try:
        d = date.fromisoformat(date_str)
        return (ref - d).days
    except (ValueError, TypeError):
        return 0


def parse_year(date_str: str | None) -> int | None:
    """Extract year from an ISO date string or bare year string."""
    if not date_str:
        return None
    try:
        return int(str(date_str)[:4])
    except (ValueError, TypeError):
        return None


def notice_penalty(notice_days: int) -> float:
    """
    0 if notice_period_days <= 30.
    Linear decay: 0.5 at 90d, 0.0 at 180d+.
    """
    if notice_days <= 30:
        return 0.0
    if notice_days >= 180:
        return 1.0  # max penalty
    # linear between 30β†’90: 0β†’0.5; 90β†’180: 0.5β†’1.0
    return min(1.0, (notice_days - 30) / 150.0)


def yoe_fit_score(yoe: float) -> float:
    """
    Gaussian-like score peaked at [6, 8] years.
    - Below 4: steep penalty
    - 4–6: ramp up
    - 6–8: peak (1.0)
    - 8–12: soft decay
    - Above 12: harder decay
    """
    if yoe < 2:
        return 0.0
    if yoe <= 6:
        return max(0.0, (yoe - 2) / 4.0)
    if yoe <= 8:
        return 1.0
    if yoe <= 12:
        return max(0.5, 1.0 - (yoe - 8) / 8.0)
    return max(0.2, 0.5 - (yoe - 12) / 20.0)


# --------------------------------------------------------------------------- #
# Text helpers
# --------------------------------------------------------------------------- #

def career_text(candidate: dict) -> str:
    """Concatenate all career description text, lowercased."""
    parts = [candidate["profile"].get("summary", ""),
             candidate["profile"].get("headline", "")]
    for job in candidate.get("career_history", []):
        parts.append(job.get("description", ""))
    return " ".join(parts).lower()


def tokenize(text: str) -> list[str]:
    """Lowercase alphanumeric tokenization β€” shared by BM25 build (offline + runtime)."""
    return re.findall(r"[a-z0-9]+", text.lower())


def candidate_to_chunks(c: dict) -> list[str]:
    """
    Text chunks fed to the dense embedder, one vector per chunk.

    SINGLE SOURCE OF TRUTH β€” used by both offline.precompute_embeddings (training)
    and src.runtime_index (rank time). The XGBoost model is trained on semantic
    features derived from these exact chunks, so offline and runtime MUST chunk
    identically or the feature distribution shifts and the model degrades.
    """
    chunks: list[str] = []
    for job in c.get("career_history", []):
        if job.get("description"):
            chunks.append(f"{job['title']} at {job['company']}: {job['description']}")
    if c["profile"].get("summary"):
        chunks.append(c["profile"]["summary"])
    if c["profile"].get("headline"):
        chunks.append(c["profile"]["headline"])
    return chunks or [c["profile"].get("current_title", "")]


def candidate_to_bm25_text(c: dict) -> str:
    """Concatenated text indexed by BM25. Shared by offline + runtime BM25 build."""
    parts = [
        c["profile"].get("headline", ""),
        c["profile"].get("summary", ""),
        c["profile"].get("current_title", ""),
    ]
    for job in c.get("career_history", []):
        parts.append(job.get("title", ""))
        parts.append(job.get("description", ""))
    for s in c.get("skills", []):
        parts.append(s.get("name", ""))
    return " ".join(parts)


def is_it_services_company(company_name: str) -> bool:
    name_lower = company_name.lower()
    return any(svc in name_lower for svc in IT_SERVICES)


def company_size_ordinal(size_str: str | None) -> int:
    return COMPANY_SIZE_ORDINAL.get(size_str or "", 0)


# --------------------------------------------------------------------------- #
# JSONL streaming loader (memory-efficient for 100K candidates)
# --------------------------------------------------------------------------- #

def stream_candidates(path: str | Path) -> Iterator[dict]:
    """Yield one candidate dict per line without loading the full file."""
    with open(path, encoding="utf-8") as f:
        for line in f:
            line = line.strip()
            if line:
                yield json.loads(line)


def load_candidates_json(path: str | Path) -> list[dict]:
    """Load sample_candidates.json (list format, not JSONL)."""
    with open(path, encoding="utf-8") as f:
        return json.load(f)