refactor(stage-1): eliminate dead parse_text, simplify task progress SQL, remove prompt hacks

This commit is contained in:
2026-10-07 14:39:53 +03:00
parent 6c2d94773c
commit 20eef2c684
3 changed files with 19 additions and 95 deletions
+1 -9
View File
@@ -74,10 +74,6 @@ def get_sentence_dictionary_hints(sentence: str, target_word: str = "") -> str:
return ""
hints = []
special_guidance = {
"owl": "owl — это сова/неясыть (ни в коем случае НЕ орёл!)",
"tawny owl": "tawny owl — это сова/неясыть (птица семейства совиных, ни в коем случае НЕ орёл!)",
}
try:
with get_db() as conn:
@@ -98,11 +94,7 @@ def get_sentence_dictionary_hints(sentence: str, target_word: str = "") -> str:
continue
seen_terms.add(cand)
trans = matched_dict[cand]
guidance = special_guidance.get(cand, "")
if guidance:
hints.append(f"- {cand}: {trans} (ВНИМАНИЕ: {guidance})")
else:
hints.append(f"- {cand}: {trans}")
hints.append(f"- {cand}: {trans}")
if len(hints) >= 8:
break
-38
View File
@@ -286,44 +286,6 @@ def split_long_text(text: str, target_words: int = 3000) -> List[str]:
return chunks
def parse_text(content: str, title: str = "Untitled", author: str = "") -> Tuple[str, str, List[Dict[str, str]]]:
"""
Parses plain text or Markdown into readable chapters.
Splits by explicit chapter markers (e.g. 'Chapter X', '# Chapter') or chunks of ~3000 words.
"""
content = content.replace("\r\n", "\n").replace("\r", "\n").strip()
if not content:
return title, author, [{"title": title if title != "Untitled" else "Chapter 1", "content": ""}]
lines = content.split("\n")
first_line = lines[0].strip()
# Extract title from first line or # Heading / <h1...> if default or generic README
if title == "Untitled" or title.lower().startswith("readme"):
if first_line.startswith("# "):
title = first_line.lstrip("# ").strip()
if len(lines) > 1:
content = "\n".join(lines[1:]).strip()
elif first_line.lower().startswith("title:"):
title = first_line.split(":", 1)[1].strip()
if len(lines) > 1:
content = "\n".join(lines[1:]).strip()
else:
h1_match = re.search(r"<h1[^>]*>(.*?)</h1>", content[:1500], re.IGNORECASE)
if h1_match:
detected = re.sub(r"<[^>]+>", "", h1_match.group(1)).strip()
if detected:
title = detected
# Extract author from next line if missing
if not author:
lines = content.split("\n")
if lines and lines[0].lower().startswith(("author:", "by:")):
author = lines[0].split(":", 1)[1].strip()
if len(lines) > 1:
content = "\n".join(lines[1:]).strip()
def detect_text_headings(content: str) -> List[Dict]:
"""
Detects section and chapter headings with high accuracy across Markdown,
+18 -48
View File
@@ -49,54 +49,24 @@ def update_task_progress(
"""
Updates the progress and status of a running task.
"""
if total is not None and message is not None:
conn.execute(
"""
UPDATE background_tasks
SET status = 'running',
progress_current = ?,
progress_total = ?,
message = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ? AND status != 'cancelled'
""",
(current, total, message, task_id),
)
elif total is not None:
conn.execute(
"""
UPDATE background_tasks
SET status = 'running',
progress_current = ?,
progress_total = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ? AND status != 'cancelled'
""",
(current, total, task_id),
)
elif message is not None:
conn.execute(
"""
UPDATE background_tasks
SET status = 'running',
progress_current = ?,
message = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ? AND status != 'cancelled'
""",
(current, message, task_id),
)
else:
conn.execute(
"""
UPDATE background_tasks
SET status = 'running',
progress_current = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ? AND status != 'cancelled'
""",
(current, task_id),
)
set_clauses = ["status = 'running'", "progress_current = ?", "updated_at = CURRENT_TIMESTAMP"]
params: List[Any] = [current]
if total is not None:
set_clauses.append("progress_total = ?")
params.append(total)
if message is not None:
set_clauses.append("message = ?")
params.append(message)
params.append(task_id)
sql = f"""
UPDATE background_tasks
SET {', '.join(set_clauses)}
WHERE id = ? AND status != 'cancelled'
"""
conn.execute(sql, params)
def complete_task(