"""`from_title`: the level a job title names, as one of `SENIORITIES`, or `''`. What fills `roles.seniority` at ingest, before `extract` has read the posting (which overwrites it). `''` and not `"mid"` when the title does not say: most titles name no level, and a filter that treats "does not say" as a level loses every role it could not read. So every rule here is written for precision, and a title that matches nothing stays unknown. The approach is `plugins/ats.classify`'s, after its substring matches went wrong (" it" inside "Italy"): the title is folded with `title_key` (case, accents and the `(m/w/d)` marker gone, punctuation to spaces) and every pattern is a word-bounded regex against that. Patterns are therefore written folded: `direktör` is `direktor`, `chef d'équipe` is `chef d equipe`; `ø` and `æ` stay as they are, since NFKD does not decompose them. The levels are the ladder of `app.models.LADDER`: from `staff` up, a rung has an individual-contributor level and a people-lead level. A title is matched twice, for the highest people level it names (`_PEOPLE`: a head, a manager, a team lead) and for the highest individual grade (`_GRADES`: Principal, Staff, Senior). A people level wins over a grade on its own rung or below: "Staff Engineering Manager" is a manager, "Senior Director" a director. A grade above the people level's rung lifts it one rung, and one only: "Principal Engineering Manager" is a senior manager. Without a people level the grade decides: "Principal Product Manager" is principal, since a product manager is an individual contributor. A slash names two levels (`_OR`): "Senior/Staff Engineer" is staff, not a Senior Staff. The one exception is `intern`, tried first: a title that says internship or thesis is one whatever it names after ("Praktikum ... Assistenz des Senior Vice Presidents"). A C-level or head named as the boss of the person hired ("Executive Assistant to the CEO", "Office of the Head of ...") is cut out before matching (`_NOISE`). Rules that decide the doubtful cases: - **exec** is C-level ("Chief ... Officer", CTO), "VP" and above (a bank's VP is mid-level, but a tech company's is not, and the filter is the reader's: exec), "Managing Director", Geschäftsführer, Vorstand and VD. - **director** is the word in English, Danish, Swedish, German, French, Spanish and Italian, "Head of" and a title's own first word "Head", and the German "Leiter" alone or of a whole department (Bereichsleiter, Abteilungsleiter). "Senior Director" and "Executive Director" are directors. - **senior_manager** is "Senior Manager", a senior people manager ("Sr. Engineering Manager"), "Associate Director", "Assistant Director" and a bank's "Assistant Vice President" (AVP, Associate VP). - **manager** is a level only where the word means someone who runs a team: a bare "Manager", "Manager, Data Science" (the word first, as a level), and the compounds whose department or place is what is managed (`_PEOPLE_MANAGER`: engineering, team, store, general ...), and a "Tech Lead Manager". Every other "X Manager" is an individual contributor by function (Account, Product, Project, Customer Success, Office, Marketing, Sales ...) and says nothing, so "Senior Product Manager" is senior. An unlisted compound stays unknown, which costs a recall the filter's "and no level" makes up. **manager** is also the head of a shop, site or office: "Store Leader", the Danish, Swedish, German and Dutch -chef, -leder, Filialleiter, Kontorchef, French "responsable" and "gérant", Spanish "jefe" and "gerente". - **lead** is a team lead: "Team Lead", "Team Leader", the floor-sized leads (`_TEAM_SIZED_LEAD`: shift, crew, kitchen ...), a "People Leader", "Supervisor", an "Assistant Store Manager" or "Associate Store Leader", and the team-sized German, Danish, Dutch, Swedish and French forms (Teamleiter, Teamleider, teamledare, chef d'équipe). - **distinguished** is "Senior Principal", "Distinguished", "Technical Fellow", "Engineering Fellow" and SAP's "Chief Expert". "Fellow" alone is academic ("Research Fellow", "Postdoctoral Fellow") and says nothing. - **principal** is "Principal" and "Senior Staff". - **staff** is "Staff", and "Lead" or "Leader" as a noun at either end that is not a team lead ("Tech Lead", "Lead Engineer", "Marketing Lead", "Sales Leader"; not "Lead Generation"). "Staff Nurse", "Staff Accountant" and their kind are left out. - **project managers stay unknown** in every language (Projektleiter, Bauleiter, chef de projet, projektleder, prosjektleder): they are Project Managers. Halbleiter and the other -leiter words for things are cut out too. - **intern** is anything that says the person is still studying or training: intern, Werkstudent, Praktikant, stagiaire, Ausbildung, apprentice, elev, studentermedhjælper, a thesis. **junior** is "Junior", "Graduate", "Trainee" and "entry level". **mid** only when the title says so: "Mid-level", "Intermediate", "II", "Medior". Known wrong, and left so: "Senior Care Assistant" and other "senior" that means elderly; "Level II" outside a role level; "Principal Investigator" and a "Staff Scientist" at a lab as principal and staff; "Staff" on a title with an unlisted staff noun; a retail "Lead Store Advisor" and a "Sales Leader" who runs a team as staff; "Distinguished Engineer, Team Lead" as a senior manager, one rung above the lead; "Field CTO" taken as unknown; "Chief of Staff" as unknown; "Assistant Manager" as unknown; French "responsable" is taken as a manager wherever it stands, and "Directeur" of a small firm as a director; "VP" as exec at a bank. """ from __future__ import annotations import re from app.models import LADDER, title_key #: A C-level title or abbreviation, for `_NOISE`. _C_LEVEL = (r"(?:ceo|cto|cfo|coo|cio|ciso|cpo|cdo|chro|caio" r"|chief(?: \w+){0,3} officer|managing director|(? tuple[tuple[str, re.Pattern[str]], ...]: """Each level's patterns as one alternation, in the same order.""" return tuple((level, re.compile("|".join(f"(?:{p})" for p in patterns))) for level, patterns in rules) _INTERN_RE = re.compile("|".join(f"(?:{p})" for p in _INTERN)) _PEOPLE_RE = _compile(_PEOPLE) _GRADES_RE = _compile(_GRADES) #: The people level one rung above each people level, for a grade that lifts it. _LIFT: dict[str, str] = { level: next((up for up, (r, t) in LADDER.items() if t == "people" and r == rung + 1), level) for level, (rung, track) in LADDER.items() if track == "people"} #: A slash between two words, which `title_key` would fold to a space: #: "Senior/Principal" names two grades, not a Senior Principal. The gender #: marker's single letters (m/w/d) are left to `title_key`. _OR = re.compile(r"(?<=\w\w|\w\.)\s*/\s*(?=\w\w)") def _first(rules: tuple[tuple[str, re.Pattern[str]], ...], folded: str) -> str: return next((level for level, pattern in rules if pattern.search(folded)), "") def from_title(title: str) -> str: """The level `title` names, one of `SENIORITIES`, or `''` when it names none. See the module docstring for what counts.""" folded = " ".join(_NOISE.sub(" ", title_key(_OR.sub(" or ", title or ""))).split()) if not folded: return "" if _INTERN_RE.search(folded): return "intern" people, grade = _first(_PEOPLE_RE, folded), _first(_GRADES_RE, folded) if not people: return grade if grade and LADDER[grade][0] > LADDER[people][0]: return _LIFT[people] return people