fix string similarity check
This commit is contained in:
parent
471ffae088
commit
7c58ab57f7
@ -105,7 +105,6 @@ exports.getSuggestions = async (query) => {
|
||||
LIMIT 10`,
|
||||
[`%${query}%`]
|
||||
);
|
||||
res = result.rows;
|
||||
return result.rows;
|
||||
};
|
||||
|
||||
|
||||
@ -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
|
||||
@ -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 });
|
||||
|
||||
@ -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) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user