Skip to content

Commit 9512f8f

Browse files
authored
[voice] Allow speech-to-text services to emit empty error events (openhab#16922)
* [voice] Align speech-to-text services error events to core Signed-off-by: Miguel Álvarez <[email protected]>
1 parent 61a47da commit 9512f8f

File tree

7 files changed

+23
-34
lines changed

7 files changed

+23
-34
lines changed

bundles/org.openhab.voice.googlestt/src/main/java/org/openhab/voice/googlestt/internal/GoogleSTTService.java

+2-10
Original file line numberDiff line numberDiff line change
@@ -405,10 +405,8 @@ public void onComplete() {
405405
String transcript = transcriptBuilder.toString();
406406
if (!transcript.isBlank()) {
407407
sttListener.sttEventReceived(new SpeechRecognitionEvent(transcript, averageConfidence));
408-
} else if (!config.noResultsMessage.isBlank()) {
409-
sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.noResultsMessage));
410408
} else {
411-
sttListener.sttEventReceived(new SpeechRecognitionErrorEvent("No results"));
409+
sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.noResultsMessage));
412410
}
413411
}
414412
}
@@ -418,13 +416,7 @@ public void onError(@Nullable Throwable t) {
418416
logger.warn("Recognition error: ", t);
419417
if (!aborted.getAndSet(true)) {
420418
sttListener.sttEventReceived(new RecognitionStopEvent());
421-
if (!config.errorMessage.isBlank()) {
422-
sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.errorMessage));
423-
} else {
424-
String errorMessage = t.getMessage();
425-
sttListener.sttEventReceived(
426-
new SpeechRecognitionErrorEvent(errorMessage != null ? errorMessage : "Unknown error"));
427-
}
419+
sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.errorMessage));
428420
}
429421
}
430422

bundles/org.openhab.voice.voskstt/src/main/java/org/openhab/voice/voskstt/internal/VoskSTTService.java

+3-15
Original file line numberDiff line numberDiff line change
@@ -271,27 +271,15 @@ private Future<?> backgroundRecognize(STTListener sttListener, InputStream audio
271271
if (!transcript.isBlank()) {
272272
sttListener.sttEventReceived(new SpeechRecognitionEvent(transcript, 1F));
273273
} else {
274-
if (!config.noResultsMessage.isBlank()) {
275-
sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.noResultsMessage));
276-
} else {
277-
sttListener.sttEventReceived(new SpeechRecognitionErrorEvent("No results"));
278-
}
274+
sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.noResultsMessage));
279275
}
280276
}
281277
} catch (IOException e) {
282278
logger.warn("Error running speech to text: {}", e.getMessage());
283-
if (config.errorMessage.isBlank()) {
284-
sttListener.sttEventReceived(new SpeechRecognitionErrorEvent("Error"));
285-
} else {
286-
sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.errorMessage));
287-
}
279+
sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.errorMessage));
288280
} catch (UnsatisfiedLinkError e) {
289281
logger.warn("Missing native dependency: {}", e.getMessage());
290-
if (config.errorMessage.isBlank()) {
291-
sttListener.sttEventReceived(new SpeechRecognitionErrorEvent("Error"));
292-
} else {
293-
sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.errorMessage));
294-
}
282+
sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.errorMessage));
295283
} finally {
296284
if (recognizer != null) {
297285
recognizer.close();

bundles/org.openhab.voice.watsonstt/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ org.openhab.voice.watsonstt:optOutLogging=false
5151
org.openhab.voice.watsonstt:smartFormatting=false
5252
org.openhab.voice.watsonstt:redaction=false
5353
org.openhab.voice.watsonstt:noResultsMessage="Sorry, I didn't understand you"
54+
org.openhab.voice.watsonstt:errorMessage="Sorry, something went wrong"
5455
```
5556

5657
### Default Speech-to-Text Configuration

bundles/org.openhab.voice.watsonstt/src/main/java/org/openhab/voice/watsonstt/internal/WatsonSTTConfiguration.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ public class WatsonSTTConfiguration {
6363
/**
6464
* Message to be told when no results
6565
*/
66-
public String noResultsMessage = "No results";
66+
public String noResultsMessage = "Sorry, I didn't understand you";
67+
68+
/**
69+
* Message to be told when an error has happened
70+
*/
71+
public String errorMessage = "Sorry, something went wrong";
72+
6773
/**
6874
* By default, all IBM Watson™ services log requests and their results. Logging is done only to improve the services
6975
* for future users. The logged data is not shared or made public.

bundles/org.openhab.voice.watsonstt/src/main/java/org/openhab/voice/watsonstt/internal/WatsonSTTService.java

+2-7
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,7 @@ public void onError(@Nullable Exception e) {
311311
}
312312
logger.warn("TranscriptionError: {}", errorMessage);
313313
if (!aborted.getAndSet(true)) {
314-
sttListener.sttEventReceived(
315-
new SpeechRecognitionErrorEvent(errorMessage != null ? errorMessage : "Unknown error"));
314+
sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.errorMessage));
316315
}
317316
}
318317

@@ -327,11 +326,7 @@ public void onDisconnected() {
327326
if (!transcript.isBlank()) {
328327
sttListener.sttEventReceived(new SpeechRecognitionEvent(transcript, averageConfidence));
329328
} else {
330-
if (!config.noResultsMessage.isBlank()) {
331-
sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.noResultsMessage));
332-
} else {
333-
sttListener.sttEventReceived(new SpeechRecognitionErrorEvent("No results"));
334-
}
329+
sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.noResultsMessage));
335330
}
336331
}
337332
}

bundles/org.openhab.voice.watsonstt/src/main/resources/OH-INF/config/config.xml

+6-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@
4747
<parameter name="noResultsMessage" type="text" groupName="stt">
4848
<label>No Results Message</label>
4949
<description>Message to be told when no transcription is done.</description>
50-
<default>No results</default>
50+
<default>Sorry, I didn't understand you</default>
51+
</parameter>
52+
<parameter name="errorMessage" type="text" groupName="stt">
53+
<label>Error Message</label>
54+
<description>Message to be told when an error has happened.</description>
55+
<default>Sorry, something went wrong</default>
5156
</parameter>
5257
<parameter name="singleUtteranceMode" type="boolean" groupName="stt">
5358
<label>Single Utterance Mode</label>

bundles/org.openhab.voice.watsonstt/src/main/resources/OH-INF/i18n/watsonstt.properties

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ voice.config.watsonstt.maxSilenceSeconds.label = Max Silence Seconds
1212
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.
1313
voice.config.watsonstt.noResultsMessage.label = No Results Message
1414
voice.config.watsonstt.noResultsMessage.description = Message to be told when no transcription is done.
15+
voice.config.watsonstt.errorMessage.label = Error Message
16+
voice.config.watsonstt.errorMessage.description = Message to be told when an error has happened.
1517
voice.config.watsonstt.optOutLogging.label = Opt Out Logging
1618
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.
1719
voice.config.watsonstt.preferMultimediaModel.label = Prefer Multimedia Model

0 commit comments

Comments
 (0)