From 9512f8f8944a045c17014b8e75aa6fa765f816bf Mon Sep 17 00:00:00 2001 From: GiviMAD Date: Sun, 23 Jun 2024 23:21:55 +0200 Subject: [PATCH] [voice] Allow speech-to-text services to emit empty error events (#16922) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * [voice] Align speech-to-text services error events to core Signed-off-by: Miguel Álvarez --- .../googlestt/internal/GoogleSTTService.java | 12 ++---------- .../voice/voskstt/internal/VoskSTTService.java | 18 +++--------------- bundles/org.openhab.voice.watsonstt/README.md | 1 + .../internal/WatsonSTTConfiguration.java | 8 +++++++- .../watsonstt/internal/WatsonSTTService.java | 9 ++------- .../main/resources/OH-INF/config/config.xml | 7 ++++++- .../resources/OH-INF/i18n/watsonstt.properties | 2 ++ 7 files changed, 23 insertions(+), 34 deletions(-) diff --git a/bundles/org.openhab.voice.googlestt/src/main/java/org/openhab/voice/googlestt/internal/GoogleSTTService.java b/bundles/org.openhab.voice.googlestt/src/main/java/org/openhab/voice/googlestt/internal/GoogleSTTService.java index a217c6a417..d3eca88a0d 100644 --- a/bundles/org.openhab.voice.googlestt/src/main/java/org/openhab/voice/googlestt/internal/GoogleSTTService.java +++ b/bundles/org.openhab.voice.googlestt/src/main/java/org/openhab/voice/googlestt/internal/GoogleSTTService.java @@ -405,10 +405,8 @@ public class GoogleSTTService implements STTService { String transcript = transcriptBuilder.toString(); if (!transcript.isBlank()) { sttListener.sttEventReceived(new SpeechRecognitionEvent(transcript, averageConfidence)); - } else if (!config.noResultsMessage.isBlank()) { - sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.noResultsMessage)); } else { - sttListener.sttEventReceived(new SpeechRecognitionErrorEvent("No results")); + sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.noResultsMessage)); } } } @@ -418,13 +416,7 @@ public class GoogleSTTService implements STTService { logger.warn("Recognition error: ", t); if (!aborted.getAndSet(true)) { sttListener.sttEventReceived(new RecognitionStopEvent()); - if (!config.errorMessage.isBlank()) { - sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.errorMessage)); - } else { - String errorMessage = t.getMessage(); - sttListener.sttEventReceived( - new SpeechRecognitionErrorEvent(errorMessage != null ? errorMessage : "Unknown error")); - } + sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.errorMessage)); } } diff --git a/bundles/org.openhab.voice.voskstt/src/main/java/org/openhab/voice/voskstt/internal/VoskSTTService.java b/bundles/org.openhab.voice.voskstt/src/main/java/org/openhab/voice/voskstt/internal/VoskSTTService.java index b4d3839214..121655b9e4 100644 --- a/bundles/org.openhab.voice.voskstt/src/main/java/org/openhab/voice/voskstt/internal/VoskSTTService.java +++ b/bundles/org.openhab.voice.voskstt/src/main/java/org/openhab/voice/voskstt/internal/VoskSTTService.java @@ -271,27 +271,15 @@ public class VoskSTTService implements STTService { if (!transcript.isBlank()) { sttListener.sttEventReceived(new SpeechRecognitionEvent(transcript, 1F)); } else { - if (!config.noResultsMessage.isBlank()) { - sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.noResultsMessage)); - } else { - sttListener.sttEventReceived(new SpeechRecognitionErrorEvent("No results")); - } + sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.noResultsMessage)); } } } catch (IOException e) { logger.warn("Error running speech to text: {}", e.getMessage()); - if (config.errorMessage.isBlank()) { - sttListener.sttEventReceived(new SpeechRecognitionErrorEvent("Error")); - } else { - sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.errorMessage)); - } + sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.errorMessage)); } catch (UnsatisfiedLinkError e) { logger.warn("Missing native dependency: {}", e.getMessage()); - if (config.errorMessage.isBlank()) { - sttListener.sttEventReceived(new SpeechRecognitionErrorEvent("Error")); - } else { - sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.errorMessage)); - } + sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.errorMessage)); } finally { if (recognizer != null) { recognizer.close(); diff --git a/bundles/org.openhab.voice.watsonstt/README.md b/bundles/org.openhab.voice.watsonstt/README.md index 46ba04c50e..c9610b56cc 100644 --- a/bundles/org.openhab.voice.watsonstt/README.md +++ b/bundles/org.openhab.voice.watsonstt/README.md @@ -51,6 +51,7 @@ org.openhab.voice.watsonstt:optOutLogging=false org.openhab.voice.watsonstt:smartFormatting=false org.openhab.voice.watsonstt:redaction=false org.openhab.voice.watsonstt:noResultsMessage="Sorry, I didn't understand you" +org.openhab.voice.watsonstt:errorMessage="Sorry, something went wrong" ``` ### Default Speech-to-Text Configuration diff --git a/bundles/org.openhab.voice.watsonstt/src/main/java/org/openhab/voice/watsonstt/internal/WatsonSTTConfiguration.java b/bundles/org.openhab.voice.watsonstt/src/main/java/org/openhab/voice/watsonstt/internal/WatsonSTTConfiguration.java index ca5a089126..818cc6f228 100644 --- a/bundles/org.openhab.voice.watsonstt/src/main/java/org/openhab/voice/watsonstt/internal/WatsonSTTConfiguration.java +++ b/bundles/org.openhab.voice.watsonstt/src/main/java/org/openhab/voice/watsonstt/internal/WatsonSTTConfiguration.java @@ -63,7 +63,13 @@ public class WatsonSTTConfiguration { /** * Message to be told when no results */ - public String noResultsMessage = "No results"; + public String noResultsMessage = "Sorry, I didn't understand you"; + + /** + * Message to be told when an error has happened + */ + public String errorMessage = "Sorry, something went wrong"; + /** * By default, all IBM Watson™ services log requests and their results. Logging is done only to improve the services * for future users. The logged data is not shared or made public. diff --git a/bundles/org.openhab.voice.watsonstt/src/main/java/org/openhab/voice/watsonstt/internal/WatsonSTTService.java b/bundles/org.openhab.voice.watsonstt/src/main/java/org/openhab/voice/watsonstt/internal/WatsonSTTService.java index 70bff2a574..2220ec1061 100644 --- a/bundles/org.openhab.voice.watsonstt/src/main/java/org/openhab/voice/watsonstt/internal/WatsonSTTService.java +++ b/bundles/org.openhab.voice.watsonstt/src/main/java/org/openhab/voice/watsonstt/internal/WatsonSTTService.java @@ -311,8 +311,7 @@ public class WatsonSTTService implements STTService { } logger.warn("TranscriptionError: {}", errorMessage); if (!aborted.getAndSet(true)) { - sttListener.sttEventReceived( - new SpeechRecognitionErrorEvent(errorMessage != null ? errorMessage : "Unknown error")); + sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.errorMessage)); } } @@ -327,11 +326,7 @@ public class WatsonSTTService implements STTService { if (!transcript.isBlank()) { sttListener.sttEventReceived(new SpeechRecognitionEvent(transcript, averageConfidence)); } else { - if (!config.noResultsMessage.isBlank()) { - sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.noResultsMessage)); - } else { - sttListener.sttEventReceived(new SpeechRecognitionErrorEvent("No results")); - } + sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.noResultsMessage)); } } } diff --git a/bundles/org.openhab.voice.watsonstt/src/main/resources/OH-INF/config/config.xml b/bundles/org.openhab.voice.watsonstt/src/main/resources/OH-INF/config/config.xml index ed54844ae9..86f85c36eb 100644 --- a/bundles/org.openhab.voice.watsonstt/src/main/resources/OH-INF/config/config.xml +++ b/bundles/org.openhab.voice.watsonstt/src/main/resources/OH-INF/config/config.xml @@ -47,7 +47,12 @@ Message to be told when no transcription is done. - No results + Sorry, I didn't understand you + + + + Message to be told when an error has happened. + Sorry, something went wrong diff --git a/bundles/org.openhab.voice.watsonstt/src/main/resources/OH-INF/i18n/watsonstt.properties b/bundles/org.openhab.voice.watsonstt/src/main/resources/OH-INF/i18n/watsonstt.properties index 6ca306aac5..e2920d37ce 100644 --- a/bundles/org.openhab.voice.watsonstt/src/main/resources/OH-INF/i18n/watsonstt.properties +++ b/bundles/org.openhab.voice.watsonstt/src/main/resources/OH-INF/i18n/watsonstt.properties @@ -12,6 +12,8 @@ voice.config.watsonstt.maxSilenceSeconds.label = Max Silence Seconds voice.config.watsonstt.maxSilenceSeconds.description = The time in seconds after which, if only silence (no speech) is detected in the audio, the connection is closed. voice.config.watsonstt.noResultsMessage.label = No Results Message voice.config.watsonstt.noResultsMessage.description = Message to be told when no transcription is done. +voice.config.watsonstt.errorMessage.label = Error Message +voice.config.watsonstt.errorMessage.description = Message to be told when an error has happened. voice.config.watsonstt.optOutLogging.label = Opt Out Logging voice.config.watsonstt.optOutLogging.description = By default, all IBM Watson™ services log requests and their results. Logging is done only to improve the services for future users. The logged data is not shared or made public. voice.config.watsonstt.preferMultimediaModel.label = Prefer Multimedia Model -- 2.47.3