From 19ff005b7d340bf271c8cde245857d512f5148db Mon Sep 17 00:00:00 2001 From: salmonik Date: Mon, 20 Oct 2025 17:45:37 +0200 Subject: simplified the searching algorithm --- cogs/music/play_utils.py | 60 +++++++++++------------------------------------- 1 file changed, 13 insertions(+), 47 deletions(-) (limited to 'cogs/music/play_utils.py') diff --git a/cogs/music/play_utils.py b/cogs/music/play_utils.py index ad2a2e1..6fa0edd 100644 --- a/cogs/music/play_utils.py +++ b/cogs/music/play_utils.py @@ -196,16 +196,15 @@ class PlayUtils: # -- LOCAL PLAY -- @staticmethod - async def search_local(query, max_results=5, min_score=30): - """Search for audio files using fuzzy matching on both filename and path. + async def search_local(query, max_results=5): + """Search for audio files that contain the query in their name (case-insensitive). Args: query: Search query string max_results: Maximum number of results to return - min_score: Minimum score threshold for matches (0-100) Returns: - List of matching file paths, sorted by relevance + List of matching file paths """ logger.info(f"Starting local search for query: '{query}'") try: @@ -214,9 +213,8 @@ class PlayUtils: search_paths = data.get('audio_paths', []) logger.info(f"Searching in paths: {search_paths}") - candidates = [] - files_checked = 0 - q = query.lower().replace('_', ' ').replace('-', ' ') + matches = [] + q = query.lower() for path in search_paths: path = Path(path) @@ -231,51 +229,19 @@ class PlayUtils: if not file_path.is_file(): continue - files_checked += 1 - # Check if file has a valid extension if file_path.suffix.lower() not in {'.mp4', '.mp3', '.wav', '.opus', '.m4a'}: continue - # Get relative path from search root - rel_path = file_path.relative_to(path).parent - - # Create searchable strings - filename = Utils.normalize_name(file_path, keep_ext=False) - full_path_str = str(rel_path / file_path.stem).lower() - - # Calculate scores - score_filename = Utils.combined_score(q, filename) - score_path = Utils.combined_score(q, full_path_str) - - # Use the better score - score = max(score_filename, score_path) - - # Apply bonuses - depth_bonus = max(0, 1.0 - (len(rel_path.parts) / 10.0)) # Prefer shallower paths - ext_bonus = 1.1 if file_path.suffix.lower() in q.split('.')[-1].lower() else 1.0 - - final_score = score * (0.9 + depth_bonus * 0.1) * ext_bonus - - if final_score >= min_score: - candidates.append((final_score, str(file_path.absolute()))) - logger.debug(f"Match: {file_path} (score: {final_score:.1f})") - - logger.info(f"Search complete. Checked {files_checked} files, found {len(candidates)} matches.") - - if not candidates: - logger.warning("No matching audio files found.") - return [] - - # Sort by score (descending) and get top results - candidates.sort(key=lambda x: x[0], reverse=True) - top_matches = candidates[:max_results] - - logger.info(f"Top {len(top_matches)} matches:") - for i, (score, path) in enumerate(top_matches, 1): - logger.info(f" {i}. {path} (score: {score:.1f})") + # Check if query is in filename (case-insensitive) + if q in file_path.name.lower(): + matches.append(str(file_path.absolute())) + if len(matches) >= max_results: + logger.info(f"Found {len(matches)} matches, stopping search") + return matches - return [c[1] for c in top_matches] + logger.info(f"Search complete. Found {len(matches)} matches.") + return matches except Exception as e: logger.error(f"Error during local search: {str(e)}", exc_info=True) -- cgit v1.2.3