From 45b620e27869d3b2b54ca46fcfad38665dd3366e Mon Sep 17 00:00:00 2001 From: kercre123 Date: Mon, 8 Apr 2024 22:50:48 -0500 Subject: [PATCH] Fix timer and weather for whisper.cpp --- chipper/pkg/wirepod/ttr/intentparam.go | 18 ++++++++++- chipper/pkg/wirepod/ttr/weather.go | 44 ++++++++++++++++++-------- chipper/pkg/wirepod/ttr/words2num.go | 32 +++++++++++++++++++ 3 files changed, 80 insertions(+), 14 deletions(-) diff --git a/chipper/pkg/wirepod/ttr/intentparam.go b/chipper/pkg/wirepod/ttr/intentparam.go index e8dc82b9..7da68457 100755 --- a/chipper/pkg/wirepod/ttr/intentparam.go +++ b/chipper/pkg/wirepod/ttr/intentparam.go @@ -22,6 +22,7 @@ func ParamChecker(req interface{}, intent string, speechText string, botSerial s var botUnits string = "F" var botPlaySpecific bool = false var botIsEarlyOpus bool = false + var DoWeatherError bool = false // see if jdoc exists botJdoc, jdocExists := vars.GetJdoc("vic:"+botSerial, "vic.RobotSettings") @@ -139,7 +140,13 @@ func ParamChecker(req interface{}, intent string, speechText string, botSerial s isParam = true newIntent = intent condition, is_forecast, local_datetime, speakable_location_string, temperature, temperature_unit := weatherParser(speechText, botLocation, botUnits) - intentParams = map[string]string{"condition": condition, "is_forecast": is_forecast, "local_datetime": local_datetime, "speakable_location_string": speakable_location_string, "temperature": temperature, "temperature_unit": temperature_unit} + if local_datetime == "test" { + newIntent = "intent_system_unmatched" + isParam = false + DoWeatherError = true + } else { + intentParams = map[string]string{"condition": condition, "is_forecast": is_forecast, "local_datetime": local_datetime, "speakable_location_string": speakable_location_string, "temperature": temperature, "temperature_unit": temperature_unit} + } } else if strings.Contains(intent, "intent_imperative_volumelevel_extend") { isParam = true newIntent = intent @@ -307,6 +314,15 @@ func ParamChecker(req interface{}, intent string, speechText string, botSerial s } } IntentPass(req, newIntent, speechText, intentParams, isParam) + if DoWeatherError { + if vars.APIConfig.Weather.Enable { + logger.Println("The weather API is not configured properly.") + KGSim(botSerial, "The weather API is not configured properly. Please check the wire pod logs for more details.") + } else { + logger.Println("The weather API is not configured.") + KGSim(botSerial, "The weather API is not configured.") + } + } } // stintent diff --git a/chipper/pkg/wirepod/ttr/weather.go b/chipper/pkg/wirepod/ttr/weather.go index 6447899c..47350961 100755 --- a/chipper/pkg/wirepod/ttr/weather.go +++ b/chipper/pkg/wirepod/ttr/weather.go @@ -12,6 +12,7 @@ import ( "strconv" "strings" "time" + "unicode" "github.com/kercre123/wire-pod/chipper/pkg/logger" "github.com/kercre123/wire-pod/chipper/pkg/vars" @@ -93,6 +94,21 @@ type openWeatherMapAPIResponseStruct struct { //2.5 API +func removeEndPunctuation(s string) string { + if s == "" { + return s + } + + runes := []rune(s) + lastIndex := len(runes) - 1 + + if unicode.IsPunct(runes[lastIndex]) { + return string(runes[:lastIndex]) + } + + return s +} + type WeatherStruct struct { Id int `json:"id"` Main string `json:"main"` @@ -362,20 +378,22 @@ func weatherParser(speechText string, botLocation string, botUnits string) (stri var speechLocation string var hoursFromNow int if strings.Contains(speechText, lcztn.GetText(lcztn.STR_WEATHER_IN)) { - splitPhrase := strings.SplitAfter(speechText, lcztn.GetText(lcztn.STR_WEATHER_IN)) + splitPhrase := strings.SplitAfter(removeEndPunctuation(speechText), lcztn.GetText(lcztn.STR_WEATHER_IN)) speechLocation = strings.TrimSpace(splitPhrase[1]) - if len(splitPhrase) == 3 { - speechLocation = speechLocation + " " + strings.TrimSpace(splitPhrase[2]) - } else if len(splitPhrase) == 4 { - speechLocation = speechLocation + " " + strings.TrimSpace(splitPhrase[2]) + " " + strings.TrimSpace(splitPhrase[3]) - } else if len(splitPhrase) > 4 { - speechLocation = speechLocation + " " + strings.TrimSpace(splitPhrase[2]) + " " + strings.TrimSpace(splitPhrase[3]) - } - splitLocation := strings.Split(speechLocation, " ") - if len(splitLocation) == 2 { - speechLocation = splitLocation[0] + ", " + splitLocation[1] - } else if len(splitLocation) == 3 { - speechLocation = splitLocation[0] + " " + splitLocation[1] + ", " + splitLocation[2] + if os.Getenv("STT_SERVICE") != "whisper.cpp" { + if len(splitPhrase) == 3 { + speechLocation = speechLocation + " " + strings.TrimSpace(splitPhrase[2]) + } else if len(splitPhrase) == 4 { + speechLocation = speechLocation + " " + strings.TrimSpace(splitPhrase[2]) + " " + strings.TrimSpace(splitPhrase[3]) + } else if len(splitPhrase) > 4 { + speechLocation = speechLocation + " " + strings.TrimSpace(splitPhrase[2]) + " " + strings.TrimSpace(splitPhrase[3]) + } + splitLocation := strings.Split(speechLocation, " ") + if len(splitLocation) == 2 { + speechLocation = splitLocation[0] + ", " + splitLocation[1] + } else if len(splitLocation) == 3 { + speechLocation = splitLocation[0] + " " + splitLocation[1] + ", " + splitLocation[2] + } } logger.Println("Location parsed from speech: " + "`" + speechLocation + "`") specificLocation = true diff --git a/chipper/pkg/wirepod/ttr/words2num.go b/chipper/pkg/wirepod/ttr/words2num.go index 71fe48d4..64e1e211 100755 --- a/chipper/pkg/wirepod/ttr/words2num.go +++ b/chipper/pkg/wirepod/ttr/words2num.go @@ -1,6 +1,8 @@ package wirepod_ttr import ( + "os" + "regexp" "strconv" "strings" ) @@ -32,7 +34,37 @@ func basicspeechText2num(speechText string) int { return 0 } +func whisperSpeechtoNum(input string) string { + // whisper returns actual numbers in its response + // ex. "set a timer for 10 minutes and 11 seconds" + totalSeconds := 0 + + minutePattern := regexp.MustCompile(`(\d+)\s*minute`) + secondPattern := regexp.MustCompile(`(\d+)\s*second`) + + minutesMatches := minutePattern.FindStringSubmatch(input) + secondsMatches := secondPattern.FindStringSubmatch(input) + + if len(minutesMatches) > 1 { + minutes, err := strconv.Atoi(minutesMatches[1]) + if err == nil { + totalSeconds += minutes * 60 + } + } + if len(secondsMatches) > 1 { + seconds, err := strconv.Atoi(secondsMatches[1]) + if err == nil { + totalSeconds += seconds + } + } + + return strconv.Itoa(totalSeconds) +} + func words2num(speechText string) string { + if os.Getenv("STT_SERVICE") == "whisper.cpp" { + return whisperSpeechtoNum(speechText) + } number = basicspeechText2num(speechText) if number == 0 { number = 1