Repository navigation
Retain uppercase acronyms through lexical front-loading - #36
Conversation
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
left a comment
There was a problem hiding this comment.
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
- Short acronyms are matched as substrings:
rnamatches "journal",carmatches "carcinoma",hivmatches "archived" (lexical.py:129). - A single acronym now disables the long-term fallback (
lexical.py:135). - Any all-caps word, of any length, is treated as an acronym (
helpers.py:56). This contradicts the CHANGELOG's "3–4 character". - Em and en dashes are mapped to
-before tokenizing, which joins neighbouring words (helpers.py:29). - Acronyms can push digit and hyphen terms past the
max_termscap (lexical.py:138). - 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).
… 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
left a comment
There was a problem hiding this comment.
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)
- 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). - The word-boundary rule rejects numbered and plural forms.
jak,stat,tlrandcarfind nothing in "JAK2/STAT3", "TLR4", "CARs";hivmisses "HIV1" (lexical.py:43). - Adding
"all"to_STOPWORDSdrops 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.
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>
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 afterquery_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, andquery_terms()wraps it. An acronym is a 3–4 character all-caps token; a lowercase pluralsis dropped (TLRs→tlr).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.Michaelis–Mentenstill joins.sqlite_norm_expr()andnormalize_for_search_py()now share one replacement table,_SQLITE_NORM_REPLACEMENTS._STOPWORDSgains a few function words (can,did,not, …) but deliberately notall.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 pastmax_terms.litkit_word_matchSQLite function, sornano 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.require_rare_termsbranches, 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.npzsegments #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 fulluv syncenvironment.ruff checkandblack --checkare clean ontests/test_retrieval.py, and there are no new ruff or black findings in the two source files.🤖 Generated with Claude Code