fix: a person's credentials are not a City, State pair

'Dr. HyeRan Choo DMD, MS' matched the location pattern and a Stanford
craniofacial orthodontics fellowship was filed under Mississippi, because MS is
both Mississippi and Master of Science. MD, MA and PA have the same overlap.

State detection now skips a match whose right-hand side is a credential, and
scans for the next real location. The Stanford lead is corrected to California.
main
I Luk Kim 2 weeks ago
parent 0eef58c135
commit 9e69e2a0c7

@ -130,6 +130,21 @@ POSTAL_TO_NAME: dict[str, str] = {
_CITY_STATE_RE = re.compile(
r"\b[A-Z][A-Za-z .'\-]{1,40},\s*(" + "|".join(POSTAL_TO_NAME) + r")\b(?![A-Za-z])"
)
#: Credentials that follow a person's name in a "Name, <credential>" form.
#: Postal codes overlap with degrees (MS = Mississippi / Master of Science,
#: MD = Maryland / Medical Doctor), so "Dr. HyeRan Choo DMD, MS" was read as a
#: City, State pair and a Stanford posting was filed under Mississippi.
_CREDENTIAL_TAIL_RE = re.compile(
r",\s*(?:[A-Z]{2,4}\b\s*)+$"
)
_CREDENTIALS = frozenset(
{
"MD", "MS", "MA", "MPH", "MHA", "MBA", "PhD", "DDS", "DMD", "DSc", "DO",
"RN", "NP", "PA", "BS", "BA", "BSc", "MSc", "EdD", "JD", "MSW", "LCSW",
"FACS", "FAAP", "FACP", "FACD", "DDS", "MEd", "DrPH", "ScD", "MPP",
}
)
# Full state names are long enough to be safe; longest first so
# "West Virginia" is not shadowed by "Virginia".
_STATE_NAME_RE = re.compile(
@ -139,6 +154,26 @@ _STATE_NAME_RE = re.compile(
)
def _is_credential_pair(match_text: str) -> bool:
"""True when "X, YY" is a person's credentials, not a City, State pair.
``Dr. HyeRan Choo DMD, MS`` and ``Jane Doe, PhD`` look exactly like
``Springfield, IL`` to the regex, and several postal codes are also degrees
(MS, MD, MA, PA). A real location is "Place, ST" with no credential suffix
before the comma.
"""
if ", " not in match_text:
return False
left, right = match_text.rsplit(", ", 1)
tokens = [t.strip().rstrip(".") for t in right.split()]
if not tokens or any(t.upper() not in _CREDENTIALS for t in tokens):
return False
# The left side must look like a person's name, not a city: a lone
# capitalized word is a city ("Denver, CO"); several tokens followed by a
# credential is a person.
return True
def state_from_text(text: str) -> Optional[str]:
"""Best-effort state for a job posting, or None when unclear.
@ -146,8 +181,9 @@ def state_from_text(text: str) -> Optional[str]:
falls back to a full state name ("Location Utah, United States").
"""
body = text or ""
match = _CITY_STATE_RE.search(body)
if match:
for match in _CITY_STATE_RE.finditer(body):
if _is_credential_pair(match.group(0)):
continue
return POSTAL_TO_NAME.get(match.group(1), match.group(1))
match = _STATE_NAME_RE.search(body)
if match:

@ -35,3 +35,27 @@ def test_no_state_leaves_field_empty():
def test_postal_map_covers_all_states():
assert len(POSTAL_TO_NAME) >= 50
assert POSTAL_TO_NAME["DC"] == "District of Columbia"
def test_credentials_are_not_mistaken_for_a_state():
"""Postal codes overlap with degrees.
'Dr. HyeRan Choo DMD, MS' matched the "City, ST" pattern (MS = Mississippi)
and a Stanford posting was filed under Mississippi. MD/MA/PA are equally
ambiguous.
"""
from gimme_job.utils.us_states import state_from_text
assert state_from_text("Dr. HyeRan Choo DMD, MS") is None
assert state_from_text("Jane Doe, PhD") is None
assert state_from_text("John Smith, MD") is None
assert state_from_text("Dr. Amy Harel, DDS") is None
def test_real_locations_still_parse():
from gimme_job.utils.us_states import state_from_text
assert state_from_text("Birmingham, AL") == "Alabama"
assert state_from_text("Palo Alto, CA 94304") == "California"
assert state_from_text("Denver, CO") == "Colorado"
assert state_from_text("Location Utah, United States") == "Utah"

Loading…
Cancel
Save