Skip to content

Commit

Permalink
Merge pull request #4 from parabible/task/integrate-word-temperature
Browse files Browse the repository at this point in the history
Integrate temp into search results
  • Loading branch information
jcuenod authored Jul 9, 2024
2 parents 23df52b + 7494141 commit 7949d44
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 29 deletions.
78 changes: 57 additions & 21 deletions src/routes/termSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,8 +116,6 @@ const get = ({
return mainResolve({
count,
matchingText: [],
matchingWords: [],
warmWords: [],
});
}

Expand Down Expand Up @@ -143,13 +177,15 @@ const get = ({
]) => {
mainResolve({
count,
matchingText: mapMatchingTextSearchResults(
matchingText: denormalizeParallelTextsIntoSearchResults(
orderedResults,
matchingText,
integrateWordTemperatureIntoParallelTexts(
matchingText,
matchingWords,
warmWords,
),
moduleIds,
),
matchingWords,
warmWords,
});
}).catch(mainReject);
},
Expand Down
9 changes: 1 addition & 8 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -63,6 +55,7 @@ type WordArray = {
leader?: string
text: string
trailer?: string
temp?: string
}[]

type ClickhouseResponse<T> = {
Expand Down

0 comments on commit 7949d44

Please sign in to comment.