fix string similarity check
All checks were successful
Build & Deploy Costco Grocery List / build (push) Successful in 12s
Build & Deploy Costco Grocery List / deploy (push) Successful in 5s
Build & Deploy Costco Grocery List / notify (push) Successful in 1s

This commit is contained in:
Nico 2026-01-02 18:51:44 -08:00
parent 471ffae088
commit 7c58ab57f7
4 changed files with 10 additions and 19 deletions

View File

@ -105,7 +105,6 @@ exports.getSuggestions = async (query) => {
LIMIT 10`,
[`%${query}%`]
);
res = result.rows;
return result.rows;
};

View File

@ -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

View File

@ -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 });

View File

@ -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) {