Direkte DeepSeek- und Mistral-Anbindung mit versionierten Aktionsprofilen, Budgetreservierung, Tokenabrechnung und administrativen Qualitätsprüfungen ersetzen die Flowise-Abhängigkeit im regulären Betrieb. 840 geprüfte A1-Aufgaben mit geschützter Testauswertung und konkreten offenen Arbeitsaufträgen ergänzen den Kurs. Inhalts- und Sprachbewertung bleiben von Azure-Aussprachewerten getrennt; historische Aufgaben und Antworten bleiben erhalten. Echte Wort- und Phonemmessungen werden mit Herkunft, Aktualität und Nachweisdichte gebündelt. Go bestimmt Trainingsziele und Wiederholungen, das Modell formuliert dazu passende Nachsprechübungen. Profilansicht und Auswahlgründe werden angezeigt. Validierung: Go-Race-Tests mit lokaler PostgreSQL-Integration, go vet, Web-Build, Audit aller 840 Aufgaben und drei Browser-Smokes mit lokalen Modell-/Speech-Fixtures bestanden. Keine Produktionsänderungen oder Live-Aufrufe.
148 lines
5.1 KiB
Go
148 lines
5.1 KiB
Go
package services
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"unicode"
|
|
"unicode/utf8"
|
|
|
|
"speech-learning-api/internal/domain"
|
|
"speech-learning-api/internal/store"
|
|
)
|
|
|
|
// Rebuild weaknesses from the current language's actual recent attempts rather
|
|
// than forwarding global aggregates or personal identifiers to the model.
|
|
func pronunciationGenerationInput(input domain.GeneratedExerciseRequest) map[string]any {
|
|
var attempts []domain.Attempt
|
|
for _, attempt := range input.Progress.RecentAttempts {
|
|
if !strings.EqualFold(attempt.TargetLanguage, input.TargetLanguage) {
|
|
continue
|
|
}
|
|
attempts = append(attempts, attempt)
|
|
}
|
|
progress := store.PronunciationEvidence(attempts)
|
|
progress = compactProgressForFlowise(progress)
|
|
avoid := []string{}
|
|
seen := map[string]bool{}
|
|
for _, text := range append(append([]string{}, input.AvoidReferenceTexts...), input.AvoidReferenceText) {
|
|
key := strings.ToLower(strings.Join(strings.Fields(text), " "))
|
|
if key != "" && !seen[key] {
|
|
seen[key] = true
|
|
avoid = append(avoid, text)
|
|
}
|
|
}
|
|
if input.TrainingPlan != nil {
|
|
plan := input.TrainingPlan
|
|
return map[string]any{"target_language": input.TargetLanguage, "cefr_level": input.CEFRLevel, "ui_language": input.UILanguage, "preferred_type": "repeat_sentence", "training_mode": plan.Mode, "training_plan": plan, "required_practice_words": plan.RequiredWords, "avoid_reference_texts": avoid}
|
|
}
|
|
if progress.WeakWords == nil {
|
|
progress.WeakWords = []domain.WordScore{}
|
|
}
|
|
if progress.WeakPhonemes == nil {
|
|
progress.WeakPhonemes = []domain.WeakPhoneme{}
|
|
}
|
|
mode := "targeted"
|
|
required := []string{}
|
|
// Select an observed word, never a language-specific sentence template.
|
|
lowest := 101.0
|
|
for _, word := range progress.WeakWords {
|
|
if word.Accuracy < lowest && strings.TrimSpace(word.Word) != "" {
|
|
lowest = word.Accuracy
|
|
required = []string{word.Word}
|
|
}
|
|
}
|
|
for _, phoneme := range progress.WeakPhonemes {
|
|
if phoneme.Accuracy < lowest && strings.TrimSpace(phoneme.Word) != "" {
|
|
lowest = phoneme.Accuracy
|
|
required = []string{phoneme.Word}
|
|
}
|
|
}
|
|
if len(progress.WeakWords) == 0 && len(progress.WeakPhonemes) == 0 {
|
|
mode = "general"
|
|
}
|
|
return map[string]any{
|
|
"target_language": input.TargetLanguage, "cefr_level": input.CEFRLevel,
|
|
"ui_language": input.UILanguage, "preferred_type": "repeat_sentence",
|
|
"training_mode": mode,
|
|
"required_practice_words": required,
|
|
"evidence_window_attempts": len(attempts),
|
|
"recent_weak_words": progress.WeakWords,
|
|
"recent_weak_phonemes": progress.WeakPhonemes,
|
|
"avoid_reference_texts": avoid,
|
|
}
|
|
}
|
|
|
|
func pronunciationTrainingValidation(ex domain.GeneratedExercise, request map[string]any) string {
|
|
if !sameReferenceText(ex.ReferenceText, ex.TTSText) {
|
|
return "tts_text must match reference_text"
|
|
}
|
|
words, _ := request["required_practice_words"].([]string)
|
|
for _, word := range words {
|
|
if !containsPracticeWord(ex.ReferenceText, word) {
|
|
return fmt.Sprintf("reference_text must include required practice word %q; keep the measured word and change only the surrounding sentence", word)
|
|
}
|
|
focused := false
|
|
for _, focus := range ex.FocusPoints {
|
|
focused = focused || containsPracticeWord(focus, word)
|
|
}
|
|
if !focused {
|
|
return fmt.Sprintf("focus_points must name required practice word %q", word)
|
|
}
|
|
}
|
|
if plan, ok := request["training_plan"].(*domain.PronunciationPlan); ok && plan.Mode == "transfer" {
|
|
word := strings.TrimSpace(ex.CandidateWord)
|
|
if !validExerciseText(word, 80) || len(strings.Fields(word)) != 1 {
|
|
return "candidate_word must name exactly one new practice word (maximum 80 characters)"
|
|
}
|
|
for _, known := range plan.KnownWords {
|
|
if strings.EqualFold(word, known) {
|
|
return "candidate_word must be new, not one of the known_words"
|
|
}
|
|
}
|
|
if !containsPracticeWord(ex.ReferenceText, word) {
|
|
return "reference_text must include candidate_word"
|
|
}
|
|
focused := false
|
|
for _, f := range ex.FocusPoints {
|
|
focused = focused || containsPracticeWord(f, word)
|
|
}
|
|
if !focused {
|
|
return "focus_points must include candidate_word"
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// Match words rather than accidental Latin substrings (cat/education). Scripts
|
|
// commonly written without spaces can occur within a longer character sequence.
|
|
func containsPracticeWord(text, word string) bool {
|
|
text = strings.ToLower(strings.Join(strings.Fields(text), " "))
|
|
word = strings.ToLower(strings.Join(strings.Fields(word), " "))
|
|
if word == "" {
|
|
return false
|
|
}
|
|
first, _ := utf8.DecodeRuneInString(word)
|
|
last, _ := utf8.DecodeLastRuneInString(word)
|
|
for offset := 0; offset < len(text); {
|
|
i := strings.Index(text[offset:], word)
|
|
if i < 0 {
|
|
return false
|
|
}
|
|
i += offset
|
|
before, _ := utf8.DecodeLastRuneInString(text[:i])
|
|
after, _ := utf8.DecodeRuneInString(text[i+len(word):])
|
|
if (!practiceWordRune(first) || !practiceWordRune(before)) && (!practiceWordRune(last) || !practiceWordRune(after)) {
|
|
return true
|
|
}
|
|
offset = i + len(word)
|
|
}
|
|
return false
|
|
}
|
|
|
|
func practiceWordRune(r rune) bool {
|
|
if unicode.In(r, unicode.Han, unicode.Hiragana, unicode.Katakana, unicode.Hangul, unicode.Thai, unicode.Lao, unicode.Khmer, unicode.Myanmar) {
|
|
return false
|
|
}
|
|
return unicode.IsLetter(r) || unicode.IsNumber(r) || unicode.IsMark(r)
|
|
}
|