Add topic suggestions: search "banana" → suggests "Fruit/Obst" topic

31 curated topics with 683 words (Fruit, Animals, Body, Eye, Sports,
School, Family, Weather, etc.). When user types a word that belongs
to a topic, the topic appears as a suggestion with "Alle laden" button.

Clicking "Alle laden" fetches all words from that topic via Kaikki
and displays them for easy selection into a learning unit.

New endpoint: GET /api/vocabulary/topics?q=banana
New table: vocabulary_topics (topic, words[], word_count)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Benjamin Admin
2026-04-29 10:56:36 +02:00
parent c2efb9934c
commit 91e8b92bdc
2 changed files with 80 additions and 1 deletions

View File

@@ -431,6 +431,32 @@ async def api_enrich_images(word_ids: List[str] = None):
return {"enriched": count, "total": len(word_ids)}
@router.get("/topics")
async def api_get_topics(q: str = Query("", description="Search topic or word")):
"""Find topics matching a search word. Returns related word lists.
If q matches a topic name → returns that topic.
If q matches a word in any topic → returns all topics containing that word.
"""
from vocabulary.db import get_pool
pool = await get_pool()
async with pool.acquire() as conn:
if not q.strip():
rows = await conn.fetch("SELECT topic, words, word_count FROM vocabulary_topics ORDER BY topic LIMIT 50")
else:
q_lower = q.strip().lower()
rows = await conn.fetch("""
SELECT topic, words, word_count FROM vocabulary_topics
WHERE lower(topic) LIKE $1 OR $2 = ANY(words)
ORDER BY word_count DESC
""", f"%{q_lower}%", q_lower)
return {
"topics": [{"topic": r["topic"], "words": list(r["words"]), "word_count": r["word_count"]} for r in rows],
"query": q,
}
class TranslateRequest(BaseModel):
word_ids: List[str]
target_language: str