refactor(stage-1): eliminate dead parse_text, simplify task progress SQL, remove prompt hacks
This commit is contained in:
1 parent
6c2d94773c
commit
20eef2c684
3 files changed
+15
-91
No files matched your search
@@ -74,10 +74,6 @@ def get_sentence_dictionary_hints(sentence: str, target_word: str = "") -> str:
|
|||||||
return ""
|
return ""
|
||||||
|
|
||||||
hints = []
|
hints = []
|
||||||
special_guidance = {
|
|
||||||
"owl": "owl — это сова/неясыть (ни в коем случае НЕ орёл!)",
|
|
||||||
"tawny owl": "tawny owl — это сова/неясыть (птица семейства совиных, ни в коем случае НЕ орёл!)",
|
|
||||||
}
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with get_db() as conn:
|
with get_db() as conn:
|
||||||
@@ -98,10 +94,6 @@ def get_sentence_dictionary_hints(sentence: str, target_word: str = "") -> str:
|
|||||||
continue
|
continue
|
||||||
seen_terms.add(cand)
|
seen_terms.add(cand)
|
||||||
trans = matched_dict[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:
|
if len(hints) >= 8:
|
||||||
|
|||||||
@@ -286,44 +286,6 @@ def split_long_text(text: str, target_words: int = 3000) -> List[str]:
|
|||||||
|
|
||||||
return chunks
|
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]:
|
def detect_text_headings(content: str) -> List[Dict]:
|
||||||
"""
|
"""
|
||||||
Detects section and chapter headings with high accuracy across Markdown,
|
Detects section and chapter headings with high accuracy across Markdown,
|
||||||
|
|||||||
+15
-45
@@ -49,54 +49,24 @@ def update_task_progress(
|
|||||||
"""
|
"""
|
||||||
Updates the progress and status of a running task.
|
Updates the progress and status of a running task.
|
||||||
"""
|
"""
|
||||||
if total is not None and message is not None:
|
set_clauses = ["status = 'running'", "progress_current = ?", "updated_at = CURRENT_TIMESTAMP"]
|
||||||
conn.execute(
|
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
|
UPDATE background_tasks
|
||||||
SET status = 'running',
|
SET {', '.join(set_clauses)}
|
||||||
progress_current = ?,
|
|
||||||
progress_total = ?,
|
|
||||||
message = ?,
|
|
||||||
updated_at = CURRENT_TIMESTAMP
|
|
||||||
WHERE id = ? AND status != 'cancelled'
|
WHERE id = ? AND status != 'cancelled'
|
||||||
""",
|
|
||||||
(current, total, message, task_id),
|
|
||||||
)
|
|
||||||
elif total is not None:
|
|
||||||
conn.execute(
|
|
||||||
"""
|
"""
|
||||||
UPDATE background_tasks
|
conn.execute(sql, params)
|
||||||
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),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def complete_task(
|
def complete_task(
|
||||||
|
|||||||
Reference in new issue
Block a user