This commit is contained in:
2025-11-21 12:29:12 -05:00
parent c6d2bad79d
commit c302b256d3
9 changed files with 1023 additions and 352 deletions

View File

@@ -212,7 +212,7 @@ class LyricSearch(FastAPI):
seeked_found_line: Optional[int] = None
# Split lyrics into lines based on <br>, newline characters, or " / "
lyrics_text = result["lyrics"].strip()
# Determine the delimiter and split accordingly
if "<br>" in lyrics_text:
lyric_lines = lyrics_text.split("<br>")
@@ -223,9 +223,9 @@ class LyricSearch(FastAPI):
else:
lyric_lines = lyrics_text.split("\n")
separator = "\n"
search_term = data.sub.strip().lower()
# First try single-line matching (existing behavior)
for i, line in enumerate(lyric_lines):
# Remove any special characters and extra spaces
@@ -233,38 +233,46 @@ class LyricSearch(FastAPI):
if search_term in cleaned_line.lower():
seeked_found_line = i
break
# If no single-line match found, try multi-line matching
if seeked_found_line is None:
# Try matching across consecutive lines (up to 5 lines for reasonable performance)
max_lines_to_check = min(5, len(lyric_lines))
for i in range(len(lyric_lines)):
for line_count in range(2, max_lines_to_check + 1):
if i + line_count <= len(lyric_lines):
# Combine consecutive lines with space separator
combined_lines = []
line_positions: list[tuple[int, int]] = [] # Track where each line starts in combined text
line_positions: list[
tuple[int, int]
] = [] # Track where each line starts in combined text
combined_text_parts: list[str] = []
for j in range(line_count):
if i + j < len(lyric_lines):
cleaned_line = regex.sub(r"\u2064", "", lyric_lines[i + j].strip())
cleaned_line = regex.sub(
r"\u2064", "", lyric_lines[i + j].strip()
)
combined_lines.append(cleaned_line)
# Track position of this line in the combined text
line_start_pos = len(" ".join(combined_text_parts).lower())
line_start_pos = len(
" ".join(combined_text_parts).lower()
)
if line_start_pos > 0:
line_start_pos += 1 # Account for space separator
line_start_pos += (
1 # Account for space separator
)
line_positions.append((i + j, line_start_pos))
combined_text_parts.append(cleaned_line)
combined_text = " ".join(combined_lines).lower()
if search_term in combined_text:
# Find which specific line the match starts in
match_pos = combined_text.find(search_term)
# Find the line that contains the start of the match
actual_start_line = i # Default fallback
for line_idx, line_start_pos in line_positions:
@@ -272,10 +280,10 @@ class LyricSearch(FastAPI):
actual_start_line = line_idx
else:
break
seeked_found_line = actual_start_line
break
if seeked_found_line is not None:
break