Phonembewertung speichern und anzeigen

Azure-Phonemwerte in Versuchen sichern, schwache Laute im Fortschritt und Flowise-Kontext auswerten und die Web-Anzeige ergänzen. README dokumentiert Datenfluss, Betrieb und Grenzen der Funktion.
This commit is contained in:
Eugenius Naumenko
2026-10-10 17:59:44 +00:00
parent bac840e091
commit 82d8768a99
13 changed files with 451 additions and 59 deletions
+42 -5
View File
@@ -54,6 +54,7 @@ func (f *FlowiseTeacher) NextExercise(ctx context.Context, input domain.Generate
ReasonForChoice: "Fallback exercise because Flowise did not return a valid exercise.",
Source: "fallback",
}
progress := compactProgressForFlowise(input.Progress)
payload := map[string]any{
"task": "next_exercise",
"user_id": input.UserID,
@@ -61,16 +62,37 @@ func (f *FlowiseTeacher) NextExercise(ctx context.Context, input domain.Generate
"cefr_level": input.CEFRLevel,
"ui_language": input.UILanguage,
"profile": input.Profile,
"progress": input.Progress,
"recent_weak_words": input.RecentWeakWords,
"progress": progress,
"recent_weak_words": withoutPhonemeScores(input.RecentWeakWords),
"recent_weak_phonemes": input.Progress.WeakPhonemes,
"preferred_type": valueOr(input.PreferredType, "repeat_sentence"),
"avoid_reference_text": input.AvoidReferenceText,
"avoid_reference_texts": input.AvoidReferenceTexts,
"rules": "Generate one A1 English pronunciation exercise. Return valid JSON only with fields: exercise_type, target_language, cefr_level, reference_text, display_prompt, tts_text, focus_points, success_criteria, reason_for_choice. The reference_text must differ from every sentence in avoid_reference_texts. The same target word may still be used.",
"rules": "Generate one A1 English pronunciation exercise. Return valid JSON only with fields: exercise_type, target_language, cefr_level, reference_text, display_prompt, tts_text, focus_points, success_criteria, reason_for_choice. The reference_text must differ from every sentence in avoid_reference_texts. Use recent_weak_phonemes only when measured scores are provided; do not invent pronunciation errors.",
}
return f.askExercise(ctx, payload, input, fallback)
}
func compactProgressForFlowise(progress domain.ProgressSummary) domain.ProgressSummary {
progress.WeakWords = withoutPhonemeScores(progress.WeakWords)
recent := make([]domain.Attempt, len(progress.RecentAttempts))
for i, attempt := range progress.RecentAttempts {
recent[i] = attempt
recent[i].WordScores = withoutPhonemeScores(attempt.WordScores)
}
progress.RecentAttempts = recent
return progress
}
func withoutPhonemeScores(words []domain.WordScore) []domain.WordScore {
clean := make([]domain.WordScore, len(words))
copy(clean, words)
for i := range clean {
clean[i].PhonemeScores = nil
}
return clean
}
func (f *FlowiseTeacher) FeedbackForAttempt(ctx context.Context, targetText string, result domain.PronunciationResult, weakWords []domain.WordScore) (domain.TeacherResponse, error) {
_ = ctx
_ = weakWords
@@ -261,10 +283,22 @@ func parseGeneratedExerciseJSON(value json.RawMessage) (domain.GeneratedExercise
func mockFeedback(targetText string, result domain.PronunciationResult) domain.TeacherResponse {
weakWord := ""
weakPhoneme := domain.PhonemeScore{}
weakPhonemeWord := ""
for _, word := range result.WordScores {
if word.Highlight == "weak" {
weakWord = word.Word
break
if weakWord == "" {
weakWord = word.Word
}
}
if strings.EqualFold(word.ErrorType, "Omission") {
continue
}
for _, phoneme := range word.PhonemeScores {
if phoneme.Accuracy < 75 && (weakPhonemeWord == "" || phoneme.Accuracy < weakPhoneme.Accuracy) {
weakPhoneme = phoneme
weakPhonemeWord = word.Word
}
}
}
feedback := "Der Satz braucht noch etwas Übung. Sprich ihn noch einmal nach."
@@ -273,6 +307,9 @@ func mockFeedback(targetText string, result domain.PronunciationResult) domain.T
case result.OverallScore >= 95 && result.AccuracyScore >= 95 && result.FluencyScore >= 95 && result.CompletenessScore >= 95:
feedback = "Ausgezeichnet gesprochen! Die Aussprache dieses Satzes wurde sehr hoch bewertet."
nextRecommendation = "Du kannst mit einer neuen Übung weitermachen."
case weakPhonemeWord != "":
feedback = fmt.Sprintf("Im Wort %q wurde der Laut /%s/ mit %.0f von 100 Punkten bewertet.", weakPhonemeWord, weakPhoneme.Phoneme, weakPhoneme.Accuracy)
nextRecommendation = fmt.Sprintf("Wiederhole den Satz und achte besonders auf %q.", weakPhonemeWord)
case weakWord != "":
feedback = fmt.Sprintf("Das Wort %q wurde noch unsicher bewertet. Übe es im Satz noch einmal.", weakWord)
nextRecommendation = fmt.Sprintf("Wiederhole den Satz und achte besonders auf %q.", weakWord)
+28 -1
View File
@@ -39,6 +39,20 @@ func TestFeedbackForWeakWordNamesObservedProblem(t *testing.T) {
}
}
func TestFeedbackNamesMeasuredWeakPhoneme(t *testing.T) {
result := domain.PronunciationResult{
OverallScore: 79,
WordScores: []domain.WordScore{{
Word: "water", Accuracy: 72, Highlight: "weak",
PhonemeScores: []domain.PhonemeScore{{Phoneme: "w", Accuracy: 90}, {Phoneme: "t", Accuracy: 58}},
}},
}
feedback := mockFeedback("I drink water.", result)
if !strings.Contains(feedback.TeacherFeedback, "/t/") || !strings.Contains(feedback.TeacherFeedback, "58") {
t.Fatalf("expected measured phoneme in feedback, got %#v", feedback)
}
}
func TestFlowiseNextExerciseSendsSessionAndParsesResponse(t *testing.T) {
var requestBody map[string]json.RawMessage
@@ -78,7 +92,14 @@ func TestFlowiseNextExerciseSendsSessionAndParsesResponse(t *testing.T) {
}, slog.New(slog.NewTextHandler(io.Discard, nil)))
exercise, err := teacher.NextExercise(context.Background(), domain.GeneratedExerciseRequest{
UserID: "usr_1",
UserID: "usr_1",
Progress: domain.ProgressSummary{
WeakPhonemes: []domain.WeakPhoneme{{Word: "water", Phoneme: "t", Accuracy: 58}},
RecentAttempts: []domain.Attempt{{WordScores: []domain.WordScore{{
Word: "water", Accuracy: 70, PhonemeScores: []domain.PhonemeScore{{Phoneme: "t", Accuracy: 58}},
}}}},
},
RecentWeakWords: []domain.WordScore{{Word: "water", Accuracy: 70, PhonemeScores: []domain.PhonemeScore{{Phoneme: "t", Accuracy: 58}}}},
TargetLanguage: "en-US",
CEFRLevel: "A1",
SessionID: "usr_1:en-US:practice",
@@ -128,6 +149,12 @@ func TestFlowiseNextExerciseSendsSessionAndParsesResponse(t *testing.T) {
if texts, ok := question["avoid_reference_texts"].([]any); !ok || len(texts) != 2 {
t.Fatalf("unexpected avoid_reference_texts %#v", question["avoid_reference_texts"])
}
if phonemes, ok := question["recent_weak_phonemes"].([]any); !ok || len(phonemes) != 1 {
t.Fatalf("weak phonemes were not sent to Flowise: %#v", question["recent_weak_phonemes"])
}
if strings.Contains(questionText, "phoneme_scores") {
t.Fatal("raw phoneme scores should be summarized before sending progress to Flowise")
}
}
func TestValidGeneratedExerciseRejectsEarlierRecentSentence(t *testing.T) {
+22 -7
View File
@@ -157,7 +157,11 @@ type azurePronunciationResponse struct {
ErrorType string `json:"ErrorType"`
} `json:"PronunciationAssessment"`
Phonemes []struct {
Phoneme string `json:"Phoneme"`
Phoneme string `json:"Phoneme"`
AccuracyScore *float64 `json:"AccuracyScore"`
PronunciationAssessment struct {
AccuracyScore *float64 `json:"AccuracyScore"`
} `json:"PronunciationAssessment"`
} `json:"Phonemes"`
} `json:"Words"`
} `json:"NBest"`
@@ -183,18 +187,29 @@ func parseAzurePronunciation(body []byte) (domain.PronunciationResult, error) {
}
for _, word := range best.Words {
phonemes := make([]string, 0, len(word.Phonemes))
phonemeScores := make([]domain.PhonemeScore, 0, len(word.Phonemes))
for _, phoneme := range word.Phonemes {
if phoneme.Phoneme != "" {
phonemes = append(phonemes, phoneme.Phoneme)
accuracy := phoneme.PronunciationAssessment.AccuracyScore
if accuracy == nil {
accuracy = phoneme.AccuracyScore
}
if accuracy != nil {
phonemeScores = append(phonemeScores, domain.PhonemeScore{
Phoneme: phoneme.Phoneme, Accuracy: *accuracy, Highlight: highlightFor(*accuracy),
})
}
}
}
score := firstScore(word.AccuracyScore, word.PronunciationAssessment.AccuracyScore)
result.WordScores = append(result.WordScores, domain.WordScore{
Word: word.Word,
Accuracy: score,
ErrorType: firstNonEmpty(word.PronunciationAssessment.ErrorType, word.ErrorType),
Phonemes: phonemes,
Highlight: highlightFor(score),
Word: word.Word,
Accuracy: score,
ErrorType: firstNonEmpty(word.PronunciationAssessment.ErrorType, word.ErrorType),
Phonemes: phonemes,
PhonemeScores: phonemeScores,
Highlight: highlightFor(score),
})
}
return result, nil
@@ -238,7 +253,7 @@ func highlightFor(score float64) string {
switch {
case score >= 85:
return "good"
case score >= 70:
case score >= 75:
return "ok"
default:
return "weak"
+34
View File
@@ -57,4 +57,38 @@ func TestParseAzurePronunciationTopLevelScores(t *testing.T) {
if result.WordScores[0].Highlight != "good" {
t.Fatalf("expected good highlight, got %s", result.WordScores[0].Highlight)
}
if len(result.WordScores[0].PhonemeScores) != 0 {
t.Fatalf("unscored phonemes must not become zero-point weaknesses: %#v", result.WordScores[0].PhonemeScores)
}
}
func TestParseAzurePronunciationKeepsPhonemeScores(t *testing.T) {
body := []byte(`{
"NBest": [{"Display":"water","PronScore":82,"Words":[{
"Word":"water","AccuracyScore":80,
"Phonemes":[
{"Phoneme":"w","PronunciationAssessment":{"AccuracyScore":93}},
{"Phoneme":"ɔ","PronunciationAssessment":{"AccuracyScore":0}},
{"Phoneme":"t","AccuracyScore":58},
{"Phoneme":"ə"}
]
}]}]
}`)
result, err := parseAzurePronunciation(body)
if err != nil {
t.Fatalf("parse azure pronunciation: %v", err)
}
word := result.WordScores[0]
if len(word.Phonemes) != 4 || len(word.PhonemeScores) != 3 {
t.Fatalf("expected four phoneme labels and three scored phonemes, got %#v", word)
}
if word.PhonemeScores[0].Accuracy != 93 || word.PhonemeScores[0].Highlight != "good" {
t.Fatalf("unexpected first phoneme score %#v", word.PhonemeScores[0])
}
if word.PhonemeScores[1].Accuracy != 0 || word.PhonemeScores[1].Highlight != "weak" {
t.Fatalf("zero is a valid low score, got %#v", word.PhonemeScores[1])
}
if word.PhonemeScores[2].Accuracy != 58 || word.PhonemeScores[2].Highlight != "weak" {
t.Fatalf("top-level phoneme score was not read: %#v", word.PhonemeScores[2])
}
}