use of org.openhab.ui.habot.nlp.IntentInterpretation in project habot by ghys.
the class ActivateObjectSkill method interpret.
@Override
public IntentInterpretation interpret(Intent intent, String language) {
IntentInterpretation interpretation = new IntentInterpretation();
Set<Item> matchedItems = findItems(intent);
if (matchedItems == null || matchedItems.isEmpty()) {
interpretation.setAnswer(answerFormatter.getRandomAnswer("nothing_activated"));
interpretation.setHint(answerFormatter.getStandardTagHint(intent.getEntities()));
} else {
interpretation.setMatchedItems(matchedItems);
// filter out the items which can't receive an ON command
List<Item> filteredItems = matchedItems.stream().filter(i -> i.getAcceptedCommandTypes().contains(OnOffType.class)).collect(Collectors.toList());
interpretation.setCard(cardBuilder.buildCard(intent, filteredItems));
if (filteredItems.isEmpty()) {
interpretation.setAnswer(answerFormatter.getRandomAnswer("nothing_activated"));
interpretation.setHint(answerFormatter.getStandardTagHint(intent.getEntities()));
} else if (filteredItems.size() == 1) {
if (filteredItems.get(0).getState().equals(OnOffType.ON)) {
interpretation.setAnswer(answerFormatter.getRandomAnswer("switch_already_on"));
} else {
eventPublisher.post(ItemEventFactory.createCommandEvent(filteredItems.get(0).getName(), OnOffType.ON));
interpretation.setAnswer(answerFormatter.getRandomAnswer("switch_activated"));
}
} else {
for (Item item : filteredItems) {
eventPublisher.post(ItemEventFactory.createCommandEvent(item.getName(), OnOffType.ON));
}
interpretation.setAnswer(answerFormatter.getRandomAnswer("switches_activated", ImmutableMap.of("count", String.valueOf(filteredItems.size()))));
}
}
return interpretation;
}
use of org.openhab.ui.habot.nlp.IntentInterpretation in project habot by ghys.
the class HistoryWeeklyGraphSkill method interpret.
@Override
public IntentInterpretation interpret(Intent intent, String language) {
IntentInterpretation interpretation = new IntentInterpretation();
Set<Item> matchedItems = findItems(intent);
if (matchedItems == null || matchedItems.isEmpty()) {
interpretation.setAnswer(answerFormatter.getRandomAnswer("answer_nothing_found"));
interpretation.setHint(answerFormatter.getStandardTagHint(intent.getEntities()));
} else {
interpretation.setMatchedItems(matchedItems);
String period = "W";
if (intent.getEntities().containsKey("period")) {
period = intent.getEntities().get("period").concat(period);
}
interpretation.setCard(this.cardBuilder.buildChartCard(intent, matchedItems, period));
}
interpretation.setAnswer(answerFormatter.getRandomAnswer("info_found_simple"));
return interpretation;
}
use of org.openhab.ui.habot.nlp.IntentInterpretation 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.IntentInterpretation in project habot by ghys.
the class CreateRuleSkill method interpret.
@Override
public IntentInterpretation interpret(Intent intent, String language) {
IntentInterpretation interpretation = new IntentInterpretation();
Card card = new Card("HbCreateRuleCard");
// TODO: try to parse a day/time to pre-configure the new rule card
interpretation.setAnswer(answerFormatter.getRandomAnswer("answer_create_rule"));
interpretation.setCard(card);
return interpretation;
}
use of org.openhab.ui.habot.nlp.IntentInterpretation in project habot by ghys.
the class SetValueSkill method interpret.
@Override
public IntentInterpretation interpret(Intent intent, String language) {
IntentInterpretation interpretation = new IntentInterpretation();
Set<Item> matchedItems = findItems(intent);
if (matchedItems == null || matchedItems.isEmpty()) {
interpretation.setAnswer(answerFormatter.getRandomAnswer("answer_nothing_found"));
interpretation.setHint(answerFormatter.getStandardTagHint(intent.getEntities()));
} else {
interpretation.setMatchedItems(matchedItems);
if (intent.getEntities().containsKey("color")) {
interpretSetColor(intent, language, interpretation, matchedItems);
} else if (intent.getEntities().containsKey("value")) {
interpretSetValue(intent, language, interpretation, matchedItems);
} else {
interpretation.setAnswer(answerFormatter.getRandomAnswer("value_misunderstood"));
}
}
return interpretation;
}
Aggregations