Skip to content

Commit 8e87f78

Browse files
committed
Merge fix/exact-name-merge: 仅按原始食材名逐字合并
2 parents 886ad45 + 09b4aad commit 8e87f78

2 files changed

Lines changed: 50 additions & 7 deletions

File tree

src/utils/ingredientNormalizer.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ export function mergeIngredients(allDishes) {
115115

116116
for (const dish of allDishes) {
117117
for (const ing of (dish.ingredients || [])) {
118-
const { normalized: name, wasRenamed, original, notes } = normalizeName(ing.name);
118+
// 完全按原始食材名合并:逐字相同才合并,不做同义词/修饰词归一化,显示名用原文
119+
const name = (ing.name || '').trim();
119120
if (!name) continue;
120121

121122
if (!merged[name]) {
@@ -139,12 +140,6 @@ export function mergeIngredients(allDishes) {
139140
if (!merged[name].sources.includes(dishName)) {
140141
merged[name].sources.push(dishName);
141142
}
142-
if (wasRenamed && !merged[name].renames.includes(original)) {
143-
merged[name].renames.push(original);
144-
}
145-
if (notes.length > 0) {
146-
merged[name].notes.push(...notes);
147-
}
148143
}
149144
}
150145

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { describe, it, expect } from 'vitest';
2+
import { mergeIngredients } from './ingredientNormalizer.js';
3+
4+
const dish = (name, ingredients) => ({ dish_name: name, ingredients });
5+
6+
describe('mergeIngredients — exact-name merging only', () => {
7+
it('does NOT merge 干辣椒 and 辣椒 (different names)', () => {
8+
const out = mergeIngredients([
9+
dish('A', [{ name: '干辣椒', amount: '40', unit: 'g', category: '香料' }]),
10+
dish('B', [{ name: '辣椒', amount: '100', unit: 'g', category: '蔬菜' }]),
11+
]);
12+
const names = out.map(i => i.name);
13+
expect(names).toContain('干辣椒');
14+
expect(names).toContain('辣椒');
15+
expect(out).toHaveLength(2);
16+
});
17+
18+
it('does NOT merge synonyms like 西红柿 and 番茄 — keeps original names verbatim', () => {
19+
const out = mergeIngredients([
20+
dish('A', [{ name: '西红柿', amount: '2', unit: '个', category: '蔬菜' }]),
21+
dish('B', [{ name: '番茄', amount: '1', unit: '个', category: '蔬菜' }]),
22+
]);
23+
const names = out.map(i => i.name);
24+
expect(names).toContain('西红柿');
25+
expect(names).toContain('番茄');
26+
expect(out).toHaveLength(2);
27+
});
28+
29+
it('merges and sums ingredients whose names are exactly identical', () => {
30+
const out = mergeIngredients([
31+
dish('A', [{ name: '辣椒', amount: '40', unit: 'g', category: '蔬菜' }]),
32+
dish('B', [{ name: '辣椒', amount: '100', unit: 'g', category: '蔬菜' }]),
33+
]);
34+
expect(out).toHaveLength(1);
35+
expect(out[0].name).toBe('辣椒');
36+
expect(out[0].amount).toContain('140');
37+
expect(out[0].sources).toEqual(['A', 'B']);
38+
});
39+
40+
it('treats names differing only by whitespace as the same (trim)', () => {
41+
const out = mergeIngredients([
42+
dish('A', [{ name: ' 盐 ', amount: '2', unit: 'g', category: '调料' }]),
43+
dish('B', [{ name: '盐', amount: '3', unit: 'g', category: '调料' }]),
44+
]);
45+
expect(out).toHaveLength(1);
46+
expect(out[0].name).toBe('盐');
47+
});
48+
});

0 commit comments

Comments
 (0)