@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); // // Assert.assertEquals("{\"order\":{\"topping\":\"salami\",\"size\":\"medium\"},\"date\":\"now\"}", // ScriptingEngine.toJSON((ScriptableObject) interpretation)); }
/** * featches the rules associated with the string * * @param result * @return * @throws GrammarException */ RuleParse getRuleParseFromString(String result) throws GrammarException { BaseRecognizer jsapiRecognizer = new BaseRecognizer(getGrammar().getGrammarManager()); try { jsapiRecognizer.allocate(); } catch (Exception e) { e.printStackTrace(); } RuleGrammar ruleGrammar = new BaseRuleGrammar(jsapiRecognizer, getGrammar().getRuleGrammar()); RuleParse ruleParse = ruleGrammar.parse(result, null); return ruleParse; }
/** * Retrieves the rule parse for the given result * * @param the recognition result * @return the rule parse for the result * @throws GrammarException */ RuleParse getRuleParse(Result result) throws GrammarException { String resultText = result.getBestFinalResultNoFiller(); BaseRecognizer jsapiRecognizer = new BaseRecognizer(getGrammar().getGrammarManager()); try { jsapiRecognizer.allocate(); } catch (EngineException | EngineStateError e) { System.err.println("failed to allocate jsapi recognizer: " + e.getMessage()); } RuleGrammar ruleGrammar = new BaseRuleGrammar(jsapiRecognizer, getGrammar().getRuleGrammar()); RuleParse ruleParse = ruleGrammar.parse(resultText, null); return ruleParse; }
@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)); }