diff --git a/src/helpers/mapTextResult.ts b/src/helpers/mapTextResult.ts new file mode 100644 index 0000000..1ed4bd5 --- /dev/null +++ b/src/helpers/mapTextResult.ts @@ -0,0 +1,25 @@ +export const mapTextResult = ( + parallelTextQueryResultRow: ParallelTextQueryResultRow, +): DisambiguatedTextResult => { + const { parallelId, moduleId, rid, text } = parallelTextQueryResultRow; + try { + const maybeWordArray = JSON.parse(text); + if (Array.isArray(maybeWordArray)) { + return { + parallelId, + moduleId, + rid, + type: "wordArray", + wordArray: maybeWordArray, + html: "", + }; + } + } catch (e) { + // Ignore this path. + + // We can't return here because we need to handle the text being + // HTML but JSON.parse also succeeding (so the output is not the + // expected array), e.g. if the text is surrounded in quotes. + } + return { parallelId, moduleId, rid, type: "html", wordArray: [], html: text }; +}; \ No newline at end of file diff --git a/src/routes/termSearch.ts b/src/routes/termSearch.ts index 6a0733f..40048b4 100644 --- a/src/routes/termSearch.ts +++ b/src/routes/termSearch.ts @@ -3,6 +3,28 @@ import { getVersificationSchemaIdFromModuleId } from "../helpers/moduleInfo.ts"; import { getTermSearchQuery } from "../helpers/termSearchQueryBuilder.ts"; import { getTextQuery } from "../helpers/parallelTextQueryBuilder.ts"; import { getWordQuery } from "../helpers/wordMapQueryBuilder.ts"; +import { mapTextResult } from "../helpers/mapTextResult.ts"; + +type MapToTermSearchResponseFunction = ( + orderedResults: number[][], + matchingText: ParallelTextQueryResult, + moduleIds: number[], +) => TermSearchTextResponse; +const mapMatchingTextSearchResults: 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 ? mapTextResult(row) : null; + }) + ) + ); type ModuleWarmWords = { moduleId: number; @@ -57,7 +79,6 @@ const get = ({ if (data.length === 0) { return mainResolve({ count, - orderedResults: [[]], matchingText: [], matchingWords: [], warmWords: [], @@ -122,8 +143,11 @@ const get = ({ ]) => { mainResolve({ count, - orderedResults, - matchingText, + matchingText: mapMatchingTextSearchResults( + orderedResults, + matchingText, + moduleIds, + ), matchingWords, warmWords, }); diff --git a/src/routes/text.ts b/src/routes/text.ts index c0cb1b9..54d1ed6 100644 --- a/src/routes/text.ts +++ b/src/routes/text.ts @@ -6,6 +6,18 @@ import { getModuleIdsFromModules, getVersificationSchemaIdFromModuleId, } from "../helpers/moduleInfo.ts"; +import { mapTextResult } from "../helpers/mapTextResult.ts"; + +const getMatchingTextForModuleAndRow = ( + moduleId: number, + parallelId: number, + matchingTextResult: ParallelTextQueryResult, +) => { + const matchingText = matchingTextResult.find( + (row) => row.moduleId === moduleId && row.parallelId === parallelId, + ); + return matchingText ? mapTextResult(matchingText) : null; +}; type Params = { modules: string; @@ -46,10 +58,17 @@ const get = ({ reference, modules }: Params) => matchingText: ParallelTextQueryResult, order: ParallelOrderingResult, ]) => { - mainResolve({ - matchingText, - order: order.map((row) => row.parallelId), - }); + mainResolve( + order.map((row) => + moduleIds.map((moduleId) => + getMatchingTextForModuleAndRow( + moduleId, + row.parallelId, + matchingText, + ) + ) + ), + ); }).catch((error) => { console.error("Error while gathering words and paralel text"); console.error(error); diff --git a/types.d.ts b/types.d.ts index 7caf3c8..c65a2ce 100644 --- a/types.d.ts +++ b/types.d.ts @@ -27,15 +27,10 @@ type WordResponse = { value: string }[] } + type TermSearchResponse = { count: number - orderedResults: number[][] - matchingText: { - parallelId: number - moduleId: number - rid: number - text: string - }[], + matchingText: TermSearchTextResponse matchingWords: { wid: number moduleId: number @@ -52,10 +47,23 @@ type HighlightResponse = { wid: number }[] } -type TextResponse = { - matchingText: ParallelTextQueryResult, - order: number[] +type DisambiguatedTextResult = { + parallelId: number + moduleId: number + rid: number + type: "wordArray" | "html" + wordArray: WordArray + html: string } +type TextResponse = (DisambiguatedTextResult | null)[][] +type TermSearchTextResponse = (DisambiguatedTextResult | null)[][][] + +type WordArray = { + wid: number + leader?: string + text: string + trailer?: string +}[] type ClickhouseResponse = { data: T @@ -79,12 +87,13 @@ type WordQueryResult = { type ParallelOrderingResult = { parallelId: number }[] -type ParallelTextQueryResult = { +type ParallelTextQueryResultRow = { parallelId: number moduleId: number rid: number text: string -}[] +} +type ParallelTextQueryResult = ParallelTextQueryResultRow[] type TermSearchQueryResult = { moduleId?: number lowestParallelId: number