feat: smart synopsis truncation at sentence boundary (500 chars)
This commit is contained in:
@@ -77,6 +77,20 @@ def get_download_manager() -> DownloadManager:
|
|||||||
# ==================== ANIME SEARCH ====================
|
# ==================== ANIME SEARCH ====================
|
||||||
|
|
||||||
|
|
||||||
|
def _truncate_at_sentence(text: str, max_len: int = 500) -> str:
|
||||||
|
"""Truncate text at the last sentence boundary before max_len."""
|
||||||
|
if not text or len(text) <= max_len:
|
||||||
|
return text
|
||||||
|
truncated = text[:max_len]
|
||||||
|
last_period = truncated.rfind(".")
|
||||||
|
if last_period > 0:
|
||||||
|
return text[: last_period + 1]
|
||||||
|
last_space = truncated.rfind(" ")
|
||||||
|
if last_space > 0:
|
||||||
|
return text[:last_space] + "..."
|
||||||
|
return truncated + "..."
|
||||||
|
|
||||||
|
|
||||||
@router.get("/anime/search")
|
@router.get("/anime/search")
|
||||||
async def search_anime_unified(
|
async def search_anime_unified(
|
||||||
request: Request,
|
request: Request,
|
||||||
@@ -189,11 +203,15 @@ async def search_anime_unified(
|
|||||||
if not isinstance(meta, Exception) and meta:
|
if not isinstance(meta, Exception) and meta:
|
||||||
results[pid][pos]["metadata"] = meta.model_dump()
|
results[pid][pos]["metadata"] = meta.model_dump()
|
||||||
|
|
||||||
# 5. Sort results
|
# 5. Sort results and truncate synopses
|
||||||
for pid in results:
|
for pid in results:
|
||||||
results[pid].sort(key=lambda x: -x.get("_relevance_boost", 0))
|
results[pid].sort(key=lambda x: -x.get("_relevance_boost", 0))
|
||||||
for item in results[pid]:
|
for item in results[pid]:
|
||||||
item.pop("_relevance_boost", None)
|
item.pop("_relevance_boost", None)
|
||||||
|
meta = item.get("metadata") or {}
|
||||||
|
syn = meta.get("synopsis")
|
||||||
|
if syn:
|
||||||
|
meta["synopsis"] = _truncate_at_sentence(syn, 500)
|
||||||
|
|
||||||
elapsed = time.time() - start_time
|
elapsed = time.time() - start_time
|
||||||
total_found = sum(len(r) for r in results.values())
|
total_found = sum(len(r) for r in results.values())
|
||||||
@@ -302,6 +320,14 @@ async def search_series_unified(
|
|||||||
meta.model_dump() if hasattr(meta, "model_dump") else meta
|
meta.model_dump() if hasattr(meta, "model_dump") else meta
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Truncate synopses at sentence boundaries
|
||||||
|
for pid in results:
|
||||||
|
for item in results[pid]:
|
||||||
|
meta = item.get("metadata") or {}
|
||||||
|
syn = meta.get("synopsis")
|
||||||
|
if syn:
|
||||||
|
meta["synopsis"] = _truncate_at_sentence(syn, 500)
|
||||||
|
|
||||||
elapsed = time.time() - start_time
|
elapsed = time.time() - start_time
|
||||||
total_found = sum(len(r) for r in results.values())
|
total_found = sum(len(r) for r in results.values())
|
||||||
print(
|
print(
|
||||||
|
|||||||
Reference in New Issue
Block a user