From 7494141e92084b70fb0e8cf2ca8606fa83fde84c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?James=20Cu=C3=A9nod?= <4253884+jcuenod@users.noreply.github.com> Date: Mon, 8 Jul 2024 22:25:53 -0500 Subject: [PATCH] Integrate temp into search results --- src/routes/termSearch.ts | 78 +++++++++++++++++++++++++++++----------- types.d.ts | 9 +---- 2 files changed, 58 insertions(+), 29 deletions(-) diff --git a/src/routes/termSearch.ts b/src/routes/termSearch.ts index 96c1d2a..09a7232 100644 --- a/src/routes/termSearch.ts +++ b/src/routes/termSearch.ts @@ -7,24 +7,60 @@ import { mapTextResult } from "../helpers/mapTextResult.ts"; type MapToTermSearchResponseFunction = ( orderedResults: number[][], - matchingText: ParallelTextQueryResult, + matchingText: DisambiguatedTextResult[], moduleIds: number[], ) => TermSearchTextResponse; -const mapMatchingTextSearchResults: MapToTermSearchResponseFunction = ( - orderedResults, +const denormalizeParallelTextsIntoSearchResults: + MapToTermSearchResponseFunction = ( + orderedResults, + matchingText, + moduleIds, + ) => + orderedResults.map((parallelIds) => + moduleIds.map((moduleId) => + parallelIds.map((parallelId) => { + const row = matchingText.find((row) => + row.parallelId === parallelId && row.moduleId === moduleId + ); + return row || null; + }).filter((parallelText) => !!parallelText) + ) + ); + +const integrateWordTemperatureIntoParallelTexts: ( + matchingText: ParallelTextQueryResult, + matchingWords: WordQueryResult, + warmWords: ModuleWarmWords[], +) => DisambiguatedTextResult[] = ( matchingText, - moduleIds, -) => - orderedResults.map((parallelIds) => - moduleIds.map((moduleId) => - parallelIds.map((parallelId) => { - const row = matchingText.find((row) => - row.parallelId === parallelId && row.moduleId === moduleId - ); - return row ? mapTextResult(row) : null; - }).filter(parallelText => !!parallelText) - ) - ); + matchingWords, + warmWords, +) => { + const warmWordsLookup = warmWords.map(({ wids, moduleId }) => + wids.map((wid) => ({ wid, moduleId })) + ).flat(); + const temperatureMap: { [key: string]: "warm" | "hot" } = { + ...Object.fromEntries(warmWordsLookup.map(({ wid, moduleId }) => [ + `${wid}-${moduleId}`, + "warm", + ])), + ...Object.fromEntries(matchingWords.map(({ wid, moduleId }) => [ + `${wid}-${moduleId}`, + "hot", + ])), + }; + + return matchingText.map((row) => { + const mappedRow = mapTextResult(row); + if (mappedRow.type === "wordArray") { + mappedRow.wordArray = mappedRow.wordArray.map((word) => ({ + ...word, + temp: temperatureMap[`${word.wid}-${row.moduleId}`] || "", + })); + } + return mappedRow; + }); +}; type ModuleWarmWords = { moduleId: number; @@ -80,8 +116,6 @@ const get = ({ return mainResolve({ count, matchingText: [], - matchingWords: [], - warmWords: [], }); } @@ -143,13 +177,15 @@ const get = ({ ]) => { mainResolve({ count, - matchingText: mapMatchingTextSearchResults( + matchingText: denormalizeParallelTextsIntoSearchResults( orderedResults, - matchingText, + integrateWordTemperatureIntoParallelTexts( + matchingText, + matchingWords, + warmWords, + ), moduleIds, ), - matchingWords, - warmWords, }); }).catch(mainReject); }, diff --git a/types.d.ts b/types.d.ts index c65a2ce..f2c798b 100644 --- a/types.d.ts +++ b/types.d.ts @@ -31,14 +31,6 @@ type WordResponse = { type TermSearchResponse = { count: number matchingText: TermSearchTextResponse - matchingWords: { - wid: number - moduleId: number - }[] - warmWords: { - wids: number[] - moduleId: number - }[] } type HighlightResponse = { data: { @@ -63,6 +55,7 @@ type WordArray = { leader?: string text: string trailer?: string + temp?: string }[] type ClickhouseResponse = {