From 7c58ab57f7bb259542b87ec7082f16816384ac5d Mon Sep 17 00:00:00 2001 From: Nico Date: Fri, 2 Jan 2026 18:51:44 -0800 Subject: [PATCH] fix string similarity check --- backend/models/list.model.js | 1 - docker-compose.prod.yml | 16 ---------------- frontend/src/pages/GroceryList.jsx | 2 +- frontend/src/utils/stringSimilarity.js | 10 +++++++++- 4 files changed, 10 insertions(+), 19 deletions(-) delete mode 100644 docker-compose.prod.yml diff --git a/backend/models/list.model.js b/backend/models/list.model.js index d3b1f27..7ac77a4 100644 --- a/backend/models/list.model.js +++ b/backend/models/list.model.js @@ -105,7 +105,6 @@ exports.getSuggestions = async (query) => { LIMIT 10`, [`%${query}%`] ); - res = result.rows; return result.rows; }; diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml deleted file mode 100644 index 007c34c..0000000 --- a/docker-compose.prod.yml +++ /dev/null @@ -1,16 +0,0 @@ -services: - frontend: - build: ./frontend - environment: - - MODE_ENV=production - ports: - - "3000:5173" - depends_on: - - backend - - backend: - build: ./backend - ports: - - "5000:5000" - env_file: - - ./backend/.env \ No newline at end of file diff --git a/frontend/src/pages/GroceryList.jsx b/frontend/src/pages/GroceryList.jsx index d1ae55f..296af46 100644 --- a/frontend/src/pages/GroceryList.jsx +++ b/frontend/src/pages/GroceryList.jsx @@ -139,7 +139,7 @@ export default function GroceryList() { // Only check for similar items if exact item doesn't exist const allItems = [...items, ...recentlyBoughtItems]; - const similar = findSimilarItems(itemName, allItems, 80); + const similar = findSimilarItems(itemName, allItems, 70); if (similar.length > 0) { // Show modal and wait for user decision setSimilarItemSuggestion({ originalName: itemName, suggestedItem: similar[0], quantity }); diff --git a/frontend/src/utils/stringSimilarity.js b/frontend/src/utils/stringSimilarity.js index 7ec1216..00200f0 100644 --- a/frontend/src/utils/stringSimilarity.js +++ b/frontend/src/utils/stringSimilarity.js @@ -39,6 +39,14 @@ export function calculateSimilarity(str1, str2) { if (lower1 === lower2) return 100; if (lower1.length === 0 || lower2.length === 0) return 0; + // Check if one string contains the other (substring match) + if (lower1.includes(lower2) || lower2.includes(lower1)) { + // Give high similarity for substring matches + const minLen = Math.min(lower1.length, lower2.length); + const maxLen = Math.max(lower1.length, lower2.length); + return Math.round((minLen / maxLen) * 100); + } + const distance = levenshteinDistance(lower1, lower2); const maxLength = Math.max(lower1.length, lower2.length); const similarity = ((maxLength - distance) / maxLength) * 100; @@ -53,7 +61,7 @@ export function calculateSimilarity(str1, str2) { * @param {number} threshold - Minimum similarity percentage (default 80) * @returns {Array} - Array of similar items sorted by similarity */ -export function findSimilarItems(inputName, existingItems, threshold = 80) { +export function findSimilarItems(inputName, existingItems, threshold = 70) { const similar = []; for (const item of existingItems) {