| 1 | """Company text that a job board repeats across its roles. |
| 2 | |
| 3 | Job descriptions open with the same company text on every role, so a sentence |
| 4 | that many distinct roles share says nothing about any one of them. Both the |
| 5 | Jobs Digest and Hiring Signals drop such sentences; the rule lives here so |
| 6 | neither module has to import the other for it. |
| 7 | """ |
| 8 | |
| 9 | from __future__ import annotations |
| 10 | |
| 11 | import re |
| 12 | from collections import Counter |
| 13 | from collections.abc import Iterable |
| 14 | |
| 15 | # A sentence found under this many distinct titles of a board is company or |
| 16 | # role-family text, not something one role says. Counting a title in a |
| 17 | # department once (however many cities or postings carry it) makes one fixed |
| 18 | # number work for a three-role board and a 2,900-role one; below three nothing |
| 19 | # is removed. |
| 20 | BOILERPLATE_MIN_ROLES = 3 |
| 21 | |
| 22 | _SENTENCE_END = re.compile(r"(?<=[.!?])\s+(?=[A-Z0-9\"“(])") |
| 23 | |
| 24 | |
| 25 | def key(text: str) -> str: |
| 26 | """Case- and whitespace-insensitive form used to compare text.""" |
| 27 | return " ".join(str(text or "").lower().split()) |
| 28 | |
| 29 | |
| 30 | def sentences(text: str) -> list[str]: |
| 31 | """Split description text into sentences, one line at a time.""" |
| 32 | out: list[str] = [] |
| 33 | for line in str(text or "").splitlines(): |
| 34 | for sentence in _SENTENCE_END.split(line): |
| 35 | sentence = " ".join(sentence.split()) |
| 36 | if sentence: |
| 37 | out.append(sentence) |
| 38 | return out |
| 39 | |
| 40 | |
| 41 | def shared_sentence_counts(roles: Iterable[tuple[str, str, str]]) -> Counter[str]: |
| 42 | """How many distinct (title, department) groups carry each sentence. |
| 43 | |
| 44 | ``roles`` holds ``(title, department, description)`` for every posting on |
| 45 | the board; postings of one title in one department count once. |
| 46 | """ |
| 47 | by_group: dict[tuple[str, str], set[str]] = {} |
| 48 | for title, department, text in roles: |
| 49 | by_group.setdefault((key(title), key(department)), set()).update( |
| 50 | key(sentence) for sentence in sentences(text) |
| 51 | ) |
| 52 | shared: Counter[str] = Counter() |
| 53 | for group_sentences in by_group.values(): |
| 54 | shared.update(group_sentences) |
| 55 | return shared |
| 56 | |
| 57 | |
| 58 | def role_sentences(text: str, shared: Counter[str]) -> list[str]: |
| 59 | """The sentences of ``text`` that fewer than ``BOILERPLATE_MIN_ROLES`` roles share.""" |
| 60 | return [ |
| 61 | sentence for sentence in sentences(text) |
| 62 | if shared[key(sentence)] < BOILERPLATE_MIN_ROLES |
| 63 | ] |
| 64 |