fix: update keyword matching logic in autoCategorize to handle short keywords correctly and prevent false positives

This commit is contained in:
Julien Froidefond
2025-11-27 10:42:42 +01:00
parent cf109984e5
commit 91e15ee07e

View File

@@ -143,9 +143,10 @@ export function autoCategorize(description: string, categories: Category[]): str
for (const keyword of category.keywords) {
const lowerKeyword = keyword.toLowerCase()
// Pour les keywords courts (< 4 chars), matcher uniquement des mots entiers
if (lowerKeyword.length < 4) {
const wordBoundary = new RegExp(`\\b${lowerKeyword}\\b`)
// Pour les keywords courts (< 6 chars), matcher uniquement des mots entiers
// Évite les faux positifs comme "chat" dans "achat"
if (lowerKeyword.length < 6) {
const wordBoundary = new RegExp(`\\b${escapeRegex(lowerKeyword)}\\b`)
if (wordBoundary.test(lowerDesc)) {
return category.id
}
@@ -161,6 +162,11 @@ export function autoCategorize(description: string, categories: Category[]): str
return null
}
// Échappe les caractères spéciaux pour les regex
function escapeRegex(str: string): string {
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
}
export function generateId(): string {
return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`
}