/** Adds an empty action to the dialogue system to start the interaction. */ @Override public void start() { Assignment emptyAction = new Assignment(system.getSettings().systemOutput, ValueFactory.none()); if (system.isPaused()) { system.getState().addToState(emptyAction); } else { system.addContent(emptyAction); } system.attachModule(RewardLearner.class); }
private void performTurn() { DialogueState systemState = system.getState(); final String outputVar = system.getSettings().systemOutput; try { Value systemAction = ValueFactory.none(); if (systemState.hasChanceNode(outputVar)) { systemAction = systemState.queryProb(outputVar).getBest(); } log.fine("Simulator input: " + systemAction); boolean turnPerformed = performTurn(systemAction); int repeat = 0; while (!turnPerformed && repeat < 5 && system.getModules().contains(this)) { turnPerformed = performTurn(systemAction); repeat++; } } catch (RuntimeException e) { log.fine("cannot update simulator: " + e); } }
/** * Performs the dialogue turn in the simulator. * * @param systemAction the last system action. @ */ private synchronized boolean performTurn(Value systemAction) { boolean turnPerformed = false; simulatorState.setParameters(domain.getParameters()); Assignment systemAssign = new Assignment(system.getSettings().systemOutput, systemAction); simulatorState.addToState(systemAssign); while (!simulatorState.getNewVariables().isEmpty()) { Set<String> toProcess = simulatorState.getNewVariables(); simulatorState.reduce(); for (Model model : domain.getModels()) { if (model.isTriggered(simulatorState, toProcess)) { boolean change = model.trigger(simulatorState); if (change && model.isBlocking()) { break; } } } if (!simulatorState.getUtilityNodeIds().isEmpty()) { double reward = simulatorState.queryUtil(); String comment = "Reward: " + StringUtils.getShortForm(reward); system.displayComment(comment); system .getState() .addEvidence(new Assignment("R(" + systemAssign.addPrimes() + ")", reward)); simulatorState.removeNodes(simulatorState.getUtilityNodeIds()); } if (addNewObservations()) { turnPerformed = true; } simulatorState.addEvidence(simulatorState.getSample()); } return turnPerformed; }
// @Test public void parsingTest() throws InterruptedException { DialogueSystem system = new DialogueSystem(XMLDomainReader.extractDomain(DOMAIN_FILE)); system.getSettings().showGUI = false; system.getSettings().params.setProperty("taggingmodel", TAGGING_MODEL); system.getSettings().params.setProperty("parsingmodel", PARSING_MODEL); system.attachModule(MaltParser.class); system.startSystem(); system.addUserInput("move to the left"); assertTrue(system.getState().hasChanceNode("parse(u_u)")); assertTrue(system.getState().hasChanceNode("a_u")); assertEquals(system.getContent("a_u").toDiscrete().getBest().toString(), "Move(left)"); system.addUserInput("what do you see now?"); assertTrue(system.getState().hasChanceNode("parse(u_u)")); assertEquals(system.getContent("a_u").toDiscrete().getBest().toString(), "Move(left)"); Map<String, Double> table = new HashMap<String, Double>(); table.put("move a little bit to the left", 0.7); table.put("move a bit to the left", 0.1); system.addUserInput(table); assertTrue(system.getState().hasChanceNode("parse(u_u)")); assertTrue(system.getState().hasChanceNode("a_u")); assertEquals(system.getContent("a_u").toDiscrete().getProb("Move(left)"), 0.8, 0.01); table = new HashMap<String, Double>(); table.put("now move a bit to the right please", 0.6); system.addUserInput(table); assertTrue(system.getState().hasChanceNode("parse(u_u)")); assertTrue(system.getState().hasChanceNode("a_u")); assertEquals(system.getContent("a_u").toDiscrete().getProb("Move(right)"), 0.6, 0.01); ParseValue pv = (ParseValue) system .getContent("parse(u_u)") .getValues() .stream() .filter(v -> v instanceof ParseValue) .findFirst() .get(); assertTrue(pv.contains(ValueFactory.create("TO DT JJ"))); assertFalse(pv.contains(ValueFactory.create("DT TT JJ"))); assertTrue(pv.contains(ValueFactory.create("(*,the,*,det,7)"))); assertTrue(pv.contains(ValueFactory.create("(7,*,JJ,*,*)"))); assertTrue(pv.contains(ValueFactory.create("(*,*,DT,det,7)"))); assertTrue(pv.contains(ValueFactory.create("(7,right,*,*,*)"))); assertFalse(pv.contains(ValueFactory.create("(7,left,*,*,*)"))); assertTrue(pv.contains(ValueFactory.create("TO the JJ"))); assertTrue(pv.contains(ValueFactory.create("to the JJ"))); assertTrue(pv.contains(ValueFactory.create("TO DT right"))); assertFalse(pv.contains(ValueFactory.create("TO DT left"))); assertTrue(pv.contains(ValueFactory.create("to/TO the/DT right/JJ"))); assertFalse(pv.contains(ValueFactory.create("to/JJ the/DT right/JJ"))); assertTrue(pv.contains(ValueFactory.create("(*,the,DT,det,7)"))); assertTrue(pv.contains(ValueFactory.create("(7,right,JJ,*,*)"))); assertTrue(pv.contains(ValueFactory.create("JJ"))); assertFalse(pv.contains(ValueFactory.create("RBR"))); table = new HashMap<String, Double>(); table.put("this is a gnome", 0.6); system.addUserInput(table); pv = (ParseValue) system .getContent("parse(u_u)") .getValues() .stream() .filter(v -> v instanceof ParseValue) .findFirst() .get(); assertTrue(pv.contains(ValueFactory.create("DT VBZ DT NN"))); assertEquals("Test successful", system.getContent("i_u").getBest().toString()); }