-
Notifications
You must be signed in to change notification settings - Fork 0
feat(medications): smarter catalog search for typos and brand names #1785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
74c3ea7
feat(medications): smarter catalog search for typos and brand names
cursoragent adb4375
docs(ledger): record medications catalog search review
cursoragent aced65e
Merge origin/main into cursor/smarter-meds-search-9c1b
cursoragent cfb5dc8
docs(ledger): supersede medications search review after main sync
cursoragent 938a9ca
Merge branch 'main' into cursor/smarter-meds-search-9c1b
BigSimmo abb827b
fix(medications): clear catalog-search review blockers on PR tip
cursoragent 940653f
Merge origin/main into cursor/smarter-meds-search-9c1b
cursoragent a4f5750
Merge origin/main into cursor/smarter-meds-search-9c1b
cursoragent 88dbdd8
Merge origin/main into cursor/smarter-meds-search-9c1b
cursoragent ce06b2b
Merge origin/main into cursor/smarter-meds-search-9c1b
cursoragent 7a0f2ee
Merge origin/main (incl. #1798) into cursor/smarter-meds-search-9c1b
cursoragent 38b3bd0
Merge origin/main after #1797 into cursor/smarter-meds-search-9c1b
cursoragent 5cb0e11
Merge origin/main into cursor/smarter-meds-search-9c1b
cursoragent 31b54d0
fix(test): re-scroll service detail to live endpoint in Production UI
cursoragent c812f5c
Merge origin/main after #1789 into cursor/smarter-meds-search-9c1b
cursoragent 2d93557
Merge origin/main after #1792 into cursor/smarter-meds-search-9c1b
cursoragent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,266 @@ | ||
| // Medications-catalog query understanding: typo correction, brand↔generic | ||
| // expansion, and conservative near-miss matching for /api/medications and the | ||
| // medications universal-search domain. Kept separate from clinical-search / | ||
| // RAG so catalog recall can grow without changing retrieval analysis. | ||
|
|
||
| import { medicationAliasesForEntity, medicationEntitiesInText } from "@/lib/medication-entities"; | ||
| import { | ||
| medicationBrandNames, | ||
| normalizeSearchText, | ||
| rankMedicationRecords, | ||
| type MedicationRecord, | ||
| type MedicationSearchMatch, | ||
| } from "@/lib/medications"; | ||
|
|
||
| export type MedicationCatalogCorrection = { | ||
| from: string; | ||
| to: string; | ||
| }; | ||
|
|
||
| export type MedicationCatalogQueryAnalysis = { | ||
| originalQuery: string; | ||
| correctedQuery: string; | ||
| corrections: MedicationCatalogCorrection[]; | ||
| expansions: string[]; | ||
| }; | ||
|
|
||
| // Curated psychotropic / common prescribing typos. Local to the catalog path so | ||
| // expanding this map cannot change RAG query analysis in clinical-search. | ||
| const medicationTypoCorrections = new Map<string, string>([ | ||
| ["sertaline", "sertraline"], | ||
| ["sertralne", "sertraline"], | ||
| ["sertralin", "sertraline"], | ||
| ["olanzpine", "olanzapine"], | ||
| ["olanzapin", "olanzapine"], | ||
| ["quietapine", "quetiapine"], | ||
| ["quetiapin", "quetiapine"], | ||
| ["quetiapne", "quetiapine"], | ||
| ["risperdone", "risperidone"], | ||
| ["risperidon", "risperidone"], | ||
| ["aripiprazol", "aripiprazole"], | ||
| ["aripiprazloe", "aripiprazole"], | ||
| ["clozapin", "clozapine"], | ||
| ["clozapene", "clozapine"], | ||
| ["clozapinw", "clozapine"], | ||
| ["mirtazpine", "mirtazapine"], | ||
| ["mirtazapin", "mirtazapine"], | ||
| ["venlafaxne", "venlafaxine"], | ||
| ["duloxetin", "duloxetine"], | ||
| ["escitaloprame", "escitalopram"], | ||
| ["fluoxetin", "fluoxetine"], | ||
| ["paroxetin", "paroxetine"], | ||
| ["haloperidl", "haloperidol"], | ||
| ["lorazapam", "lorazepam"], | ||
| ["diazapam", "diazepam"], | ||
| ["valporate", "valproate"], | ||
| ["valproat", "valproate"], | ||
| ["lithum", "lithium"], | ||
| ["acamprosat", "acamprosate"], | ||
| ["camprl", "campral"], | ||
| ["zolof", "zoloft"], | ||
| ["zyprex", "zyprexa"], | ||
| ["seroqel", "seroquel"], | ||
| ]); | ||
|
|
||
| const fuzzyMinTokenLength = 6; | ||
| const maxExpansions = 16; | ||
|
|
||
| function queryTokens(query: string): string[] { | ||
| return normalizeSearchText(query) | ||
| .split(/\s+/) | ||
| .filter((token) => token.length > 0); | ||
| } | ||
|
|
||
| function addNormalizedTokens(target: Set<string>, value: string) { | ||
| for (const token of queryTokens(value)) { | ||
| if (token.length > 1) target.add(token); | ||
| } | ||
| } | ||
|
|
||
| /** Vocabulary of generic names, slugs, brand tokens, and safety-identity aliases. */ | ||
| export function buildMedicationCatalogVocabulary(records: MedicationRecord[]): Set<string> { | ||
| const vocab = new Set<string>(); | ||
| for (const record of records) { | ||
| addNormalizedTokens(vocab, record.name); | ||
| addNormalizedTokens(vocab, record.slug.replace(/-/g, " ")); | ||
| for (const brand of medicationBrandNames(record)) { | ||
| addNormalizedTokens(vocab, brand); | ||
| } | ||
| for (const alias of medicationAliasesForEntity(record.name)) { | ||
| addNormalizedTokens(vocab, alias); | ||
| } | ||
| } | ||
| // Also index identity aliases that may not share a catalog name token | ||
| // (e.g. "zyprexa" when only formulation-named records exist). | ||
| for (const record of records) { | ||
| for (const brand of medicationBrandNames(record)) { | ||
| for (const alias of medicationAliasesForEntity(brand)) { | ||
| addNormalizedTokens(vocab, alias); | ||
| } | ||
| } | ||
| } | ||
| return vocab; | ||
| } | ||
|
|
||
| /** True when Levenshtein distance is exactly 1 (substitution, insertion, or deletion). */ | ||
| export function isEditDistanceOne(left: string, right: string): boolean { | ||
| if (left === right) return false; | ||
| const a = left; | ||
| const b = right; | ||
| const lenA = a.length; | ||
| const lenB = b.length; | ||
| if (Math.abs(lenA - lenB) > 1) return false; | ||
|
|
||
| let i = 0; | ||
| let j = 0; | ||
| let edits = 0; | ||
| while (i < lenA && j < lenB) { | ||
| if (a[i] === b[j]) { | ||
| i += 1; | ||
| j += 1; | ||
| continue; | ||
| } | ||
| edits += 1; | ||
| if (edits > 1) return false; | ||
| if (lenA > lenB) i += 1; | ||
| else if (lenB > lenA) j += 1; | ||
| else { | ||
| i += 1; | ||
| j += 1; | ||
| } | ||
| } | ||
| if (i < lenA || j < lenB) edits += 1; | ||
| return edits === 1; | ||
| } | ||
|
|
||
| function uniqueEditDistanceOneMatch(token: string, vocabulary: Set<string>): string | undefined { | ||
| if (token.length < fuzzyMinTokenLength || vocabulary.has(token)) return undefined; | ||
| let match: string | undefined; | ||
| for (const candidate of vocabulary) { | ||
| if (Math.abs(candidate.length - token.length) > 1) continue; | ||
| if (!isEditDistanceOne(token, candidate)) continue; | ||
| if (match && match !== candidate) return undefined; | ||
| match = candidate; | ||
| } | ||
| return match; | ||
| } | ||
|
|
||
| function brandOrNameExpansions(token: string, records: MedicationRecord[]): string[] { | ||
| const expansions: string[] = []; | ||
| const seen = new Set<string>(); | ||
| const push = (value: string) => { | ||
| const normalized = normalizeSearchText(value); | ||
| if (!normalized || seen.has(normalized)) return; | ||
| seen.add(normalized); | ||
| expansions.push(normalized); | ||
| }; | ||
|
|
||
| for (const entity of medicationEntitiesInText(token)) { | ||
| push(entity); | ||
| for (const alias of medicationAliasesForEntity(entity)) push(alias); | ||
| } | ||
|
|
||
| const needle = normalizeSearchText(token); | ||
| if (!needle) return expansions; | ||
|
|
||
| for (const record of records) { | ||
| const name = normalizeSearchText(record.name); | ||
| const slug = normalizeSearchText(record.slug.replace(/-/g, " ")); | ||
| const brands = medicationBrandNames(record).map((brand) => normalizeSearchText(brand)); | ||
| const hitsName = name === needle || name.split(/\s+/).includes(needle) || slug.split(/\s+/).includes(needle); | ||
| const hitsBrand = brands.some((brand) => brand === needle || brand.split(/\s+/).includes(needle)); | ||
| if (!hitsName && !hitsBrand) continue; | ||
| // Expand to the canonical identity only. Sibling brands must not enter the | ||
| // expanded content lane — substring includes() would let "eleva" match | ||
| // "elevated" and pull unrelated records into brand queries like "zoloft". | ||
| push(record.name); | ||
| push(record.slug.replace(/-/g, " ")); | ||
| for (const alias of medicationAliasesForEntity(record.name)) push(alias); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| return expansions; | ||
| } | ||
|
|
||
| /** | ||
| * Correct typos and collect brand/generic expansions for medication catalog ranking. | ||
| * Does not call clinical-search or RAG helpers. | ||
| */ | ||
| export function analyzeMedicationCatalogQuery( | ||
| query: string, | ||
| records: MedicationRecord[], | ||
| ): MedicationCatalogQueryAnalysis { | ||
| const originalQuery = query.trim(); | ||
| if (!originalQuery) { | ||
| return { originalQuery: "", correctedQuery: "", corrections: [], expansions: [] }; | ||
| } | ||
|
|
||
| const vocabulary = buildMedicationCatalogVocabulary(records); | ||
| const rawTokens = originalQuery.split(/\s+/).filter(Boolean); | ||
| const corrections: MedicationCatalogCorrection[] = []; | ||
| const correctedTokens: string[] = []; | ||
|
|
||
| for (const raw of rawTokens) { | ||
| const lower = raw.toLowerCase(); | ||
| const normalized = normalizeSearchText(raw); | ||
| const mapped = medicationTypoCorrections.get(lower) ?? medicationTypoCorrections.get(normalized); | ||
| if (mapped && mapped !== normalized && mapped !== lower) { | ||
| corrections.push({ from: lower, to: mapped }); | ||
| correctedTokens.push(mapped); | ||
| continue; | ||
| } | ||
| const fuzzy = normalized ? uniqueEditDistanceOneMatch(normalized, vocabulary) : undefined; | ||
| if (fuzzy) { | ||
| corrections.push({ from: normalized, to: fuzzy }); | ||
| correctedTokens.push(fuzzy); | ||
| continue; | ||
| } | ||
| correctedTokens.push(raw); | ||
| } | ||
|
|
||
| const correctedQuery = correctedTokens.join(" "); | ||
| const baseTokens = new Set(queryTokens(correctedQuery)); | ||
| const expansionSeen = new Set<string>(baseTokens); | ||
| const expansions: string[] = []; | ||
|
|
||
| for (const token of queryTokens(correctedQuery)) { | ||
| for (const term of brandOrNameExpansions(token, records)) { | ||
| for (const piece of queryTokens(term)) { | ||
| if (piece.length < 2 || expansionSeen.has(piece)) continue; | ||
| expansionSeen.add(piece); | ||
| expansions.push(piece); | ||
| if (expansions.length >= maxExpansions) { | ||
| return { originalQuery, correctedQuery, corrections, expansions }; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return { originalQuery, correctedQuery, corrections, expansions }; | ||
| } | ||
|
|
||
| export function searchMedicationCatalog( | ||
| records: MedicationRecord[], | ||
| query: string, | ||
| limit = 50, | ||
| ): { | ||
| matches: MedicationSearchMatch[]; | ||
| analysis: MedicationCatalogQueryAnalysis; | ||
| } { | ||
| const analysis = analyzeMedicationCatalogQuery(query, records); | ||
| const matches = analysis.correctedQuery | ||
| ? rankMedicationRecords(records, analysis.correctedQuery, limit, analysis.expansions) | ||
| : []; | ||
| return { matches, analysis }; | ||
| } | ||
|
|
||
| export function medicationCatalogInterpretation(analysis: MedicationCatalogQueryAnalysis) { | ||
| if (!analysis.corrections.length && !analysis.expansions.length) return undefined; | ||
| return { | ||
| correctedQuery: | ||
| analysis.corrections.length && analysis.correctedQuery !== analysis.originalQuery | ||
| ? analysis.correctedQuery | ||
| : undefined, | ||
| corrections: analysis.corrections.length ? analysis.corrections : undefined, | ||
| appliedExpansions: analysis.expansions.length ? analysis.expansions : undefined, | ||
| }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.