Search in sources :

Example 1 with InterpretationException

use of org.eclipse.smarthome.core.voice.text.InterpretationException in project smarthome by eclipse.

the class VoiceResource method interpret.

@POST
@Path("/interpreters/{id: [a-zA-Z_0-9]*}")
@Consumes(MediaType.TEXT_PLAIN)
@ApiOperation(value = "Sends a text to a given human language interpreter.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 404, message = "No human language interpreter was found."), @ApiResponse(code = 400, message = "interpretation exception occurs") })
public Response interpret(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @ApiParam(value = "language") String language, @ApiParam(value = "text to interpret", required = true) String text, @PathParam("id") @ApiParam(value = "interpreter id", required = true) String id) {
    final Locale locale = LocaleUtil.getLocale(language);
    HumanLanguageInterpreter hli = voiceManager.getHLI(id);
    if (hli != null) {
        try {
            hli.interpret(locale, text);
            return Response.ok(null, MediaType.TEXT_PLAIN).build();
        } catch (InterpretationException e) {
            return JSONResponse.createErrorResponse(Status.BAD_REQUEST, e.getMessage());
        }
    } else {
        return JSONResponse.createErrorResponse(Status.NOT_FOUND, "Interpreter not found");
    }
}
Also used : Locale(java.util.Locale) InterpretationException(org.eclipse.smarthome.core.voice.text.InterpretationException) HumanLanguageInterpreter(org.eclipse.smarthome.core.voice.text.HumanLanguageInterpreter) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with InterpretationException

use of org.eclipse.smarthome.core.voice.text.InterpretationException in project habot by ghys.

the class OpenNLPInterpreter method reply.

/**
 * This variant of interpret() returns a more complete interpretation result.
 *
 * @param locale the locale of the query
 * @param text the query text
 * @return the interpretation result as a {@link ChatReply} object
 * @throws InterpretationException
 */
public ChatReply reply(Locale locale, String text) throws InterpretationException {
    if (!locale.equals(currentLocale) || intentTrainer == null) {
        try {
            intentTrainer = new IntentTrainer(locale.getLanguage(), skills.values().stream().sorted(new Comparator<Skill>() {

                @Override
                public int compare(Skill o1, Skill o2) {
                    if (o1.getIntentId().equals("get-status")) {
                        return -1;
                    }
                    if (o2.getIntentId().equals("get-status")) {
                        return 1;
                    }
                    return o1.getIntentId().compareTo(o2.getIntentId());
                }
            }).collect(Collectors.toList()), getNameFinderTrainingDataFromTags(), this.tokenizerId);
            currentLocale = locale;
        } catch (Exception e) {
            InterpretationException fe = new InterpretationException("Error during trainer initialization: " + e.getMessage());
            fe.initCause(e);
            throw fe;
        }
    }
    ChatReply reply = new ChatReply(locale, text);
    Intent intent;
    // categorizer.
    if (!this.itemRegistry.getItemsByTag("object:" + text.toLowerCase()).isEmpty()) {
        intent = new Intent("get-status");
        intent.setEntities(new HashMap<String, String>());
        intent.getEntities().put("object", text.toLowerCase());
    } else if (!this.itemRegistry.getItemsByTag("location:" + text.toLowerCase()).isEmpty()) {
        intent = new Intent("get-status");
        intent.setEntities(new HashMap<String, String>());
        intent.getEntities().put("location", text.toLowerCase());
    } else {
        // Else, run it through the IntentTrainer
        intent = intentTrainer.interpret(text);
    }
    reply.setIntent(intent);
    Skill skill = skills.get(intent.getName());
    if (skill != null) {
        IntentInterpretation intentInterpretation = skill.interpret(intent, locale.getLanguage());
        if (intentInterpretation != null) {
            reply.setAnswer(intentInterpretation.getAnswer());
            if (intentInterpretation.getHint() != null) {
                reply.setHint(intentInterpretation.getHint());
            }
            if (intentInterpretation.getMatchedItems() != null) {
                reply.setMatchedItems(intentInterpretation.getMatchedItems().stream().map(i -> i.getName()).collect(Collectors.toList()).toArray(new String[0]));
            }
            if (intentInterpretation.getCard() != null) {
                reply.setCard(intentInterpretation.getCard());
            }
        }
    }
    return reply;
}
Also used : Arrays(java.util.Arrays) HashMap(java.util.HashMap) InterpretationException(org.eclipse.smarthome.core.voice.text.InterpretationException) RegistryChangeListener(org.eclipse.smarthome.core.common.registry.RegistryChangeListener) HashSet(java.util.HashSet) Component(org.osgi.service.component.annotations.Component) Locale(java.util.Locale) Skill(org.openhab.ui.habot.nlp.Skill) Map(java.util.Map) ChatReply(org.openhab.ui.habot.nlp.ChatReply) Activate(org.osgi.service.component.annotations.Activate) Intent(org.openhab.ui.habot.nlp.Intent) IntentInterpretation(org.openhab.ui.habot.nlp.IntentInterpretation) Deactivate(org.osgi.service.component.annotations.Deactivate) Collection(java.util.Collection) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ReferencePolicy(org.osgi.service.component.annotations.ReferencePolicy) Collectors(java.util.stream.Collectors) Item(org.eclipse.smarthome.core.items.Item) BundleContext(org.osgi.framework.BundleContext) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) IOUtils(org.apache.commons.io.IOUtils) Modified(org.osgi.service.component.annotations.Modified) Comparator(java.util.Comparator) HumanLanguageInterpreter(org.eclipse.smarthome.core.voice.text.HumanLanguageInterpreter) Reference(org.osgi.service.component.annotations.Reference) Collections(java.util.Collections) NonNull(org.eclipse.jdt.annotation.NonNull) InputStream(java.io.InputStream) Skill(org.openhab.ui.habot.nlp.Skill) InterpretationException(org.eclipse.smarthome.core.voice.text.InterpretationException) HashMap(java.util.HashMap) ChatReply(org.openhab.ui.habot.nlp.ChatReply) Intent(org.openhab.ui.habot.nlp.Intent) IntentInterpretation(org.openhab.ui.habot.nlp.IntentInterpretation) InterpretationException(org.eclipse.smarthome.core.voice.text.InterpretationException) Comparator(java.util.Comparator)

Example 3 with InterpretationException

use of org.eclipse.smarthome.core.voice.text.InterpretationException in project habot by ghys.

the class HABotResource method chat.

@POST
@RolesAllowed({ Role.USER, Role.ADMIN })
@Path("/chat")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Send a query to HABot to interpret.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = ChatReply.class), @ApiResponse(code = 500, message = "An interpretation error occured") })
public Response chat(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @ApiParam(value = "language") String language, @ApiParam(value = "human language query", required = true) String query) throws Exception {
    final Locale locale = (this.localeProvider != null && this.localeProvider.getLocale() != null) ? this.localeProvider.getLocale() : LocaleUtil.getLocale(language);
    // interpret
    OpenNLPInterpreter hli = (OpenNLPInterpreter) voiceManager.getHLI(OPENNLP_HLI);
    if (hli == null) {
        throw new InterpretationException("The OpenNLP interpreter is not available");
    }
    ChatReply reply = hli.reply(locale, query);
    return Response.ok(reply).build();
}
Also used : Locale(java.util.Locale) OpenNLPInterpreter(org.openhab.ui.habot.nlp.internal.OpenNLPInterpreter) InterpretationException(org.eclipse.smarthome.core.voice.text.InterpretationException) ChatReply(org.openhab.ui.habot.nlp.ChatReply) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 4 with InterpretationException

use of org.eclipse.smarthome.core.voice.text.InterpretationException in project smarthome by eclipse.

the class DialogProcessor method sttEventReceived.

@Override
public synchronized void sttEventReceived(STTEvent sttEvent) {
    if (sttEvent instanceof SpeechRecognitionEvent) {
        if (false == this.isSTTServerAborting) {
            this.sttServiceHandle.abort();
            this.isSTTServerAborting = true;
            SpeechRecognitionEvent sre = (SpeechRecognitionEvent) sttEvent;
            String question = sre.getTranscript();
            try {
                toggleProcessing(false);
                String answer = hli.interpret(this.locale, question);
                if (answer != null) {
                    say(answer);
                }
            } catch (InterpretationException e) {
                say(e.getMessage());
            }
        }
    } else if (sttEvent instanceof RecognitionStopEvent) {
        toggleProcessing(false);
    } else if (sttEvent instanceof SpeechRecognitionErrorEvent) {
        if (false == this.isSTTServerAborting) {
            this.sttServiceHandle.abort();
            this.isSTTServerAborting = true;
            toggleProcessing(false);
            SpeechRecognitionErrorEvent sre = (SpeechRecognitionErrorEvent) sttEvent;
            say("Encountered error: " + sre.getMessage());
        }
    }
}
Also used : RecognitionStopEvent(org.eclipse.smarthome.core.voice.RecognitionStopEvent) InterpretationException(org.eclipse.smarthome.core.voice.text.InterpretationException) SpeechRecognitionErrorEvent(org.eclipse.smarthome.core.voice.SpeechRecognitionErrorEvent) SpeechRecognitionEvent(org.eclipse.smarthome.core.voice.SpeechRecognitionEvent)

Aggregations

InterpretationException (org.eclipse.smarthome.core.voice.text.InterpretationException)4 Locale (java.util.Locale)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 Consumes (javax.ws.rs.Consumes)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 HumanLanguageInterpreter (org.eclipse.smarthome.core.voice.text.HumanLanguageInterpreter)2 ChatReply (org.openhab.ui.habot.nlp.ChatReply)2 InputStream (java.io.InputStream)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 RolesAllowed (javax.annotation.security.RolesAllowed)1