use of org.openhab.ui.habot.nlp.ChatReply 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;
}
use of org.openhab.ui.habot.nlp.ChatReply in project habot by ghys.
the class HABotResource method greet.
@GET
@RolesAllowed({ Role.USER, Role.ADMIN })
@Path("/greet")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Retrieves a first greeting message from the bot in the specified or configured language.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = ChatReply.class), @ApiResponse(code = 500, message = "There is no support for the configured language") })
public Response greet(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @ApiParam(value = "language (will use the default if omitted)") String language) {
final Locale locale = (this.localeProvider != null && this.localeProvider.getLocale() != null) ? this.localeProvider.getLocale() : LocaleUtil.getLocale(language);
AnswerFormatter answerFormatter = new AnswerFormatter(locale);
String greeting = answerFormatter.getRandomAnswer("greeting");
ChatReply reply = new ChatReply(locale);
reply.setAnswer(greeting);
return Response.ok(reply).build();
}
use of org.openhab.ui.habot.nlp.ChatReply 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();
}
Aggregations