- Ajoute une expression régulière anglaise à chacun des 26 TechStepMapping
du catalogue (locale "en"), en plus du "fr" existant — les recettes en
anglais (TheMealDB, etc.) peuvent désormais matcher leurs étapes.
- Nouveau apps/api/src/lib/ingredient-matcher.ts : moteur de matching pur
(nom d'ingrédient, unité, quantité) contre les catalogues Ingredient/Unit,
à partir de labels anglais écrits à la main (packages/shared/src/data/
catalog-labels-en.ts — 546 INGREDIENT_LABELS_EN + 17 UNIT_LABELS_EN avec
synonymes/abréviations). Tokenise et stem naïvement les deux côtés pour
tolérer pluriels et mots descriptifs superflus ; la correspondance la
plus spécifique (le plus de mots) l'emporte en cas de recoupement.
- extractQuantity() : lit un nombre en tête de texte libre (entier,
décimal, fraction simple ou nombre mixte) pour déduire la quantité et
l'unité quand la source ne les fournit pas séparément.
- Étend recipe-translation.ts : translateRecipe(recipe, locale) résout
aussi ingredientId/unitId/quantity de chaque ligne d'ingrédient — mais
uniquement pour locale "en" (seules langue avec des labels), pour ne pas
interroger la base inutilement ni halluciner un match dans une autre
langue.
- Ajoute cup/ounce/pound au catalogue Unit (toBaseFactor réel), absents
jusqu'ici alors que très fréquents dans les recettes anglaises.
- Vérifié en conditions réelles contre TheMealDB (Teriyaki Chicken
Casserole) : 8/9 ingrédients résolus avec la bonne quantité/unité, le
seul raté ("stir-fry vegetables") étant un mélange sans entrée dédiée au
catalogue — dégradation gracieuse (unitId/ingredientId: null) comme prévu.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
218 lines
7.9 KiB
TypeScript
218 lines
7.9 KiB
TypeScript
import { expect } from "chai";
|
|
import { prisma } from "../src/db/prisma.js";
|
|
import {
|
|
type IngredientMatchEntry,
|
|
type UnitMatchEntry,
|
|
extractQuantity,
|
|
loadIngredientCatalog,
|
|
loadUnitCatalog,
|
|
matchIngredientName,
|
|
matchUnit,
|
|
} from "../src/lib/ingredient-matcher.js";
|
|
import { resetDatabase } from "../test-support/reset-db.js";
|
|
|
|
describe("ingredient-matcher", () => {
|
|
describe("matchIngredientName", () => {
|
|
const tomato: IngredientMatchEntry = { ingredientId: 1, label: "Tomato" };
|
|
const chicken: IngredientMatchEntry = { ingredientId: 2, label: "Chicken" };
|
|
const chickenBreast: IngredientMatchEntry = { ingredientId: 3, label: "Chicken breast" };
|
|
const onion: IngredientMatchEntry = { ingredientId: 4, label: "Onion" };
|
|
const allPurposeFlour: IngredientMatchEntry = { ingredientId: 5, label: "All-purpose flour" };
|
|
const catalog = [tomato, chicken, chickenBreast, onion, allPurposeFlour];
|
|
|
|
it("matches an exact single-word label", () => {
|
|
expect(matchIngredientName("tomato", catalog)).to.equal(tomato.ingredientId);
|
|
});
|
|
|
|
it("is case- and accent-insensitive", () => {
|
|
expect(matchIngredientName("TOMATO", catalog)).to.equal(tomato.ingredientId);
|
|
expect(matchIngredientName("Tömato", catalog)).to.equal(tomato.ingredientId);
|
|
});
|
|
|
|
it("tolerates a regular plural", () => {
|
|
expect(matchIngredientName("tomatoes", catalog)).to.equal(tomato.ingredientId);
|
|
expect(matchIngredientName("onions", catalog)).to.equal(onion.ingredientId);
|
|
});
|
|
|
|
it("tolerates extra descriptive words around the match", () => {
|
|
expect(matchIngredientName("2 large diced yellow onions", catalog)).to.equal(
|
|
onion.ingredientId,
|
|
);
|
|
});
|
|
|
|
it("prefers the more specific multi-word label over a shorter one it contains", () => {
|
|
expect(matchIngredientName("boneless skinless chicken breasts", catalog)).to.equal(
|
|
chickenBreast.ingredientId,
|
|
);
|
|
});
|
|
|
|
it("still matches the shorter label when the more specific one isn't mentioned", () => {
|
|
expect(matchIngredientName("diced chicken thighs", catalog)).to.equal(chicken.ingredientId);
|
|
});
|
|
|
|
it("matches a hyphenated multi-word label", () => {
|
|
expect(matchIngredientName("2 cups all-purpose flour", catalog)).to.equal(
|
|
allPurposeFlour.ingredientId,
|
|
);
|
|
});
|
|
|
|
it("doesn't false-positive a short label inside an unrelated longer word", () => {
|
|
// "egg" must not match inside "eggplant" — whole-token comparison, not substring.
|
|
const eggplant: IngredientMatchEntry = { ingredientId: 6, label: "Eggplant" };
|
|
const egg: IngredientMatchEntry = { ingredientId: 7, label: "Egg" };
|
|
expect(matchIngredientName("eggplant", [egg, eggplant])).to.equal(eggplant.ingredientId);
|
|
});
|
|
|
|
it("returns null when nothing matches", () => {
|
|
expect(matchIngredientName("mango", catalog)).to.equal(null);
|
|
});
|
|
|
|
it("returns null for an empty catalog", () => {
|
|
expect(matchIngredientName("tomato", [])).to.equal(null);
|
|
});
|
|
|
|
it("returns null for an empty name", () => {
|
|
expect(matchIngredientName("", catalog)).to.equal(null);
|
|
});
|
|
|
|
it("breaks a same-specificity tie by the lowest ingredientId", () => {
|
|
const onionA: IngredientMatchEntry = { ingredientId: 20, label: "Onion" };
|
|
const onionB: IngredientMatchEntry = { ingredientId: 21, label: "Onion" };
|
|
expect(matchIngredientName("onion", [onionB, onionA])).to.equal(20);
|
|
});
|
|
});
|
|
|
|
describe("matchUnit", () => {
|
|
const gram: UnitMatchEntry = { unitId: 1, synonyms: ["g", "gram", "grams"] };
|
|
const tablespoon: UnitMatchEntry = {
|
|
unitId: 2,
|
|
synonyms: ["tbsp", "tbs", "tablespoon", "tablespoons"],
|
|
};
|
|
const cup: UnitMatchEntry = { unitId: 3, synonyms: ["cup", "cups"] };
|
|
const catalog = [gram, tablespoon, cup];
|
|
|
|
it("matches a full word synonym", () => {
|
|
expect(matchUnit("tablespoon", catalog)).to.equal(tablespoon.unitId);
|
|
});
|
|
|
|
it("matches an abbreviation synonym", () => {
|
|
expect(matchUnit("tbsp", catalog)).to.equal(tablespoon.unitId);
|
|
});
|
|
|
|
it("matches a plural synonym via the same stemming as ingredients", () => {
|
|
expect(matchUnit("cups", catalog)).to.equal(cup.unitId);
|
|
});
|
|
|
|
it("is case-insensitive", () => {
|
|
expect(matchUnit("TBSP", catalog)).to.equal(tablespoon.unitId);
|
|
});
|
|
|
|
it("only looks at the first word — ignores trailing text", () => {
|
|
expect(matchUnit("cup flour", catalog)).to.equal(cup.unitId);
|
|
});
|
|
|
|
it("doesn't match a short abbreviation inside an unrelated word", () => {
|
|
// "g" alone must not match "grated" — whole-token comparison.
|
|
expect(matchUnit("grated", catalog)).to.equal(null);
|
|
});
|
|
|
|
it("returns null when nothing matches", () => {
|
|
expect(matchUnit("pound", catalog)).to.equal(null);
|
|
});
|
|
|
|
it("returns null for an empty catalog", () => {
|
|
expect(matchUnit("cup", [])).to.equal(null);
|
|
});
|
|
|
|
it("returns null for an empty string", () => {
|
|
expect(matchUnit("", catalog)).to.equal(null);
|
|
});
|
|
});
|
|
|
|
describe("extractQuantity", () => {
|
|
it("extracts a plain integer", () => {
|
|
expect(extractQuantity("2 onions")).to.deep.equal({ quantity: 2, remainder: "onions" });
|
|
});
|
|
|
|
it("extracts a decimal using a dot", () => {
|
|
expect(extractQuantity("1.5 cups flour")).to.deep.equal({
|
|
quantity: 1.5,
|
|
remainder: "cups flour",
|
|
});
|
|
});
|
|
|
|
it("extracts a decimal using a comma", () => {
|
|
expect(extractQuantity("1,5 cups flour")).to.deep.equal({
|
|
quantity: 1.5,
|
|
remainder: "cups flour",
|
|
});
|
|
});
|
|
|
|
it("extracts a simple fraction", () => {
|
|
expect(extractQuantity("1/2 cup sugar")).to.deep.equal({
|
|
quantity: 0.5,
|
|
remainder: "cup sugar",
|
|
});
|
|
});
|
|
|
|
it("extracts a mixed number", () => {
|
|
expect(extractQuantity("1 1/2 cups sugar")).to.deep.equal({
|
|
quantity: 1.5,
|
|
remainder: "cups sugar",
|
|
});
|
|
});
|
|
|
|
it("returns null quantity and the trimmed original text when there's no leading number", () => {
|
|
expect(extractQuantity("salt to taste")).to.deep.equal({
|
|
quantity: null,
|
|
remainder: "salt to taste",
|
|
});
|
|
});
|
|
|
|
it("trims surrounding whitespace", () => {
|
|
expect(extractQuantity(" 2 eggs ")).to.deep.equal({ quantity: 2, remainder: "eggs" });
|
|
});
|
|
|
|
it("only takes the first number of a hyphenated range", () => {
|
|
expect(extractQuantity("2-3 carrots")).to.deep.equal({
|
|
quantity: 2,
|
|
remainder: "-3 carrots",
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("loadIngredientCatalog / loadUnitCatalog", () => {
|
|
beforeEach(async () => {
|
|
await resetDatabase();
|
|
});
|
|
|
|
after(async () => {
|
|
await prisma.$disconnect();
|
|
});
|
|
|
|
it("loads one entry per Ingredient that has an English label, keyed by real ingredientId", async () => {
|
|
const tomato = await prisma.ingredient.findFirstOrThrow({ where: { key: "tomato" } });
|
|
const ingredientCount = await prisma.ingredient.count();
|
|
|
|
const catalog = await loadIngredientCatalog();
|
|
|
|
// Every seeded ingredient has an authored English label (verified at
|
|
// generation time — see packages/shared/src/data/catalog-labels-en.ts),
|
|
// so nothing should be silently skipped.
|
|
expect(catalog).to.have.length(ingredientCount);
|
|
const tomatoEntry = catalog.find((entry) => entry.ingredientId === tomato.id);
|
|
expect(tomatoEntry?.label).to.equal("Tomato");
|
|
});
|
|
|
|
it("loads one entry per Unit that has English synonyms, keyed by real unitId", async () => {
|
|
const cup = await prisma.unit.findFirstOrThrow({ where: { key: "cup" } });
|
|
const unitCount = await prisma.unit.count();
|
|
|
|
const catalog = await loadUnitCatalog();
|
|
|
|
expect(catalog).to.have.length(unitCount);
|
|
const cupEntry = catalog.find((entry) => entry.unitId === cup.id);
|
|
expect(cupEntry?.synonyms).to.deep.equal(["cup", "cups"]);
|
|
});
|
|
});
|
|
});
|