Skip to content

Commit

Permalink
[Voice] Allow hli list
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Álvarez Díez <miguelwork92@gmail.com>
  • Loading branch information
GiviMAD committed Apr 10, 2022
1 parent 90f6a95 commit 100b934
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public static String interpret(@ParamDoc(name = "text") Object text) {
* In case of interpretation error, the error message is played using the default audio sink.
*
* @param text The text to interpret
* @param interpreter The Human Language Interpreter to be used
* @param interpreter Comma separate list of human language text interpreters to use or null to use the default services
*/
@ActionDoc(text = "interprets a given text by a given human language interpreter", returns = "human language response")
public static String interpret(@ParamDoc(name = "text") Object text,
Expand All @@ -180,7 +180,7 @@ public static String interpret(@ParamDoc(name = "text") Object text,
* If sink parameter is null, the error message is simply not played.
*
* @param text The text to interpret
* @param interpreter The Human Language Interpreter to be used
* @param interpreter Comma separate list of human language text interpreters to use or null to use the default services
* @param sink The name of audio sink to be used to play the error message
*/
@ActionDoc(text = "interprets a given text by a given human language interpreter", returns = "human language response")
Expand Down Expand Up @@ -218,10 +218,10 @@ public static void startDialog(@ParamDoc(name = "source") @Nullable String sourc
* @param ks the keyword spotting service to use or null to use the default service
* @param stt the speech-to-text service to use or null to use the default service
* @param tts the text-to-speech service to use or null to use the default service
* @param interpreter the human language text interpreter to use or null to use the default service
* @param interpreter comma separate list of human language text interpreters to use or null to use the default services
* @param source the name of audio source to use or null to use the default source
* @param sink the name of audio sink to use or null to use the default sink
* @param Locale the locale to use or null to use the default locale
* @param locale the locale to use or null to use the default locale
* @param keyword the keyword to use during keyword spotting or null to use the default keyword
* @param listeningItem the item to switch ON while listening to a question
*/
Expand Down Expand Up @@ -339,10 +339,10 @@ public static void listenAndAnswer(@ParamDoc(name = "source") @Nullable String s
*
* @param stt the speech-to-text service to use or null to use the default service
* @param tts the text-to-speech service to use or null to use the default service
* @param interpreter the human language text interpreter to use or null to use the default service
* @param interpreter comma separate list of human language text interpreters to use or null to use the default services
* @param source the name of audio source to use or null to use the default source
* @param sink the name of audio sink to use or null to use the default sink
* @param Locale the locale to use or null to use the default locale
* @param locale the locale to use or null to use the default locale
* @param listeningItem the item to switch ON while listening to a question
*/
@ActionDoc(text = "executes a simple dialog sequence without keyword spotting for a given audio source")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public interface VoiceManager {
* Interprets the passed string using a particular HLI service and the default locale.
*
* @param text The text to interpret
* @param hliId The id of the HLI service to use or null
* @param hliId Comma separate list of HLI service ids to use or null
* @throws InterpretationException
* @return a human language response
*/
Expand Down Expand Up @@ -144,17 +144,17 @@ public interface VoiceManager {
* @param ks the keyword spotting service to use or null to use the default service
* @param stt the speech-to-text service to use or null to use the default service
* @param tts the text-to-speech service to use or null to use the default service
* @param hli the human language text interpreter to use or null to use the default service
* @param hli collection of human language text interpreters to use or null to use the default services
* @param source the audio source to use or null to use the default source
* @param sink the audio sink to use or null to use the default sink
* @param Locale the locale to use or null to use the default locale
* @param locale the locale to use or null to use the default locale
* @param keyword the keyword to use during keyword spotting or null to use the default keyword
* @param listeningItem the item to switch ON while listening to a question
* @throws IllegalStateException if required services are not all available or the provided locale is not supported
* by all these services or a dialog is already started for this audio source
*/
void startDialog(@Nullable KSService ks, @Nullable STTService stt, @Nullable TTSService tts,
@Nullable HumanLanguageInterpreter hli, @Nullable AudioSource source, @Nullable AudioSink sink,
@Nullable Collection<HumanLanguageInterpreter> hli, @Nullable AudioSource source, @Nullable AudioSink sink,
@Nullable Locale locale, @Nullable String keyword, @Nullable String listeningItem)
throws IllegalStateException;

Expand Down Expand Up @@ -187,17 +187,17 @@ void startDialog(@Nullable KSService ks, @Nullable STTService stt, @Nullable TTS
*
* @param stt the speech-to-text service to use or null to use the default service
* @param tts the text-to-speech service to use or null to use the default service
* @param hli the human language text interpreter to use or null to use the default service
* @param hli collection of human language text interpreters to use or null to use the default services
* @param source the audio source to use or null to use the default source
* @param sink the audio sink to use or null to use the default sink
* @param locale the locale to use or null to use the default locale
* @param listeningItem the item to switch ON while listening to a question
* @throws IllegalStateException if required services are not all available or the provided locale is not supported
* by all these services or a dialog is already started for this audio source
*/
void listenAndAnswer(@Nullable STTService stt, @Nullable TTSService tts, @Nullable HumanLanguageInterpreter hli,
@Nullable AudioSource source, @Nullable AudioSink sink, @Nullable Locale locale,
@Nullable String listeningItem) throws IllegalStateException;
void listenAndAnswer(@Nullable STTService stt, @Nullable TTSService tts,
@Nullable Collection<HumanLanguageInterpreter> hli, @Nullable AudioSource source, @Nullable AudioSink sink,
@Nullable Locale locale, @Nullable String listeningItem) throws IllegalStateException;

/**
* Retrieves a TTS service.
Expand Down Expand Up @@ -281,15 +281,17 @@ void listenAndAnswer(@Nullable STTService stt, @Nullable TTSService tts, @Nullab
Collection<KSService> getKSs();

/**
* Retrieves a HumanLanguageInterpreter.
* If a default name is configured and the service available, this is returned. Otherwise, the first available
* Retrieves a HumanLanguageInterpreter collection.
* If a default names are configured and the services are available, those are returned. Otherwise, the first
* available
* service is returned.
*
* @return a HumanLanguageInterpreter or null, if no service is available or if a default is configured, but no
* according service is found
* @return a Collection<HumanLanguageInterpreter> or null, if no services are available or if some defaults are
* configured, but no
* according services are found
*/
@Nullable
HumanLanguageInterpreter getHLI();
Collection<HumanLanguageInterpreter> getHLI();

/**
* Retrieves a HumanLanguageInterpreter with the given id.
Expand All @@ -298,7 +300,7 @@ void listenAndAnswer(@Nullable STTService stt, @Nullable TTSService tts, @Nullab
* @return a HumanLanguageInterpreter or null, if no interpreter with this id exists
*/
@Nullable
HumanLanguageInterpreter getHLI(String id);
Collection<HumanLanguageInterpreter> getHLI(String id);

/**
* Retrieves all HumanLanguageInterpreters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.openhab.core.voice.internal;

import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Locale;

Expand Down Expand Up @@ -74,7 +75,7 @@ public class DialogProcessor implements KSListener, STTListener {
private final @Nullable KSService ks;
private final STTService stt;
private final TTSService tts;
private final HumanLanguageInterpreter hli;
private final Collection<HumanLanguageInterpreter> hli;
private final AudioSource source;
private final AudioSink sink;
private final Locale locale;
Expand Down Expand Up @@ -104,7 +105,7 @@ public class DialogProcessor implements KSListener, STTListener {
private @Nullable AudioStream streamKS;
private @Nullable AudioStream streamSTT;

public DialogProcessor(KSService ks, STTService stt, TTSService tts, HumanLanguageInterpreter hli,
public DialogProcessor(KSService ks, STTService stt, TTSService tts, Collection<HumanLanguageInterpreter> hli,
AudioSource source, AudioSink sink, Locale locale, String keyword, @Nullable String listeningItem,
EventPublisher eventPublisher, TranslationProvider i18nProvider, Bundle bundle) {
this.locale = locale;
Expand All @@ -124,7 +125,7 @@ public DialogProcessor(KSService ks, STTService stt, TTSService tts, HumanLangua
this.ttsFormat = VoiceManagerImpl.getBestMatch(tts.getSupportedFormats(), sink.getSupportedFormats());
}

public DialogProcessor(STTService stt, TTSService tts, HumanLanguageInterpreter hli, AudioSource source,
public DialogProcessor(STTService stt, TTSService tts, Collection<HumanLanguageInterpreter> hli, AudioSource source,
AudioSink sink, Locale locale, @Nullable String listeningItem, EventPublisher eventPublisher,
TranslationProvider i18nProvider, Bundle bundle) {
this.locale = locale;
Expand Down Expand Up @@ -282,15 +283,20 @@ public synchronized void sttEventReceived(STTEvent sttEvent) {
SpeechRecognitionEvent sre = (SpeechRecognitionEvent) sttEvent;
String question = sre.getTranscript();
logger.debug("Text recognized: {}", question);
try {
toggleProcessing(false);
String answer = hli.interpret(locale, question);
logger.debug("Interpretation result: {}", answer);
say(answer);
} catch (InterpretationException e) {
logger.debug("Interpretation exception: {}", e.getMessage());
say(e.getMessage());
String answer = null;
String error = null;
for (var interpreter : hli) {
try {
answer = interpreter.interpret(locale, question);
logger.debug("Interpretation result: {}", answer);
break;
} catch (InterpretationException e) {
logger.debug("Interpretation exception: {}", e.getMessage());
error = e.getMessage();
}
}
toggleProcessing(false);
say(answer != null ? answer : error);
abortSTT();
}
} else if (sttEvent instanceof RecognitionStartEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ public void execute(String[] args, Console console) {
try {
AudioSource source = args.length < 2 ? null : audioManager.getSource(args[1]);
AudioSink sink = args.length < 3 ? null : audioManager.getSink(args[2]);
HumanLanguageInterpreter hli = args.length < 4 ? null : voiceManager.getHLI(args[3]);
Collection<HumanLanguageInterpreter> hli = args.length < 4 ? null
: voiceManager.getHLI(args[3]);
TTSService tts = args.length < 5 ? null : voiceManager.getTTS(args[4]);
STTService stt = args.length < 6 ? null : voiceManager.getSTT(args[5]);
KSService ks = args.length < 7 ? null : voiceManager.getKS(args[6]);
Expand All @@ -158,7 +159,8 @@ public void execute(String[] args, Console console) {
try {
AudioSource source = args.length < 2 ? null : audioManager.getSource(args[1]);
AudioSink sink = args.length < 3 ? null : audioManager.getSink(args[2]);
HumanLanguageInterpreter hli = args.length < 4 ? null : voiceManager.getHLI(args[3]);
Collection<HumanLanguageInterpreter> hli = args.length < 4 ? null
: voiceManager.getHLI(args[3]);
TTSService tts = args.length < 5 ? null : voiceManager.getTTS(args[4]);
STTService stt = args.length < 6 ? null : voiceManager.getSTT(args[5]);
voiceManager.listenAndAnswer(stt, tts, hli, source, sink, null, null);
Expand Down Expand Up @@ -243,11 +245,12 @@ private void say(String[] args, Console console) {
private void listInterpreters(Console console) {
Collection<HumanLanguageInterpreter> interpreters = voiceManager.getHLIs();
if (!interpreters.isEmpty()) {
HumanLanguageInterpreter defaultHLI = voiceManager.getHLI();
var defaultHLIs = voiceManager.getHLI();
Locale locale = localeProvider.getLocale();
interpreters.stream().sorted(comparing(s -> s.getLabel(locale))).forEach(hli -> {
console.println(String.format("%s %s (%s)", hli.equals(defaultHLI) ? "*" : " ", hli.getLabel(locale),
hli.getId()));
console.println(String.format("%s %s (%s)",
(defaultHLIs != null && defaultHLIs.stream().anyMatch(hli::equals)) ? "*" : " ",
hli.getLabel(locale), hli.getId()));
});
} else {
console.println("No interpreters found.");
Expand Down
Loading

0 comments on commit 100b934

Please sign in to comment.