updating nlp policies; more robust recognition beyond rules

This commit is contained in:
Roberto Treviño Cervantes
2026-11-13 11:52:36 -06:00
parent ce67de17bc
commit 8f84253205
9 changed files with 495 additions and 6 deletions
+28 -1
View File
@@ -8,7 +8,17 @@ import typing
from datetime import date, datetime
from extraction.nlp import FieldKind, FieldSpec
from extraction.nlp.vocabulary import CLINICAL_TERMS, FREE_TEXT_FIELDS, PATTERNS, labels_for
from extraction.nlp.vocabulary import (
ANCHORS,
CLINICAL_TERMS,
FREE_TEXT_FIELDS,
NEGATED_VALUES,
PATTERNS,
SEVERITY,
SUBJECTS,
UNITS,
labels_for,
)
from webapi.imports.schemas.registry import EntitySpec
# Surrogate keys, stored files and OCR bookkeeping are never read from a document.
@@ -42,11 +52,28 @@ def field_specs_for(entity: EntitySpec, *, key_pattern: str | None = None) -> li
scan_free_text=name in FREE_TEXT_FIELDS or (pattern is not None and pattern == key_pattern),
multiline=kind is FieldKind.TEXT and bool(_LONG_TEXT.search(name)),
required=info.is_required(),
anchors=ANCHORS.get(name, ()),
severity=SEVERITY.get(name, ()),
negated_value=NEGATED_VALUES.get(name),
unit=UNITS.get(name),
)
)
return specs
def subject_for(entity: EntitySpec) -> tuple[str, ...]:
"""What a document feeding this entity is about (`vocabulary.SUBJECTS`)."""
return SUBJECTS.get(entity.model.__table__.name, ())
def foreign_subjects_for(entity: EntitySpec) -> tuple[str, ...]:
"""What documents feeding other entities are about, less this one's own terms."""
own = set(subject_for(entity))
if not own:
return ()
return tuple(sorted({term for table, terms in SUBJECTS.items() if table != entity.model.__table__.name for term in terms} - own))
def key_field_for(entity: EntitySpec) -> str | None:
"""The field that identifies whose record a page belongs to."""
fields = entity.schema.model_fields