-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
63 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,70 @@ | ||
import unidecode | ||
from fuzzywuzzy import fuzz | ||
from thefuzz import process | ||
from loguru import logger | ||
from app.models import ProductMatch, ProductPublic | ||
from typing import List, Optional | ||
|
||
|
||
def find_closest_products_task( | ||
products: List[ProductPublic] = [], | ||
products: List[ProductPublic], | ||
item_name: Optional[str] = None, | ||
item_price: Optional[float] = None, | ||
threshold: float = 60.0, | ||
max_matches: int = 10, | ||
price_tolerance: float = 0.2, | ||
) -> List[ProductMatch]: | ||
""" | ||
Task to find closest products based on name and price similarity. | ||
Find closest products based on name and price similarity using thefuzz process. | ||
""" | ||
logger.info( | ||
f"Processing product matching task for '{item_name}' with price {item_price}" | ||
) | ||
if not products: | ||
logger.warning("No products provided for matching.") | ||
return [] | ||
matches = [] | ||
|
||
for product in products: | ||
name_score: float = 0.0 | ||
price_score: float = 0.0 | ||
|
||
if item_name is not None: | ||
name_score = fuzz.token_set_ratio( | ||
unidecode.unidecode(item_name.lower()), | ||
unidecode.unidecode(product.name.lower()), | ||
) | ||
|
||
if item_price: | ||
price_diff = abs(product.price - item_price) | ||
price_score = max(0, 100 - (price_diff / item_price) * 100) | ||
|
||
combined_score = (name_score * 0.7) + (price_score * 0.3) | ||
|
||
if combined_score >= threshold: | ||
matches.append( | ||
ProductMatch( | ||
score=combined_score, product=ProductPublic.model_validate(product) | ||
matches = [] | ||
if item_name is not None: | ||
product_names = [unidecode.unidecode(p.name.lower()) for p in products] | ||
name_matches = process.extract( | ||
unidecode.unidecode(item_name.lower()), product_names, limit=len(products) | ||
) | ||
for name, name_score in name_matches: | ||
index = product_names.index(name) | ||
product = products[index] | ||
# Calculate price score if price is provided | ||
price_score = 100.0 | ||
if item_price is not None: | ||
price_diff = abs(product.price - item_price) | ||
if price_diff / item_price > price_tolerance: | ||
price_score = 0.0 | ||
else: | ||
price_score = max(0, 100 - (price_diff / item_price) * 100) | ||
# Calculate combined score | ||
combined_score = (name_score * 0.7) + (price_score * 0.3) | ||
if combined_score >= threshold: | ||
matches.append( | ||
ProductMatch( | ||
score=combined_score, | ||
product=ProductPublic.model_validate(product), | ||
) | ||
) | ||
) | ||
|
||
# Sort matches by score in descending order | ||
matches.sort(key=lambda x: x.score, reverse=True) | ||
# Remove duplicate products, keeping the highest scoring match for each product | ||
unique_matches = [] | ||
seen_products = set() | ||
for match in matches: | ||
if match.product.id not in seen_products: | ||
unique_matches.append(match) | ||
seen_products.add(match.product.id) | ||
# Log debug information | ||
logger.debug( | ||
f"Found {len(matches)} matches for item '{item_name}' with price {item_price}" | ||
f"Found {len(unique_matches)} unique matches for item '{item_name}' with price {item_price}" | ||
) | ||
return matches[:max_matches] | ||
for match in unique_matches[:5]: | ||
logger.debug( | ||
f" Match: {match.product.name}, {match.product.price:.2f} € (Score: {match.score:.2f})" | ||
) | ||
return unique_matches[:max_matches] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ celery | |
click | ||
fastapi[standard] | ||
flower | ||
fuzzywuzzy | ||
thefuzz | ||
google-generativeai | ||
loguru | ||
ocrmypdf | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters