File size: 45,689 Bytes
b7972bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
"""CPPS core — shared parsing, skill normalization, scoring, and debug helpers.

Single source of truth for the production pipeline and the import-backed notebooks.

Phase 1: L6 (centralized blacklist/whitelist), L7 (unified alias map),
         L9 (debug log), L10 (pipeline consistency).
Phase 2: L1 (adaptive threshold), L3 (semantic synonyms),
         L4 (POS filtering), L12 (dynamic top_n).
"""

from __future__ import annotations

import logging
import re
from collections import Counter
from dataclasses import dataclass, field
from typing import Dict, Iterable, List, Optional, Tuple

logger = logging.getLogger(__name__)


# ---------------------------------------------------------------------------
# L6 — Centralized noise blacklist (union of prior NB3 + NB8 sets)
# ---------------------------------------------------------------------------

NON_SKILL_WORDS: set = {
    # JD framing
    "hiring", "looking", "required", "preferred", "experience", "years",
    "education", "degree", "field", "related", "bachelor", "master",
    "candidate", "position", "role", "opportunity",
    # Connective / stopword-like
    "of", "in", "and", "or", "the", "a", "an", "to", "for", "with",
    "on", "at", "by", "is", "are", "we", "our", "their", "your",
    # Generic nouns that leak through
    "skills", "data", "analysis", "deep", "platforms", "team",
    "work", "tools", "strong", "good", "best", "knowledge",
    # Section labels
    "required skills", "preferred skills", "related field",
    "qualifications", "requirements", "responsibilities",
    "team player", "ability", "experience in", "knowledge of",
    "good to have", "nice to have", "must have",
    "certifications", "overview", "company", "about", "benefits",
    "perks", "salary", "date", "hands", "engineers", "implement",
    "controls", "specifications",
    "load", "e.g", "eg",
}

JD_NOISE_PHRASES: set = {
    "company overview", "role overview", "key responsibilities",
    "required qualifications", "preferred qualifications",
    "technical skills", "overview", "responsibilities",
    "qualifications", "requirements", "benefits", "perks",
    "job description", "about us", "about the role",
    "what you will do", "what we look for",
    "job title", "must have", "good to have", "nice to have",
    "compensation", "work mode", "hybrid", "remote", "onsite",
    "about the company", "minimum qualifications", "basic qualifications",
    "years of experience", "strong communication", "excellent communication",
    "or related field", "related field", "and related field",
    "about the team", "job responsibilities", "skills and capabilities",
    "maintain consistent communication", "including personal banking",
    "equal opportunity employer", "sexual orientation", "national origin",
    "race", "religion", "gender", "age", "color",
    "business requirements", "technical specifications", "strong controls",
    "bachelor s degree", "hands on experience",
    "deployment of data", "analytics technologies", "strategy to reporting",
}

# Substring-filtered noise ("ability to work with X" -> reject anything
# containing "ability", "experience", etc.)
SKILL_NOISE_SUBSTRINGS: List[str] = [
    "ability", "experience", "knowledge",
    "responsible", "working with", "understanding",
    "proficiency", "familiarity", "expertise",
]

JD_NOISE_SUBSTRINGS: List[str] = [
    "about the",
    "job responsibilities",
    "responsibilit",
    "equal opportunity",
    "sexual orientation",
    "national origin",
    "interpersonal",
    "communication",
    "results-driven",
    "including ",
    "maintain consistent",
]

JD_TOKEN_BLACKLIST: set = {
    "you", "your", "we", "our", "us", "them", "they", "the", "a", "an",
    "this", "that", "these", "those", "both", "regards",
}

JD_ACTION_VERBS: set = {
    "collaborate", "analyze", "design", "develop", "monitor", "provide",
    "translate", "document", "possess", "create", "creating", "eagerly",
    "maintain", "conduct", "work", "stay", "transform", "test", "understand",
}

JD_GENERIC_TAIL_TOKENS: set = {
    "team", "teams", "needs", "objectives", "products", "users", "training",
    "support", "insights", "trends", "documentation", "capabilities",
    "levels", "assignments", "timelines", "efforts", "progress", "future",
    "growth", "engagement", "history", "solutions", "businesses", "consumers",
}

TECH_SIGNAL_TOKENS: set = {
    "access", "agile", "ai", "alteryx", "amazon", "analytics",
    "api", "artificial", "aws", "azure", "bi", "cloud", "dashboard",
    "data", "database", "docker", "excel", "etl", "gcp", "google",
    "java", "javascript", "kubernetes", "machine", "microsoft", "model",
    "mongodb", "mysql", "office", "oracle", "pipeline", "postgresql",
    "power", "python", "reporting", "scikit-learn", "snowflake", "sql",
    "tableau", "tensorflow", "teradata", "visualization",
}

JD_BUCKET_WEIGHTS: Dict[str, float] = {
    "required": 1.0,
    "preferred": 0.75,
    "optional": 0.5,
}

MATCH_SCORE_BY_TYPE: Dict[str, Tuple[float, float]] = {
    "exact": (1.0, 1.0),
    "alias": (1.0, 1.0),
    "synonym": (0.9, 0.9),
    "semantic": (0.55, 0.80),
    "partial": (0.30, 0.50),
}

SKILL_SCORE_COMPONENT_WEIGHTS: Dict[str, float] = {
    "weighted_jd_coverage": 0.45,
    "avg_match_confidence": 0.35,
    "resume_pool_coverage": 0.20,
}

SKILL_CLUSTERS: Dict[str, List[str]] = {
    "python_ecosystem": ["python", "pandas", "numpy", "scipy", "matplotlib"],
    "cloud_platforms": ["aws", "azure", "gcp", "cloud computing", "google cloud platform"],
    "web_frontend": ["react", "angular", "vue", "html", "css", "javascript"],
    "databases": ["sql", "mysql", "postgresql", "mongodb", "redis"],
}

# Soft skills — not matchable via SBERT against technical JDs.
SOFT_SKILL_BLACKLIST: set = {
    "communication", "teamwork", "leadership", "problem-solving",
    "problem solving", "collaboration", "adaptability", "creativity",
    "time management", "critical thinking", "interpersonal skills",
}

# Whitelist: phrases matching these patterns are ALWAYS kept even if short
# or stopword-ish (e.g. "C++", "C#", "SQL", "React.js").
SHORT_SKILL_WHITELIST: set = {
    "sql", "aws", "gcp", "api", "apis", "etl", "bi", "ui", "ux",
    "ml", "ai", "nlp", "cv", "sap", "erp", "crm", "css", "html",
}

WHITELIST_PATTERNS: List[re.Pattern] = [
    re.compile(r"^[a-z]+\+\+$"),          # C++
    re.compile(r"^[a-z]+#$"),              # C#, F#
    re.compile(r"^[a-z]+\.[a-z]+$"),       # react.js, node.js, vue.js
]


def is_whitelisted(phrase: str) -> bool:
    """Return True if phrase matches a tech-skill whitelist pattern."""
    if not phrase:
        return False
    p = phrase.strip().lower()
    return p in SHORT_SKILL_WHITELIST or any(pat.match(p) for pat in WHITELIST_PATTERNS)


# ---------------------------------------------------------------------------
# L7 — Unified alias / normalization map
# ---------------------------------------------------------------------------

SKILL_ALIAS_MAP: Dict[str, str] = {
    # Abbreviations
    "ml": "machine learning",
    "dl": "deep learning",
    "nlp": "natural language processing",
    "cv": "computer vision",
    "ai": "artificial intelligence",
    "js": "javascript",
    "ts": "typescript",
    "py": "python",
    "tf": "tensorflow",
    "k8s": "kubernetes",
    "gcp": "google cloud platform",
    "aws": "amazon web services",
    "gke": "google kubernetes engine",
    "eks": "amazon elastic kubernetes service",
    "ci/cd": "continuous integration",
    "oop": "object-oriented programming",
    "db": "database",
    "rdbms": "relational database",
    "eda": "exploratory data analysis",

    # Common variants / spellings
    "sklearn": "scikit-learn",
    "scikit learn": "scikit-learn",
    "postgres": "postgresql",
    "mongo": "mongodb",
    "powerbi": "power bi",
    "power-bi": "power bi",
    "reactjs": "react",
    "react.js": "react",
    "nodejs": "node.js",
    "node js": "node.js",
    "vuejs": "vue",
    "vue.js": "vue",
    "angularjs": "angular",
    "angular.js": "angular",
    "c++": "cpp",
    "c#": "csharp",
    ".net": "dotnet",
    "dot net": "dotnet",
    "tensor flow": "tensorflow",
    "py torch": "pytorch",
    "torch": "pytorch",

    # Conceptual equivalences (lightweight)
    "data wrangling": "data manipulation",
    "data cleaning": "data preprocessing",
    "etl": "data pipeline",
    "rest api": "rest",
    "restful api": "rest",
    "restful apis": "rest",
    "amazon web services": "aws",
    "google cloud": "google cloud platform",
    "google cloud services": "google cloud platform",
    "postgres sql": "postgresql",
    "mongo db": "mongodb",
    "expressjs": "express",
    "react js": "react",
    "java script": "javascript",
    "scikit learn library": "scikit-learn",

    # Frontend / backend / full-stack variants
    "frontend": "frontend development",
    "front-end": "frontend development",
    "front end": "frontend development",
    "backend": "backend development",
    "back-end": "backend development",
    "back end": "backend development",
    "full-stack": "full stack development",
    "fullstack": "full stack development",
    "full stack": "full stack development",
    "dashboarding tools": "dashboarding",
    "dashboard tools": "dashboarding",
    "rest apis": "rest",
}


def normalize_skill(skill: str) -> str:
    """Resolve abbreviations/variants to canonical form."""
    if not skill:
        return ""
    key = skill.lower().strip()
    return SKILL_ALIAS_MAP.get(key, key)


# ---------------------------------------------------------------------------
# Text normalization (shared by NB3 + NB8)
# ---------------------------------------------------------------------------

def normalize_text_for_skills(text: Optional[str]) -> str:
    """Normalize skill-focused text while preserving skill-list boundaries."""
    if not isinstance(text, str) or not text.strip():
        return ""
    cleaned = text.strip().lower()
    cleaned = cleaned.replace("\r\n", "\n").replace("\r", "\n")
    cleaned = re.sub(r"[\n;|]+", ", ", cleaned)
    cleaned = re.sub(r"[^a-z0-9,\+#\./\- ]+", " ", cleaned)
    cleaned = re.sub(r"\s*,\s*", ", ", cleaned)
    cleaned = re.sub(r"\s+", " ", cleaned)
    return cleaned.strip(" ,")


# Backward-compat alias for NB3 callers using `normalize_text`.
normalize_text = normalize_text_for_skills


def normalize_phrase(phrase: Optional[str]) -> str:
    """Normalize a single skill phrase for comparison and output."""
    normalized = normalize_text_for_skills(phrase)
    normalized = normalized.replace(",", " ")
    normalized = re.sub(r"\s+", " ", normalized)
    return normalized.strip()


# ---------------------------------------------------------------------------
# Skill filtering / cleaning
# ---------------------------------------------------------------------------

def is_valid_skill(phrase: str) -> bool:
    """Return True if phrase passes the unified blacklist/whitelist filter."""
    if not phrase:
        return False
    p = phrase.strip().lower()
    if len(p) < 2:
        return False
    if is_whitelisted(p):
        return True
    if p in NON_SKILL_WORDS:
        return False
    if p in JD_NOISE_PHRASES:
        return False
    if p in SOFT_SKILL_BLACKLIST:
        return False
    if len(p.split()) > 3:
        return False
    if any(sub in p for sub in SKILL_NOISE_SUBSTRINGS):
        return False
    return True


def is_valid_jd_skill(phrase: str) -> bool:
    """JD-specific filter that removes legal/boilerplate and soft-skill noise."""
    p = normalize_skill(normalize_phrase(phrase))
    if not is_valid_skill(p):
        return False
    tokens = p.split()
    if any(token in JD_TOKEN_BLACKLIST for token in tokens):
        return False
    if tokens and tokens[0] in JD_ACTION_VERBS:
        return False
    if tokens and tokens[-1] in JD_GENERIC_TAIL_TOKENS:
        return False
    if any(sub in p for sub in JD_NOISE_SUBSTRINGS):
        return False
    if re.search(r"\b(?:gender|religion|race|color|age|disability)\b", p):
        return False
    if re.search(r"\b\d+\+?\s*years?\b", p):
        return False
    if re.search(r"\b(?:engineers?|implement|hands?|degree|bachelor|master|phd|date)\b", p):
        return False
    if len(tokens) == 1 and not (
        is_whitelisted(p) or
        p in SKILL_ALIAS_MAP or
        p in SKILL_ALIAS_MAP.values() or
        p in SEMANTIC_SYNONYMS or
        p in _SYNONYM_REVERSE or
        p in TECH_SIGNAL_TOKENS
    ):
        return False
    return True


def clean_skills(skills: Iterable[str], cap: Optional[int] = 30) -> List[str]:
    """Filter noise, normalize aliases, deduplicate, cap length."""
    cleaned: List[str] = []
    for s in skills:
        if not s:
            continue
        p = s.lower().strip()
        if not is_valid_skill(p):
            continue
        p = normalize_skill(p)
        if p not in cleaned:
            cleaned.append(p)
    if cap is None:
        return cleaned
    return cleaned[:cap]


def has_technical_signal(phrase: str) -> bool:
    """Return True when a phrase contains at least one technical anchor."""
    p = normalize_skill(normalize_phrase(phrase))
    if not p:
        return False
    if is_whitelisted(p) or p in SKILL_ALIAS_MAP or p in SKILL_ALIAS_MAP.values():
        return True
    if p in SEMANTIC_SYNONYMS or p in _SYNONYM_REVERSE:
        return True
    tokens = {tok for tok in re.split(r"[\s/\-]+", p) if tok}
    return bool(tokens & TECH_SIGNAL_TOKENS)


def compute_token_overlap_ratio(phrase_a: str, phrase_b: str) -> float:
    """Compute overlap on normalized, non-trivial tokens."""
    stop_tokens = NON_SKILL_WORDS | JD_TOKEN_BLACKLIST | {"s"}
    tokens_a = {
        tok for tok in re.split(r"[\s/\-]+", normalize_phrase(phrase_a))
        if tok and tok not in stop_tokens
    }
    tokens_b = {
        tok for tok in re.split(r"[\s/\-]+", normalize_phrase(phrase_b))
        if tok and tok not in stop_tokens
    }
    if not tokens_a or not tokens_b:
        return 0.0
    return len(tokens_a & tokens_b) / float(max(len(tokens_a), len(tokens_b)))


def _cluster_names_for_skill(phrase: str) -> set:
    tokens = {tok for tok in re.split(r"[\s/\-]+", normalize_skill(normalize_phrase(phrase))) if tok}
    normalized = normalize_skill(normalize_phrase(phrase))
    clusters = set()
    for name, members in SKILL_CLUSTERS.items():
        normalized_members = {normalize_skill(normalize_phrase(member)) for member in members}
        member_tokens = set().union(*(member.split() for member in normalized_members))
        if normalized in normalized_members or tokens & member_tokens:
            clusters.add(name)
    return clusters


def skills_share_cluster(left: str, right: str) -> bool:
    """Return True when two skills belong to at least one ontology cluster."""
    return bool(_cluster_names_for_skill(left) & _cluster_names_for_skill(right))


def clamp_match_score(match_type: str, similarity: float) -> float:
    """Clamp score into the configured range for a match tier."""
    low, high = MATCH_SCORE_BY_TYPE.get(match_type, (0.0, 1.0))
    if low == high:
        return round(low, 4)
    bounded = max(low, min(float(similarity or 0.0), high))
    return round(bounded, 4)


def split_skill_line(raw_line: str) -> List[str]:
    """Split multi-skill resume lines ('Languages: Python, Java') into tokens.

    Skips soft-skills categories entirely (non-technical, not useful).
    """
    if not raw_line:
        return []
    if ":" in raw_line:
        prefix = raw_line.split(":", 1)[0].strip().lower()
        if "soft" in prefix:
            return []
        raw_line = raw_line.split(":", 1)[1]
    tokens = re.split(r"[,;|]+", raw_line)
    results: List[str] = []
    for token in tokens:
        token = token.strip()
        if token and len(token) >= 2:
            results.append(token)
    return results


def deduplicate_phrases(phrases: Iterable[str]) -> List[str]:
    """Remove exact and substring-redundant duplicates while preserving order."""
    unique: List[str] = []
    for phrase in phrases:
        np_ = normalize_phrase(phrase)
        if len(np_) < 2 or np_ in NON_SKILL_WORDS or np_ in unique:
            continue
        redundant = False
        to_remove: List[str] = []
        np_tokens = np_.split()
        for existing in unique:
            existing_tokens = existing.split()
            if np_ == existing:
                redundant = True
                break
            if np_ in existing and len(np_tokens) <= len(existing_tokens):
                to_remove.append(existing)
            elif existing in np_ and len(existing_tokens) <= len(np_tokens):
                redundant = True
                break
        if redundant:
            continue
        if to_remove:
            unique = [item for item in unique if item not in to_remove]
        unique.append(np_)
    return unique


def merge_unique_skills(*skill_groups: Iterable[str], cap: Optional[int] = None) -> List[str]:
    """Merge multiple skill collections into a cleaned, ordered list."""
    merged: List[str] = []
    for group in skill_groups:
        for phrase in group or []:
            normalized = normalize_skill(normalize_phrase(phrase))
            if normalized and is_valid_skill(normalized) and normalized not in merged:
                merged.append(normalized)
                if cap is not None and len(merged) >= cap:
                    return merged
    return merged


# ---------------------------------------------------------------------------
# L8 — Multi-source skill aggregation helper
# ---------------------------------------------------------------------------

_TECH_STACK_RE = re.compile(r"tech\s*stack\s*[:\-]\s*(.+)", re.IGNORECASE)

RESUME_SECTION_ALIASES: Dict[str, Tuple[str, ...]] = {
    "skills": (
        "skills", "technical skills", "core skills", "key skills", "skill set",
        "technical expertise", "technologies", "tools and technologies",
        "tools & technologies", "programming languages", "languages",
        "competencies", "core competencies", "technical proficiency",
        "technical toolkit", "technology stack", "tech stack",
    ),
    "projects": (
        "project", "projects", "academic project", "academic projects",
        "personal project", "personal projects", "key projects",
        "project experience", "notable projects", "project work",
        "projects & achievements", "major projects", "side projects",
        "project details", "project detail",
    ),
    "certifications": (
        "certification", "certifications", "certificate", "certificates",
        "licenses & certifications", "professional certifications",
        "courses & certifications", "online courses", "training",
        "courses", "professional development",
    ),
    "internships": (
        "internship", "internships", "internship experience",
        "industrial training", "industry experience", "summer internship",
        "summer internships", "internship & training", "internship details",
    ),
    "work_experience": (
        "work experience", "experience", "professional experience",
        "employment history", "employment", "career history",
        "relevant experience", "job experience", "work history",
    ),
    "education": (
        "education", "educational background", "academic background",
        "academic qualification", "academic qualifications", "qualifications",
        "academic details", "scholastic details", "academic history",
    ),
    "achievements": (
        "achievement", "achievements", "accomplishment", "accomplishments",
        "award", "awards", "honors", "honours", "awards & honors",
        "awards & achievements", "recognition",
        "extra-curricular activities", "extracurricular activities",
        "extracurriculars", "leadership", "activities", "competition",
        "competitions", "hackathon", "hackathons",
    ),
}


def _compile_section_patterns() -> Dict[str, re.Pattern]:
    patterns: Dict[str, re.Pattern] = {}
    for section_key, aliases in RESUME_SECTION_ALIASES.items():
        escaped = "|".join(re.escape(alias) for alias in aliases)
        patterns[section_key] = re.compile(rf"^\s*(?:{escaped})\s*[:\-]?\s*$", re.IGNORECASE)
    return patterns


SECTION_PATTERNS: Dict[str, re.Pattern] = _compile_section_patterns()


def split_resume_into_sections(raw_text: str) -> Dict[str, List[str]]:
    """Split resume text into structured sections using shared header patterns."""
    sections = {key: [] for key in RESUME_SECTION_ALIASES}
    if not raw_text:
        return sections

    current_section = None
    for line in raw_text.splitlines():
        stripped = line.strip()
        if not stripped:
            continue
        matched_section = None
        for section_key, pattern in SECTION_PATTERNS.items():
            if pattern.match(stripped):
                matched_section = section_key
                break
        if matched_section:
            current_section = matched_section
            continue
        if current_section is not None:
            sections[current_section].append(stripped)
    return sections


def extract_skills_from_section(section_entries: Iterable) -> List[str]:
    """Pull skill-like tokens from a free-form section (projects, internships,
    work_experience). Looks for 'Tech Stack:' / 'Technologies:' / 'Skills:'
    lines and comma-separated lists.
    """
    results: List[str] = []
    if not section_entries:
        return results
    for entry in section_entries:
        text = entry if isinstance(entry, str) else (
            " ".join(str(v) for v in entry.values()) if isinstance(entry, dict) else ""
        )
        if not text:
            continue
        for line in text.splitlines():
            line = line.strip()
            m = _TECH_STACK_RE.search(line)
            payload = None
            if m:
                payload = m.group(1)
            elif ":" in line and any(
                kw in line.lower().split(":", 1)[0]
                for kw in ("technolog", "skill", "stack", "tools")
            ):
                payload = line.split(":", 1)[1]
            if payload:
                for tok in split_skill_line(payload):
                    p = normalize_skill(normalize_phrase(tok))
                    if is_valid_skill(p) and p not in results:
                        results.append(p)
    return results


_KNOWN_FALLBACK_SKILLS: Optional[List[str]] = None


def _get_known_fallback_skills() -> List[str]:
    global _KNOWN_FALLBACK_SKILLS
    if _KNOWN_FALLBACK_SKILLS is None:
        phrases = (
            list(SKILL_ALIAS_MAP.keys()) +
            list(SKILL_ALIAS_MAP.values()) +
            list(SEMANTIC_SYNONYMS.keys()) +
            [syn for syns in SEMANTIC_SYNONYMS.values() for syn in syns]
        )
        _KNOWN_FALLBACK_SKILLS = sorted({
            normalize_skill(phrase)
            for phrase in phrases
            if phrase and is_valid_skill(normalize_phrase(phrase))
        }, key=lambda item: (-len(item.split()), -len(item), item))
    return list(_KNOWN_FALLBACK_SKILLS)


def extract_skills_from_full_text(text: str, cap: int = 25) -> List[str]:
    """Fallback skill mining for resumes without reliable sectioning."""
    if not text or not text.strip():
        return []

    candidates: List[str] = []
    for line in text.splitlines():
        stripped = line.strip()
        if not stripped:
            continue
        for chunk in re.split(r"[,;|\u2022/\t]+", stripped):
            phrase = normalize_skill(normalize_phrase(chunk))
            if phrase and is_valid_skill(phrase):
                candidates.append(phrase)

    normalized_text = f" {normalize_text_for_skills(text)} "
    for phrase in _get_known_fallback_skills():
        if f" {phrase} " in normalized_text:
            candidates.append(phrase)

    return clean_skills(deduplicate_phrases(candidates), cap=cap)


def clean_jd_skill_phrases(phrases: Iterable[str], cap: int = 40) -> List[str]:
    """Normalize and filter JD skill phrases more aggressively than resume skills."""
    cleaned: List[str] = []
    for phrase in phrases or []:
        normalized = normalize_skill(normalize_phrase(phrase))
        if not normalized or not is_valid_jd_skill(normalized):
            continue
        if normalized not in cleaned:
            cleaned.append(normalized)
            if len(cleaned) >= cap:
                break
    return cleaned


# ---------------------------------------------------------------------------
# L9 — Debug & transparency layer
# ---------------------------------------------------------------------------

@dataclass
class DebugLog:
    """Structured debug log accumulated through the skill-extraction pipeline.

    Attach to a pipeline run, populate at each stage, and emit under a
    `_debug` key in the API response when `debug=True`.
    """
    raw_resume_skills: List[str] = field(default_factory=list)
    filtered_resume_skills: List[str] = field(default_factory=list)
    normalized_resume_skills: List[str] = field(default_factory=list)
    jd_skills_raw: List[str] = field(default_factory=list)
    jd_skills_filtered: List[str] = field(default_factory=list)
    required_skills: List[str] = field(default_factory=list)
    preferred_skills: List[str] = field(default_factory=list)
    optional_skills: List[str] = field(default_factory=list)
    rejected_jd_phrases: List[Dict] = field(default_factory=list)
    exact_matches: List[Dict] = field(default_factory=list)
    alias_matches: List[Dict] = field(default_factory=list)
    semantic_matches: List[Dict] = field(default_factory=list)
    synonym_matches: List[Dict] = field(default_factory=list)
    partial_matches: List[Dict] = field(default_factory=list)
    rejected_matches: List[Dict] = field(default_factory=list)
    final_missing: List[str] = field(default_factory=list)
    resume_skill_source_counts: Dict[str, int] = field(default_factory=dict)
    jd_bucket_counts: Dict[str, int] = field(default_factory=dict)
    match_type_counts: Dict[str, int] = field(default_factory=dict)
    skills_score_S_sk: float = 0.0
    weighted_jd_total: float = 0.0
    weighted_match_total: float = 0.0
    weighted_jd_coverage: float = 0.0
    avg_match_confidence: float = 0.0
    resume_pool_coverage: float = 0.0
    empty_resume_sections: List[str] = field(default_factory=list)
    notes: List[str] = field(default_factory=list)

    def note(self, message: str) -> None:
        self.notes.append(message)

    def to_dict(self) -> Dict:
        return {
            "raw_resume_skills": list(self.raw_resume_skills),
            "filtered_resume_skills": list(self.filtered_resume_skills),
            "normalized_resume_skills": list(self.normalized_resume_skills),
            "jd_skills_raw": list(self.jd_skills_raw),
            "jd_skills_filtered": list(self.jd_skills_filtered),
            "raw_jd_skills": list(self.jd_skills_raw),
            "filtered_jd_skills": list(self.jd_skills_filtered),
            "required_skills": list(self.required_skills),
            "preferred_skills": list(self.preferred_skills),
            "optional_skills": list(self.optional_skills),
            "rejected_jd_phrases": list(self.rejected_jd_phrases),
            "exact_matches": list(self.exact_matches),
            "alias_matches": list(self.alias_matches),
            "semantic_matches": list(self.semantic_matches),
            "synonym_matches": list(self.synonym_matches),
            "partial_matches": list(self.partial_matches),
            "rejected_matches": list(self.rejected_matches),
            "final_missing": list(self.final_missing),
            "resume_skill_source_counts": dict(self.resume_skill_source_counts),
            "jd_bucket_counts": dict(self.jd_bucket_counts),
            "match_type_counts": dict(self.match_type_counts),
            "skills_score_S_sk": float(self.skills_score_S_sk),
            "weighted_jd_total": float(self.weighted_jd_total),
            "weighted_match_total": float(self.weighted_match_total),
            "weighted_jd_coverage": float(self.weighted_jd_coverage),
            "avg_match_confidence": float(self.avg_match_confidence),
            "resume_pool_coverage": float(self.resume_pool_coverage),
            "empty_resume_sections": list(self.empty_resume_sections),
            "notes": list(self.notes),
        }


# ---------------------------------------------------------------------------
# L1 — Adaptive cosine-similarity threshold (Phase 2)
# ---------------------------------------------------------------------------

# Default fallback when adaptive thresholding is not used.
DEFAULT_MATCH_THRESHOLD = 0.75

# Length-band thresholds: shorter phrases need tighter matching (less context
# makes SBERT similarity noisier), longer phrases can tolerate lower scores
# because there is more semantic signal.
_THRESHOLD_BY_WORDS: List[Tuple[int, float]] = [
    (1, 0.65),   # single-word: "Python", "SQL" — strictest (short phrases are ambiguous)
    (2, 0.58),   # bigrams: "machine learning" — moderate
    (3, 0.52),   # 3+ words: "natural language processing" — more context allows lower threshold
]


def compute_adaptive_threshold(jd_phrase: str) -> float:
    """Return a cosine-similarity threshold adapted to *jd_phrase* length.

    Shorter JD phrases are inherently more ambiguous in SBERT space, so they
    require a higher similarity to count as a match.  Longer phrases carry
    more semantic context, allowing a lower threshold.
    """
    word_count = len(jd_phrase.strip().split())
    for max_words, threshold in _THRESHOLD_BY_WORDS:
        if word_count <= max_words:
            return threshold
    return _THRESHOLD_BY_WORDS[-1][1]  # longest band


# ---------------------------------------------------------------------------
# L3 — Semantic synonym map (Phase 2)
# ---------------------------------------------------------------------------

SEMANTIC_SYNONYMS: Dict[str, List[str]] = {
    # Data / ML tools
    "pandas": ["data manipulation", "dataframes", "data wrangling"],
    "numpy": ["numerical computing", "numerical python", "array computing"],
    "scipy": ["scientific computing", "scientific python"],
    "matplotlib": ["data visualization", "plotting"],
    "seaborn": ["data visualization", "statistical plotting"],
    "scikit-learn": ["machine learning library", "sklearn"],
    "tensorflow": ["deep learning framework", "tf"],
    "pytorch": ["deep learning framework", "torch"],
    "keras": ["deep learning framework", "neural network library"],
    "opencv": ["computer vision library", "image processing"],

    # Databases
    "mongodb": ["nosql", "document database", "nosql database"],
    "postgresql": ["postgres", "relational database"],
    "mysql": ["relational database", "sql database"],
    "redis": ["in-memory database", "caching", "key-value store"],
    "elasticsearch": ["search engine", "full-text search"],

    # Web / frontend
    "react": ["reactjs", "react.js", "frontend framework"],
    "angular": ["angularjs", "angular.js", "frontend framework"],
    "vue": ["vuejs", "vue.js", "frontend framework"],
    "node.js": ["nodejs", "server-side javascript", "backend javascript"],
    "express": ["expressjs", "node framework", "web framework"],
    "django": ["python web framework", "web framework"],
    "flask": ["python web framework", "micro framework"],
    "fastapi": ["python web framework", "async web framework"],

    # DevOps / Cloud
    "docker": ["containerization", "containers", "container platform"],
    "kubernetes": ["k8s", "container orchestration", "orchestration"],
    "terraform": ["infrastructure as code", "iac"],
    "jenkins": ["ci/cd", "continuous integration", "build automation"],
    "github actions": ["ci/cd", "continuous integration"],
    "aws": ["amazon web services", "cloud computing"],
    "gcp": ["google cloud platform", "cloud computing"],
    "azure": ["microsoft azure", "cloud computing"],

    # Languages / general
    "python": ["py"],
    "javascript": ["js", "ecmascript"],
    "typescript": ["ts"],
    "java": ["jvm"],
    "golang": ["go programming", "go language"],

    # Concepts
    "deep learning": ["neural networks", "dl"],
    "machine learning": ["ml", "predictive modeling"],
    "natural language processing": ["nlp", "text mining", "text analytics"],
    "computer vision": ["cv", "image recognition", "image classification"],
    "data pipeline": ["etl", "data engineering", "data workflow"],
    "rest": ["rest api", "restful api", "restful apis"],
    "graphql": ["graph query language", "api query language"],
    "microservices": ["micro services", "service-oriented architecture"],
    "object-oriented programming": ["oop", "java", "cpp", "csharp", "polymorphism"],
    "software development": ["software engineering", "application development", "coding"],
    "web development": ["frontend development", "full stack development", "web application"],
    "dashboard development": ["data visualization", "reporting", "business intelligence"],
    "data cleaning": ["data preprocessing", "data wrangling", "data preparation"],
    "data extraction": ["data mining", "data scraping", "etl"],
    "predictive models": ["machine learning models", "predictive analytics", "ml models"],

    # Frontend / Full-stack
    "frontend development": ["frontend", "front-end development", "ui development", "client-side"],
    "backend development": ["backend", "back-end development", "server-side"],
    "full stack development": ["full stack", "fullstack", "full-stack", "frontend backend"],
    "rest apis": ["rest", "restful api", "api development", "rest api"],

    # BI / Dashboarding tools
    "tableau": ["data visualization", "business intelligence", "dashboarding"],
    "power bi": ["data visualization", "business intelligence", "powerbi", "dashboarding"],
    "dashboarding": ["dashboard development", "data visualization", "reporting", "dashboarding tools"],
    "business reporting": ["data visualization", "reporting", "business intelligence"],
}

# Build reverse lookup: synonym -> set of canonical skills that claim it.
_SYNONYM_REVERSE: Dict[str, List[str]] = {}
for _canonical, _syns in SEMANTIC_SYNONYMS.items():
    for _syn in _syns:
        _s = _syn.lower()
        _SYNONYM_REVERSE.setdefault(_s, []).append(_canonical)


def get_synonyms(phrase: str) -> List[str]:
    """Return all known synonyms for *phrase* (canonical or reverse lookup)."""
    key = phrase.strip().lower()
    # Forward: phrase is a canonical skill
    results = list(SEMANTIC_SYNONYMS.get(key, []))
    # Reverse: phrase is a synonym of some canonical skill(s)
    for canonical in _SYNONYM_REVERSE.get(key, []):
        if canonical not in results:
            results.append(canonical)
    return results


def is_synonym_match(phrase_a: str, phrase_b: str) -> bool:
    """Return True if *phrase_a* and *phrase_b* are synonyms of each other."""
    a = phrase_a.strip().lower()
    b = phrase_b.strip().lower()
    if a == b:
        return True
    # a's synonyms contain b?
    if b in [s.lower() for s in get_synonyms(a)]:
        return True
    # b's synonyms contain a?
    if a in [s.lower() for s in get_synonyms(b)]:
        return True
    return False


# ---------------------------------------------------------------------------
# L4 — POS-based skill filtering (Phase 2)
# ---------------------------------------------------------------------------

# Lazy-loaded spaCy model.  We only import once and cache it.  If the model
# is not installed the feature degrades gracefully (no POS filtering).
_spacy_nlp = None  # type: ignore[assignment]
_SPACY_AVAILABLE: Optional[bool] = None


def _load_spacy():
    """Load spaCy en_core_web_sm lazily. Sets _SPACY_AVAILABLE flag."""
    global _spacy_nlp, _SPACY_AVAILABLE
    if _SPACY_AVAILABLE is not None:
        return
    try:
        import spacy
        _spacy_nlp = spacy.load("en_core_web_sm", disable=["parser", "ner"])
        _SPACY_AVAILABLE = True
        logger.info("spaCy en_core_web_sm loaded — POS filtering enabled.")
    except Exception:
        _SPACY_AVAILABLE = False
        logger.info("spaCy model not available — POS filtering disabled (graceful fallback).")


def filter_non_skill_phrases_pos(phrases: List[str]) -> List[str]:
    """Keep only noun-bearing phrases using spaCy POS tagging.

    If spaCy is unavailable, returns the input unchanged (graceful fallback).
    """
    _load_spacy()
    if not _SPACY_AVAILABLE or _spacy_nlp is None:
        return list(phrases)

    filtered: List[str] = []
    for phrase in phrases:
        # Always keep whitelisted phrases (acronyms, C++, etc.)
        if is_whitelisted(phrase):
            filtered.append(phrase)
            continue
        doc = _spacy_nlp(phrase)
        # Keep if at least one token is a noun or proper noun
        has_noun = any(t.pos_ in ("NOUN", "PROPN") for t in doc)
        # Reject if ALL tokens are non-noun (verb, adverb, adjective, etc.)
        all_non_noun = all(t.pos_ in ("VERB", "ADV", "ADJ", "ADP", "DET", "AUX", "SCONJ", "CCONJ") for t in doc)
        if has_noun and not all_non_noun:
            filtered.append(phrase)
    return filtered


# ---------------------------------------------------------------------------
# L12 — Dynamic top_n tuning (Phase 2)
# ---------------------------------------------------------------------------

def compute_optimal_top_n(text: str, requested_top_n: int = 30) -> int:
    """Scale KeyBERT top_n with text complexity (unique-word proxy).

    Short texts get fewer extractions to avoid noise padding;
    rich texts get more to capture diverse skills.
    """
    if not text or not text.strip():
        return 3
    words = text.split()
    word_count = len(words)
    unique_words = len(set(w.lower() for w in words))
    if unique_words < 20:
        optimal = 5
    elif unique_words < 50:
        optimal = 15
    else:
        optimal = min(30, unique_words // 3)
    # Never exceed explicit request, floor at 3
    return max(3, min(optimal, requested_top_n, max(3, word_count // 4)))


def compute_phrase_specificity_weight(phrase: str) -> float:
    """Weight JD phrases by specificity so longer phrases matter slightly more."""
    word_count = len(normalize_phrase(phrase).split())
    if word_count <= 1:
        return 1.0
    if word_count == 2:
        return 1.25
    return 1.5


def compute_weighted_skill_score(
    jd_entries: Iterable[Dict],
    matched_pairs: Iterable[Dict],
    total_resume_phrases: int,
) -> Dict:
    """Compute the root-PDF weighted skill score and compatibility metrics."""
    jd_list: List[Dict] = []
    for entry in jd_entries or []:
        phrase = normalize_skill(normalize_phrase(entry.get("phrase", "")))
        bucket = str(entry.get("bucket", "required")).strip().lower() or "required"
        if not phrase or bucket not in JD_BUCKET_WEIGHTS:
            continue
        jd_list.append({"phrase": phrase, "bucket": bucket})

    matched_list = list(matched_pairs or [])
    total_weight = round(sum(JD_BUCKET_WEIGHTS[item["bucket"]] for item in jd_list), 6)
    matched_weight = 0.0
    matched_resume_phrases = 0
    confidences: List[float] = []
    seen_resume = set()
    match_type_counts: Counter = Counter()
    bucket_counts: Counter = Counter(item["bucket"] for item in jd_list)

    unmatched_by_phrase: Counter = Counter((item["phrase"], item["bucket"]) for item in jd_list)
    for pair in matched_list:
        jd_phrase = normalize_skill(normalize_phrase(pair.get("jd_phrase", "")))
        bucket = str(pair.get("jd_bucket", "required")).strip().lower() or "required"
        key = (jd_phrase, bucket)
        if not jd_phrase or key not in unmatched_by_phrase or unmatched_by_phrase[key] <= 0:
            continue
        unmatched_by_phrase[key] -= 1
        matched_weight += JD_BUCKET_WEIGHTS.get(bucket, 1.0)
        confidences.append(max(0.0, min(float(pair.get("similarity", 0.0) or 0.0), 1.0)))
        match_type = str(pair.get("match_type", "semantic")).strip().lower() or "semantic"
        match_type_counts[match_type] += 1
        resume_phrase = normalize_skill(normalize_phrase(pair.get("resume_phrase", "")))
        if resume_phrase and resume_phrase not in seen_resume:
            seen_resume.add(resume_phrase)
            matched_resume_phrases += 1

    avg_match_confidence = sum(confidences) / len(confidences) if confidences else 0.0
    resume_pool_coverage = (
        matched_resume_phrases / float(total_resume_phrases)
        if total_resume_phrases > 0 else 0.0
    )
    weighted_jd_coverage = (matched_weight / total_weight) if total_weight else 0.0
    score = (
        SKILL_SCORE_COMPONENT_WEIGHTS["weighted_jd_coverage"] * weighted_jd_coverage +
        SKILL_SCORE_COMPONENT_WEIGHTS["avg_match_confidence"] * avg_match_confidence +
        SKILL_SCORE_COMPONENT_WEIGHTS["resume_pool_coverage"] * resume_pool_coverage
    )

    return {
        "score": round(min(max(score, 0.0), 1.0), 4),
        "matched_count": len(matched_list),
        "total_jd_count": len(jd_list),
        "weighted_jd_total": round(total_weight, 4),
        "weighted_match_total": round(matched_weight, 4),
        "weighted_jd_coverage": round(weighted_jd_coverage, 4),
        "avg_match_confidence": round(avg_match_confidence, 4),
        "matched_resume_phrases": matched_resume_phrases,
        "total_resume_phrases": int(total_resume_phrases),
        "resume_pool_coverage": round(resume_pool_coverage, 4),
        "jd_bucket_counts": {bucket: int(bucket_counts.get(bucket, 0)) for bucket in JD_BUCKET_WEIGHTS},
        "match_type_counts": dict(match_type_counts),
        "score_component_weights": dict(SKILL_SCORE_COMPONENT_WEIGHTS),
    }


def compute_hybrid_skill_score(
    jd_phrases: Iterable[str],
    matched_pairs: Iterable[Dict],
    total_resume_phrases: int,
) -> Dict[str, float]:
    """Compute the authoritative hybrid skills score."""
    jd_list = [normalize_skill(normalize_phrase(item)) for item in jd_phrases or [] if normalize_phrase(item)]
    matched_list = list(matched_pairs or [])

    total_jd_weight = round(sum(compute_phrase_specificity_weight(item) for item in jd_list), 6)
    matched_jd_weight = 0.0
    matched_resume_phrases = 0
    confidences: List[float] = []
    seen_jd: Counter = Counter()
    seen_resume = set()

    for pair in matched_list:
        jd_phrase = normalize_skill(normalize_phrase(pair.get("jd_phrase", "")))
        resume_phrase = normalize_skill(normalize_phrase(pair.get("resume_phrase", "")))
        if not jd_phrase or jd_phrase not in jd_list:
            continue
        if seen_jd[jd_phrase] >= jd_list.count(jd_phrase):
            continue
        seen_jd[jd_phrase] += 1
        matched_jd_weight += compute_phrase_specificity_weight(jd_phrase)
        confidence = float(pair.get("similarity", 0.0) or 0.0)
        confidences.append(max(0.0, min(confidence, 1.0)))
        if resume_phrase and resume_phrase not in seen_resume:
            seen_resume.add(resume_phrase)
            matched_resume_phrases += 1

    weighted_jd_coverage = matched_jd_weight / total_jd_weight if total_jd_weight else 0.0
    avg_match_confidence = sum(confidences) / len(confidences) if confidences else 0.0
    resume_pool_coverage = (
        matched_resume_phrases / float(total_resume_phrases)
        if total_resume_phrases > 0 else 0.0
    )
    score = (
        SKILL_SCORE_COMPONENT_WEIGHTS["weighted_jd_coverage"] * weighted_jd_coverage +
        SKILL_SCORE_COMPONENT_WEIGHTS["avg_match_confidence"] * avg_match_confidence +
        SKILL_SCORE_COMPONENT_WEIGHTS["resume_pool_coverage"] * resume_pool_coverage
    )

    return {
        "score": round(min(score, 1.0), 4),
        "matched_count": len(matched_list),
        "total_jd_count": len(jd_list),
        "weighted_jd_total": round(total_jd_weight, 4),
        "weighted_match_total": round(matched_jd_weight, 4),
        "weighted_jd_coverage": round(weighted_jd_coverage, 4),
        "avg_match_confidence": round(avg_match_confidence, 4),
        "matched_resume_phrases": matched_resume_phrases,
        "total_resume_phrases": int(total_resume_phrases),
        "resume_pool_coverage": round(resume_pool_coverage, 4),
    }


__all__ = [
    # Phase 1 — L6/L7/L9/L10
    "NON_SKILL_WORDS",
    "JD_NOISE_PHRASES",
    "SKILL_NOISE_SUBSTRINGS",
    "SOFT_SKILL_BLACKLIST",
    "WHITELIST_PATTERNS",
    "SKILL_ALIAS_MAP",
    "JD_BUCKET_WEIGHTS",
    "MATCH_SCORE_BY_TYPE",
    "SKILL_SCORE_COMPONENT_WEIGHTS",
    "SKILL_CLUSTERS",
    "is_whitelisted",
    "is_valid_skill",
    "has_technical_signal",
    "normalize_skill",
    "normalize_text_for_skills",
    "normalize_text",
    "normalize_phrase",
    "clean_skills",
    "compute_token_overlap_ratio",
    "skills_share_cluster",
    "clamp_match_score",
    "split_skill_line",
    "deduplicate_phrases",
    "merge_unique_skills",
    "RESUME_SECTION_ALIASES",
    "SECTION_PATTERNS",
    "split_resume_into_sections",
    "extract_skills_from_section",
    "extract_skills_from_full_text",
    "clean_jd_skill_phrases",
    "DebugLog",
    # Phase 2 — L1/L3/L4/L12
    "DEFAULT_MATCH_THRESHOLD",
    "compute_adaptive_threshold",
    "SEMANTIC_SYNONYMS",
    "get_synonyms",
    "is_synonym_match",
    "filter_non_skill_phrases_pos",
    "compute_optimal_top_n",
    "compute_phrase_specificity_weight",
    "compute_weighted_skill_score",
    "compute_hybrid_skill_score",
    "is_valid_jd_skill",
]