172 lines
7.5 KiB
Python
172 lines
7.5 KiB
Python
"""A host's word to the app's vocabulary: seniority, employment and remote.
|
|
|
|
Each field is looked up the same way: the host's table in `hosts`, keyed by
|
|
the word lowercased, then a word that already is a value of the vocabulary
|
|
(what readers stored before they handed on the host's words, and what a
|
|
host with the app's own words sends), then, for remote, the shared word map.
|
|
An employment word may also match a host's `MATCH` fragments. A word none
|
|
of these knows is unmapped: it maps to '' and `lookup` says so,
|
|
so the prep stage can list it.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from collections.abc import Iterable, Mapping
|
|
from typing import Any
|
|
|
|
from app.models import EMPLOYMENTS, REMOTE, SENIORITIES, remote_of
|
|
from app.prep.hosts import TABLES
|
|
from app.seniority import from_title
|
|
|
|
#: `host -> field -> {word: value}`, plus `"match"` (`hosts.load`).
|
|
Tables = Mapping[str, Mapping[str, Any]]
|
|
|
|
VOCABULARIES: dict[str, tuple[str, ...]] = {
|
|
"seniority": SENIORITIES, "employment": EMPLOYMENTS, "remote": REMOTE}
|
|
|
|
#: How hosts without a table of their own write remote, after `_squash`:
|
|
#: case, spaces, `-` and `_` gone ("On-site", "on_site", "in office").
|
|
REMOTE_WORDS: dict[str, str] = {
|
|
"remote": "remote", "fullyremote": "remote", "remotefirst": "remote",
|
|
"hybrid": "hybrid",
|
|
"onsite": "onsite", "office": "onsite", "inoffice": "onsite", "inperson": "onsite",
|
|
"officebased": "onsite", "onpremise": "onsite",
|
|
}
|
|
|
|
|
|
def _match(rules: Mapping[str, Any], key: str) -> str | None:
|
|
"""The value of the first `MATCH` fragment `key` holds, by its mode."""
|
|
parts = set(re.split(r"[_-]", key)) if rules["mode"] == "token" else set()
|
|
for fragment, value in rules["rules"]:
|
|
if fragment in parts if rules["mode"] == "token" else fragment in key:
|
|
return str(value)
|
|
return None
|
|
|
|
|
|
#: Remote and hybrid in a line of Danish, Swedish or English that `remote_of`
|
|
#: does not read: "Nordhavn og mulighed for hjemmearbejde", "Arbete på distans".
|
|
#: Remote first, since "fuldt hjemmearbejde" is remote.
|
|
_REMOTE_LINE = re.compile(
|
|
r"\bremote\b|fjernarbejde|fuldt hjemmearbejde|distansarbete|på distans", re.IGNORECASE)
|
|
_HYBRID_LINE = re.compile(r"\bhybrid|hjemmearbejde", re.IGNORECASE)
|
|
|
|
|
|
#: "Remote" in a title that names a field or a product, not how the role is
|
|
#: worked: "Remote Sensing Specialist", "Remote Control Engineer".
|
|
_NOT_REMOTE_WORK = re.compile(r"\bremote[\s-]+(?:sensing|control\w*|monitoring)\b",
|
|
re.IGNORECASE)
|
|
|
|
|
|
#: Phrases in a title or text that say the role is worked remotely, and the
|
|
#: phrases that take one back. Both are read on the lowercased text with its
|
|
#: hyphens and runs of spaces squashed to one space. A match marks the role as
|
|
#: possibly remote (`remote_in_text`); it decides nothing by itself.
|
|
REMOTE_TEXT: tuple[str, ...] = (
|
|
"fully remote", "100% remote", "100 % remote", "remote first", "remote work",
|
|
"remote working", "work remotely", "working remotely", "remote position", "remote role",
|
|
"remote job", "remote team", "remote friendly", "remote possible", "remote option",
|
|
"remote ok", "work from home", "working from home", "work from anywhere", "home based",
|
|
"home office", "homeoffice", "hjemmearbejde", "fjernarbejde", "distansarbete",
|
|
)
|
|
REMOTE_TEXT_NOT: tuple[str, ...] = (
|
|
"no remote", "not remote", "not a remote", "non remote", "not be remote", "cannot be remote",
|
|
"no home office", "kein homeoffice", "not available remotely", "not open to remote",
|
|
"remote work is not", "remote working is not", "is not a remote", "remote sensing",
|
|
"remote control", "remote monitoring",
|
|
)
|
|
|
|
_REMOTE_TEXT = re.compile("|".join(rf"\b{re.escape(p)}\b" for p in REMOTE_TEXT))
|
|
_REMOTE_TEXT_NOT = re.compile("|".join(rf"\b{re.escape(p)}\b" for p in REMOTE_TEXT_NOT))
|
|
_SQUASHED = re.compile(r"[\s-]+")
|
|
|
|
|
|
def remote_in_text(title: str, text: str) -> bool:
|
|
"""Whether the title or the text says the role is worked remotely: a
|
|
phrase of `REMOTE_TEXT` left once those of `REMOTE_TEXT_NOT` are taken
|
|
out. Cheap evidence, no model: a role marked so passes a location gate
|
|
on remote until extract has read it (ADR Fetch prep and score 1.1), so a
|
|
false mark costs a cut slot at most, and a missed one nothing new."""
|
|
folded = _SQUASHED.sub(" ", f"{title or ''} {text or ''}".lower())
|
|
return bool(_REMOTE_TEXT.search(_REMOTE_TEXT_NOT.sub(" ", folded)))
|
|
|
|
|
|
def _line_word(text: str) -> str:
|
|
"""`remote_of`'s reading of a line, else its Danish and Swedish words."""
|
|
if found := remote_of(text):
|
|
return found
|
|
if _REMOTE_LINE.search(text):
|
|
return "remote"
|
|
return "hybrid" if _HYBRID_LINE.search(text) else ""
|
|
|
|
|
|
def _squash(word: str) -> str:
|
|
return "".join(c for c in word.lower() if c not in " -_")
|
|
|
|
|
|
def lookup(field: str, host: str, word: str,
|
|
tables: Tables = TABLES) -> str | None:
|
|
"""The value `word` maps to for `host`, '' for a known word that names
|
|
none, None for a word nothing knows. An empty word is ''."""
|
|
key = (word or "").strip().lower()
|
|
if not key:
|
|
return ""
|
|
table = tables.get(host, {})
|
|
found = table.get(field, {}).get(key)
|
|
if found is None and field == "employment" and "match" in table:
|
|
found = _match(table["match"], key)
|
|
if found is not None:
|
|
return found
|
|
if key in VOCABULARIES[field]:
|
|
return key
|
|
if field == "remote":
|
|
return REMOTE_WORDS.get(_squash(key))
|
|
return None
|
|
|
|
|
|
def seniority(host: str, word: str, title: str,
|
|
tables: Tables = TABLES) -> tuple[str, str]:
|
|
"""`(stated, from the title)`: the host's word through its table, and
|
|
`seniority.from_title`. `roles.seniority` is the model's level, else the
|
|
stated one, else the title's (`Store.write_prep`)."""
|
|
return lookup("seniority", host, word, tables) or "", from_title(title)
|
|
|
|
|
|
def employment(host: str, word: str,
|
|
tables: Tables = TABLES) -> str:
|
|
return lookup("employment", host, word, tables) or ""
|
|
|
|
|
|
def place_text(place: Any) -> str:
|
|
"""A place of `raw`'s `places`, a dict with `text` or a string, as its text."""
|
|
return str((place.get("text") if isinstance(place, Mapping) else place) or "")
|
|
|
|
|
|
def remote(host: str, value: Any, places: Iterable[Any] = (), line: str = "",
|
|
tables: Tables = TABLES, title: str = "") -> str:
|
|
"""How the role is worked, one of `REMOTE` or ''.
|
|
|
|
`value` is the host's own (a bool, or its word; `host_word`'s 'true' and
|
|
'false' are bools). True is remote. False says nothing more than the
|
|
places do: SmartRecruiters, Ashby, Recruitee and Workable send it for
|
|
every role that is not remote, hybrid ones included. A word goes
|
|
through the host's table, else the shared map. When the value says
|
|
nothing, the place line or the place texts are read for a word
|
|
(`remote_of`: "Hybrid, Berlin", and the Danish and Swedish words), then
|
|
`title` when given, which the stage gives for a search provider's role
|
|
only ("Backend Developer - Remote", but not "Remote Sensing Analyst"),
|
|
and last a place the reader marked remote is remote."""
|
|
if value is True or value == "true":
|
|
return "remote"
|
|
said = value not in (None, False, "false")
|
|
if said and (found := lookup("remote", host, str(value), tables)):
|
|
return found
|
|
places = list(places)
|
|
if found := _line_word(line or "; ".join(map(place_text, places))):
|
|
return found
|
|
if title and (found := _line_word(_NOT_REMOTE_WORK.sub(" ", title))):
|
|
return found
|
|
if any(isinstance(p, Mapping) and p.get("remote") is True for p in places):
|
|
return "remote"
|
|
return ""
|