use of org.openhab.ui.habot.nlp.Intent in project habot by ghys.
the class DeactivateObjectSkill 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_deactivated"));
interpretation.setHint(answerFormatter.getStandardTagHint(intent.getEntities()));
} else {
interpretation.setMatchedItems(matchedItems);
// filter out the items which can't receive an OFF 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_deactivated"));
interpretation.setHint(answerFormatter.getStandardTagHint(intent.getEntities()));
} else if (filteredItems.size() == 1) {
if (filteredItems.get(0).getState().equals(OnOffType.OFF)) {
interpretation.setAnswer(answerFormatter.getRandomAnswer("switch_already_off"));
} else {
eventPublisher.post(ItemEventFactory.createCommandEvent(filteredItems.get(0).getName(), OnOffType.OFF));
interpretation.setAnswer(answerFormatter.getRandomAnswer("switch_deactivated"));
}
} else {
for (Item item : filteredItems) {
eventPublisher.post(ItemEventFactory.createCommandEvent(item.getName(), OnOffType.OFF));
}
interpretation.setAnswer(answerFormatter.getRandomAnswer("switches_deactivated", ImmutableMap.of("count", String.valueOf(filteredItems.size()))));
}
}
return interpretation;
}
use of org.openhab.ui.habot.nlp.Intent in project habot by ghys.
the class HistoryLastChangesSkill 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()));
return interpretation;
}
Set<String> tags = intent.getEntities().entrySet().stream().map(e -> e.getKey() + ":" + e.getValue()).collect(Collectors.toSet());
Card card = new Card("HbCard");
card.addTags(tags);
card.updateTimestamp();
card.setEphemeral(true);
card.setAddToDeckDenied(true);
Component timeline = new Component("HbTimeline");
if (matchedItems.size() == 1) {
Item item = matchedItems.stream().findFirst().get();
// TODO figure out a solution
HistoricItem historicItem = PersistenceExtensions.previousState(item, false);
if (historicItem == null) {
interpretation.setAnswer(answerFormatter.getRandomAnswer("answer_nothing_found"));
interpretation.setHint(answerFormatter.getRandomAnswer("no_historical_data"));
return interpretation;
}
card.setTitle(item.getLabel());
card.setSubtitle(item.getName());
DateFormat dateFormat = new SimpleDateFormat();
Component pastTimelineEntry = new Component("HbTimelineEntry");
pastTimelineEntry.addConfig("title", formatState(item, historicItem.getState()));
pastTimelineEntry.addConfig("subtitle", dateFormat.format(historicItem.getTimestamp()));
timeline.addComponent("main", pastTimelineEntry);
Component nowTimelineEntry = new Component("HbTimelineEntry");
nowTimelineEntry.addConfig("title", formatState(item, historicItem.getState()));
nowTimelineEntry.addConfig("subtitle", dateFormat.format(new Date()));
timeline.addComponent("main", nowTimelineEntry);
} else {
interpretation.setAnswer(answerFormatter.getRandomAnswer("multiple_items_error"));
return interpretation;
}
card.addComponent("main", timeline);
this.cardRegistry.add(card);
interpretation.setAnswer(answerFormatter.getRandomAnswer("info_found_simple"));
interpretation.setCard(card);
return interpretation;
}
use of org.openhab.ui.habot.nlp.Intent in project habot by ghys.
the class CardBuilder method buildCard.
/**
* Retrieves or build a card for the specified intent and matched items
*
* @param intent the intent including entities
* @param matchedItems the matched items
* @return the card (either retrieved or built)
*/
public Card buildCard(Intent intent, Collection<Item> matchedItems) {
Set<String> tags = intent.getEntities().entrySet().stream().filter(e -> e.getKey().equals("object") || e.getKey().equals("location")).map(e -> e.getKey() + ":" + e.getValue()).collect(Collectors.toSet());
Collection<Card> cardsInRegistry = this.cardRegistry.getCardByTags(tags).stream().filter(c -> !c.isNotReuseableInChat() && !c.isEphemeral()).collect(Collectors.toList());
if (cardsInRegistry.size() > 0) {
// don't handle multiple cards, just return the first one
Card existingCard = cardsInRegistry.iterator().next();
existingCard.updateTimestamp();
cardRegistry.update(existingCard);
return existingCard;
}
Card card = new Card("HbCard");
card.addTags(tags);
card.setEphemeral(true);
card.addConfig("bigger", true);
card.updateTimestamp();
if (matchedItems.size() == 1) {
Item item = matchedItems.stream().findFirst().get();
card.setTitle(item.getLabel());
card.setSubtitle(item.getName());
switch(item.getType()) {
case CoreItemFactory.SWITCH:
Component switchComponent = new Component("HbSwitch");
switchComponent.addConfig("item", item.getName());
card.addComponent("right", switchComponent);
break;
case CoreItemFactory.DIMMER:
if (item.hasTag("habot:switchable")) {
Component dimmerSwitchComponent = new Component("HbSwitch");
dimmerSwitchComponent.addConfig("item", item.getName());
card.addComponent("right", dimmerSwitchComponent);
} else if (!item.hasTag("habot:control:knob")) {
Component dimmerValueComponent = new Component("HbSingleItemValue");
dimmerValueComponent.addConfig("item", item.getName());
card.addComponent("right", dimmerValueComponent);
}
Component dimmerContainerComponent = new Component("HbContainer");
dimmerContainerComponent.addConfig("classes", new String[] { "full-width", "text-center" });
if (item.hasTag("habot:control:knob")) {
Component knobComponent = new Component("HbKnob");
knobComponent.addConfig("item", item.getName());
knobComponent.addConfig("size", "200px");
knobComponent.addConfig("textSize", "2rem");
knobComponent.addConfig("color", "primary");
dimmerContainerComponent.addComponent("main", knobComponent);
} else {
Component sliderComponent = new Component("HbSlider");
sliderComponent.addConfig("item", item.getName());
dimmerContainerComponent.addComponent("main", sliderComponent);
}
card.addComponent("main", dimmerContainerComponent);
break;
case CoreItemFactory.ROLLERSHUTTER:
Component shutterValueComponent = new Component("HbSingleItemValue");
shutterValueComponent.addConfig("item", item.getName());
card.addComponent("right", shutterValueComponent);
Component shutterContainerComponent = new Component("HbContainer");
shutterContainerComponent.addConfig("classes", new String[] { "full-width", "text-center" });
Component shutterControlComponent = new Component("HbShutterControl");
shutterControlComponent.addConfig("item", item.getName());
shutterControlComponent.addConfig("size", "lg");
shutterControlComponent.addConfig("rounded", true);
shutterControlComponent.addConfig("glossy", true);
shutterControlComponent.addConfig("push", true);
shutterControlComponent.addConfig("stopIcon", "close");
shutterContainerComponent.addComponent("main", shutterControlComponent);
card.addComponent("main", shutterContainerComponent);
break;
case CoreItemFactory.PLAYER:
Component playerContainerComponent = new Component("HbContainer");
playerContainerComponent.addConfig("classes", new String[] { "full-width", "text-center" });
Component playerComponent = new Component("HbPlayer");
playerComponent.addConfig("item", item.getName());
playerComponent.addConfig("size", "lg");
playerContainerComponent.addComponent("main", playerComponent);
card.addComponent("main", playerContainerComponent);
break;
case CoreItemFactory.COLOR:
Component colorPickerComponent = new Component("HbColorPicker");
colorPickerComponent.addConfig("item", item.getName());
card.addComponent("right", colorPickerComponent);
Component brightnessDimmerComponent = new Component("HbSwitch");
brightnessDimmerComponent.addConfig("item", item.getName());
card.addComponent("right", brightnessDimmerComponent);
Component brightnessDimmerContainerComponent = new Component("HbContainer");
brightnessDimmerContainerComponent.addConfig("classes", new String[] { "full-width", "text-center" });
Component brightnessSliderComponent = new Component("HbSlider");
brightnessSliderComponent.addConfig("item", item.getName());
brightnessDimmerContainerComponent.addComponent("main", brightnessSliderComponent);
card.addComponent("main", brightnessDimmerContainerComponent);
break;
default:
if (item.getType() == CoreItemFactory.IMAGE || item.getTags().stream().anyMatch(t -> t.startsWith("habot:image:sitemap:"))) {
/*
* If the item is an image (or a String with a tag indicating it's an image), build a
* HbImage component in the "media" slot
*/
Component singleImageComponent = new Component("HbImage");
singleImageComponent.addConfig("item", item.getName());
card.addComponent("media", singleImageComponent);
} else {
/*
* Try to get a formatted state to determine whether it's small enough to display
* in the "right" slot - otherwise add it to the "main" slot
*/
String formattedState = formatState(item, item.getState());
Component singleItemComponent = new Component("HbSingleItemValue");
singleItemComponent.addConfig("item", item.getName());
if (formattedState.length() < 10) {
card.addComponent("right", singleItemComponent);
} else {
card.addComponent("main", singleItemComponent);
}
}
break;
}
} else {
card.setTitle(getCardTitleFromGroupLabels(tags));
// TODO: i18n
card.setSubtitle(matchedItems.size() + " items");
// TODO: detect images and build a HbCarousel with them - for webcams etc.
Component list = new Component("HbList");
for (Item item : matchedItems) {
Component listItem = new Component("HbListItem");
listItem.addConfig("item", item.getName());
listItem.addConfig("label", item.getLabel());
list.addComponent("items", listItem);
}
card.addComponent("list", list);
}
// Adds the (ephemeral) card to the registry anyways so it appears in the "recent cards" page
cardRegistry.add(card);
return card;
}
use of org.openhab.ui.habot.nlp.Intent in project habot by ghys.
the class AbstractTrainerTest method interpret.
protected Intent interpret(String query) {
System.out.println("----");
System.out.println("\"" + query + "\"");
System.out.println(new TreeMap<>(this.trainer.getScoreMap(query)).descendingMap().toString());
Intent intent = this.trainer.interpret(query);
System.out.println(intent.toString());
return intent;
}
use of org.openhab.ui.habot.nlp.Intent in project habot by ghys.
the class TrainerDeTest method testGetStatus.
@Test
public void testGetStatus() throws Exception {
Intent actual;
this.trainer = new IntentTrainer("de", skills);
actual = interpret("Heizung in der Küche");
assertEquals(Skills.GET_STATUS, actual.getName());
assertEquals(2, actual.getEntities().size());
assertEquals("heizung", actual.getEntities().get("object"));
assertEquals("küche", actual.getEntities().get("location"));
actual = interpret("wie hoch ist die Temperatur im Wohnzimmer");
assertEquals(Skills.GET_STATUS, actual.getName());
assertEquals(2, actual.getEntities().size());
assertEquals("temperatur", actual.getEntities().get("object"));
assertEquals("wohnzimmer", actual.getEntities().get("location"));
actual = interpret("wie hoch ist die Temperatur im Keller");
assertEquals(Skills.GET_STATUS, actual.getName());
assertEquals(2, actual.getEntities().size());
assertEquals("temperatur", actual.getEntities().get("object"));
assertEquals("keller", actual.getEntities().get("location"));
}
Aggregations