Skip to content

Retain uppercase acronyms through lexical front-loading - #36

Merged
wshlavacek merged 3 commits into
mainfrom
fix/lexical-acronym-frontload
Dec 1, 2026
Merged

wshlavacek merged 3 commits into
mainfrom
fix/lexical-acronym-frontload

Conversation

@wshlavacek

@wshlavacek wshlavacek commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #35 / #9. Stage-2 lexical front-loading now searches 3–4 letter all-caps acronyms (HIV, EGFR, ALL). Before, they were dropped after query_terms() lowercased them. This PR adds them without letting them flood the results.

Term extraction (src/litkit/retrieval/helpers.py)

  • _extract_query_terms() returns (term, is_acronym) pairs, and query_terms() wraps it. An acronym is a 3–4 character all-caps token; a lowercase plural s is dropped (TLRs → tlr).
  • In all-caps prose (WHAT IS THE ROLE OF RNA …, B CELL LYMPHOMA), ordinary words aren't treated as acronyms. Prose is detected from stopwords or long, vowel-rich words, so keyword queries (EGFR NSCLC, HIV CRISPR) keep their acronyms.
  • Tokenizing applies only the dash and subscript replacements that SQLite also applies (no NFKC until Keyword search misses text with ligatures, fullwidth digits or superscripts #20). Em dashes and spaced en dashes split words; Michaelis–Menten still joins.
  • sqlite_norm_expr() and normalize_for_search_py() now share one replacement table, _SQLITE_NORM_REPLACEMENTS.
  • _STOPWORDS gains a few function words (can, did, not, …) but deliberately not all.

Rare-term selection and matching (src/litkit/retrieval/lexical.py)

  • find_rare_terms() orders terms by tier: digit and hyphen terms first, then the long-term fallback (which acronyms no longer switch off), then acronyms. Acronyms therefore never push digit terms past max_terms.
  • Short alphabetic terms must match on ASCII word boundaries, through a litkit_word_match SQLite function, so rna no longer matches "journal" or "internal". Plural, inhibitor and numbered forms still match (RNAs, RNAi, ACEi, TLR4, JAK2/STAT3). The function takes the raw column, so the normalization chain runs once per row.
  • Removed unused imports and constants, collapsed the duplicate require_rare_terms branches, and updated the docstrings.

Known limitation: a 3–4 letter word written in caps for emphasis in a mixed-case question ("What is the ROLE of HIV?") can't be told apart from an acronym, so it is searched (word-bounded). A test documents this.

Verification

  • pytest tests/test_retrieval.py tests/test_formatting.py tests/test_db.py tests/test_index.py tests/test_segments.py tests/test_contributors.py: 133 passed, 8 expected xfails (Consumer picks up unfinished *_pending.npz segments #4, Keyword search misses text with ligatures, fullwidth digits or superscripts #20). This ran on Python 3.12 with numpy 1.26 and faiss-cpu 1.8 on Intel macOS, not the full uv sync environment.
  • The new assertions fail against the previous commit (fc4204c) and pass now.
  • ruff check and black --check are clean on tests/test_retrieval.py, and there are no new ruff or black findings in the two source files.
  • On a synthetic 40k-chunk SQLite table, the word-boundary filter went from 2.66 s to 1.98 s with the raw-column UDF, with the same hits.

🤖 Generated with Claude Code

Carry uppercase acronym detection from query extraction into find_rare_terms() so Stage-2 lexical front-loading actually searches acronym-only queries (such as HIV and EGFR). Normalize Unicode compatibility forms, hyphen variants, and subscript digits before tokenizing, ignore punctuation-only tokens, update docstrings, and remove unused code in lexical.py.

Signed-off-by: Bill Hlavacek <bill.hlavacek@gmail.com>

@wshlavacek wshlavacek left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: changes requested before merge

The refactor itself is clean (shared _normalize_unicode(), dead imports gone, the require_rare_terms branches collapsed), and the end-to-end Stage-2 test is a good addition. But making acronyms "rare", and normalizing before tokenizing, changes which terms Stage 2 searches in ways that make retrieval worse for many common questions. Six of the inline comments are blocking. The last three are nits.

Blocking

  1. Short acronyms are matched as substrings: rna matches "journal", car matches "carcinoma", hiv matches "archived" (lexical.py:129).
  2. A single acronym now disables the long-term fallback (lexical.py:135).
  3. Any all-caps word, of any length, is treated as an acronym (helpers.py:56). This contradicts the CHANGELOG's "3–4 character".
  4. Em and en dashes are mapped to - before tokenizing, which joins neighbouring words (helpers.py:29).
  5. Acronyms can push digit and hyphen terms past the max_terms cap (lexical.py:138).
  6. NFKC before tokenizing creates terms the SQL side can't match (#20) (helpers.py:50).

Nits: the dash table is duplicated in sqlite_norm_expr, the require_rare_terms docstring is wrong, and the %/\ check can never be true.

How I checked: I ran each example question through find_rare_terms() on both main and this branch, and checked the LIKE patterns in SQLite. I did not run the full pytest suite.

Suggested tests to add: an all-caps question, a question with an em dash, an acronym plus a long term (the fallback should survive), and a negative lexical case (a chunk that says "journal" for an RNA query should not be front-loaded).

Comment thread src/litkit/retrieval/lexical.py Resolved
Comment thread src/litkit/retrieval/lexical.py Resolved
Comment thread src/litkit/retrieval/helpers.py Resolved
Comment thread src/litkit/retrieval/helpers.py Resolved
Comment thread src/litkit/retrieval/lexical.py Resolved
Comment thread src/litkit/retrieval/helpers.py Resolved
Comment thread src/litkit/retrieval/helpers.py Resolved
Comment thread src/litkit/retrieval/lexical.py Resolved
Comment thread src/litkit/retrieval/lexical.py Resolved
… handling

Match 3-4 character alphabetic terms on ASCII word boundaries in SQLite, prioritize digit/hyphen terms and long-term fallback ahead of acronyms, cap acronyms at 3-4 characters while skipping all-caps prose, keep NFKC out of tokenization until #20, map em dashes and spaced en dashes to spaces, share the SQLite normalization replacement table, and expand tests.

Signed-off-by: Bill Hlavacek <bill.hlavacek@gmail.com>

@wshlavacek wshlavacek left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review of fc4204c: all 9 earlier comments are fixed; 3 new issues to fix before merge

Thanks for the thorough follow-up. I checked every earlier comment against the new code by re-running the original example questions, and all of them now behave correctly. I also ran the PR's own tests for query terms, rare terms, and normalization against fc4204c (all pass), and checked the shared replacement table by comparing Python and SQLite output.

Earlier comment Status
Short acronyms matched as substrings ✅ Fixed: word-boundary UDF (but see the new comment on lexical.py:43)
Acronym switched off the long-term fallback ✅ Fixed: "HIV integrase … methyltransferase" → ['integrase', 'methyltransferase', 'hiv']
All-caps words of any length counted as acronyms ✅ Fixed: 3–4 characters plus all-caps detection (but see the new comment on helpers.py:70)
Em dashes joined neighbouring words ✅ Fixed: "HIV—a retrovirus" → ['retrovirus', 'hiv']
Acronyms pushed digit terms past max_terms ✅ Fixed: il-6 now comes first
NFKC before tokenizing ✅ Fixed: tokenizing no longer uses NFKC
Duplicated dash table ✅ Fixed: _SQLITE_NORM_REPLACEMENTS
require_rare_terms docstring ✅ Fixed
Dead %/\ check ✅ Fixed

New issues (blocking)

  1. The all-caps detector switches off acronyms for keyword-style queries. "EGFR NSCLC", "KRAS NSCLC", "VEGFR HCC" and "HIV CRISPR" now produce no search terms at all (helpers.py:70).
  2. The word-boundary rule rejects numbered and plural forms. jak, stat, tlr and car find nothing in "JAK2/STAT3", "TLR4", "CARs"; hiv misses "HIV1" (lexical.py:43).
  3. Adding "all" to _STOPWORDS drops ALL (acute lymphoblastic leukemia) (helpers.py:19).

Nits: short all-caps words used for emphasis in a mixed-case question still count as acronyms (helpers.py:94); the norm expression runs twice per LIKE-matching row (lexical.py:219); and the PR description is out of date. It still describes the removed _normalize_unicode() with NFKC-before-tokenizing and lists re as a removed import, but re is back in lexical.py.

Not checked: the faiss-dependent tests, including the Stage-2 end-to-end test, and test_lexical_search_word_boundaries_for_short_acronyms under pytest. I don't have a faiss environment on this machine. I did run the word-boundary SQL directly against an in-memory SQLite database.

Comment thread src/litkit/retrieval/helpers.py Resolved
Comment thread src/litkit/retrieval/lexical.py Resolved
Comment thread src/litkit/retrieval/helpers.py Resolved
Comment thread src/litkit/retrieval/helpers.py Resolved
Comment thread src/litkit/retrieval/lexical.py Resolved
Address the second review round on #36:

- Decide all-caps prose from stopwords or long, vowel-rich words, so
  keyword queries such as "EGFR NSCLC" and "HIV CRISPR" keep their
  acronyms while "B CELL LYMPHOMA" is still treated as prose.
- Accept plural, inhibitor, and numbered suffixes on word-boundary
  matches (RNAs, RNAi, ACEi, TLR4, JAK2/STAT3), and search plural
  acronyms in questions by their stem (TLRs -> tlr).
- Remove "all" from the stopwords so ALL (acute lymphoblastic
  leukemia) is searched.
- Pass the raw column to litkit_word_match so SQLite evaluates the
  normalization chain once per row instead of twice.
- Document the mixed-case emphasis-caps limitation in tests and
  update the CHANGELOG entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Bill Hlavacek <bill.hlavacek@gmail.com>
@wshlavacek
wshlavacek merged commit bdcb7f8 into main Dec 1, 2026
4 checks passed
@wshlavacek
wshlavacek deleted the fix/lexical-acronym-frontload branch December 2, 2026 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant