Anyone have a sample grammar that includes semantics and works in Unity (2018.3)? I haven't been able to cobble together a working example. Mike
I'm stumped. Here's my grammar (example from web) and script. I don't get any recognition events unless the semantic tags are removed. Status of the recognizer is "running" either way. Could use a good reference for creating semantic tags as well. <?xml version="1.0" encoding="UTF-8" ?> <grammar xmlns="http://www.w3.org/2001/06/grammar" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/06/grammar http://www.w3.org/TR/speech-grammar/grammar.xsd" xml:lang="en" version="1.0" mode="voice" root="main"> <rule id="main"> <one-of> <item><ruleref uri="#rule1" /></item> </one-of> </rule> <rule id="rule1" scope="public"> <one-of> <item>1<tag>out.start="1";</tag></item> <item>2<tag>out.start="2";</tag></item> <item>3<tag>out.start="3";</tag></item> </one-of> </rule> </grammar> Code (CSharp): using System.Collections; using System.Collections.Generic; using System.Linq; using System.IO; using UnityEngine; using UnityEngine.Windows.Speech; public class SpeechGrammarTest : MonoBehaviour { public string grammarPath; [Header("Read Only")] public string dateTime; public string fullPath; public bool running; public string utterance; public List<string> semantics; GrammarRecognizer recognizer; // Start is called before the first frame update void Start() { fullPath = Path.Combine(Application.streamingAssetsPath, grammarPath); recognizer = new GrammarRecognizer(fullPath, ConfidenceLevel.Low); recognizer.OnPhraseRecognized += PhraseRecognized; recognizer.Start(); running = recognizer.IsRunning; } public bool hasSemantics = false; private void PhraseRecognized(PhraseRecognizedEventArgs args) { utterance = args.text; hasSemantics = args.semanticMeanings != null; semantics.Clear(); if (args.semanticMeanings != null) { foreach (SemanticMeaning sm in args.semanticMeanings) { string newSm = sm.key; foreach (string val in sm.values) newSm = newSm + " " + val; semantics.Add(newSm); } } } // Update is called once per frame void Update() { dateTime = System.DateTime.Now.ToString(); } }
I think I've figured it out. The grammar header must have a tag-format declaration if you're doing semantic interpretation. So, the last line of the grammar node should be version="1.0" mode="voice" tag-format="semantics/1.0" root="main"> Found the issue when I tried to load the grammar into a regular MS speech grammar (non-Unity) and it threw an error. Hopefully, this can help someone else. Good resources here. https://www.w3.org/TR/speech-grammar/ https://www.w3.org/TR/semantic-interpretation/