Skip to content

Commit

Permalink
Merge pull request #2 from parabible/task/denormalize-text
Browse files Browse the repository at this point in the history
Denormalize matchingText in text and termSearch endpoints
  • Loading branch information
jcuenod authored Jun 9, 2024
2 parents a4aad0a + e54b97d commit 34ab027
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 19 deletions.
25 changes: 25 additions & 0 deletions src/helpers/mapTextResult.ts
Original file line number Diff line number Diff line change
@@ -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 };
};
30 changes: 27 additions & 3 deletions src/routes/termSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,7 +79,6 @@ const get = ({
if (data.length === 0) {
return mainResolve({
count,
orderedResults: [[]],
matchingText: [],
matchingWords: [],
warmWords: [],
Expand Down Expand Up @@ -122,8 +143,11 @@ const get = ({
]) => {
mainResolve({
count,
orderedResults,
matchingText,
matchingText: mapMatchingTextSearchResults(
orderedResults,
matchingText,
moduleIds,
),
matchingWords,
warmWords,
});
Expand Down
27 changes: 23 additions & 4 deletions src/routes/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
33 changes: 21 additions & 12 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<T> = {
data: T
Expand All @@ -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
Expand Down

0 comments on commit 34ab027

Please sign in to comment.