Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2025-10-31 - Memoizing Lexicon Regex Compilations
**Learning:** In the openmed NLP pipelines (e.g., `openmed.clinical.context`), deterministic regex compilations (`_compiled_context_lexicon`) and lexicon generation can cause severe performance bottlenecks during repeated evaluations across text spans.
**Action:** Always memoize deterministic lexicon and regex compilations (using `@functools.lru_cache`) when processing spans or lexicons dynamically in text pipelines.
2 changes: 2 additions & 0 deletions openmed/openmed/clinical/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

from __future__ import annotations

import functools
import re
from collections.abc import Iterable, Iterator, Mapping, Sequence
from dataclasses import dataclass, replace
Expand Down Expand Up @@ -154,6 +155,7 @@ class _CompiledContextLexicon:
backward_context_cues: frozenset[str]


@functools.lru_cache(maxsize=16)
def _compiled_context_lexicon(language: str | None = None) -> _CompiledContextLexicon:
lexicon = get_clinical_cue_lexicon(language)
token_boundaries = lexicon.token_boundaries
Expand Down
2 changes: 2 additions & 0 deletions openmed/openmed/clinical/experiencer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from __future__ import annotations

import functools
import re
from collections.abc import Mapping
from dataclasses import dataclass, replace
Expand Down Expand Up @@ -106,6 +107,7 @@
_CLAUSE_BOUNDARY_RE = re.compile(r"[.!?;]")


@functools.lru_cache(maxsize=512)
def _cue_pattern(cues: tuple[str, ...]) -> re.Pattern[str]:
alternation = "|".join(
r"\s+".join(re.escape(part) for part in cue.split())
Expand Down