Skip to content

Commit

Permalink
extended tests
Browse files Browse the repository at this point in the history
Signed-off-by: Yannick Schaus <[email protected]>
  • Loading branch information
stritti authored and ghys committed Mar 14, 2018
1 parent 5d01ae0 commit f9da068
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 61 deletions.
28 changes: 3 additions & 25 deletions src/main/java/org/openhab/ui/habot/nlp/internal/IntentTrainer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.openhab.ui.habot.nlp.internal;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -26,10 +25,8 @@
import opennlp.tools.namefind.TokenNameFinderModel;
import opennlp.tools.tokenize.Tokenizer;
import opennlp.tools.tokenize.WhitespaceTokenizer;
import opennlp.tools.util.InputStreamFactory;
import opennlp.tools.util.ObjectStream;
import opennlp.tools.util.ObjectStreamUtils;
import opennlp.tools.util.PlainTextByLineStream;
import opennlp.tools.util.Span;
import opennlp.tools.util.TrainingParameters;

Expand Down Expand Up @@ -58,13 +55,7 @@ public IntentTrainer(String language, Collection<Skill> skills, InputStream addi

try {
InputStream trainingData = skill.getTrainingData(language);
ObjectStream<String> lineStream = new PlainTextByLineStream(new InputStreamFactory() {

@Override
public InputStream createInputStream() throws IOException {
return trainingData;
}
}, "UTF-8");
ObjectStream<String> lineStream = new LowerCasePlainTextByLineStream(trainingData);

ObjectStream<DocumentSample> documentSampleStream = new IntentDocumentSampleStream(intent, lineStream);
categoryStreams.add(documentSampleStream);
Expand Down Expand Up @@ -94,13 +85,7 @@ public InputStream createInputStream() throws IOException {
for (Skill skill : skills) {
try {
InputStream trainingData = skill.getTrainingData(language);
ObjectStream<String> lineStream = new PlainTextByLineStream(new InputStreamFactory() {

@Override
public InputStream createInputStream() throws IOException {
return trainingData;
}
}, "UTF-8");
ObjectStream<String> lineStream = new LowerCasePlainTextByLineStream(trainingData);
ObjectStream<NameSample> nameSampleStream = new NameSampleDataStream(lineStream);
nameStreams.add(nameSampleStream);
} catch (UnsupportedLanguageException e) {
Expand All @@ -111,14 +96,7 @@ public InputStream createInputStream() throws IOException {

/* Also use the additional training data for entity extraction (i.e. actual items' tags) if provided */
if (additionalNameSamples != null) {
ObjectStream<String> additionalLineStream = new PlainTextByLineStream(new InputStreamFactory() {

@Override
public InputStream createInputStream() throws IOException {
return additionalNameSamples;
}

}, "UTF-8");
ObjectStream<String> additionalLineStream = new LowerCasePlainTextByLineStream(additionalNameSamples);
ObjectStream<NameSample> additionalNameSamplesStream = new NameSampleDataStream(additionalLineStream);
nameStreams.add(additionalNameSamplesStream);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.openhab.ui.habot.nlp.internal;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import opennlp.tools.util.ObjectStream;

public class LowerCasePlainTextByLineStream implements ObjectStream<String> {

BufferedReader in;
InputStream inputStream;
final String encoding = "UTF-8";

public LowerCasePlainTextByLineStream(InputStream inputStream) throws IOException {
this.inputStream = inputStream;
reset();
}

@Override
public String read() throws IOException {
String line = in.readLine();
if (line == null) {
return null;
}
return line.toLowerCase().replace("<start:", "<START:").replace("<end>", "<END>");
}

@Override
public void reset() throws IOException {
in = new BufferedReader(new InputStreamReader(inputStream, encoding));
}

@Override
public void close() throws IOException {
if (in != null) {
in.close();
}
}
}
127 changes: 91 additions & 36 deletions src/test/java/org/openhab/ui/habot/test/TrainerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ protected Intent interpret(String query) {

@Test
public void testEN() throws Exception {

Intent actual;
Intent actual;
this.trainer = new IntentTrainer("en", skills);

actual = interpret("Temperature in the bedroom?");
Expand All @@ -81,59 +80,115 @@ public void testEN() throws Exception {
assertEquals("bedroom", actual.getEntities().get("location"));
assertEquals("temperature", actual.getEntities().get("object"));


actual= interpret("show me the temperature in the kitchen");
actual = interpret("show me the temperature in the kitchen");
assertEquals("get-status", actual.getName());
assertEquals(2, actual.getEntities().size());
assertEquals("kitchen", actual.getEntities().get("location"));
assertEquals("temperature", actual.getEntities().get("object"));
interpret("what's the temperature in the garage?");

interpret("start the heating in the garage");
interpret("start the ac please!");
interpret("turn on the lights in the kitchen");
interpret("please switch the lights in the kitchen on");
interpret("please turn off the radiators");
interpret("deactivate the alarm");
interpret("stop the music in the living room");

interpret("show me a graph of the temperature in the living room for the last 3 hours");
interpret("graph the water consumption for the last 2 days");
interpret("i'd like a chart of the humidity over 2 weeks");
interpret("temperature in the downstairs corridor for the last month");
interpret("luminosity in the living room over 6 months");

interpret("when was the alarm turned on for the last time?");
interpret("show me the last state changes of the window in the bedroom");
assertEquals("temperature", actual.getEntities().get("object"));

actual = interpret("what's the temperature in the garage?");

actual = interpret("start the heating in the garage");
actual = interpret("start the ac please!");
actual = interpret("turn on the lights in the kitchen");
actual = interpret("please switch the lights in the kitchen on");
actual = interpret("please turn off the radiators");
actual = interpret("deactivate the alarm");
actual = interpret("stop the music in the living room");

actual = interpret("show me a graph of the temperature in the living room for the last 3 hours");
actual = interpret("graph the water consumption for the last 2 days");
actual = interpret("i'd like a chart of the humidity over 2 weeks");
actual = interpret("temperature in the downstairs corridor for the last month");
actual = interpret("luminosity in the living room over 6 months");

actual = interpret("when was the alarm turned on for the last time?");
actual = interpret("show me the last state changes of the window in the bedroom");
}

@Test
public void testFR() throws Exception {
Intent actual;
this.trainer = new IntentTrainer("fr", skills);

interpret("donne-moi un graphique de la température du salon pour les dernières heures");
interpret("montre-moi la température du salon");
interpret("Température du salon ?");
interpret("active le thermostat dans le garage");
interpret("mets en route la climatisation");
interpret("montre le graphique de la consommation électrique pour les 2 derniers jours");
actual = interpret("donne-moi un graphique de la température du salon pour les dernières heures");
actual = interpret("montre-moi la température du salon");
actual = interpret("Température du salon ?");
actual = interpret("active le thermostat dans le garage");

actual = interpret("mets en route la climatisation");
assertEquals("activate-object", actual.getName());
assertEquals(1, actual.getEntities().size());
assertEquals("climatisation", actual.getEntities().get("object"));

actual = interpret("montre le graphique de la consommation électrique pour les 2 derniers jours");
assertEquals("get-history-daily", actual.getName());
assertEquals(2, actual.getEntities().size());
assertEquals("2", actual.getEntities().get("period"));
assertEquals("consommation électrique", actual.getEntities().get("object"));
}

@Test
public void testDE() throws Exception {
public void testDE_activateObjects() throws Exception {

Intent actual;
Intent actual;
this.trainer = new IntentTrainer("de", skills);

actual = interpret("mach den Fernseher an");
assertEquals("activate-object", actual.getName());
assertEquals(1, actual.getEntities().size());
assertEquals("fernseher", actual.getEntities().get("object"));
assertEquals("fernseher", actual.getEntities().get("object"));

actual = interpret("bitte mache das Licht an");
assertEquals("activate-object", actual.getName());
assertEquals(1, actual.getEntities().size());
assertEquals("Licht", actual.getEntities().get("object"));
}
assertEquals("licht", actual.getEntities().get("object"));
}

@Test
public void testDE_deactivateObjects() throws Exception {

Intent actual;
this.trainer = new IntentTrainer("de", skills);

actual = interpret("Licht aus");
assertEquals("deactivate-object", actual.getName());
assertEquals(1, actual.getEntities().size());
assertEquals("licht", actual.getEntities().get("object"));

actual = interpret("mach den Fernseher aus");
assertEquals("deactivate-object", actual.getName());
assertEquals(1, actual.getEntities().size());
assertEquals("fernseher", actual.getEntities().get("object"));

actual = interpret("bitte mache das Licht aus");
assertEquals("deactivate-object", actual.getName());
assertEquals(1, actual.getEntities().size());
assertEquals("licht", actual.getEntities().get("object"));
}

@Test
public void testDE_getStatus() throws Exception {

Intent actual;
this.trainer = new IntentTrainer("de", skills);

actual = interpret("Heizung in der Küche");
assertEquals("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("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("get-status", actual.getName());
assertEquals(2, actual.getEntities().size());
assertEquals("temperatur", actual.getEntities().get("object"));
assertEquals("keller", actual.getEntities().get("location"));
}
}

0 comments on commit f9da068

Please sign in to comment.