@ -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 :