Skip to content

Commit

Permalink
fix: Types
Browse files Browse the repository at this point in the history
  • Loading branch information
bprusinowski committed Oct 26, 2023
1 parent fbba91b commit 467a17a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 34 deletions.
16 changes: 2 additions & 14 deletions app/rdf/query-search-score-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const parseFloatZeroed = (s: string) => {
export const weights = {
title: 5,
description: 2,
themeName: 1,
themeLabel: 1,
publisher: 1,
creatorLabel: 1,
};
Expand All @@ -24,13 +24,7 @@ const isStopword = (d: string) => {
*/
export const computeScores = (
cubes: ParsedRawSearchCube[],
{
query,
locale,
}: {
query?: string | null;
locale?: string | null;
}
{ query }: { query?: string | null }
) => {
const infoPerCube: Record<string, { score: number }> = {};

Expand Down Expand Up @@ -60,12 +54,6 @@ export const computeScores = (
}
}

// Cubes with properties in the current language get a bonus,
// as generally we expect the user to be interested in those.
if (cube.lang === locale) {
score *= langMultiplier;
}

if (
infoPerCube[cube.iri] === undefined ||
score > infoPerCube[cube.iri].score
Expand Down
15 changes: 6 additions & 9 deletions app/rdf/query-search.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ jest.mock("@tpluscode/sparql-builder", () => ({}));

describe("compute scores", () => {
const scores = [
{ lang: "en", iri: "a", title: "national" },
{ lang: "en", iri: "b", title: "national", description: "economy" },
{ lang: "de", iri: "c", creatorLabel: "national" },
{ lang: "de", iri: "d", creatorLabel: "" },
{ lang: "en", iri: "e", title: "National Economy of Switzerland" },
{ iri: "a", title: "national" },
{ iri: "b", title: "national", description: "economy" },
{ iri: "c", creatorLabel: "national" },
{ iri: "d", creatorLabel: "" },
{ iri: "e", title: "National Economy of Switzerland" },
] as unknown as ParsedRawSearchCube[];

it("should compute weighted score per cube from score rows", () => {
const reduced = computeScores(scores, {
query: "national economy",
locale: "en",
});
const reduced = computeScores(scores, { query: "national economy" });
expect(reduced.a.score).toEqual(weights.title * langMultiplier);
expect(reduced.b.score).toEqual(
(weights.title + weights.description) * langMultiplier
Expand Down
15 changes: 4 additions & 11 deletions app/rdf/query-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,26 +216,19 @@ export const searchCubes = async ({
const versionHistoryPerCube = Object.fromEntries(
rawCubes.map((d) => [d.iri, d.versionHistory])
);
const infoByCube = computeScores(rawCubes, {
query,
locale,
});

if (!locale) {
throw new Error("Must pass locale");
}
const infoByCube = computeScores(rawCubes, { query });

const seen = new Set<string>();
const seenCubes = new Set<string>();
const cubes = rawCubes
.map((cube) => {
const versionHistory = versionHistoryPerCube[cube.iri];
const dedupIdentifier = versionHistory ?? cube.iri;

if (seen.has(dedupIdentifier)) {
if (seenCubes.has(dedupIdentifier)) {
return null;
}

seen.add(dedupIdentifier);
seenCubes.add(dedupIdentifier);

const rawCubes = rawCubesByIri.get(cube.iri);

Expand Down

0 comments on commit 467a17a

Please sign in to comment.