Search in sources :

Example 1 with Intent

use of org.openhab.ui.habot.nlp.Intent 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;
}
Also used : IntentInterpretation(org.openhab.ui.habot.nlp.IntentInterpretation) ItemEventFactory(org.eclipse.smarthome.core.items.events.ItemEventFactory) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) CardBuilder(org.openhab.ui.habot.card.CardBuilder) Collectors(java.util.stream.Collectors) Item(org.eclipse.smarthome.core.items.Item) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) List(java.util.List) Skill(org.openhab.ui.habot.nlp.Skill) AbstractItemIntentInterpreter(org.openhab.ui.habot.nlp.AbstractItemIntentInterpreter) Reference(org.osgi.service.component.annotations.Reference) Intent(org.openhab.ui.habot.nlp.Intent) Item(org.eclipse.smarthome.core.items.Item) IntentInterpretation(org.openhab.ui.habot.nlp.IntentInterpretation)

Example 2 with Intent

use of org.openhab.ui.habot.nlp.Intent in project habot by ghys.

the class IntentTrainer method interpret.

/**
 * Tries to understand the natural language query into an {@link Intent}
 *
 * @param query the natural language query
 * @return the resulting @{link Intent}
 */
public Intent interpret(String query) {
    String[] tokens = this.tokenizer.tokenize(query.toLowerCase());
    // remove eventual trailing punctuation
    tokens[tokens.length - 1] = tokens[tokens.length - 1].replaceAll("\\s*[!?.]+$", "");
    double[] outcome = categorizer.categorize(tokens);
    logger.debug("{}", categorizer.getAllResults(outcome));
    Intent intent = new Intent(categorizer.getBestCategory(outcome));
    Span[] spans = nameFinder.find(tokens);
    String[] names = Span.spansToStrings(spans, tokens);
    for (int i = 0; i < spans.length; i++) {
        intent.getEntities().put(spans[i].getType(), names[i]);
    }
    logger.debug("{}", intent.toString());
    return intent;
}
Also used : Intent(org.openhab.ui.habot.nlp.Intent) Span(opennlp.tools.util.Span)

Example 3 with Intent

use of org.openhab.ui.habot.nlp.Intent 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 4 with Intent

use of org.openhab.ui.habot.nlp.Intent in project habot by ghys.

the class CardBuilder method buildChartCard.

/**
 * Builds a card with a chart from an intent and matched items
 *
 * @param intent the intent
 * @param matchedItems the matched items
 * @param period the chart period
 * @return the card
 */
public Card buildChartCard(Intent intent, Collection<Item> matchedItems, String period) {
    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.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());
    } else {
        card.setTitle(getCardTitleFromGroupLabels(tags));
        // TODO: i18n
        card.setSubtitle(matchedItems.size() + " items");
    }
    Component chart = new Component("HbChartImage");
    chart.addConfig("items", matchedItems.stream().map(i -> i.getName()).collect(Collectors.toList()).toArray(new String[0]));
    chart.addConfig("period", period);
    Component analyzeButton = new Component("HbAnalyzeActionButton");
    analyzeButton.addConfig("items", chart.getConfig().get("items"));
    analyzeButton.addConfig("period", chart.getConfig().get("period"));
    card.addComponent("media", chart);
    card.addComponent("actions", analyzeButton);
    cardRegistry.add(card);
    return card;
}
Also used : CardRegistry(org.openhab.ui.habot.card.internal.CardRegistry) TransformationHelper(org.eclipse.smarthome.core.transform.TransformationHelper) Collection(java.util.Collection) Set(java.util.Set) HistoryLastChangesSkill(org.openhab.ui.habot.nlp.internal.skill.HistoryLastChangesSkill) Collectors(java.util.stream.Collectors) Item(org.eclipse.smarthome.core.items.Item) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) CoreItemFactory(org.eclipse.smarthome.core.library.CoreItemFactory) StateDescription(org.eclipse.smarthome.core.types.StateDescription) State(org.eclipse.smarthome.core.types.State) Reference(org.osgi.service.component.annotations.Reference) Intent(org.openhab.ui.habot.nlp.Intent) FrameworkUtil(org.osgi.framework.FrameworkUtil) Item(org.eclipse.smarthome.core.items.Item)

Example 5 with Intent

use of org.openhab.ui.habot.nlp.Intent in project habot by ghys.

the class AbstractTrainerTest method checkInterpretation.

protected void checkInterpretation(String intentName, String query, String object, String location) {
    Intent actual = interpret(query);
    assertEquals(intentName, actual.getName());
    if (object != null) {
        assertTrue(actual.getEntities().containsKey("object"));
        assertEquals(object, actual.getEntities().get("object"));
    }
    if (location != null) {
        assertTrue(actual.getEntities().containsKey("location"));
        assertEquals(location, actual.getEntities().get("location"));
    }
}
Also used : Intent(org.openhab.ui.habot.nlp.Intent)

Aggregations

Intent (org.openhab.ui.habot.nlp.Intent)21 Test (org.junit.Test)11 IntentTrainer (org.openhab.ui.habot.nlp.internal.IntentTrainer)11 Set (java.util.Set)7 Collectors (java.util.stream.Collectors)7 Item (org.eclipse.smarthome.core.items.Item)7 ItemRegistry (org.eclipse.smarthome.core.items.ItemRegistry)7 Reference (org.osgi.service.component.annotations.Reference)7 IntentInterpretation (org.openhab.ui.habot.nlp.IntentInterpretation)5 Skill (org.openhab.ui.habot.nlp.Skill)5 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)4 AbstractItemIntentInterpreter (org.openhab.ui.habot.nlp.AbstractItemIntentInterpreter)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Collection (java.util.Collection)3 List (java.util.List)3 ItemEventFactory (org.eclipse.smarthome.core.items.events.ItemEventFactory)3 TransformationHelper (org.eclipse.smarthome.core.transform.TransformationHelper)3 State (org.eclipse.smarthome.core.types.State)3 StateDescription (org.eclipse.smarthome.core.types.StateDescription)3 CardBuilder (org.openhab.ui.habot.card.CardBuilder)3