Files
miem_workers/app/parser/education.py
T
codex 8dde82ab8a
CI / test (pull_request) Successful in 12m55s
CI / Fingerprint diagnostics (production deployment temporarily disabled) (pull_request) Skipped
Исправить подсчёт ДПО и индикацию загрузки справочника
2026-11-24 16:55:22 +00:00

81 lines
4.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Assemble additional education events from the public profile's mixed markup."""
import re
YEAR = re.compile(r"\b(?:19|20)\d{2}\b")
_QUOTE = re.compile(r'«([^»]+)»|"([^"]+)"')
_EVENT = re.compile(r"стажиров|стипенди|исследовательск|обучени|квалификаци|переподготовк|прош[её]л|прошла|семинар|сертификаци|программ[аеуы]|курсы?\b", re.I)
_CONTINUES = re.compile(r"(?:по программе|присвоением степени|на тему|по курсу|по теме|по специальности)[:\s]*$", re.I)
_CAPTION = re.compile(r"^(?:удостоверение|сертификат|свидетельство)(?: о повышении квалификации)?[.:]?$", re.I)
_DATED_ROW = re.compile(r'\s+(?=(?:19|20)\d{2}\s*г\.\s*["«])')
def education_title(title):
labels = {"дополнительное образование", "повышение квалификации", "курсы повышения квалификации"}
return any(part.strip().rstrip(":") in labels for part in str(title).casefold().split("/"))
def split_dated_rows(text):
return _DATED_ROW.split(text) if re.match(r"^(?:19|20)\d{2}\s*г\.", text) else [text]
def _key(text):
return " ".join(re.findall(r"\w+", text.casefold().replace("ё", "е")))
def _provider(text):
text = _key(text)
for variants in (("вшэ", "высшая школа экономики", "высшей школе экономики"),
("мифи", "московский инженерно физический институт")):
if any(v in text.split() if " " not in v else v in text for v in variants):
return variants[0]
return text
def assemble(rows):
events = []
unresolved = []
pending = []
for text, year, structured, list_item in rows:
if _CAPTION.fullmatch(text):
continue
if (pending and not structured and not list_item and not YEAR.match(text)
and (_CONTINUES.search(pending[-1][0]) or
(pending[-1][3] and not _EVENT.search(text[:20]) and not YEAR.search(text)) or
(pending[-1][3] and text.startswith("Курс ") and not YEAR.search(text)))):
pending[-1][0] += " " + text
pending[-1][3] = False
else:
pending.append([text, year, structured, list_item])
for text, year, structured, _ in pending:
certificate = re.match(r"^сертификат\s+[«\"]", text, re.I)
if not structured and (len(_key(text).split()) < 2 or not (_EVENT.search(text) or YEAR.match(text) or certificate)):
unresolved.append(text)
continue
quotes = [a or b for a, b in _QUOTE.findall(text)]
title = _key(quotes[-1]) if quotes else ""
if re.match(r'^(?:19|20)\d{2}\s*г\.\s*["«]', text) and quotes:
title = _key(quotes[0])
if structured and text.lstrip("0123456789 ").startswith("«"):
title = _key(text.split("«", 1)[1].rsplit("»", 1)[0])
provider = _provider(text.rsplit("»", 1)[-1] if structured and "»" in text else text)
if not year:
match = YEAR.search(_QUOTE.sub("", text))
year = int(match[0]) if match else None
matches = [e for e in events if title and e["title"] == title and e["provider"] == provider
and (e["year"] == year or not year or not e["year"])]
if len(matches) == 1:
target = matches[0]
hours = re.search(r"\b\d+\s+час[а-я]*", text, re.I)
if hours and hours[0] not in target["text"]:
target["text"] += " (" + hours[0] + ")"
continue
if any(e["text"] == text for e in events):
continue
if title and any(e["title"] == title and e["year"] == year for e in events):
unresolved.append("Возможный повтор: " + text)
events.append(dict(text=text, year=year, structured=structured, title=title, provider=provider))
return events, unresolved