Skip to content

Commit

Permalink
fix: Search test
Browse files Browse the repository at this point in the history
  • Loading branch information
bprusinowski committed Apr 14, 2023
1 parent daeb901 commit e176adf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
8 changes: 5 additions & 3 deletions app/rdf/query-search-score-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ export const parseFloatZeroed = (s: string) => {
}
};

export const weights: Record<string, number> = {
export const weights = {
name: 5,
description: 2,
themeName: 1,
publisher: 1,
creatorLabel: 1,
};
export const langMultiplier = 1.5;
export const exactMatchPoints = weights["name"] * 2;

const isStopword = (d: string) => {
return d.length < 3 && d.toLowerCase() === d;
Expand Down Expand Up @@ -53,14 +55,14 @@ export const computeScores = (

// Bonus points for exact match.
if (val.includes(query.toLowerCase())) {
score += weight * 2;
score += exactMatchPoints;
}
}

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

if (
Expand Down
26 changes: 19 additions & 7 deletions app/rdf/query-search.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { computeScores, weights } from "./query-search-score-utils";
import {
computeScores,
exactMatchPoints,
langMultiplier,
weights,
} from "./query-search-score-utils";

// jest.mock("rdf-ext", () => ({}));
// jest.mock("@rdf-esm/data-model", () => ({}));
Expand All @@ -11,20 +16,27 @@ jest.mock("@tpluscode/sparql-builder", () => ({}));

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

it("should compute weighted score per cube from score rows", () => {
const reduced = computeScores(scores, {
query: "national economy",
identifierName: "cube",
lang: "en",
});
expect(reduced["a"].score).toEqual(weights.name);
expect(reduced["b"].score).toEqual(weights.name + weights.description);
expect(reduced["a"].score).toEqual(weights.name * langMultiplier);
expect(reduced["b"].score).toEqual(
(weights.name + weights.description) * langMultiplier
);
expect(reduced["c"].score).toEqual(weights.creatorLabel);
expect(reduced["d"]).toBeUndefined();
expect(reduced["e"].score).toEqual(
(weights.name * 2 + exactMatchPoints) * langMultiplier
);
});
});

0 comments on commit e176adf

Please sign in to comment.