Langsame Hörhilfen für Ausspracheübungen ergänzen
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
"speech-learning-api/internal/config"
|
||||
"speech-learning-api/internal/domain"
|
||||
@@ -306,12 +307,31 @@ func (s *Server) exerciseAudio(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusNotFound, err)
|
||||
return
|
||||
}
|
||||
mode := r.URL.Query().Get("mode")
|
||||
if mode == "" {
|
||||
mode = "normal"
|
||||
}
|
||||
if mode != "normal" && mode != "slow" && mode != "focus" {
|
||||
writeError(w, http.StatusBadRequest, errors.New("unsupported audio mode"))
|
||||
return
|
||||
}
|
||||
focusWord := r.URL.Query().Get("word")
|
||||
if mode == "focus" {
|
||||
focusWord = exerciseFocusWord(exercise.TargetText, focusWord)
|
||||
if focusWord == "" {
|
||||
writeError(w, http.StatusBadRequest, errors.New("word must occur in the exercise sentence"))
|
||||
return
|
||||
}
|
||||
} else if focusWord != "" {
|
||||
writeError(w, http.StatusBadRequest, errors.New("word is only supported in focus mode"))
|
||||
return
|
||||
}
|
||||
language, err := s.store.GetLanguage(r.Context(), exercise.TargetLanguage)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
audio, mime, err := s.speech.Synthesize(r.Context(), language.AzureLocale, language.DefaultTTSVoice, exercise.TargetText)
|
||||
audio, mime, err := s.speech.SynthesizeExercise(r.Context(), language.AzureLocale, language.DefaultTTSVoice, exercise.TargetText, mode, focusWord)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadGateway, err)
|
||||
return
|
||||
@@ -326,6 +346,20 @@ func (s *Server) exerciseAudio(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = w.Write(audio)
|
||||
}
|
||||
|
||||
func exerciseFocusWord(sentence, requested string) string {
|
||||
requested = strings.TrimFunc(strings.TrimSpace(requested), func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsNumber(r) })
|
||||
if requested == "" {
|
||||
return ""
|
||||
}
|
||||
for _, token := range strings.Fields(sentence) {
|
||||
word := strings.TrimFunc(token, func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsNumber(r) })
|
||||
if strings.EqualFold(word, requested) {
|
||||
return word
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (s *Server) createAttempt(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseMultipartForm(24 << 20); err != nil {
|
||||
writeError(w, http.StatusBadRequest, err)
|
||||
|
||||
Reference in New Issue
Block a user