Jsapi20RecognitionResult implements RecognitionResult { @Override public Object getSemanticInterpretation(final DataModel model) throws SemanticError { if (interpretation == null) { final FinalResult finalResult = (FinalResult) result; final Object[] objecttags = finalResult.getTags(0); if ((objecttags == null) || (objecttags.length == 0)) { return null; } final String[] tags = toString(objecttags); model.createVariable("out"); for (String tag : tags) { if (tag.trim().endsWith(";")) { model.evaluateExpression(tag, Object.class); } else { model.updateVariable("out", tag); } } interpretation = model.readVariable("out", Object.class); final String log = model.toString(interpretation); LOGGER.info("created semantic interpretation '" + log + "'"); } return interpretation; } Jsapi20RecognitionResult(final Result res); @Override String getUtterance(); @Override boolean isAccepted(); @Override void setMark(final String mark); @Override String getMark(); @Override float getConfidence(); @Override float[] getWordsConfidence(); @Override String[] getWords(); @Override ModeType getMode(); @Override Object getSemanticInterpretation(final DataModel model); @Override String toString();  }
@Test public void testGetSemanticInterpretation() throws Exception, SemanticError { EngineManager.registerEngineListFactory(SphinxEngineListFactory.class .getName()); Recognizer recognizer = (Recognizer) EngineManager .createEngine(new RecognizerMode(SpeechLocale.ENGLISH)); recognizer.allocate(); final GrammarManager manager = recognizer.getGrammarManager(); final RuleGrammar grammar = manager.createRuleGrammar("grammar:test", "test"); final RuleComponent[] components = new RuleComponent[] { new RuleToken("test"), new RuleTag("T") }; final RuleSequence sequence = new RuleSequence(components); final Rule root = new Rule("test", sequence, Rule.PUBLIC); grammar.addRule(root); recognizer.processGrammars(); final BaseResult result = new BaseResult(grammar, "test"); final Jsapi20RecognitionResult res = new Jsapi20RecognitionResult( result); final DataModel model = Mockito.mock(DataModel.class); Assert.assertEquals("T", res.getSemanticInterpretation(model)); }
@Test public void testGetSemanticInterpretationCompundObject() throws Exception, SemanticError { EngineManager.registerEngineListFactory(SphinxEngineListFactory.class .getName()); Recognizer recognizer = (Recognizer) EngineManager .createEngine(new RecognizerMode(SpeechLocale.ENGLISH)); recognizer.allocate(); final GrammarManager manager = recognizer.getGrammarManager(); final RuleGrammar grammar = manager.createRuleGrammar("grammar:test", "test"); final RuleComponent[] components = new RuleComponent[] { new RuleToken("test"), new RuleTag("out = new Object(); out.order = new Object();"), new RuleTag("out.order.topping=\"salami\";"), new RuleTag("out.order.size=\"medium\";"), new RuleTag("out.date=\"now\";")}; final RuleSequence sequence = new RuleSequence(components); final Rule root = new Rule("test", sequence, Rule.PUBLIC); grammar.addRule(root); recognizer.processGrammars(); final BaseResult result = new BaseResult(grammar, "test"); final Jsapi20RecognitionResult res = new Jsapi20RecognitionResult( result); final DataModel model = Mockito.mock(DataModel.class); res.getSemanticInterpretation(model); Mockito.verify(model).updateVariable("test", 5); }