feat(parser): assign statuses to labelled kittens
This commit is contained in:
1 parent
6af34d4706
commit
f106d706e4
5 files changed
+48
-4
No files matched your search
+36
-4
@@ -146,6 +146,30 @@ def _natural_people(text: str) -> tuple[list[tuple[str, int]], list[tuple[str, i
|
||||
return escorts, kittens, people
|
||||
|
||||
|
||||
def _labelled_kitten_statuses(text: str) -> dict[int, str]:
|
||||
"""Read a status written beside an explicitly labelled ward.
|
||||
|
||||
A final standalone ``сам``/``несам`` still applies to the whole entry, but
|
||||
a status inside the same comma/semicolon-delimited phrase as ``ученик`` or
|
||||
``котёнок`` is stronger evidence for that particular participant.
|
||||
"""
|
||||
statuses: dict[int, str] = {}
|
||||
matches = list(_LABELED_PERSON_RE.finditer(text))
|
||||
for index, match in enumerate(matches):
|
||||
role = match.group("role").casefold()
|
||||
if not re.match(r"^(ученик|котенок|котёнок|бельчонок|подопечн)", role):
|
||||
continue
|
||||
next_start = matches[index + 1].start() if index + 1 < len(matches) else len(text)
|
||||
fragment = text[match.end():next_start]
|
||||
fragment = re.split(r"[,;\n]", fragment, maxsplit=1)[0]
|
||||
status = re.search(r"(?i)\b(самостоятельно|самостоятельный|несамостоятельно|несамостоятельный|сам|несам)\b", fragment)
|
||||
if status:
|
||||
statuses[int(match.group("id"))] = (
|
||||
"несамостоятельно" if status.group(1).casefold().startswith("несам") else "самостоятельно"
|
||||
)
|
||||
return statuses
|
||||
|
||||
|
||||
def _normalise_natural_text(text: str) -> str:
|
||||
"""Make common free-form date/time/ID variations safe for the parser."""
|
||||
text = unicodedata.normalize("NFKC", text).replace("\u00a0", " ")
|
||||
@@ -467,16 +491,24 @@ def parse_natural_bulk(text: str) -> tuple[dict[str, Any], list[str]] | None:
|
||||
fields["kitten_name"], fields["kitten_id"] = (kittens or fallback_kittens)[0]
|
||||
else:
|
||||
questions.append("Не вижу котёнка в формате Имя и ID.")
|
||||
participants = kittens or fallback_kittens
|
||||
labelled_statuses = _labelled_kitten_statuses(text)
|
||||
independence_match = re.search(r"(?i)\b(самостоятельно|самостоятельный|несамостоятельно|несамостоятельный|сам|несам)\b", text)
|
||||
if independence_match:
|
||||
primary_status = labelled_statuses.get(participants[0][1]) if participants else None
|
||||
if primary_status is None and independence_match:
|
||||
value = independence_match.group(1).lower()
|
||||
fields["kitten_independence"] = "несамостоятельно" if value.startswith("несам") else "самостоятельно"
|
||||
primary_status = "несамостоятельно" if value.startswith("несам") else "самостоятельно"
|
||||
if primary_status:
|
||||
fields["kitten_independence"] = primary_status
|
||||
else:
|
||||
questions.append("Котёнок самостоятельный или несамостоятельный?")
|
||||
extra_kittens = (kittens or fallback_kittens)[1:]
|
||||
extra_kittens = participants[1:]
|
||||
if extra_kittens:
|
||||
primary = fields.get("kitten_independence", "самостоятельно")
|
||||
fields["extra_kittens"] = [(name, kitten_id, primary) for name, kitten_id in extra_kittens]
|
||||
fields["extra_kittens"] = [
|
||||
(name, kitten_id, labelled_statuses.get(kitten_id, primary))
|
||||
for name, kitten_id in extra_kittens
|
||||
]
|
||||
if skill:
|
||||
try:
|
||||
_validate_independence(skill, fields.get("kitten_independence", "самостоятельно"), fields.get("extra_kittens", ()))
|
||||
|
||||
Reference in new issue
Block a user